2013-01-03 18:24:24 +01:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
2012-04-03 03:34:16 +02:00
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "tests/unittests/test_handler.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/base/cef_bind.h"
|
2012-04-12 22:21:50 +02:00
|
|
|
#include "include/cef_command_line.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_stream.h"
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/wrapper/cef_stream_resource_handler.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void NotifyEvent(base::WaitableEvent* event) {
|
|
|
|
event->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
// TestHandler::CompletionState
|
|
|
|
|
|
|
|
TestHandler::CompletionState::CompletionState(int total)
|
|
|
|
: total_(total),
|
|
|
|
count_(0),
|
|
|
|
event_(true, false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::CompletionState::TestComplete() {
|
|
|
|
if (++count_ == total_) {
|
|
|
|
count_ = 0;
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
// Signal that the test is now complete. Do not access any object members
|
|
|
|
// after this call because |this| might be deleted.
|
|
|
|
event_.Signal();
|
2013-07-15 22:25:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::CompletionState::WaitForTests() {
|
|
|
|
// Wait for the test to complete
|
|
|
|
event_.Wait();
|
|
|
|
|
|
|
|
// Reset the event so the same test can be executed again.
|
|
|
|
event_.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TestHandler::Collection
|
|
|
|
|
|
|
|
TestHandler::Collection::Collection(CompletionState* completion_state)
|
|
|
|
: completion_state_(completion_state) {
|
2014-01-28 00:31:03 +01:00
|
|
|
EXPECT_TRUE(completion_state_);
|
2013-07-15 22:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::Collection::AddTestHandler(TestHandler* test_handler) {
|
2014-01-28 00:31:03 +01:00
|
|
|
EXPECT_EQ(test_handler->completion_state_, completion_state_);
|
2013-07-15 22:25:59 +02:00
|
|
|
handler_list_.push_back(test_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::Collection::ExecuteTests() {
|
2014-01-28 00:31:03 +01:00
|
|
|
EXPECT_GT(handler_list_.size(), 0UL);
|
2013-07-15 22:25:59 +02:00
|
|
|
|
|
|
|
TestHandlerList::const_iterator it;
|
|
|
|
|
|
|
|
it = handler_list_.begin();
|
|
|
|
for (; it != handler_list_.end(); ++it)
|
|
|
|
(*it)->SetupTest();
|
|
|
|
|
|
|
|
completion_state_->WaitForTests();
|
|
|
|
|
|
|
|
it = handler_list_.begin();
|
|
|
|
for (; it != handler_list_.end(); ++it)
|
|
|
|
(*it)->RunTest();
|
|
|
|
|
|
|
|
completion_state_->WaitForTests();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// TestHandler::UIThreadHelper
|
|
|
|
|
|
|
|
TestHandler::UIThreadHelper::UIThreadHelper()
|
|
|
|
: weak_ptr_factory_(this) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::UIThreadHelper::PostTask(const base::Closure& task) {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
CefPostTask(
|
|
|
|
TID_UI,
|
|
|
|
base::Bind(&UIThreadHelper::TaskHelper,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), task));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::UIThreadHelper::PostDelayedTask(
|
|
|
|
const base::Closure& task, int delay_ms) {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
CefPostDelayedTask(
|
|
|
|
TID_UI,
|
|
|
|
base::Bind(&UIThreadHelper::TaskHelper,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), task),
|
|
|
|
delay_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::UIThreadHelper::TaskHelper(const base::Closure& task) {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
task.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// TestHandler
|
|
|
|
|
2013-03-19 23:59:33 +01:00
|
|
|
int TestHandler::browser_count_ = 0;
|
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
TestHandler::TestHandler(CompletionState* completion_state)
|
2014-01-28 00:31:03 +01:00
|
|
|
: first_browser_id_(0),
|
2015-01-10 00:40:26 +01:00
|
|
|
signal_completion_when_all_browsers_close_(true),
|
|
|
|
destroy_event_(NULL),
|
|
|
|
destroy_test_expected_(true),
|
|
|
|
destroy_test_called_(false) {
|
2013-07-15 22:25:59 +02:00
|
|
|
if (completion_state) {
|
|
|
|
completion_state_ = completion_state;
|
|
|
|
completion_state_owned_ = false;
|
|
|
|
} else {
|
|
|
|
completion_state_ = new CompletionState(1);
|
|
|
|
completion_state_owned_ = true;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TestHandler::~TestHandler() {
|
2015-01-10 00:40:26 +01:00
|
|
|
DCHECK(!ui_thread_helper_.get());
|
|
|
|
if (destroy_test_expected_)
|
|
|
|
EXPECT_TRUE(destroy_test_called_);
|
|
|
|
else
|
|
|
|
EXPECT_FALSE(destroy_test_called_);
|
2014-01-28 00:31:03 +01:00
|
|
|
EXPECT_TRUE(browser_map_.empty());
|
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
if (completion_state_owned_)
|
|
|
|
delete completion_state_;
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
if (destroy_event_)
|
|
|
|
destroy_event_->Signal();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
|
2014-01-28 00:31:03 +01:00
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
|
2013-03-19 23:59:33 +01:00
|
|
|
browser_count_++;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
if (!browser->IsPopup()) {
|
2014-01-28 00:31:03 +01:00
|
|
|
// Keep non-popup browsers.
|
|
|
|
const int browser_id = browser->GetIdentifier();
|
|
|
|
EXPECT_EQ(browser_map_.find(browser_id), browser_map_.end());
|
|
|
|
if (browser_map_.empty()) {
|
|
|
|
first_browser_id_ = browser_id;
|
|
|
|
first_browser_ = browser;
|
|
|
|
}
|
|
|
|
browser_map_.insert(std::make_pair(browser_id, browser));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
|
2014-01-28 00:31:03 +01:00
|
|
|
EXPECT_UI_THREAD();
|
2013-03-19 23:59:33 +01:00
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
if (!browser->IsPopup()) {
|
|
|
|
// Free the browser pointer so that the browser can be destroyed.
|
|
|
|
const int browser_id = browser->GetIdentifier();
|
|
|
|
BrowserMap::iterator it = browser_map_.find(browser_id);
|
|
|
|
EXPECT_NE(it, browser_map_.end());
|
|
|
|
browser_map_.erase(it);
|
|
|
|
|
|
|
|
if (browser_id == first_browser_id_) {
|
|
|
|
first_browser_id_ = 0;
|
|
|
|
first_browser_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browser_map_.empty() &&
|
|
|
|
signal_completion_when_all_browsers_close_) {
|
2013-03-19 23:59:33 +01:00
|
|
|
// Signal that the test is now complete.
|
2014-01-28 00:31:03 +01:00
|
|
|
TestComplete();
|
2013-03-19 23:59:33 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2013-03-19 23:59:33 +01:00
|
|
|
|
|
|
|
browser_count_--;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefResourceHandler> TestHandler::GetResourceHandler(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request) {
|
2014-01-28 00:31:03 +01:00
|
|
|
EXPECT_IO_THREAD();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
if (resource_map_.size() > 0) {
|
|
|
|
CefString url = request->GetURL();
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
// Ignore the query component, if any.
|
|
|
|
std::string urlStr = url;
|
2013-08-14 23:45:22 +02:00
|
|
|
size_t idx = urlStr.find('?');
|
2012-04-16 23:15:27 +02:00
|
|
|
if (idx > 0)
|
|
|
|
urlStr = urlStr.substr(0, idx);
|
|
|
|
|
|
|
|
ResourceMap::const_iterator it = resource_map_.find(urlStr);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (it != resource_map_.end()) {
|
|
|
|
// Return the previously mapped resource
|
|
|
|
CefRefPtr<CefStreamReader> stream =
|
|
|
|
CefStreamReader::CreateForData(
|
|
|
|
static_cast<void*>(const_cast<char*>(it->second.first.c_str())),
|
|
|
|
it->second.first.length());
|
|
|
|
return new CefStreamResourceHandler(it->second.second, stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
CefRefPtr<CefBrowser> TestHandler::GetBrowser() {
|
|
|
|
return first_browser_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestHandler::GetBrowserId() {
|
|
|
|
return first_browser_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::GetAllBrowsers(BrowserMap* map) {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
EXPECT_TRUE(map);
|
|
|
|
*map = browser_map_;
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void TestHandler::ExecuteTest() {
|
2014-01-28 00:31:03 +01:00
|
|
|
EXPECT_EQ(completion_state_->total(), 1);
|
2013-07-15 22:25:59 +02:00
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// Reset any state from the previous run.
|
|
|
|
if (destroy_test_called_)
|
|
|
|
destroy_test_called_ = false;
|
|
|
|
|
|
|
|
// Run the test.
|
2012-04-03 03:34:16 +02:00
|
|
|
RunTest();
|
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// Wait for the test to complete.
|
2013-07-15 22:25:59 +02:00
|
|
|
completion_state_->WaitForTests();
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
void TestHandler::SetupComplete() {
|
|
|
|
// Signal that the test setup is complete.
|
|
|
|
completion_state_->TestComplete();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::DestroyTest() {
|
2014-01-28 00:31:03 +01:00
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2014-07-15 20:10:40 +02:00
|
|
|
CefPostTask(TID_UI, base::Bind(&TestHandler::DestroyTest, this));
|
2014-01-28 00:31:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
EXPECT_TRUE(destroy_test_expected_);
|
|
|
|
if (destroy_test_called_)
|
|
|
|
return;
|
|
|
|
destroy_test_called_ = true;
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
if (!browser_map_.empty()) {
|
|
|
|
// Use a copy of the map since the original may be modified while we're
|
|
|
|
// iterating.
|
|
|
|
BrowserMap browser_map = browser_map_;
|
|
|
|
|
|
|
|
// Tell all non-popup browsers to close.
|
|
|
|
BrowserMap::const_iterator it = browser_map.begin();
|
|
|
|
for (; it != browser_map.end(); ++it)
|
|
|
|
it->second->GetHost()->CloseBrowser(false);
|
|
|
|
}
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
if (ui_thread_helper_.get())
|
|
|
|
ui_thread_helper_.reset(NULL);
|
|
|
|
}
|
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
void TestHandler::OnTestTimeout(int timeout_ms, bool treat_as_error) {
|
2015-01-10 00:40:26 +01:00
|
|
|
EXPECT_UI_THREAD();
|
2015-05-23 03:57:31 +02:00
|
|
|
if (treat_as_error)
|
|
|
|
EXPECT_TRUE(false) << "Test timed out after " << timeout_ms << "ms";
|
2015-01-10 00:40:26 +01:00
|
|
|
DestroyTest();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
void TestHandler::CreateBrowser(
|
|
|
|
const CefString& url,
|
|
|
|
CefRefPtr<CefRequestContext> request_context) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefWindowInfo windowInfo;
|
|
|
|
CefBrowserSettings settings;
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
windowInfo.SetAsPopup(NULL, "CefUnitTest");
|
|
|
|
windowInfo.style |= WS_VISIBLE;
|
|
|
|
#endif
|
2015-02-18 18:28:56 +01:00
|
|
|
PopulateBrowserSettings(&settings);
|
2013-09-03 18:43:31 +02:00
|
|
|
CefBrowserHost::CreateBrowser(windowInfo, this, url, settings,
|
|
|
|
request_context);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-04-16 23:15:27 +02:00
|
|
|
void TestHandler::AddResource(const std::string& url,
|
2012-04-03 03:34:16 +02:00
|
|
|
const std::string& content,
|
2012-04-16 23:15:27 +02:00
|
|
|
const std::string& mimeType) {
|
2014-01-28 00:31:03 +01:00
|
|
|
if (!CefCurrentlyOn(TID_IO)) {
|
|
|
|
CefPostTask(TID_IO,
|
2014-07-15 20:10:40 +02:00
|
|
|
base::Bind(&TestHandler::AddResource, this, url, content, mimeType));
|
2014-01-28 00:31:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-16 23:15:27 +02:00
|
|
|
// Ignore the query component, if any.
|
|
|
|
std::string urlStr = url;
|
2013-08-14 23:45:22 +02:00
|
|
|
size_t idx = urlStr.find('?');
|
2012-04-16 23:15:27 +02:00
|
|
|
if (idx > 0)
|
|
|
|
urlStr = urlStr.substr(0, idx);
|
|
|
|
|
|
|
|
resource_map_.insert(
|
|
|
|
std::make_pair(urlStr, std::make_pair(content, mimeType)));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestHandler::ClearResources() {
|
2014-01-28 00:31:03 +01:00
|
|
|
if (!CefCurrentlyOn(TID_IO)) {
|
2014-07-15 20:10:40 +02:00
|
|
|
CefPostTask(TID_IO, base::Bind(&TestHandler::ClearResources, this));
|
2014-01-28 00:31:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
resource_map_.clear();
|
|
|
|
}
|
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
void TestHandler::SetTestTimeout(int timeout_ms, bool treat_as_error) {
|
2015-01-10 00:40:26 +01:00
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
|
|
CefPostTask(TID_UI, base::Bind(&TestHandler::SetTestTimeout, this,
|
2015-05-23 03:57:31 +02:00
|
|
|
timeout_ms, treat_as_error));
|
2015-01-10 00:40:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-23 03:57:31 +02:00
|
|
|
if (treat_as_error &&
|
|
|
|
CefCommandLine::GetGlobalCommandLine()->HasSwitch(
|
|
|
|
"disable-test-timeout")) {
|
2015-01-10 00:40:26 +01:00
|
|
|
return;
|
2015-05-23 03:57:31 +02:00
|
|
|
}
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Use a weak reference to |this| via UIThreadHelper so that the TestHandler
|
|
|
|
// can be destroyed before the timeout expires.
|
|
|
|
GetUIThreadHelper()->PostDelayedTask(
|
|
|
|
base::Bind(&TestHandler::OnTestTimeout, base::Unretained(this),
|
2015-05-23 03:57:31 +02:00
|
|
|
timeout_ms, treat_as_error),
|
2015-01-10 00:40:26 +01:00
|
|
|
timeout_ms);
|
|
|
|
}
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
void TestHandler::TestComplete() {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2014-07-15 20:10:40 +02:00
|
|
|
CefPostTask(TID_UI, base::Bind(&TestHandler::TestComplete, this));
|
2014-01-28 00:31:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(browser_map_.empty());
|
|
|
|
completion_state_->TestComplete();
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
TestHandler::UIThreadHelper* TestHandler::GetUIThreadHelper() {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
EXPECT_FALSE(destroy_test_called_);
|
|
|
|
|
|
|
|
if (!ui_thread_helper_.get())
|
|
|
|
ui_thread_helper_.reset(new UIThreadHelper());
|
|
|
|
return ui_thread_helper_.get();
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// global functions
|
|
|
|
|
|
|
|
void WaitForThread(CefThreadId thread_id) {
|
|
|
|
base::WaitableEvent event(true, false);
|
2014-07-15 20:10:40 +02:00
|
|
|
CefPostTask(thread_id, base::Bind(&NotifyEvent, &event));
|
2012-04-03 03:34:16 +02:00
|
|
|
event.Wait();
|
|
|
|
}
|
2012-04-12 22:21:50 +02:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
void WaitForThread(CefRefPtr<CefTaskRunner> task_runner) {
|
|
|
|
base::WaitableEvent event(true, false);
|
2014-07-15 20:10:40 +02:00
|
|
|
task_runner->PostTask(CefCreateClosureTask(base::Bind(&NotifyEvent, &event)));
|
2013-01-03 18:24:24 +01:00
|
|
|
event.Wait();
|
|
|
|
}
|
|
|
|
|
2012-04-12 22:21:50 +02:00
|
|
|
bool TestFailed() {
|
|
|
|
CefRefPtr<CefCommandLine> command_line =
|
|
|
|
CefCommandLine::GetGlobalCommandLine();
|
|
|
|
if (command_line->HasSwitch("single-process")) {
|
|
|
|
// Check for a failure on the current test only.
|
|
|
|
return ::testing::UnitTest::GetInstance()->current_test_info()->result()->
|
|
|
|
Failed();
|
|
|
|
} else {
|
|
|
|
// Check for any global failure.
|
|
|
|
return ::testing::UnitTest::GetInstance()->Failed();
|
|
|
|
}
|
|
|
|
}
|