Make sure webrtc::VideoSource is released when WebRtcVideoTrackAdapter is destroyed.
[chromium-blink-merge.git] / content / test / webkit_support.cc
blob0ef980a57ad864e2362416767fca6ef90ea0683c
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"
7 #include <string>
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"
26 #endif
28 #if defined(OS_MACOSX)
29 #include "base/test/mock_chrome_application_mac.h"
30 #endif
32 namespace content {
34 namespace {
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());
44 void ParseBlinkCommandLineArgumentsForUnitTests() {
45 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
46 EnableBlinkPlatformLogChannels(
47 command_line.GetSwitchValueASCII(switches::kBlinkPlatformLogChannels));
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
59 TestEnvironment() {
60 main_message_loop_.reset(new MessageLoopType);
62 // TestWebKitPlatformSupport must be instantiated after MessageLoopType.
63 webkit_platform_support_.reset(new TestWebKitPlatformSupport);
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
73 ~TestEnvironment() {
74 #if defined(OS_WIN)
75 ui::ResourceBundle::CleanupSharedInstance();
76 #endif
79 TestWebKitPlatformSupport* webkit_platform_support() const {
80 return webkit_platform_support_.get();
83 private:
84 scoped_ptr<MessageLoopType> main_message_loop_;
85 scoped_ptr<TestWebKitPlatformSupport> webkit_platform_support_;
88 TestEnvironment* test_environment;
90 } // namespace
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);
101 #endif
103 #if defined(OS_MACOSX)
104 mock_cr_app::RegisterMockCrApp();
105 #endif
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;
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