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.
4 
5 #include "content/public/test/blink_test_environment.h"
6 
7 #include <string>
8 
9 #include "base/command_line.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string_tokenizer.h"
12 #include "base/test/test_discardable_memory_allocator.h"
13 #include "build/build_config.h"
14 #include "content/common/content_switches_internal.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/blink/public/platform/web_cache.h"
20 #include "third_party/blink/public/platform/web_runtime_features.h"
21 #include "third_party/blink/public/web/blink.h"
22 
23 #if defined(OS_WIN)
24 #include "ui/display/win/dpi.h"
25 #endif
26 
27 #if defined(OS_MAC)
28 #include "base/test/mock_chrome_application_mac.h"
29 #endif
30 
31 namespace content {
32 
33 namespace {
34 
35 class TestEnvironment {
36  public:
TestEnvironment()37   TestEnvironment()
38       : blink_test_support_(
39             TestBlinkWebUnitTestSupport::SchedulerType::kRealScheduler) {
40     base::DiscardableMemoryAllocator::SetInstance(
41         &discardable_memory_allocator_);
42   }
43 
~TestEnvironment()44   ~TestEnvironment() {}
45 
46   // This returns when both the main thread and the TaskSchedules queues are
47   // empty.
RunUntilIdle()48   void RunUntilIdle() { base::RunLoop().RunUntilIdle(); }
49 
50  private:
51   TestBlinkWebUnitTestSupport blink_test_support_;
52   TestContentClientInitializer content_initializer_;
53   base::TestDiscardableMemoryAllocator discardable_memory_allocator_;
54 };
55 
56 TestEnvironment* test_environment;
57 
58 }  // namespace
59 
SetUpBlinkTestEnvironment()60 void SetUpBlinkTestEnvironment() {
61   blink::WebRuntimeFeatures::EnableExperimentalFeatures(true);
62   blink::WebRuntimeFeatures::EnableTestOnlyFeatures(true);
63 
64   const base::CommandLine& command_line =
65       *base::CommandLine::ForCurrentProcess();
66   for (const std::string& feature : content::FeaturesFromSwitch(
67            command_line, switches::kEnableBlinkFeatures)) {
68     blink::WebRuntimeFeatures::EnableFeatureFromString(feature, true);
69   }
70   for (const std::string& feature : content::FeaturesFromSwitch(
71            command_line, switches::kDisableBlinkFeatures)) {
72     blink::WebRuntimeFeatures::EnableFeatureFromString(feature, false);
73   }
74 
75 #if defined(OS_MAC)
76   mock_cr_app::RegisterMockCrApp();
77 #endif
78 
79 #if defined(OS_WIN)
80   display::win::SetDefaultDeviceScaleFactor(1.0f);
81 #endif
82 
83   test_environment = new TestEnvironment;
84 }
85 
TearDownBlinkTestEnvironment()86 void TearDownBlinkTestEnvironment() {
87   // Flush any remaining messages before we kill ourselves.
88   // http://code.google.com/p/chromium/issues/detail?id=9500
89   test_environment->RunUntilIdle();
90 
91   delete test_environment;
92   test_environment = nullptr;
93 }
94 
95 }  // namespace content
96