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/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_tokenizer.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/common/user_agent.h"
16 #include "content/test/test_webkit_platform_support.h"
17 #include "third_party/WebKit/public/web/WebCache.h"
18 #include "third_party/WebKit/public/web/WebKit.h"
19 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "url/url_util.h"
23 #if defined(OS_ANDROID)
24 #include "base/android/jni_android.h"
25 #include "net/android/network_library.h"
28 #if defined(OS_MACOSX)
29 #include "base/test/mock_chrome_application_mac.h"
36 void EnableBlinkPlatformLogChannels(const std::string
& channels
) {
39 base::StringTokenizer
t(channels
, ", ");
41 blink::enableLogChannel(t
.token().c_str());
44 void ParseBlinkCommandLineArgumentsForUnitTests() {
45 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
46 EnableBlinkPlatformLogChannels(
47 command_line
.GetSwitchValueASCII(switches::kBlinkPlatformLogChannels
));
50 class TestEnvironment
{
52 #if defined(OS_ANDROID)
53 // Android UI message loop goes through Java, so don't use it in tests.
54 typedef base::MessageLoop MessageLoopType
;
56 typedef base::MessageLoopForUI MessageLoopType
;
60 main_message_loop_
.reset(new MessageLoopType
);
62 // TestWebKitPlatformSupport must be instantiated after MessageLoopType.
63 webkit_platform_support_
.reset(new TestWebKitPlatformSupport
);
66 base::FilePath pak_file
;
67 PathService::Get(base::DIR_MODULE
, &pak_file
);
68 pak_file
= pak_file
.AppendASCII("ui_test.pak");
69 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file
);
75 ui::ResourceBundle::CleanupSharedInstance();
79 TestWebKitPlatformSupport
* webkit_platform_support() const {
80 return webkit_platform_support_
.get();
84 scoped_ptr
<MessageLoopType
> main_message_loop_
;
85 scoped_ptr
<TestWebKitPlatformSupport
> webkit_platform_support_
;
88 TestEnvironment
* test_environment
;
92 void SetUpTestEnvironmentForUnitTests() {
93 ParseBlinkCommandLineArgumentsForUnitTests();
95 blink::WebRuntimeFeatures::enableExperimentalFeatures(true);
96 blink::WebRuntimeFeatures::enableTestOnlyFeatures(true);
98 #if defined(OS_ANDROID)
99 JNIEnv
* env
= base::android::AttachCurrentThread();
100 net::android::RegisterNetworkLibrary(env
);
103 #if defined(OS_MACOSX)
104 mock_cr_app::RegisterMockCrApp();
107 // Explicitly initialize the GURL library before spawning any threads.
108 // Otherwise crash may happend when different threads try to create a GURL
111 test_environment
= new TestEnvironment
;
114 void TearDownTestEnvironment() {
115 // Flush any remaining messages before we kill ourselves.
116 // http://code.google.com/p/chromium/issues/detail?id=9500
117 base::RunLoop().RunUntilIdle();
119 if (RunningOnValgrind())
120 blink::WebCache::clear();
121 delete test_environment
;
122 test_environment
= NULL
;
125 } // namespace content