• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_callback.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(base::OnceClosure task)147 void TestHandler::UIThreadHelper::PostTask(base::OnceClosure task) {
148   EXPECT_UI_THREAD();
149   CefPostTask(TID_UI,
150               base::BindOnce(&UIThreadHelper::TaskHelper,
151                              weak_ptr_factory_.GetWeakPtr(), std::move(task)));
152 }
153 
PostDelayedTask(base::OnceClosure task,int delay_ms)154 void TestHandler::UIThreadHelper::PostDelayedTask(base::OnceClosure task,
155                                                   int delay_ms) {
156   EXPECT_UI_THREAD();
157   CefPostDelayedTask(
158       TID_UI,
159       base::BindOnce(&UIThreadHelper::TaskHelper,
160                      weak_ptr_factory_.GetWeakPtr(), std::move(task)),
161       delay_ms);
162 }
163 
TaskHelper(base::OnceClosure task)164 void TestHandler::UIThreadHelper::TaskHelper(base::OnceClosure task) {
165   EXPECT_UI_THREAD();
166   std::move(task).Run();
167 }
168 
169 // TestHandler
170 
171 int TestHandler::browser_count_ = 0;
172 
TestHandler(CompletionState * completion_state)173 TestHandler::TestHandler(CompletionState* completion_state)
174     : first_browser_id_(0),
175       signal_completion_when_all_browsers_close_(true),
176       destroy_event_(nullptr),
177       destroy_test_expected_(true),
178       destroy_test_called_(false) {
179   if (completion_state) {
180     completion_state_ = completion_state;
181     completion_state_owned_ = false;
182   } else {
183     completion_state_ = new CompletionState(1);
184     completion_state_owned_ = true;
185   }
186 }
187 
~TestHandler()188 TestHandler::~TestHandler() {
189   DCHECK(!ui_thread_helper_.get());
190   if (destroy_test_expected_)
191     EXPECT_TRUE(destroy_test_called_);
192   else
193     EXPECT_FALSE(destroy_test_called_);
194   EXPECT_TRUE(browser_map_.empty());
195 
196   if (completion_state_owned_)
197     delete completion_state_;
198 
199   if (destroy_event_)
200     destroy_event_->Signal();
201 }
202 
OnAfterCreated(CefRefPtr<CefBrowser> browser)203 void TestHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
204   EXPECT_UI_THREAD();
205 
206   browser_count_++;
207 
208   const int browser_id = browser->GetIdentifier();
209   EXPECT_EQ(browser_map_.find(browser_id), browser_map_.end());
210   if (browser_map_.empty()) {
211     first_browser_id_ = browser_id;
212     first_browser_ = browser;
213   }
214   browser_map_.insert(std::make_pair(browser_id, browser));
215 }
216 
OnBeforeClose(CefRefPtr<CefBrowser> browser)217 void TestHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
218   EXPECT_UI_THREAD();
219 
220   // Free the browser pointer so that the browser can be destroyed.
221   const int browser_id = browser->GetIdentifier();
222   BrowserMap::iterator it = browser_map_.find(browser_id);
223   EXPECT_NE(it, browser_map_.end());
224   browser_map_.erase(it);
225 
226   if (browser_id == first_browser_id_) {
227     first_browser_id_ = 0;
228     first_browser_ = nullptr;
229   }
230 
231   if (browser_map_.empty() && signal_completion_when_all_browsers_close_) {
232     // Signal that the test is now complete.
233     TestComplete();
234   }
235 
236   browser_count_--;
237 }
238 
239 namespace {
240 
ToCefHeaderMap(const ResourceContent::HeaderMap & headerMap)241 CefResponse::HeaderMap ToCefHeaderMap(
242     const ResourceContent::HeaderMap& headerMap) {
243   CefResponse::HeaderMap result;
244   ResourceContent::HeaderMap::const_iterator it = headerMap.begin();
245   for (; it != headerMap.end(); ++it) {
246     result.insert(std::pair<CefString, CefString>(it->first, it->second));
247   }
248   return result;
249 }
250 
251 }  // namespace
252 
GetResourceHandler(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefRequest> request)253 CefRefPtr<CefResourceHandler> TestHandler::GetResourceHandler(
254     CefRefPtr<CefBrowser> browser,
255     CefRefPtr<CefFrame> frame,
256     CefRefPtr<CefRequest> request) {
257   EXPECT_IO_THREAD();
258 
259   if (resource_map_.size() > 0) {
260     const std::string& url = test_request::GetPathURL(request->GetURL());
261     ResourceMap::const_iterator it = resource_map_.find(url);
262     if (it != resource_map_.end()) {
263       // Return the previously mapped resource
264       CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForData(
265           static_cast<void*>(const_cast<char*>(it->second.content().c_str())),
266           it->second.content().length());
267       return new CefStreamResourceHandler(
268           200, "OK", it->second.mimeType(),
269           ToCefHeaderMap(it->second.headerMap()), stream);
270     }
271   }
272 
273   return nullptr;
274 }
275 
OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,TerminationStatus status)276 void TestHandler::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
277                                             TerminationStatus status) {
278   LOG(WARNING) << "OnRenderProcessTerminated: status = " << status << ".";
279 }
280 
GetBrowser() const281 CefRefPtr<CefBrowser> TestHandler::GetBrowser() const {
282   return first_browser_;
283 }
284 
GetBrowserId() const285 int TestHandler::GetBrowserId() const {
286   return first_browser_id_;
287 }
288 
GetAllBrowsers(BrowserMap * map)289 void TestHandler::GetAllBrowsers(BrowserMap* map) {
290   EXPECT_UI_THREAD();
291   EXPECT_TRUE(map);
292   *map = browser_map_;
293 }
294 
ExecuteTest()295 void TestHandler::ExecuteTest() {
296   EXPECT_EQ(completion_state_->total(), 1);
297 
298   // Reset any state from the previous run.
299   if (destroy_test_called_)
300     destroy_test_called_ = false;
301 
302   // Run the test.
303   RunTest();
304 
305   // Wait for the test to complete.
306   completion_state_->WaitForTests();
307 }
308 
SetupComplete()309 void TestHandler::SetupComplete() {
310   // Signal that the test setup is complete.
311   completion_state_->TestComplete();
312 }
313 
DestroyTest()314 void TestHandler::DestroyTest() {
315   if (!CefCurrentlyOn(TID_UI)) {
316     CefPostTask(TID_UI, base::BindOnce(&TestHandler::DestroyTest, this));
317     return;
318   }
319 
320   EXPECT_TRUE(destroy_test_expected_);
321   if (destroy_test_called_)
322     return;
323   destroy_test_called_ = true;
324 
325   if (!browser_map_.empty()) {
326     // Use a copy of the map since the original may be modified while we're
327     // iterating.
328     BrowserMap browser_map = browser_map_;
329 
330     // Tell all browsers to close.
331     BrowserMap::const_iterator it = browser_map.begin();
332     for (; it != browser_map.end(); ++it)
333       CloseBrowser(it->second, false);
334   }
335 
336   if (ui_thread_helper_.get())
337     ui_thread_helper_.reset(nullptr);
338 }
339 
OnTestTimeout(int timeout_ms,bool treat_as_error)340 void TestHandler::OnTestTimeout(int timeout_ms, bool treat_as_error) {
341   EXPECT_UI_THREAD();
342   if (treat_as_error) {
343     EXPECT_TRUE(false) << "Test timed out after " << timeout_ms << "ms";
344   }
345   DestroyTest();
346 }
347 
CreateBrowser(const CefString & url,CefRefPtr<CefRequestContext> request_context,CefRefPtr<CefDictionaryValue> extra_info)348 void TestHandler::CreateBrowser(const CefString& url,
349                                 CefRefPtr<CefRequestContext> request_context,
350                                 CefRefPtr<CefDictionaryValue> extra_info) {
351   const bool use_views = CefCommandLine::GetGlobalCommandLine()->HasSwitch(
352       client::switches::kUseViews);
353   if (use_views && !CefCurrentlyOn(TID_UI)) {
354     // Views classes must be accessed on the UI thread.
355     CefPostTask(TID_UI, base::BindOnce(&TestHandler::CreateBrowser, this, url,
356                                        request_context, extra_info));
357     return;
358   }
359 
360   CefWindowInfo windowInfo;
361   CefBrowserSettings settings;
362   PopulateBrowserSettings(&settings);
363 
364   if (use_views) {
365     // Create the BrowserView.
366     CefRefPtr<CefBrowserView> browser_view = CefBrowserView::CreateBrowserView(
367         this, url, settings, extra_info, request_context,
368         new TestBrowserViewDelegate());
369 
370     // Create the Window. It will show itself after creation.
371     TestWindowDelegate::CreateBrowserWindow(browser_view, std::string());
372   } else {
373 #if defined(OS_WIN)
374     windowInfo.SetAsPopup(nullptr, "CefUnitTest");
375     windowInfo.style |= WS_VISIBLE;
376 #endif
377     CefBrowserHost::CreateBrowser(windowInfo, this, url, settings, extra_info,
378                                   request_context);
379   }
380 }
381 
382 // static
CloseBrowser(CefRefPtr<CefBrowser> browser,bool force_close)383 void TestHandler::CloseBrowser(CefRefPtr<CefBrowser> browser,
384                                bool force_close) {
385   browser->GetHost()->CloseBrowser(force_close);
386 }
387 
AddResource(const std::string & url,const std::string & content,const std::string & mime_type)388 void TestHandler::AddResource(const std::string& url,
389                               const std::string& content,
390                               const std::string& mime_type) {
391   ResourceContent::HeaderMap headerMap = ResourceContent::HeaderMap();
392   ResourceContent rc = ResourceContent(content, mime_type, headerMap);
393   AddResourceEx(url, rc);
394 }
395 
AddResource(const std::string & url,const std::string & content,const std::string & mime_type,const ResourceContent::HeaderMap & header_map)396 void TestHandler::AddResource(const std::string& url,
397                               const std::string& content,
398                               const std::string& mime_type,
399                               const ResourceContent::HeaderMap& header_map) {
400   ResourceContent rc = ResourceContent(content, mime_type, header_map);
401   AddResourceEx(url, rc);
402 }
403 
AddResourceEx(const std::string & url,const ResourceContent & content)404 void TestHandler::AddResourceEx(const std::string& url,
405                                 const ResourceContent& content) {
406   if (!CefCurrentlyOn(TID_IO)) {
407     CefPostTask(TID_IO, base::BindOnce(&TestHandler::AddResourceEx, this, url,
408                                        content));
409     return;
410   }
411 
412   // Ignore the query component, if any.
413   std::string urlStr = url;
414   size_t idx = urlStr.find('?');
415   if (idx > 0)
416     urlStr = urlStr.substr(0, idx);
417 
418   resource_map_.insert(std::make_pair(urlStr, content));
419 }
420 
ClearResources()421 void TestHandler::ClearResources() {
422   if (!CefCurrentlyOn(TID_IO)) {
423     CefPostTask(TID_IO, base::BindOnce(&TestHandler::ClearResources, this));
424     return;
425   }
426 
427   resource_map_.clear();
428 }
429 
SetTestTimeout(int timeout_ms,bool treat_as_error)430 void TestHandler::SetTestTimeout(int timeout_ms, bool treat_as_error) {
431   if (!CefCurrentlyOn(TID_UI)) {
432     CefPostTask(TID_UI, base::BindOnce(&TestHandler::SetTestTimeout, this,
433                                        timeout_ms, treat_as_error));
434     return;
435   }
436 
437   if (destroy_test_called_) {
438     // No need to set the timeout if the test has already completed.
439     return;
440   }
441 
442   if (treat_as_error && CefCommandLine::GetGlobalCommandLine()->HasSwitch(
443                             "disable-test-timeout")) {
444     return;
445   }
446 
447   // Use a weak reference to |this| via UIThreadHelper so that the TestHandler
448   // can be destroyed before the timeout expires.
449   GetUIThreadHelper()->PostDelayedTask(
450       base::BindOnce(&TestHandler::OnTestTimeout, base::Unretained(this),
451                      timeout_ms, treat_as_error),
452       timeout_ms);
453 }
454 
TestComplete()455 void TestHandler::TestComplete() {
456   if (!CefCurrentlyOn(TID_UI)) {
457     CefPostTask(TID_UI, base::BindOnce(&TestHandler::TestComplete, this));
458     return;
459   }
460 
461   EXPECT_TRUE(browser_map_.empty());
462   completion_state_->TestComplete();
463 }
464 
GetUIThreadHelper()465 TestHandler::UIThreadHelper* TestHandler::GetUIThreadHelper() {
466   EXPECT_UI_THREAD();
467   CHECK(!destroy_test_called_);
468 
469   if (!ui_thread_helper_.get())
470     ui_thread_helper_.reset(new UIThreadHelper());
471   return ui_thread_helper_.get();
472 }
473 
474 // global functions
475 
TestFailed()476 bool TestFailed() {
477   CefRefPtr<CefCommandLine> command_line =
478       CefCommandLine::GetGlobalCommandLine();
479   if (command_line->HasSwitch("single-process")) {
480     // Check for a failure on the current test only.
481     return ::testing::UnitTest::GetInstance()
482         ->current_test_info()
483         ->result()
484         ->Failed();
485   } else {
486     // Check for any global failure.
487     return ::testing::UnitTest::GetInstance()->Failed();
488   }
489 }
490