1// Copyright 2014 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 "base/bind.h"
6#include "base/files/file_path.h"
7#include "base/path_service.h"
8#include "base/test/launcher/unit_test_launcher.h"
9#include "base/test/test_suite.h"
10#include "build/build_config.h"
11#include "ui/base/resource/resource_bundle.h"
12#include "ui/base/ui_base_paths.h"
13#include "ui/gfx/gfx_paths.h"
14
15#if defined(OS_ANDROID)
16#include "base/android/jni_android.h"
17#include "ui/base/android/ui_base_jni_registrar.h"
18#include "ui/gfx/android/gfx_jni_registrar.h"
19#endif
20
21#if defined(OS_MACOSX) && !defined(OS_IOS)
22#include "base/mac/bundle_locations.h"
23#endif
24
25#if defined(OS_WIN)
26#include "ui/gfx/win/dpi.h"
27#endif
28
29namespace {
30
31class UIBaseTestSuite : public base::TestSuite {
32 public:
33  UIBaseTestSuite(int argc, char** argv);
34
35 protected:
36  // base::TestSuite:
37  virtual void Initialize() OVERRIDE;
38  virtual void Shutdown() OVERRIDE;
39
40 private:
41  DISALLOW_COPY_AND_ASSIGN(UIBaseTestSuite);
42};
43
44UIBaseTestSuite::UIBaseTestSuite(int argc, char** argv)
45    : base::TestSuite(argc, argv) {}
46
47void UIBaseTestSuite::Initialize() {
48  base::TestSuite::Initialize();
49
50#if defined(OS_WIN)
51  gfx::ForceHighDPISupportForTesting(1.0);
52#endif
53
54#if defined(OS_ANDROID)
55  // Register JNI bindings for android.
56  gfx::android::RegisterJni(base::android::AttachCurrentThread());
57  ui::android::RegisterJni(base::android::AttachCurrentThread());
58#endif
59
60  ui::RegisterPathProvider();
61  gfx::RegisterPathProvider();
62
63  base::FilePath exe_path;
64  PathService::Get(base::DIR_EXE, &exe_path);
65
66#if defined(OS_MACOSX) && !defined(OS_IOS)
67  // On Mac, a test Framework bundle is created that links locale.pak and
68  // chrome_100_percent.pak at the appropriate places to ui_test.pak.
69  base::mac::SetOverrideFrameworkBundlePath(
70      exe_path.AppendASCII("ui_unittests Framework.framework"));
71  ui::ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
72
73#elif defined(OS_IOS) || defined(OS_ANDROID)
74  // On iOS, the ui_unittests binary is itself a mini bundle, with resources
75  // built in. On Android, ui_unittests_apk provides the necessary framework.
76  ui::ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
77
78#else
79  // On other platforms, the (hardcoded) paths for chrome_100_percent.pak and
80  // locale.pak get populated by later build steps. To avoid clobbering them,
81  // load the test .pak files directly.
82  ui::ResourceBundle::InitSharedInstanceWithPakPath(
83      exe_path.AppendASCII("ui_test.pak"));
84
85  // ui_unittests can't depend on the locales folder which Chrome will make
86  // later, so use the path created by ui_test_pak.
87  PathService::Override(ui::DIR_LOCALES, exe_path.AppendASCII("ui"));
88#endif
89}
90
91void UIBaseTestSuite::Shutdown() {
92  ui::ResourceBundle::CleanupSharedInstance();
93
94#if defined(OS_MACOSX) && !defined(OS_IOS)
95  base::mac::SetOverrideFrameworkBundle(NULL);
96#endif
97  base::TestSuite::Shutdown();
98}
99
100}  // namespace
101
102int main(int argc, char** argv) {
103  UIBaseTestSuite test_suite(argc, argv);
104
105  return base::LaunchUnitTests(argc,
106                               argv,
107                               base::Bind(&UIBaseTestSuite::Run,
108                                          base::Unretained(&test_suite)));
109}
110