1 // Copyright 2015 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 "third_party/blink/renderer/core/testing/sim/sim_test.h"
6 
7 #include "base/run_loop.h"
8 #include "content/test/test_blink_web_unit_test_support.h"
9 #include "third_party/blink/public/platform/web_cache.h"
10 #include "third_party/blink/public/web/web_navigation_params.h"
11 #include "third_party/blink/renderer/core/dom/document.h"
12 #include "third_party/blink/renderer/core/exported/web_view_impl.h"
13 #include "third_party/blink/renderer/core/frame/local_dom_window.h"
14 #include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
15 #include "third_party/blink/renderer/core/loader/document_loader.h"
16 #include "third_party/blink/renderer/core/scroll/scrollbar_theme.h"
17 #include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
18 
19 namespace blink {
20 
SimTest()21 SimTest::SimTest() {
22   Document::SetThreadedParsingEnabledForTesting(false);
23   // Threaded animations are usually enabled for blink. However these tests use
24   // synchronous compositing, which can not run threaded animations.
25   bool was_threaded_animation_enabled =
26       content::TestBlinkWebUnitTestSupport::SetThreadedAnimationEnabled(false);
27   // If this fails, we'd be resetting IsThreadedAnimationEnabled() to the wrong
28   // thing in the destructor.
29   DCHECK(was_threaded_animation_enabled);
30 }
31 
~SimTest()32 SimTest::~SimTest() {
33   Document::SetThreadedParsingEnabledForTesting(true);
34   content::TestBlinkWebUnitTestSupport::SetThreadedAnimationEnabled(true);
35   WebCache::Clear();
36 }
37 
SetUp()38 void SimTest::SetUp() {
39   Test::SetUp();
40 
41   // SimCompositor overrides the LayerTreeViewDelegate to respond to
42   // BeginMainFrame(), which will update and paint the main frame of the
43   // WebViewImpl given to SetWebView().
44   network_ = std::make_unique<SimNetwork>();
45   compositor_ = std::make_unique<SimCompositor>();
46   web_frame_client_ =
47       std::make_unique<frame_test_helpers::TestWebFrameClient>();
48   web_view_client_ = std::make_unique<frame_test_helpers::TestWebViewClient>();
49   page_ = std::make_unique<SimPage>();
50   web_view_helper_ = std::make_unique<frame_test_helpers::WebViewHelper>();
51 
52   web_view_helper_->Initialize(web_frame_client_.get(), web_view_client_.get(),
53                                compositor_.get());
54   compositor_->SetWebView(WebView(), *web_view_client_);
55   page_->SetPage(WebView().GetPage());
56   local_frame_root_ = WebView().MainFrameImpl();
57 }
58 
TearDown()59 void SimTest::TearDown() {
60   // Pump the message loop to process the load event.
61   //
62   // Use RunUntilIdle() instead of blink::test::RunPendingTask(), because
63   // blink::test::RunPendingTask() posts directly to
64   // Thread::Current()->GetTaskRunner(), which makes it incompatible with a
65   // TestingPlatformSupportWithMockScheduler.
66   base::RunLoop().RunUntilIdle();
67 
68   // Shut down this stuff before settings change to keep the world
69   // consistent, and before the subclass tears down.
70   web_view_helper_.reset();
71   page_.reset();
72   web_view_client_.reset();
73   web_frame_client_.reset();
74   compositor_.reset();
75   network_.reset();
76   local_frame_root_ = nullptr;
77   base::RunLoop().RunUntilIdle();
78 }
79 
InitializeRemote()80 void SimTest::InitializeRemote() {
81   web_view_helper_->InitializeRemote();
82   compositor_->SetWebView(WebView(), *web_view_client_);
83   page_->SetPage(WebView().GetPage());
84   web_frame_client_ =
85       std::make_unique<frame_test_helpers::TestWebFrameClient>();
86   local_frame_root_ = frame_test_helpers::CreateLocalChild(
87       *WebView().MainFrame()->ToWebRemoteFrame(), "local_frame_root",
88       WebFrameOwnerProperties(), nullptr, web_frame_client_.get(),
89       compositor_.get());
90 }
91 
LoadURL(const String & url_string)92 void SimTest::LoadURL(const String& url_string) {
93   KURL url(url_string);
94   frame_test_helpers::LoadFrameDontWait(local_frame_root_.Get(), url);
95   if (DocumentLoader::WillLoadUrlAsEmpty(url) || url.ProtocolIsData()) {
96     // Empty documents and data urls are not using mocked out SimRequests,
97     // but instead load data directly.
98     frame_test_helpers::PumpPendingRequestsForFrameToLoad(
99         local_frame_root_.Get());
100   }
101 }
102 
Window()103 LocalDOMWindow& SimTest::Window() {
104   return *GetDocument().domWindow();
105 }
106 
GetPage()107 SimPage& SimTest::GetPage() {
108   return *page_;
109 }
110 
GetDocument()111 Document& SimTest::GetDocument() {
112   return *WebView().MainFrameImpl()->GetFrame()->GetDocument();
113 }
114 
WebView()115 WebViewImpl& SimTest::WebView() {
116   return *web_view_helper_->GetWebView();
117 }
118 
MainFrame()119 WebLocalFrameImpl& SimTest::MainFrame() {
120   return *WebView().MainFrameImpl();
121 }
122 
LocalFrameRoot()123 WebLocalFrameImpl& SimTest::LocalFrameRoot() {
124   return *local_frame_root_;
125 }
126 
WebViewClient()127 frame_test_helpers::TestWebViewClient& SimTest::WebViewClient() {
128   return *web_view_client_;
129 }
130 
WebWidgetClient()131 frame_test_helpers::TestWebWidgetClient& SimTest::WebWidgetClient() {
132   return *compositor_;
133 }
134 
WebFrameClient()135 frame_test_helpers::TestWebFrameClient& SimTest::WebFrameClient() {
136   return *web_frame_client_;
137 }
138 
Compositor()139 SimCompositor& SimTest::Compositor() {
140   return *compositor_;
141 }
142 
ConsoleMessages()143 Vector<String>& SimTest::ConsoleMessages() {
144   return web_frame_client_->ConsoleMessages();
145 }
146 
147 }  // namespace blink
148