webkit_test_platform_support_win.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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/file_util.h"
13#include "base/files/file_path.h"
14#include "base/logging.h"
15#include "base/path_service.h"
16#include "base/strings/utf_string_conversions.h"
17
18#define SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(struct_name, member) \
19    offsetof(struct_name, member) + \
20    (sizeof static_cast<struct_name*>(0)->member)
21#define NONCLIENTMETRICS_SIZE_PRE_VISTA \
22    SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(NONCLIENTMETRICS, lfMessageFont)
23
24namespace content {
25
26namespace {
27
28bool SetupFonts() {
29  // Load Ahem font.
30  // AHEM____.TTF is copied to the directory of DumpRenderTree.exe by
31  // WebKit.gyp.
32  base::FilePath base_path;
33  PathService::Get(base::DIR_MODULE, &base_path);
34  base::FilePath font_path =
35      base_path.Append(FILE_PATH_LITERAL("/AHEM____.TTF"));
36
37  std::string font_buffer;
38  if (!base::ReadFileToString(font_path, &font_buffer)) {
39    std::cerr << "Failed to load font " << base::WideToUTF8(font_path.value())
40              << "\n";
41    return false;
42  }
43
44  DWORD num_fonts = 1;
45  HANDLE font_handle =
46      ::AddFontMemResourceEx(const_cast<char*>(font_buffer.c_str()),
47                             font_buffer.length(),
48                             0,
49                             &num_fonts);
50  if (!font_handle) {
51    std::cerr << "Failed to register Ahem font\n";
52    return false;
53  }
54  return true;
55}
56
57}  // namespace
58
59bool CheckLayoutSystemDeps() {
60  std::list<std::string> errors;
61
62  // This metric will be 17 when font size is "Normal".
63  // The size of drop-down menus depends on it.
64  if (::GetSystemMetrics(SM_CXVSCROLL) != 17)
65    errors.push_back("Must use normal size fonts (96 dpi).");
66
67  // Check that we're using the default system fonts.
68  OSVERSIONINFO version_info = {0};
69  version_info.dwOSVersionInfoSize = sizeof(version_info);
70  ::GetVersionEx(&version_info);
71  bool is_vista_or_later = (version_info.dwMajorVersion >= 6);
72  NONCLIENTMETRICS metrics = {0};
73  metrics.cbSize = is_vista_or_later ? (sizeof NONCLIENTMETRICS)
74                                     : NONCLIENTMETRICS_SIZE_PRE_VISTA;
75  bool success = !!::SystemParametersInfo(
76      SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
77  CHECK(success);
78  LOGFONTW* system_fonts[] =
79      {&metrics.lfStatusFont, &metrics.lfMenuFont, &metrics.lfSmCaptionFont};
80  const wchar_t* required_font = is_vista_or_later ? L"Segoe UI" : L"Tahoma";
81  int required_font_size = is_vista_or_later ? -12 : -11;
82  for (size_t i = 0; i < arraysize(system_fonts); ++i) {
83    if (system_fonts[i]->lfHeight != required_font_size ||
84        wcscmp(required_font, system_fonts[i]->lfFaceName)) {
85      errors.push_back(is_vista_or_later
86                           ? "Must use either the Aero or Basic theme."
87                           : "Must use the default XP theme (Luna).");
88      break;
89    }
90  }
91
92  for (std::list<std::string>::iterator it = errors.begin(); it != errors.end();
93       ++it) {
94    std::cerr << *it << "\n";
95  }
96  return errors.empty();
97}
98
99bool WebKitTestPlatformInitialize() {
100  return SetupFonts();
101}
102
103}  // namespace content
104