[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / test / blink_test_environment.cc
blobc20e986ad0cb67fd1ebb1191d24b6bf145c7873f
1 // Copyright 2014 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/blink_test_environment.h"
7 #include <string>
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 "base/test/test_discardable_memory_allocator.h"
14 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/user_agent.h"
17 #include "content/public/test/test_content_client_initializer.h"
18 #include "content/test/test_blink_web_unit_test_support.h"
19 #include "third_party/WebKit/public/web/WebCache.h"
20 #include "third_party/WebKit/public/web/WebKit.h"
21 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
22 #include "url/url_util.h"
24 #if defined(OS_WIN)
25 #include "ui/gfx/win/dpi.h"
26 #endif
28 #if defined(OS_ANDROID)
29 #include "base/android/jni_android.h"
30 #include "net/android/network_library.h"
31 #endif
33 #if defined(OS_MACOSX)
34 #include "base/test/mock_chrome_application_mac.h"
35 #endif
37 namespace content {
39 namespace {
41 void EnableBlinkPlatformLogChannels(const std::string& channels) {
42 if (channels.empty())
43 return;
44 base::StringTokenizer t(channels, ", ");
45 while (t.GetNext())
46 blink::enableLogChannel(t.token().c_str());
49 void ParseBlinkCommandLineArgumentsForUnitTests() {
50 const base::CommandLine& command_line =
51 *base::CommandLine::ForCurrentProcess();
52 EnableBlinkPlatformLogChannels(
53 command_line.GetSwitchValueASCII(switches::kBlinkPlatformLogChannels));
56 class TestEnvironment {
57 public:
58 #if defined(OS_ANDROID)
59 // Android UI message loop goes through Java, so don't use it in tests.
60 typedef base::MessageLoop MessageLoopType;
61 #else
62 typedef base::MessageLoopForUI MessageLoopType;
63 #endif
65 TestEnvironment() {
66 main_message_loop_.reset(new MessageLoopType);
68 // TestBlinkWebUnitTestSupport must be instantiated after MessageLoopType.
69 blink_test_support_.reset(new TestBlinkWebUnitTestSupport);
70 content_initializer_.reset(new content::TestContentClientInitializer());
72 base::DiscardableMemoryAllocator::SetInstance(
73 &discardable_memory_allocator_);
76 ~TestEnvironment() {
79 TestBlinkWebUnitTestSupport* blink_platform_impl() const {
80 return blink_test_support_.get();
83 private:
84 scoped_ptr<MessageLoopType> main_message_loop_;
85 scoped_ptr<TestBlinkWebUnitTestSupport> blink_test_support_;
86 scoped_ptr<TestContentClientInitializer> content_initializer_;
87 base::TestDiscardableMemoryAllocator discardable_memory_allocator_;
90 TestEnvironment* test_environment;
92 } // namespace
94 void SetUpBlinkTestEnvironment() {
95 ParseBlinkCommandLineArgumentsForUnitTests();
97 blink::WebRuntimeFeatures::enableExperimentalFeatures(true);
98 blink::WebRuntimeFeatures::enableTestOnlyFeatures(true);
100 #if defined(OS_ANDROID)
101 JNIEnv* env = base::android::AttachCurrentThread();
102 net::android::RegisterNetworkLibrary(env);
103 #endif
105 #if defined(OS_MACOSX)
106 mock_cr_app::RegisterMockCrApp();
107 #endif
109 #if defined(OS_WIN)
110 gfx::InitDeviceScaleFactor(1.0f);
111 #endif
113 // Explicitly initialize the GURL library before spawning any threads.
114 // Otherwise crash may happend when different threads try to create a GURL
115 // at same time.
116 url::Initialize();
117 test_environment = new TestEnvironment;
120 void TearDownBlinkTestEnvironment() {
121 // Flush any remaining messages before we kill ourselves.
122 // http://code.google.com/p/chromium/issues/detail?id=9500
123 base::RunLoop().RunUntilIdle();
125 if (RunningOnValgrind())
126 blink::WebCache::clear();
127 delete test_environment;
128 test_environment = NULL;
131 } // namespace content