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>
|
|
|
|
|
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_client.h"
|
|
|
|
#include "include/cef_frame.h"
|
|
|
|
#include "include/cef_task.h"
|
|
|
|
#include "base/synchronization/waitable_event.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
class TrackCallback {
|
|
|
|
public:
|
|
|
|
TrackCallback(): gotit_(false) {}
|
|
|
|
void yes() { gotit_ = true; }
|
|
|
|
bool isSet() { return gotit_; }
|
|
|
|
void reset() { gotit_ = false; }
|
|
|
|
operator bool() const { return gotit_; }
|
|
|
|
protected:
|
|
|
|
bool gotit_;
|
|
|
|
};
|
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
|
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,
|
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
|
|
|
|
base::WaitableEvent event_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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_;
|
|
|
|
};
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
typedef std::map<int, CefRefPtr<CefBrowser> > BrowserMap;
|
|
|
|
|
2013-07-15 22:25:59 +02:00
|
|
|
// The |completion_state| object if specified must outlive this class.
|
|
|
|
explicit TestHandler(CompletionState* completion_state = NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
virtual ~TestHandler();
|
|
|
|
|
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.
|
2012-04-03 03:34:16 +02:00
|
|
|
virtual void RunTest() =0;
|
|
|
|
|
|
|
|
// CefClient methods. Add new methods as needed by test cases.
|
2012-10-16 21:28:07 +02:00
|
|
|
virtual CefRefPtr<CefDialogHandler> GetDialogHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
2013-02-13 23:47:04 +01:00
|
|
|
virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
2012-10-19 20:39:44 +02:00
|
|
|
virtual CefRefPtr<CefGeolocationHandler> GetGeolocationHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
2012-04-16 23:15:27 +02:00
|
|
|
virtual CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-02-13 23:47:04 +01:00
|
|
|
// CefDownloadHandler methods
|
|
|
|
virtual void OnBeforeDownload(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefDownloadItem> download_item,
|
|
|
|
const CefString& suggested_name,
|
|
|
|
CefRefPtr<CefBeforeDownloadCallback> callback) OVERRIDE {}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// CefLifeSpanHandler methods
|
|
|
|
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
|
|
|
|
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
|
|
|
|
|
|
|
|
// CefRequestHandler methods
|
|
|
|
virtual CefRefPtr<CefResourceHandler> GetResourceHandler(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request) OVERRIDE;
|
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
void CreateBrowser(const CefString& url,
|
|
|
|
CefRefPtr<CefRequestContext> request_context = NULL);
|
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,
|
2012-04-16 23:15:27 +02:00
|
|
|
const std::string& mimeType);
|
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
|
|
|
|
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
|
|
|
|
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.
|
2012-04-16 23:15:27 +02:00
|
|
|
typedef std::map<std::string, std::pair<std::string, std::string> >
|
|
|
|
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_;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Include the default reference counting implementation.
|
|
|
|
IMPLEMENT_REFCOUNTING(TestHandler);
|
2013-03-19 23:59:33 +01:00
|
|
|
|
|
|
|
// Used to track the number of currently existing browser windows.
|
|
|
|
static int browser_count_;
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Post a task to the specified thread and wait for the task to execute as
|
|
|
|
// indication that all previously pending tasks on that thread have completed.
|
|
|
|
void WaitForThread(CefThreadId thread_id);
|
2013-01-03 18:24:24 +01:00
|
|
|
void WaitForThread(CefRefPtr<CefTaskRunner> task_runner);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#define WaitForIOThread() WaitForThread(TID_IO)
|
|
|
|
#define WaitForUIThread() WaitForThread(TID_UI)
|
|
|
|
|
2012-04-12 22:21:50 +02:00
|
|
|
// Returns true if the currently running test has failed.
|
|
|
|
bool TestFailed();
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
#define EXPECT_UI_THREAD() EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
|
|
|
#define EXPECT_IO_THREAD() EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
2014-02-05 21:35:45 +01:00
|
|
|
#define EXPECT_FILE_THREAD() EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
2014-01-28 00:31:03 +01:00
|
|
|
#define EXPECT_RENDERER_THREAD() EXPECT_TRUE(CefCurrentlyOn(TID_RENDERER));
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#endif // CEF_TESTS_UNITTESTS_TEST_HANDLER_H_
|