• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/test/webkit_support.h"
6 
7 #include <string>
8 
9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_tokenizer.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/common/user_agent.h"
15 #include "content/test/test_webkit_platform_support.h"
16 #include "third_party/WebKit/public/web/WebCache.h"
17 #include "third_party/WebKit/public/web/WebKit.h"
18 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
19 #include "url/url_util.h"
20 
21 #if defined(OS_WIN)
22 #include "ui/gfx/win/dpi.h"
23 #endif
24 
25 #if defined(OS_ANDROID)
26 #include "base/android/jni_android.h"
27 #include "net/android/network_library.h"
28 #endif
29 
30 #if defined(OS_MACOSX)
31 #include "base/test/mock_chrome_application_mac.h"
32 #endif
33 
34 namespace content {
35 
36 namespace {
37 
EnableBlinkPlatformLogChannels(const std::string & channels)38 void EnableBlinkPlatformLogChannels(const std::string& channels) {
39   if (channels.empty())
40     return;
41   base::StringTokenizer t(channels, ", ");
42   while (t.GetNext())
43     blink::enableLogChannel(t.token().c_str());
44 }
45 
ParseBlinkCommandLineArgumentsForUnitTests()46 void ParseBlinkCommandLineArgumentsForUnitTests() {
47   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
48   EnableBlinkPlatformLogChannels(
49       command_line.GetSwitchValueASCII(switches::kBlinkPlatformLogChannels));
50 }
51 
52 class TestEnvironment {
53  public:
54 #if defined(OS_ANDROID)
55   // Android UI message loop goes through Java, so don't use it in tests.
56   typedef base::MessageLoop MessageLoopType;
57 #else
58   typedef base::MessageLoopForUI MessageLoopType;
59 #endif
60 
TestEnvironment()61   TestEnvironment() {
62     main_message_loop_.reset(new MessageLoopType);
63 
64     // TestWebKitPlatformSupport must be instantiated after MessageLoopType.
65     webkit_platform_support_.reset(new TestWebKitPlatformSupport);
66   }
67 
~TestEnvironment()68   ~TestEnvironment() {
69   }
70 
webkit_platform_support() const71   TestWebKitPlatformSupport* webkit_platform_support() const {
72     return webkit_platform_support_.get();
73   }
74 
75  private:
76   scoped_ptr<MessageLoopType> main_message_loop_;
77   scoped_ptr<TestWebKitPlatformSupport> webkit_platform_support_;
78 };
79 
80 TestEnvironment* test_environment;
81 
82 }  // namespace
83 
SetUpTestEnvironmentForUnitTests()84 void SetUpTestEnvironmentForUnitTests() {
85   ParseBlinkCommandLineArgumentsForUnitTests();
86 
87   blink::WebRuntimeFeatures::enableExperimentalFeatures(true);
88   blink::WebRuntimeFeatures::enableTestOnlyFeatures(true);
89 
90 #if defined(OS_ANDROID)
91   JNIEnv* env = base::android::AttachCurrentThread();
92   net::android::RegisterNetworkLibrary(env);
93 #endif
94 
95 #if defined(OS_MACOSX)
96   mock_cr_app::RegisterMockCrApp();
97 #endif
98 
99 #if defined(OS_WIN)
100   gfx::InitDeviceScaleFactor(1.0f);
101 #endif
102 
103   // Explicitly initialize the GURL library before spawning any threads.
104   // Otherwise crash may happend when different threads try to create a GURL
105   // at same time.
106   url::Initialize();
107   test_environment = new TestEnvironment;
108 }
109 
TearDownTestEnvironment()110 void TearDownTestEnvironment() {
111   // Flush any remaining messages before we kill ourselves.
112   // http://code.google.com/p/chromium/issues/detail?id=9500
113   base::RunLoop().RunUntilIdle();
114 
115   if (RunningOnValgrind())
116     blink::WebCache::clear();
117   delete test_environment;
118   test_environment = NULL;
119 }
120 
121 }  // namespace content
122