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 "content/public/test/content_test_suite_base.h"
6
7#include "base/basictypes.h"
8#include "base/compiler_specific.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/metrics/statistics_recorder.h"
11#include "base/test/test_suite.h"
12#include "base/threading/sequenced_worker_pool.h"
13#include "content/browser/browser_thread_impl.h"
14#include "content/common/url_schemes.h"
15#include "content/gpu/in_process_gpu_thread.h"
16#include "content/public/common/content_client.h"
17#include "content/renderer/in_process_renderer_thread.h"
18#include "content/utility/in_process_utility_thread.h"
19#include "testing/gtest/include/gtest/gtest.h"
20#include "ui/base/ui_base_paths.h"
21
22#if !defined(OS_IOS)
23#include "content/browser/gpu/gpu_process_host.h"
24#include "content/browser/renderer_host/render_process_host_impl.h"
25#include "content/browser/utility_process_host_impl.h"
26#endif
27
28#if defined(OS_ANDROID)
29#include "base/android/jni_android.h"
30#include "content/browser/android/browser_jni_registrar.h"
31#include "content/common/android/common_jni_registrar.h"
32#include "content/public/browser/android/compositor.h"
33#include "media/base/android/media_jni_registrar.h"
34#include "net/android/net_jni_registrar.h"
35#include "ui/base/android/ui_base_jni_registrar.h"
36#include "ui/gfx/android/gfx_jni_registrar.h"
37#include "ui/gl/android/gl_jni_registrar.h"
38#include "ui/shell_dialogs/android/shell_dialogs_jni_registrar.h"
39#endif
40
41namespace content {
42
43class ContentTestSuiteBaseListener : public testing::EmptyTestEventListener {
44 public:
45  ContentTestSuiteBaseListener() {
46  }
47  virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
48    BrowserThreadImpl::FlushThreadPoolHelperForTesting();
49  }
50 private:
51  DISALLOW_COPY_AND_ASSIGN(ContentTestSuiteBaseListener);
52};
53
54ContentTestSuiteBase::ContentTestSuiteBase(int argc, char** argv)
55    : base::TestSuite(argc, argv) {
56}
57
58void ContentTestSuiteBase::Initialize() {
59  base::TestSuite::Initialize();
60
61  // Initialize the histograms subsystem, so that any histograms hit in tests
62  // are correctly registered with the statistics recorder and can be queried
63  // by tests.
64  base::StatisticsRecorder::Initialize();
65
66#if defined(OS_ANDROID)
67  // Register JNI bindings for android.
68  JNIEnv* env = base::android::AttachCurrentThread();
69  content::android::RegisterCommonJni(env);
70  content::android::RegisterBrowserJni(env);
71  gfx::android::RegisterJni(env);
72  media::RegisterJni(env);
73  net::android::RegisterJni(env);
74  ui::android::RegisterJni(env);
75  ui::shell_dialogs::RegisterJni(env);
76
77  content::Compositor::Initialize();
78#endif
79
80  testing::UnitTest::GetInstance()->listeners().Append(
81      new ContentTestSuiteBaseListener);
82}
83
84void ContentTestSuiteBase::RegisterContentSchemes(
85    ContentClient* content_client) {
86  SetContentClient(content_client);
87  content::RegisterContentSchemes(false);
88  SetContentClient(NULL);
89}
90
91void ContentTestSuiteBase::RegisterInProcessThreads() {
92#if !defined(OS_IOS)
93  UtilityProcessHostImpl::RegisterUtilityMainThreadFactory(
94      CreateInProcessUtilityThread);
95  RenderProcessHostImpl::RegisterRendererMainThreadFactory(
96      CreateInProcessRendererThread);
97  GpuProcessHost::RegisterGpuMainThreadFactory(CreateInProcessGpuThread);
98#endif
99}
100
101}  // namespace content
102