webkit_test_platform_support_win.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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  // TODO(scottmg): http://crbug.com/333029 This changes layout test
64  // expectations even when DirectWrite isn't on.
65#if 0
66  // DirectWrite sandbox registration.
67  CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
68  command_line.AppendSwitchASCII(switches::kRegisterFontFiles,
69                                 base::WideToUTF8(font_path.value()));
70#endif
71
72  return true;
73}
74
75}  // namespace
76
77bool CheckLayoutSystemDeps() {
78  std::list<std::string> errors;
79
80  // This metric will be 17 when font size is "Normal".
81  // The size of drop-down menus depends on it.
82  if (::GetSystemMetrics(SM_CXVSCROLL) != 17)
83    errors.push_back("Must use normal size fonts (96 dpi).");
84
85  // Check that we're using the default system fonts.
86  OSVERSIONINFO version_info = {0};
87  version_info.dwOSVersionInfoSize = sizeof(version_info);
88  ::GetVersionEx(&version_info);
89  bool is_vista_or_later = (version_info.dwMajorVersion >= 6);
90  NONCLIENTMETRICS metrics = {0};
91  metrics.cbSize = is_vista_or_later ? (sizeof NONCLIENTMETRICS)
92                                     : NONCLIENTMETRICS_SIZE_PRE_VISTA;
93  bool success = !!::SystemParametersInfo(
94      SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
95  CHECK(success);
96  LOGFONTW* system_fonts[] =
97      {&metrics.lfStatusFont, &metrics.lfMenuFont, &metrics.lfSmCaptionFont};
98  const wchar_t* required_font = is_vista_or_later ? L"Segoe UI" : L"Tahoma";
99  int required_font_size = is_vista_or_later ? -12 : -11;
100  for (size_t i = 0; i < arraysize(system_fonts); ++i) {
101    if (system_fonts[i]->lfHeight != required_font_size ||
102        wcscmp(required_font, system_fonts[i]->lfFaceName)) {
103      errors.push_back(is_vista_or_later
104                           ? "Must use either the Aero or Basic theme."
105                           : "Must use the default XP theme (Luna).");
106      break;
107    }
108  }
109
110  for (std::list<std::string>::iterator it = errors.begin(); it != errors.end();
111       ++it) {
112    std::cerr << *it << "\n";
113  }
114  return errors.empty();
115}
116
117bool WebKitTestPlatformInitialize() {
118  return SetupFonts();
119}
120
121}  // namespace content
122