• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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/memory/scoped_ptr.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 "content/public/test/test_content_client_initializer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/base/ui_base_paths.h"
14 
15 #if defined(OS_MACOSX)
16 #include "base/mac/bundle_locations.h"
17 #endif
18 
19 #if !defined(OS_IOS)
20 #include "ui/gl/gl_surface.h"
21 #endif
22 
23 #if defined(OS_ANDROID)
24 #include "base/android/jni_android.h"
25 #include "ui/base/android/ui_base_jni_registrar.h"
26 #include "ui/gfx/android/gfx_jni_registrar.h"
27 #endif
28 
29 namespace {
30 
31 class ComponentsTestSuite : public base::TestSuite {
32  public:
ComponentsTestSuite(int argc,char ** argv)33   ComponentsTestSuite(int argc, char** argv) : base::TestSuite(argc, argv) {}
34 
35  private:
Initialize()36   virtual void Initialize() OVERRIDE {
37     base::TestSuite::Initialize();
38 #if !defined(OS_IOS)
39     gfx::GLSurface::InitializeOneOffForTests();
40 #endif
41 #if defined(OS_ANDROID)
42     // Register JNI bindings for android.
43     JNIEnv* env = base::android::AttachCurrentThread();
44     gfx::android::RegisterJni(env);
45     ui::android::RegisterJni(env);
46 #endif
47 
48 #if defined(OS_MACOSX) && !defined(OS_IOS)
49     // Look in the framework bundle for resources.
50     base::FilePath path;
51     PathService::Get(base::DIR_EXE, &path);
52 
53     // TODO(tfarina): This is temporary. The right fix is to write a
54     // framework-Info.plist and integrate that into the build.
55     // Hardcode the framework name here to avoid having to depend on chrome's
56     // common target for chrome::kFrameworkName.
57 #if defined(GOOGLE_CHROME_BUILD)
58     path = path.AppendASCII("Google Chrome Framework.framework");
59 #elif defined(CHROMIUM_BUILD)
60     path = path.AppendASCII("Chromium Framework.framework");
61 #else
62 #error Unknown branding
63 #endif
64 
65     base::mac::SetOverrideFrameworkBundlePath(path);
66 #endif
67 
68     ui::RegisterPathProvider();
69 
70     // TODO(tfarina): This should be changed to InitSharedInstanceWithPakFile()
71     // so we can load our pak file instead of chrome.pak. crbug.com/348563
72     ui::ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
73     base::FilePath resources_pack_path;
74     PathService::Get(base::DIR_MODULE, &resources_pack_path);
75     ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
76         resources_pack_path.AppendASCII("resources.pak"),
77         ui::SCALE_FACTOR_NONE);
78   }
79 
Shutdown()80   virtual void Shutdown() OVERRIDE {
81     ui::ResourceBundle::CleanupSharedInstance();
82 
83 #if defined(OS_MACOSX) && !defined(OS_IOS)
84   base::mac::SetOverrideFrameworkBundle(NULL);
85 #endif
86 
87     base::TestSuite::Shutdown();
88   }
89 
90   DISALLOW_COPY_AND_ASSIGN(ComponentsTestSuite);
91 };
92 
93 class ComponentsUnitTestEventListener : public testing::EmptyTestEventListener {
94  public:
ComponentsUnitTestEventListener()95   ComponentsUnitTestEventListener() {}
~ComponentsUnitTestEventListener()96   virtual ~ComponentsUnitTestEventListener() {}
97 
OnTestStart(const testing::TestInfo & test_info)98   virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
99     content_initializer_.reset(new content::TestContentClientInitializer());
100   }
101 
OnTestEnd(const testing::TestInfo & test_info)102   virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
103     content_initializer_.reset();
104   }
105 
106  private:
107   scoped_ptr<content::TestContentClientInitializer> content_initializer_;
108 
109   DISALLOW_COPY_AND_ASSIGN(ComponentsUnitTestEventListener);
110 };
111 
112 }  // namespace
113 
main(int argc,char ** argv)114 int main(int argc, char** argv) {
115   ComponentsTestSuite test_suite(argc, argv);
116 
117   // The listener will set up common test environment for all components unit
118   // tests.
119   testing::TestEventListeners& listeners =
120       testing::UnitTest::GetInstance()->listeners();
121   listeners.Append(new ComponentsUnitTestEventListener());
122 
123   return base::LaunchUnitTests(
124       argc, argv, base::Bind(&base::TestSuite::Run,
125                              base::Unretained(&test_suite)));
126 }
127