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