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.
5 #include "content/test/webkit_support.h"
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"
22 #include "ui/gfx/win/dpi.h"
25 #if defined(OS_ANDROID)
26 #include "base/android/jni_android.h"
27 #include "net/android/network_library.h"
30 #if defined(OS_MACOSX)
31 #include "base/test/mock_chrome_application_mac.h"
38 void EnableBlinkPlatformLogChannels(const std::string
& channels
) {
41 base::StringTokenizer
t(channels
, ", ");
43 blink::enableLogChannel(t
.token().c_str());
46 void ParseBlinkCommandLineArgumentsForUnitTests() {
47 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
48 EnableBlinkPlatformLogChannels(
49 command_line
.GetSwitchValueASCII(switches::kBlinkPlatformLogChannels
));
52 class TestEnvironment
{
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
;
58 typedef base::MessageLoopForUI MessageLoopType
;
62 main_message_loop_
.reset(new MessageLoopType
);
64 // TestWebKitPlatformSupport must be instantiated after MessageLoopType.
65 webkit_platform_support_
.reset(new TestWebKitPlatformSupport
);
71 TestWebKitPlatformSupport
* webkit_platform_support() const {
72 return webkit_platform_support_
.get();
76 scoped_ptr
<MessageLoopType
> main_message_loop_
;
77 scoped_ptr
<TestWebKitPlatformSupport
> webkit_platform_support_
;
80 TestEnvironment
* test_environment
;
84 void SetUpTestEnvironmentForUnitTests() {
85 ParseBlinkCommandLineArgumentsForUnitTests();
87 blink::WebRuntimeFeatures::enableExperimentalFeatures(true);
88 blink::WebRuntimeFeatures::enableTestOnlyFeatures(true);
90 #if defined(OS_ANDROID)
91 JNIEnv
* env
= base::android::AttachCurrentThread();
92 net::android::RegisterNetworkLibrary(env
);
95 #if defined(OS_MACOSX)
96 mock_cr_app::RegisterMockCrApp();
100 gfx::InitDeviceScaleFactor(1.0f
);
103 // Explicitly initialize the GURL library before spawning any threads.
104 // Otherwise crash may happend when different threads try to create a GURL
107 test_environment
= new TestEnvironment
;
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();
115 if (RunningOnValgrind())
116 blink::WebCache::clear();
117 delete test_environment
;
118 test_environment
= NULL
;
121 } // namespace content