1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "tests/ceftests/test_handler.h"
6
7 #include "include/base/cef_bind.h"
8 #include "include/base/cef_logging.h"
9 #include "include/cef_command_line.h"
10 #include "include/cef_stream.h"
11 #include "include/views/cef_browser_view.h"
12 #include "include/views/cef_window.h"
13 #include "include/wrapper/cef_closure_task.h"
14 #include "include/wrapper/cef_stream_resource_handler.h"
15 #include "tests/ceftests/test_request.h"
16 #include "tests/shared/common/client_switches.h"
17
18 namespace {
19
20 // Delegate implementation for the CefWindow that will host the Views-based
21 // browser.
22 class TestWindowDelegate : public CefWindowDelegate {
23 public:
24 // Create a new top-level Window hosting |browser_view|.
CreateBrowserWindow(CefRefPtr<CefBrowserView> browser_view,const std::string & title)25 static void CreateBrowserWindow(CefRefPtr<CefBrowserView> browser_view,
26 const std::string& title) {
27 CefWindow::CreateTopLevelWindow(
28 new TestWindowDelegate(browser_view, "CefUnitTestViews " + title));
29 }
30
31 // CefWindowDelegate methods:
32
OnWindowCreated(CefRefPtr<CefWindow> window)33 void OnWindowCreated(CefRefPtr<CefWindow> window) override {
34 // Add the browser view and show the window.
35 window->CenterWindow(CefSize(800, 600));
36 window->SetTitle(title_);
37 window->AddChildView(browser_view_);
38 window->Show();
39 }
40
OnWindowDestroyed(CefRefPtr<CefWindow> window)41 void OnWindowDestroyed(CefRefPtr<CefWindow> window) override {
42 browser_view_ = nullptr;
43 }
44
CanClose(CefRefPtr<CefWindow> window)45 bool CanClose(CefRefPtr<CefWindow> window) override {
46 // Allow the window to close if the browser says it's OK.
47 CefRefPtr<CefBrowser> browser = browser_view_->GetBrowser();
48 if (browser)
49 return browser->GetHost()->TryCloseBrowser();
50 return true;
51 }
52
53 private:
TestWindowDelegate(CefRefPtr<CefBrowserView> browser_view,const CefString & title)54 TestWindowDelegate(CefRefPtr<CefBrowserView> browser_view,
55 const CefString& title)
56 : browser_view_(browser_view), title_(title) {}
57
58 CefRefPtr<CefBrowserView> browser_view_;
59 CefString title_;
60
61 IMPLEMENT_REFCOUNTING(TestWindowDelegate);
62 DISALLOW_COPY_AND_ASSIGN(TestWindowDelegate);
63 };
64
65 // Delegate implementation for the CefBrowserView.
66 class TestBrowserViewDelegate : public CefBrowserViewDelegate {
67 public:
TestBrowserViewDelegate()68 TestBrowserViewDelegate() {}
69
70 // CefBrowserViewDelegate methods:
71
OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,CefRefPtr<CefBrowserView> popup_browser_view,bool is_devtools)72 bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
73 CefRefPtr<CefBrowserView> popup_browser_view,
74 bool is_devtools) override {
75 // Create our own Window for popups. It will show itself after creation.
76 TestWindowDelegate::CreateBrowserWindow(popup_browser_view,
77 is_devtools ? "DevTools" : "Popup");
78 return true;
79 }
80
81 private:
82 IMPLEMENT_REFCOUNTING(TestBrowserViewDelegate);
83 DISALLOW_COPY_AND_ASSIGN(TestBrowserViewDelegate);
84 };
85
86 } // namespace
87
88 // TestHandler::CompletionState
89
CompletionState(int total)90 TestHandler::CompletionState::CompletionState(int total)
91 : total_(total), count_(0) {
92 event_ = CefWaitableEvent::CreateWaitableEvent(true, false);
93 }
94
TestComplete()95 void TestHandler::CompletionState::TestComplete() {
96 if (++count_ == total_) {
97 count_ = 0;
98
99 // Signal that the test is now complete. Do not access any object members
100 // after this call because |this| might be deleted.
101 event_->Signal();
102 }
103 }
104
WaitForTests()105 void TestHandler::CompletionState::WaitForTests() {
106 // Wait for the test to complete
107 event_->Wait();
108
109 // Reset the event so the same test can be executed again.
110 event_->Reset();
111 }
112
113 // TestHandler::Collection
114
Collection(CompletionState * completion_state)115 TestHandler::Collection::Collection(CompletionState* completion_state)
116 : completion_state_(completion_state) {
117 EXPECT_TRUE(completion_state_);
118 }
119
AddTestHandler(TestHandler * test_handler)120 void TestHandler::Collection::AddTestHandler(TestHandler* test_handler) {
121 EXPECT_EQ(test_handler->completion_state_, completion_state_);
122 handler_list_.push_back(test_handler);
123 }
124
ExecuteTests()125 void TestHandler::Collection::ExecuteTests() {
126 EXPECT_GT(handler_list_.size(), 0UL);
127
128 TestHandlerList::const_iterator it;
129
130 it = handler_list_.begin();
131 for (; it != handler_list_.end(); ++it)
132 (*it)->SetupTest();
133
134 completion_state_->WaitForTests();
135
136 it = handler_list_.begin();
137 for (; it != handler_list_.end(); ++it)
138 (*it)->RunTest();
139
140 completion_state_->WaitForTests();
141 }
142
143 // TestHandler::UIThreadHelper
144
UIThreadHelper()145 TestHandler::UIThreadHelper::UIThreadHelper() : weak_ptr_factory_(this) {}
146
PostTask(const base::Closure & task)147 void TestHandler::UIThreadHelper::PostTask(const base::Closure& task) {
148 EXPECT_UI_THREAD();
149 CefPostTask(TID_UI, base::Bind(&UIThreadHelper::TaskHelper,
150 weak_ptr_factory_.GetWeakPtr(), task));
151 }
152
PostDelayedTask(const base::Closure & task,int delay_ms)153 void TestHandler::UIThreadHelper::PostDelayedTask(const base::Closure& task,
154 int delay_ms) {
155 EXPECT_UI_THREAD();
156 CefPostDelayedTask(TID_UI,
157 base::Bind(&UIThreadHelper::TaskHelper,
158 weak_ptr_factory_.GetWeakPtr(), task),
159 delay_ms);
160 }
161
TaskHelper(const base::Closure & task)162 void TestHandler::UIThreadHelper::TaskHelper(const base::Closure& task) {
163 EXPECT_UI_THREAD();
164 task.Run();
165 }
166
167 // TestHandler
168
169 int TestHandler::browser_count_ = 0;
170
TestHandler(CompletionState * completion_state)171 TestHandler::TestHandler(CompletionState* completion_state)
172 : first_browser_id_(0),
173 signal_completion_when_all_browsers_close_(true),
174 destroy_event_(nullptr),
175 destroy_test_expected_(true),
176 destroy_test_called_(false) {
177 if (completion_state) {
178 completion_state_ = completion_state;
179 completion_state_owned_ = false;
180 } else {
181 completion_state_ = new CompletionState(1);
182 completion_state_owned_ = true;
183 }
184 }
185
~TestHandler()186 TestHandler::~TestHandler() {
187 DCHECK(!ui_thread_helper_.get());
188 if (destroy_test_expected_)
189 EXPECT_TRUE(destroy_test_called_);
190 else
191 EXPECT_FALSE(destroy_test_called_);
192 EXPECT_TRUE(browser_map_.empty());
193
194 if (completion_state_owned_)
195 delete completion_state_;
196
197 if (destroy_event_)
198 destroy_event_->Signal();
199 }
200
OnAfterCreated(CefRefPtr<CefBrowser> browser)201 void TestHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
202 EXPECT_UI_THREAD();
203
204 browser_count_++;
205
206 const int browser_id = browser->GetIdentifier();
207 EXPECT_EQ(browser_map_.find(browser_id), browser_map_.end());
208 if (browser_map_.empty()) {
209 first_browser_id_ = browser_id;
210 first_browser_ = browser;
211 }
212 browser_map_.insert(std::make_pair(browser_id, browser));
213 }
214
OnBeforeClose(CefRefPtr<CefBrowser> browser)215 void TestHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
216 EXPECT_UI_THREAD();
217
218 // Free the browser pointer so that the browser can be destroyed.
219 const int browser_id = browser->GetIdentifier();
220 BrowserMap::iterator it = browser_map_.find(browser_id);
221 EXPECT_NE(it, browser_map_.end());
222 browser_map_.erase(it);
223
224 if (browser_id == first_browser_id_) {
225 first_browser_id_ = 0;
226 first_browser_ = nullptr;
227 }
228
229 if (browser_map_.empty() && signal_completion_when_all_browsers_close_) {
230 // Signal that the test is now complete.
231 TestComplete();
232 }
233
234 browser_count_--;
235 }
236
237 namespace {
238
ToCefHeaderMap(const ResourceContent::HeaderMap & headerMap)239 CefResponse::HeaderMap ToCefHeaderMap(
240 const ResourceContent::HeaderMap& headerMap) {
241 CefResponse::HeaderMap result;
242 ResourceContent::HeaderMap::const_iterator it = headerMap.begin();
243 for (; it != headerMap.end(); ++it) {
244 result.insert(std::pair<CefString, CefString>(it->first, it->second));
245 }
246 return result;
247 }
248
249 } // namespace
250
GetResourceHandler(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request)251 CefRefPtr<CefResourceHandler> TestHandler::GetResourceHandler(
252 CefRefPtr<CefBrowser> browser,
253 CefRefPtr<CefFrame> frame,
254 CefRefPtr<CefRequest> request) {
255 EXPECT_IO_THREAD();
256
257 if (resource_map_.size() > 0) {
258 const std::string& url = test_request::GetPathURL(request->GetURL());
259 ResourceMap::const_iterator it = resource_map_.find(url);
260 if (it != resource_map_.end()) {
261 // Return the previously mapped resource
262 CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForData(
263 static_cast<void*>(const_cast<char*>(it->second.content().c_str())),
264 it->second.content().length());
265 return new CefStreamResourceHandler(
266 200, "OK", it->second.mimeType(),
267 ToCefHeaderMap(it->second.headerMap()), stream);
268 }
269 }
270
271 return nullptr;
272 }
273
OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,TerminationStatus status)274 void TestHandler::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
275 TerminationStatus status) {
276 LOG(WARNING) << "OnRenderProcessTerminated: status = " << status << ".";
277 }
278
GetBrowser()279 CefRefPtr<CefBrowser> TestHandler::GetBrowser() {
280 return first_browser_;
281 }
282
GetBrowserId()283 int TestHandler::GetBrowserId() {
284 return first_browser_id_;
285 }
286
GetAllBrowsers(BrowserMap * map)287 void TestHandler::GetAllBrowsers(BrowserMap* map) {
288 EXPECT_UI_THREAD();
289 EXPECT_TRUE(map);
290 *map = browser_map_;
291 }
292
ExecuteTest()293 void TestHandler::ExecuteTest() {
294 EXPECT_EQ(completion_state_->total(), 1);
295
296 // Reset any state from the previous run.
297 if (destroy_test_called_)
298 destroy_test_called_ = false;
299
300 // Run the test.
301 RunTest();
302
303 // Wait for the test to complete.
304 completion_state_->WaitForTests();
305 }
306
SetupComplete()307 void TestHandler::SetupComplete() {
308 // Signal that the test setup is complete.
309 completion_state_->TestComplete();
310 }
311
DestroyTest()312 void TestHandler::DestroyTest() {
313 if (!CefCurrentlyOn(TID_UI)) {
314 CefPostTask(TID_UI, base::Bind(&TestHandler::DestroyTest, this));
315 return;
316 }
317
318 EXPECT_TRUE(destroy_test_expected_);
319 if (destroy_test_called_)
320 return;
321 destroy_test_called_ = true;
322
323 if (!browser_map_.empty()) {
324 // Use a copy of the map since the original may be modified while we're
325 // iterating.
326 BrowserMap browser_map = browser_map_;
327
328 // Tell all browsers to close.
329 BrowserMap::const_iterator it = browser_map.begin();
330 for (; it != browser_map.end(); ++it)
331 CloseBrowser(it->second, false);
332 }
333
334 if (ui_thread_helper_.get())
335 ui_thread_helper_.reset(nullptr);
336 }
337
OnTestTimeout(int timeout_ms,bool treat_as_error)338 void TestHandler::OnTestTimeout(int timeout_ms, bool treat_as_error) {
339 EXPECT_UI_THREAD();
340 if (treat_as_error) {
341 EXPECT_TRUE(false) << "Test timed out after " << timeout_ms << "ms";
342 }
343 DestroyTest();
344 }
345
CreateBrowser(const CefString & url,CefRefPtr<CefRequestContext> request_context,CefRefPtr<CefDictionaryValue> extra_info)346 void TestHandler::CreateBrowser(const CefString& url,
347 CefRefPtr<CefRequestContext> request_context,
348 CefRefPtr<CefDictionaryValue> extra_info) {
349 const bool use_views = CefCommandLine::GetGlobalCommandLine()->HasSwitch(
350 client::switches::kUseViews);
351 if (use_views && !CefCurrentlyOn(TID_UI)) {
352 // Views classes must be accessed on the UI thread.
353 CefPostTask(TID_UI, base::Bind(&TestHandler::CreateBrowser, this, url,
354 request_context, extra_info));
355 return;
356 }
357
358 CefWindowInfo windowInfo;
359 CefBrowserSettings settings;
360 PopulateBrowserSettings(&settings);
361
362 if (use_views) {
363 // Create the BrowserView.
364 CefRefPtr<CefBrowserView> browser_view = CefBrowserView::CreateBrowserView(
365 this, url, settings, extra_info, request_context,
366 new TestBrowserViewDelegate());
367
368 // Create the Window. It will show itself after creation.
369 TestWindowDelegate::CreateBrowserWindow(browser_view, std::string());
370 } else {
371 #if defined(OS_WIN)
372 windowInfo.SetAsPopup(nullptr, "CefUnitTest");
373 windowInfo.style |= WS_VISIBLE;
374 #endif
375 CefBrowserHost::CreateBrowser(windowInfo, this, url, settings, extra_info,
376 request_context);
377 }
378 }
379
380 // static
CloseBrowser(CefRefPtr<CefBrowser> browser,bool force_close)381 void TestHandler::CloseBrowser(CefRefPtr<CefBrowser> browser,
382 bool force_close) {
383 browser->GetHost()->CloseBrowser(force_close);
384 }
385
AddResource(const std::string & url,const std::string & content,const std::string & mime_type)386 void TestHandler::AddResource(const std::string& url,
387 const std::string& content,
388 const std::string& mime_type) {
389 ResourceContent::HeaderMap headerMap = ResourceContent::HeaderMap();
390 ResourceContent rc = ResourceContent(content, mime_type, headerMap);
391 AddResourceEx(url, rc);
392 }
393
AddResource(const std::string & url,const std::string & content,const std::string & mime_type,const ResourceContent::HeaderMap & header_map)394 void TestHandler::AddResource(const std::string& url,
395 const std::string& content,
396 const std::string& mime_type,
397 const ResourceContent::HeaderMap& header_map) {
398 ResourceContent rc = ResourceContent(content, mime_type, header_map);
399 AddResourceEx(url, rc);
400 }
401
AddResourceEx(const std::string & url,const ResourceContent & content)402 void TestHandler::AddResourceEx(const std::string& url,
403 const ResourceContent& content) {
404 if (!CefCurrentlyOn(TID_IO)) {
405 CefPostTask(TID_IO,
406 base::Bind(&TestHandler::AddResourceEx, this, url, content));
407 return;
408 }
409
410 // Ignore the query component, if any.
411 std::string urlStr = url;
412 size_t idx = urlStr.find('?');
413 if (idx > 0)
414 urlStr = urlStr.substr(0, idx);
415
416 resource_map_.insert(std::make_pair(urlStr, content));
417 }
418
ClearResources()419 void TestHandler::ClearResources() {
420 if (!CefCurrentlyOn(TID_IO)) {
421 CefPostTask(TID_IO, base::Bind(&TestHandler::ClearResources, this));
422 return;
423 }
424
425 resource_map_.clear();
426 }
427
SetTestTimeout(int timeout_ms,bool treat_as_error)428 void TestHandler::SetTestTimeout(int timeout_ms, bool treat_as_error) {
429 if (!CefCurrentlyOn(TID_UI)) {
430 CefPostTask(TID_UI, base::Bind(&TestHandler::SetTestTimeout, this,
431 timeout_ms, treat_as_error));
432 return;
433 }
434
435 if (destroy_test_called_) {
436 // No need to set the timeout if the test has already completed.
437 return;
438 }
439
440 if (treat_as_error && CefCommandLine::GetGlobalCommandLine()->HasSwitch(
441 "disable-test-timeout")) {
442 return;
443 }
444
445 // Use a weak reference to |this| via UIThreadHelper so that the TestHandler
446 // can be destroyed before the timeout expires.
447 GetUIThreadHelper()->PostDelayedTask(
448 base::Bind(&TestHandler::OnTestTimeout, base::Unretained(this),
449 timeout_ms, treat_as_error),
450 timeout_ms);
451 }
452
TestComplete()453 void TestHandler::TestComplete() {
454 if (!CefCurrentlyOn(TID_UI)) {
455 CefPostTask(TID_UI, base::Bind(&TestHandler::TestComplete, this));
456 return;
457 }
458
459 EXPECT_TRUE(browser_map_.empty());
460 completion_state_->TestComplete();
461 }
462
GetUIThreadHelper()463 TestHandler::UIThreadHelper* TestHandler::GetUIThreadHelper() {
464 EXPECT_UI_THREAD();
465 CHECK(!destroy_test_called_);
466
467 if (!ui_thread_helper_.get())
468 ui_thread_helper_.reset(new UIThreadHelper());
469 return ui_thread_helper_.get();
470 }
471
472 // global functions
473
TestFailed()474 bool TestFailed() {
475 CefRefPtr<CefCommandLine> command_line =
476 CefCommandLine::GetGlobalCommandLine();
477 if (command_line->HasSwitch("single-process")) {
478 // Check for a failure on the current test only.
479 return ::testing::UnitTest::GetInstance()
480 ->current_test_info()
481 ->result()
482 ->Failed();
483 } else {
484 // Check for any global failure.
485 return ::testing::UnitTest::GetInstance()->Failed();
486 }
487 }
488