webkit_test_platform_support_win.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/shell/app/webkit_test_platform_support.h"
6
7#include <windows.h>
8#include <iostream>
9#include <list>
10#include <string>
11
12#include "base/command_line.h"
13#include "base/file_util.h"
14#include "base/files/file_path.h"
15#include "base/logging.h"
16#include "base/path_service.h"
17#include "base/strings/utf_string_conversions.h"
18#include "content/shell/common/shell_switches.h"
19
20#define SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(struct_name, member) \
21    offsetof(struct_name, member) + \
22    (sizeof static_cast<struct_name*>(0)->member)
23#define NONCLIENTMETRICS_SIZE_PRE_VISTA \
24    SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(NONCLIENTMETRICS, lfMessageFont)
25
26namespace content {
27
28namespace {
29
30bool SetupFonts() {
31  // Load Ahem font.
32  // AHEM____.TTF is copied to the directory of DumpRenderTree.exe by
33  // WebKit.gyp.
34  base::FilePath base_path;
35  PathService::Get(base::DIR_MODULE, &base_path);
36  base::FilePath font_path =
37      base_path.Append(FILE_PATH_LITERAL("/AHEM____.TTF"));
38
39  // We do two registrations:
40  // 1. For GDI font rendering via ::AddFontMemResourceEx.
41  // 2. For DirectWrite rendering by appending a command line flag that tells
42  //    the sandbox policy/warmup to grant access to the given path.
43
44  // GDI registration.
45  std::string font_buffer;
46  if (!base::ReadFileToString(font_path, &font_buffer)) {
47    std::cerr << "Failed to load font " << base::WideToUTF8(font_path.value())
48              << "\n";
49    return false;
50  }
51
52  DWORD num_fonts = 1;
53  HANDLE font_handle =
54      ::AddFontMemResourceEx(const_cast<char*>(font_buffer.c_str()),
55                             font_buffer.length(),
56                             0,
57                             &num_fonts);
58  if (!font_handle) {
59    std::cerr << "Failed to register Ahem font\n";
60    return false;
61  }
62
63  // DirectWrite sandbox registration.
64  CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
65  command_line.AppendSwitchASCII(switches::kRegisterFontFiles,
66                                 base::WideToUTF8(font_path.value()));
67
68  return true;
69}
70
71}  // namespace
72
73bool CheckLayoutSystemDeps() {
74  std::list<std::string> errors;
75
76  // This metric will be 17 when font size is "Normal".
77  // The size of drop-down menus depends on it.
78  if (::GetSystemMetrics(SM_CXVSCROLL) != 17)
79    errors.push_back("Must use normal size fonts (96 dpi).");
80
81  // Check that we're using the default system fonts.
82  OSVERSIONINFO version_info = {0};
83  version_info.dwOSVersionInfoSize = sizeof(version_info);
84  ::GetVersionEx(&version_info);
85  bool is_vista_or_later = (version_info.dwMajorVersion >= 6);
86  NONCLIENTMETRICS metrics = {0};
87  metrics.cbSize = is_vista_or_later ? sizeof(NONCLIENTMETRICS)
88                                     : NONCLIENTMETRICS_SIZE_PRE_VISTA;
89  bool success = !!::SystemParametersInfo(
90      SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
91  CHECK(success);
92  LOGFONTW* system_fonts[] =
93      {&metrics.lfStatusFont, &metrics.lfMenuFont, &metrics.lfSmCaptionFont};
94  const wchar_t* required_font = is_vista_or_later ? L"Segoe UI" : L"Tahoma";
95  int required_font_size = is_vista_or_later ? -12 : -11;
96  for (size_t i = 0; i < arraysize(system_fonts); ++i) {
97    if (system_fonts[i]->lfHeight != required_font_size ||
98        wcscmp(required_font, system_fonts[i]->lfFaceName)) {
99      errors.push_back(is_vista_or_later
100                           ? "Must use either the Aero or Basic theme."
101                           : "Must use the default XP theme (Luna).");
102      break;
103    }
104  }
105
106  for (std::list<std::string>::iterator it = errors.begin(); it != errors.end();
107       ++it) {
108    std::cerr << *it << "\n";
109  }
110  return errors.empty();
111}
112
113bool WebKitTestPlatformInitialize() {
114  return SetupFonts();
115}
116
117}  // namespace content
118