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.
|
|
|
|
|
|
|
|
#ifndef CEF_TESTS_UNITTESTS_TEST_HANDLER_H_
|
|
|
|
#define CEF_TESTS_UNITTESTS_TEST_HANDLER_H_
|
|
|
|
#pragma once
|
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
#include <list>
|
2012-04-03 03:34:16 +02:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
#include "include/base/cef_bind.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "include/base/cef_scoped_ptr.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_client.h"
|
|
|
|
#include "include/cef_frame.h"
|
|
|
|
#include "include/cef_task.h"
|
2016-11-15 22:18:41 +01:00
|
|
|
#include "include/cef_waitable_event.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/thread_helper.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
class TrackCallback {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
TrackCallback() : gotit_(false) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
void yes() { gotit_ = true; }
|
|
|
|
bool isSet() { return gotit_; }
|
|
|
|
void reset() { gotit_ = false; }
|
|
|
|
operator bool() const { return gotit_; }
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
protected:
|
|
|
|
bool gotit_;
|
|
|
|
};
|
|
|
|
|
2016-10-27 18:34:19 +02:00
|
|
|
class ResourceContent {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
typedef std::multimap<std::string, std::string> HeaderMap;
|
2016-10-27 18:34:19 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
ResourceContent(const std::string& content,
|
|
|
|
const std::string& mime_type,
|
|
|
|
const HeaderMap& header_map)
|
|
|
|
: content_(content), mime_type_(mime_type), header_map_(header_map) {}
|
2016-10-27 18:34:19 +02:00
|
|
|
|
|
|
|
const std::string& content() const { return content_; }
|
|
|
|
const std::string& mimeType() const { return mime_type_; }
|
|
|
|
const HeaderMap& headerMap() const { return header_map_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string content_;
|
|
|
|
std::string mime_type_;
|
|
|
|
HeaderMap header_map_;
|
|
|
|
};
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Base implementation of CefClient for unit tests. Add new interfaces as needed
|
|
|
|
// by test cases.
|
|
|
|
class TestHandler : public CefClient,
|
2012-10-16 21:28:07 +02:00
|
|
|
public CefDialogHandler,
|
2012-04-03 03:34:16 +02:00
|
|
|
public CefDisplayHandler,
|
2013-02-13 23:47:04 +01:00
|
|
|
public CefDownloadHandler,
|
2015-04-24 15:48:32 +02:00
|
|
|
public CefDragHandler,
|
2012-10-19 20:39:44 +02:00
|
|
|
public CefGeolocationHandler,
|
2012-04-16 23:15:27 +02:00
|
|
|
public CefJSDialogHandler,
|
2012-04-03 03:34:16 +02:00
|
|
|
public CefLifeSpanHandler,
|
|
|
|
public CefLoadHandler,
|
|
|
|
public CefRequestHandler {
|
|
|
|
public:
|
2013-07-15 22:25:59 +02:00
|
|
|
// Tracks the completion state of related test runs.
|
|
|
|
class CompletionState {
|
|
|
|
public:
|
|
|
|
// |total| is the number of times that TestComplete() must be called before
|
|
|
|
// WaitForTests() will return.
|
|
|
|
explicit CompletionState(int total);
|
|
|
|
|
|
|
|
// Call this method to indicate that a test has completed.
|
|
|
|
void TestComplete();
|
|
|
|
|
|
|
|
// This method blocks until TestComplete() has been called the required
|
|
|
|
// number of times.
|
|
|
|
void WaitForTests();
|
|
|
|
|
|
|
|
int total() const { return total_; }
|
|
|
|
int count() const { return count_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int total_;
|
|
|
|
int count_;
|
|
|
|
|
|
|
|
// Handle used to notify when the test is complete
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event_;
|
2013-07-15 22:25:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Represents a collection of related tests that need to be run
|
|
|
|
// simultaniously.
|
|
|
|
class Collection {
|
|
|
|
public:
|
|
|
|
// The |completion_state| object must outlive this class.
|
|
|
|
explicit Collection(CompletionState* completion_state);
|
|
|
|
|
|
|
|
// The |test_handler| object must outlive this class and it must share the
|
|
|
|
// same CompletionState object passed to the constructor.
|
|
|
|
void AddTestHandler(TestHandler* test_handler);
|
|
|
|
|
|
|
|
// Manages the test run.
|
|
|
|
// 1. Calls TestHandler::SetupTest() for all of the test objects.
|
|
|
|
// 2. Waits for all TestHandler objects to report that initial setup is
|
|
|
|
// complete by calling TestHandler::SetupComplete().
|
|
|
|
// 3. Calls TestHandler::RunTest() for all of the test objects.
|
|
|
|
// 4. Waits for all TestHandler objects to report that the test is
|
|
|
|
// complete by calling TestHandler::DestroyTest().
|
|
|
|
void ExecuteTests();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CompletionState* completion_state_;
|
|
|
|
|
|
|
|
typedef std::list<TestHandler*> TestHandlerList;
|
|
|
|
TestHandlerList handler_list_;
|
|
|
|
};
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
typedef std::map<int, CefRefPtr<CefBrowser>> BrowserMap;
|
2014-01-28 00:31:03 +01:00
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// Helper for executing methods using WeakPtr references to TestHandler.
|
|
|
|
class UIThreadHelper {
|
|
|
|
public:
|
|
|
|
UIThreadHelper();
|
|
|
|
|
|
|
|
// Pass in a |task| with an unretained reference to TestHandler. |task| will
|
|
|
|
// be executed only if TestHandler::DestroyTest has not yet been called.
|
|
|
|
// For example:
|
|
|
|
// GetUIThreadHelper()->PostTask(
|
|
|
|
// base::Bind(&TestHandler::DoSomething, base::Unretained(this)));
|
|
|
|
void PostTask(const base::Closure& task);
|
|
|
|
void PostDelayedTask(const base::Closure& task, int delay_ms);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void TaskHelper(const base::Closure& task);
|
|
|
|
|
|
|
|
// Must be the last member.
|
|
|
|
base::WeakPtrFactory<UIThreadHelper> weak_ptr_factory_;
|
|
|
|
};
|
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
// The |completion_state| object if specified must outlive this class.
|
|
|
|
explicit TestHandler(CompletionState* completion_state = NULL);
|
2014-11-12 20:25:15 +01:00
|
|
|
~TestHandler() override;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
// Implement this method to set up the test. Only used in combination with a
|
|
|
|
// Collection. Call SetupComplete() once the setup is complete.
|
|
|
|
virtual void SetupTest() {}
|
|
|
|
|
|
|
|
// Implement this method to run the test. Call DestroyTest() once the test is
|
|
|
|
// complete.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual void RunTest() = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefClient methods. Add new methods as needed by test cases.
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefDialogHandler> GetDialogHandler() override { return this; }
|
|
|
|
CefRefPtr<CefDisplayHandler> GetDisplayHandler() override { return this; }
|
|
|
|
CefRefPtr<CefDownloadHandler> GetDownloadHandler() override { return this; }
|
|
|
|
CefRefPtr<CefDragHandler> GetDragHandler() override { return this; }
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefGeolocationHandler> GetGeolocationHandler() override {
|
2012-10-19 20:39:44 +02:00
|
|
|
return this;
|
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() override { return this; }
|
|
|
|
CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override { return this; }
|
|
|
|
CefRefPtr<CefLoadHandler> GetLoadHandler() override { return this; }
|
|
|
|
CefRefPtr<CefRequestHandler> GetRequestHandler() override { return this; }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-02-13 23:47:04 +01:00
|
|
|
// CefDownloadHandler methods
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnBeforeDownload(
|
2013-02-13 23:47:04 +01:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefDownloadItem> download_item,
|
|
|
|
const CefString& suggested_name,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefBeforeDownloadCallback> callback) override {}
|
2013-02-13 23:47:04 +01:00
|
|
|
|
2015-04-24 15:48:32 +02:00
|
|
|
// CefDragHandler methods
|
|
|
|
void OnDraggableRegionsChanged(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
const std::vector<CefDraggableRegion>& regions) override {}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// CefLifeSpanHandler methods
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
|
|
|
|
void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefRequestHandler methods
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefResourceHandler> GetResourceHandler(
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefRequest> request) override;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
// These methods should only be used if at most one non-popup browser exists.
|
|
|
|
CefRefPtr<CefBrowser> GetBrowser();
|
|
|
|
int GetBrowserId();
|
|
|
|
|
|
|
|
// Copies the map of all the currently existing browsers into |map|. Must be
|
|
|
|
// called on the UI thread.
|
|
|
|
void GetAllBrowsers(BrowserMap* map);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
// Called by the test function to execute the test. This method blocks until
|
2012-04-03 03:34:16 +02:00
|
|
|
// the test is complete. Do not reference the object after this method
|
2013-07-15 22:25:59 +02:00
|
|
|
// returns. Do not use this method if the CompletionState object is shared by
|
|
|
|
// multiple handlers or when using a Collection object.
|
2012-04-03 03:34:16 +02:00
|
|
|
void ExecuteTest();
|
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// Event that will be signaled from the TestHandler destructor.
|
|
|
|
// Used by ReleaseAndWaitForDestructor.
|
2016-11-15 22:18:41 +01:00
|
|
|
void SetDestroyEvent(CefRefPtr<CefWaitableEvent> event) {
|
|
|
|
destroy_event_ = event;
|
|
|
|
}
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// If a test will not call DestroyTest() indicate so using this method.
|
|
|
|
void SetDestroyTestExpected(bool expected) {
|
|
|
|
destroy_test_expected_ = expected;
|
|
|
|
}
|
|
|
|
|
2013-03-19 23:59:33 +01:00
|
|
|
// Returns true if a browser currently exists.
|
|
|
|
static bool HasBrowser() { return browser_count_ > 0; }
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
protected:
|
2013-07-15 22:25:59 +02:00
|
|
|
// Indicate that test setup is complete. Only used in combination with a
|
|
|
|
// Collection.
|
|
|
|
virtual void SetupComplete();
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
// Close any existing non-popup browsers. Test completion will be signaled
|
|
|
|
// once all the browsers have closed if
|
|
|
|
// |signal_completion_when_all_browsers_close_| is true (default value).
|
|
|
|
// If no browsers exist then this method will do nothing and
|
|
|
|
// TestComplete() must be called manually.
|
2012-10-16 21:28:07 +02:00
|
|
|
virtual void DestroyTest();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// Called on the UI thread if the test times out as a result of calling
|
|
|
|
// SetTestTimeout(). Calls DestroyTest() by default.
|
2015-05-23 03:57:31 +02:00
|
|
|
virtual void OnTestTimeout(int timeout_ms, bool treat_as_error);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
2015-02-18 18:28:56 +01:00
|
|
|
// Called from CreateBrowser() to optionally set per-browser settings.
|
|
|
|
virtual void PopulateBrowserSettings(CefBrowserSettings* settings) {}
|
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
void CreateBrowser(const CefString& url,
|
|
|
|
CefRefPtr<CefRequestContext> request_context = NULL);
|
2017-05-17 11:29:28 +02:00
|
|
|
static void CloseBrowser(CefRefPtr<CefBrowser> browser, bool force_close);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-04-16 23:15:27 +02:00
|
|
|
void AddResource(const std::string& url,
|
2012-04-03 03:34:16 +02:00
|
|
|
const std::string& content,
|
2016-10-27 18:34:19 +02:00
|
|
|
const std::string& mime_type);
|
|
|
|
|
|
|
|
void AddResource(const std::string& url,
|
|
|
|
const std::string& content,
|
|
|
|
const std::string& mime_type,
|
|
|
|
const ResourceContent::HeaderMap& header_map);
|
|
|
|
|
|
|
|
void AddResourceEx(const std::string& url, const ResourceContent& content);
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void ClearResources();
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
void SetSignalCompletionWhenAllBrowsersClose(bool val) {
|
|
|
|
signal_completion_when_all_browsers_close_ = val;
|
|
|
|
}
|
|
|
|
bool SignalCompletionWhenAllBrowsersClose() const {
|
|
|
|
return signal_completion_when_all_browsers_close_;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// Call OnTestTimeout() after the specified amount of time.
|
2015-05-23 03:57:31 +02:00
|
|
|
void SetTestTimeout(int timeout_ms = 5000, bool treat_as_error = true);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
// Signal that the test is complete. This will be called automatically when
|
|
|
|
// all existing non-popup browsers are closed if
|
|
|
|
// |signal_completion_when_all_browsers_close_| is true (default value). It
|
|
|
|
// is an error to call this method before all browsers have closed.
|
|
|
|
void TestComplete();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// Returns the single UIThreadHelper instance, creating it if necessary. Must
|
|
|
|
// be called on the UI thread.
|
|
|
|
UIThreadHelper* GetUIThreadHelper();
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
private:
|
|
|
|
// Used to notify when the test is complete. Can be accessed on any thread.
|
2013-07-15 22:25:59 +02:00
|
|
|
CompletionState* completion_state_;
|
|
|
|
bool completion_state_owned_;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
// Map browser ID to browser object for non-popup browsers. Only accessed on
|
|
|
|
// the UI thread.
|
|
|
|
BrowserMap browser_map_;
|
|
|
|
|
|
|
|
// Values for the first created browser. Modified on the UI thread but can be
|
|
|
|
// accessed on any thread.
|
|
|
|
int first_browser_id_;
|
|
|
|
CefRefPtr<CefBrowser> first_browser_;
|
|
|
|
|
|
|
|
// Map of resources that can be automatically loaded. Only accessed on the
|
|
|
|
// IO thread.
|
2017-05-17 11:29:28 +02:00
|
|
|
typedef std::map<std::string, ResourceContent> ResourceMap;
|
2012-04-03 03:34:16 +02:00
|
|
|
ResourceMap resource_map_;
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
// If true test completion will be signaled when all browsers have closed.
|
|
|
|
bool signal_completion_when_all_browsers_close_;
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> destroy_event_;
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Tracks whether DestroyTest() is expected or has been called.
|
|
|
|
bool destroy_test_expected_;
|
|
|
|
bool destroy_test_called_;
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
scoped_ptr<UIThreadHelper> ui_thread_helper_;
|
2015-01-10 00:40:26 +01:00
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
// Used to track the number of currently existing browser windows.
|
|
|
|
static int browser_count_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestHandler);
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
2015-01-10 00:40:26 +01:00
|
|
|
// Release |handler| and wait for the destructor to be called.
|
|
|
|
// This function is used to avoid test state leakage and to verify that
|
|
|
|
// all Handler references have been released on test completion.
|
2017-05-17 11:29:28 +02:00
|
|
|
template <typename T>
|
2015-01-10 00:40:26 +01:00
|
|
|
void ReleaseAndWaitForDestructor(CefRefPtr<T>& handler, int delay_ms = 2000) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
|
|
|
handler->SetDestroyEvent(event);
|
2015-10-14 18:42:42 +02:00
|
|
|
T* _handler_ptr = handler.get();
|
2015-01-10 00:40:26 +01:00
|
|
|
handler = NULL;
|
2016-11-15 22:18:41 +01:00
|
|
|
bool handler_destructed = event->TimedWait(delay_ms);
|
2015-01-10 00:40:26 +01:00
|
|
|
EXPECT_TRUE(handler_destructed);
|
2015-10-14 18:42:42 +02:00
|
|
|
if (!handler_destructed) {
|
2017-05-17 11:29:28 +02:00
|
|
|
// |event| is a stack variable so clear the reference before returning.
|
2015-10-14 18:42:42 +02:00
|
|
|
_handler_ptr->SetDestroyEvent(NULL);
|
|
|
|
}
|
2015-01-10 00:40:26 +01:00
|
|
|
}
|
|
|
|
|
2012-04-12 22:21:50 +02:00
|
|
|
// Returns true if the currently running test has failed.
|
|
|
|
bool TestFailed();
|
|
|
|
|
2014-09-24 17:38:11 +02:00
|
|
|
// Helper macros for executing checks in a method with a boolean return value.
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// bool VerifyVals(bool a, bool b) {
|
|
|
|
// V_DECLARE();
|
|
|
|
// V_EXPECT_TRUE(a);
|
|
|
|
// V_EXPECT_FALSE(b);
|
|
|
|
// V_RETURN();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// EXPECT_TRUE(VerifyVals(true, false));
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#define V_DECLARE() \
|
|
|
|
bool __verify = true; \
|
|
|
|
bool __result
|
2014-09-24 17:38:11 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#define V_RETURN() return __verify
|
2014-09-24 17:38:11 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#define V_EXPECT_TRUE(condition) \
|
|
|
|
__result = !!(condition); \
|
|
|
|
__verify &= __result; \
|
|
|
|
GTEST_TEST_BOOLEAN_(__result, #condition, false, true, \
|
|
|
|
GTEST_NONFATAL_FAILURE_)
|
2014-09-24 17:38:11 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#define V_EXPECT_FALSE(condition) \
|
|
|
|
__result = !!(condition); \
|
|
|
|
__verify &= !__result; \
|
|
|
|
GTEST_TEST_BOOLEAN_(!(__result), #condition, true, false, \
|
|
|
|
GTEST_NONFATAL_FAILURE_)
|
2014-09-24 17:38:11 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#endif // CEF_TESTS_UNITTESTS_TEST_HANDLER_H_
|