Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / test / webkit_support.cc
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/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"
22
23 #if defined(OS_ANDROID)
24 #include "base/android/jni_android.h"
25 #include "net/android/network_library.h"
26 #endif
27
28 #if defined(OS_MACOSX)
29 #include "base/test/mock_chrome_application_mac.h"
30 #endif
31
32 namespace content {
33
34 namespace {
35
36 void EnableBlinkPlatformLogChannels(const std::string& channels) {
37   if (channels.empty())
38     return;
39   base::StringTokenizer t(channels, ", ");
40   while (t.GetNext())
41     blink::enableLogChannel(t.token().c_str());
42 }
43
44 void ParseBlinkCommandLineArgumentsForUnitTests() {
45   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
46   EnableBlinkPlatformLogChannels(
47       command_line.GetSwitchValueASCII(switches::kBlinkPlatformLogChannels));
48 }
49
50 class TestEnvironment {
51  public:
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;
55 #else
56   typedef base::MessageLoopForUI MessageLoopType;
57 #endif
58
59   TestEnvironment() {
60     main_message_loop_.reset(new MessageLoopType);
61
62     // TestWebKitPlatformSupport must be instantiated after MessageLoopType.
63     webkit_platform_support_.reset(new TestWebKitPlatformSupport);
64
65 #if defined(OS_WIN)
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);
70 #endif
71   }
72
73   ~TestEnvironment() {
74 #if defined(OS_WIN)
75     ui::ResourceBundle::CleanupSharedInstance();
76 #endif
77   }
78
79   TestWebKitPlatformSupport* webkit_platform_support() const {
80     return webkit_platform_support_.get();
81   }
82
83  private:
84   scoped_ptr<MessageLoopType> main_message_loop_;
85   scoped_ptr<TestWebKitPlatformSupport> webkit_platform_support_;
86 };
87
88 TestEnvironment* test_environment;
89
90 }  // namespace
91
92 void SetUpTestEnvironmentForUnitTests() {
93   ParseBlinkCommandLineArgumentsForUnitTests();
94
95   blink::WebRuntimeFeatures::enableExperimentalFeatures(true);
96   blink::WebRuntimeFeatures::enableTestOnlyFeatures(true);
97
98 #if defined(OS_ANDROID)
99   JNIEnv* env = base::android::AttachCurrentThread();
100   net::android::RegisterNetworkLibrary(env);
101 #endif
102
103 #if defined(OS_MACOSX)
104   mock_cr_app::RegisterMockCrApp();
105 #endif
106
107   // Explicitly initialize the GURL library before spawning any threads.
108   // Otherwise crash may happend when different threads try to create a GURL
109   // at same time.
110   url::Initialize();
111   test_environment = new TestEnvironment;
112 }
113
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();
118
119   if (RunningOnValgrind())
120     blink::WebCache::clear();
121   delete test_environment;
122   test_environment = NULL;
123 }
124
125 }  // namespace content