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.
5 #ifndef CEF_TESTS_UNITTESTS_TEST_HANDLER_H_
6 #define CEF_TESTS_UNITTESTS_TEST_HANDLER_H_
14 #include "include/base/cef_bind.h"
15 #include "include/base/cef_scoped_ptr.h"
16 #include "include/cef_browser.h"
17 #include "include/cef_client.h"
18 #include "include/cef_frame.h"
19 #include "include/cef_task.h"
20 #include "include/cef_waitable_event.h"
21 #include "tests/ceftests/thread_helper.h"
22 #include "tests/gtest/include/gtest/gtest.h"
26 TrackCallback(): gotit_(false) {}
27 void yes() { gotit_ = true; }
28 bool isSet() { return gotit_; }
29 void reset() { gotit_ = false; }
30 operator bool() const { return gotit_; }
35 class ResourceContent {
37 typedef std::multimap<std::string, std::string> HeaderMap;
39 ResourceContent(const std::string& content,
40 const std::string& mime_type,
41 const HeaderMap& header_map)
43 mime_type_(mime_type),
44 header_map_(header_map)
47 const std::string& content() const { return content_; }
48 const std::string& mimeType() const { return mime_type_; }
49 const HeaderMap& headerMap() const { return header_map_; }
53 std::string mime_type_;
54 HeaderMap header_map_;
58 // Base implementation of CefClient for unit tests. Add new interfaces as needed
60 class TestHandler : public CefClient,
61 public CefDialogHandler,
62 public CefDisplayHandler,
63 public CefDownloadHandler,
64 public CefDragHandler,
65 public CefGeolocationHandler,
66 public CefJSDialogHandler,
67 public CefLifeSpanHandler,
68 public CefLoadHandler,
69 public CefRequestHandler {
71 // Tracks the completion state of related test runs.
72 class CompletionState {
74 // |total| is the number of times that TestComplete() must be called before
75 // WaitForTests() will return.
76 explicit CompletionState(int total);
78 // Call this method to indicate that a test has completed.
81 // This method blocks until TestComplete() has been called the required
85 int total() const { return total_; }
86 int count() const { return count_; }
92 // Handle used to notify when the test is complete
93 CefRefPtr<CefWaitableEvent> event_;
96 // Represents a collection of related tests that need to be run
100 // The |completion_state| object must outlive this class.
101 explicit Collection(CompletionState* completion_state);
103 // The |test_handler| object must outlive this class and it must share the
104 // same CompletionState object passed to the constructor.
105 void AddTestHandler(TestHandler* test_handler);
107 // Manages the test run.
108 // 1. Calls TestHandler::SetupTest() for all of the test objects.
109 // 2. Waits for all TestHandler objects to report that initial setup is
110 // complete by calling TestHandler::SetupComplete().
111 // 3. Calls TestHandler::RunTest() for all of the test objects.
112 // 4. Waits for all TestHandler objects to report that the test is
113 // complete by calling TestHandler::DestroyTest().
117 CompletionState* completion_state_;
119 typedef std::list<TestHandler*> TestHandlerList;
120 TestHandlerList handler_list_;
123 typedef std::map<int, CefRefPtr<CefBrowser> > BrowserMap;
125 // Helper for executing methods using WeakPtr references to TestHandler.
126 class UIThreadHelper {
130 // Pass in a |task| with an unretained reference to TestHandler. |task| will
131 // be executed only if TestHandler::DestroyTest has not yet been called.
133 // GetUIThreadHelper()->PostTask(
134 // base::Bind(&TestHandler::DoSomething, base::Unretained(this)));
135 void PostTask(const base::Closure& task);
136 void PostDelayedTask(const base::Closure& task, int delay_ms);
139 void TaskHelper(const base::Closure& task);
141 // Must be the last member.
142 base::WeakPtrFactory<UIThreadHelper> weak_ptr_factory_;
145 // The |completion_state| object if specified must outlive this class.
146 explicit TestHandler(CompletionState* completion_state = NULL);
147 ~TestHandler() override;
149 // Implement this method to set up the test. Only used in combination with a
150 // Collection. Call SetupComplete() once the setup is complete.
151 virtual void SetupTest() {}
153 // Implement this method to run the test. Call DestroyTest() once the test is
155 virtual void RunTest() =0;
157 // CefClient methods. Add new methods as needed by test cases.
158 CefRefPtr<CefDialogHandler> GetDialogHandler() override {
161 CefRefPtr<CefDisplayHandler> GetDisplayHandler() override {
164 CefRefPtr<CefDownloadHandler> GetDownloadHandler() override {
167 CefRefPtr<CefDragHandler> GetDragHandler() override {
170 CefRefPtr<CefGeolocationHandler> GetGeolocationHandler() override {
173 CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() override {
176 CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override {
179 CefRefPtr<CefLoadHandler> GetLoadHandler() override {
182 CefRefPtr<CefRequestHandler> GetRequestHandler() override {
186 // CefDownloadHandler methods
187 void OnBeforeDownload(
188 CefRefPtr<CefBrowser> browser,
189 CefRefPtr<CefDownloadItem> download_item,
190 const CefString& suggested_name,
191 CefRefPtr<CefBeforeDownloadCallback> callback) override {}
193 // CefDragHandler methods
194 void OnDraggableRegionsChanged(
195 CefRefPtr<CefBrowser> browser,
196 const std::vector<CefDraggableRegion>& regions) override {}
198 // CefLifeSpanHandler methods
199 void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
200 void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
202 // CefRequestHandler methods
203 CefRefPtr<CefResourceHandler> GetResourceHandler(
204 CefRefPtr<CefBrowser> browser,
205 CefRefPtr<CefFrame> frame,
206 CefRefPtr<CefRequest> request) override;
208 // These methods should only be used if at most one non-popup browser exists.
209 CefRefPtr<CefBrowser> GetBrowser();
212 // Copies the map of all the currently existing browsers into |map|. Must be
213 // called on the UI thread.
214 void GetAllBrowsers(BrowserMap* map);
216 // Called by the test function to execute the test. This method blocks until
217 // the test is complete. Do not reference the object after this method
218 // returns. Do not use this method if the CompletionState object is shared by
219 // multiple handlers or when using a Collection object.
222 // Event that will be signaled from the TestHandler destructor.
223 // Used by ReleaseAndWaitForDestructor.
224 void SetDestroyEvent(CefRefPtr<CefWaitableEvent> event) {
225 destroy_event_ = event;
228 // If a test will not call DestroyTest() indicate so using this method.
229 void SetDestroyTestExpected(bool expected) {
230 destroy_test_expected_ = expected;
233 // Returns true if a browser currently exists.
234 static bool HasBrowser() { return browser_count_ > 0; }
237 // Indicate that test setup is complete. Only used in combination with a
239 virtual void SetupComplete();
241 // Close any existing non-popup browsers. Test completion will be signaled
242 // once all the browsers have closed if
243 // |signal_completion_when_all_browsers_close_| is true (default value).
244 // If no browsers exist then this method will do nothing and
245 // TestComplete() must be called manually.
246 virtual void DestroyTest();
248 // Called on the UI thread if the test times out as a result of calling
249 // SetTestTimeout(). Calls DestroyTest() by default.
250 virtual void OnTestTimeout(int timeout_ms, bool treat_as_error);
252 // Called from CreateBrowser() to optionally set per-browser settings.
253 virtual void PopulateBrowserSettings(CefBrowserSettings* settings) {}
255 void CreateBrowser(const CefString& url,
256 CefRefPtr<CefRequestContext> request_context = NULL);
257 static void CloseBrowser(CefRefPtr<CefBrowser> browser,
260 void AddResource(const std::string& url,
261 const std::string& content,
262 const std::string& mime_type);
264 void AddResource(const std::string& url,
265 const std::string& content,
266 const std::string& mime_type,
267 const ResourceContent::HeaderMap& header_map);
269 void AddResourceEx(const std::string& url, const ResourceContent& content);
271 void ClearResources();
273 void SetSignalCompletionWhenAllBrowsersClose(bool val) {
274 signal_completion_when_all_browsers_close_ = val;
276 bool SignalCompletionWhenAllBrowsersClose() const {
277 return signal_completion_when_all_browsers_close_;
280 // Call OnTestTimeout() after the specified amount of time.
281 void SetTestTimeout(int timeout_ms = 5000, bool treat_as_error = true);
283 // Signal that the test is complete. This will be called automatically when
284 // all existing non-popup browsers are closed if
285 // |signal_completion_when_all_browsers_close_| is true (default value). It
286 // is an error to call this method before all browsers have closed.
289 // Returns the single UIThreadHelper instance, creating it if necessary. Must
290 // be called on the UI thread.
291 UIThreadHelper* GetUIThreadHelper();
294 // Used to notify when the test is complete. Can be accessed on any thread.
295 CompletionState* completion_state_;
296 bool completion_state_owned_;
298 // Map browser ID to browser object for non-popup browsers. Only accessed on
300 BrowserMap browser_map_;
302 // Values for the first created browser. Modified on the UI thread but can be
303 // accessed on any thread.
304 int first_browser_id_;
305 CefRefPtr<CefBrowser> first_browser_;
307 // Map of resources that can be automatically loaded. Only accessed on the
309 typedef std::map<std::string, ResourceContent > ResourceMap;
310 ResourceMap resource_map_;
312 // If true test completion will be signaled when all browsers have closed.
313 bool signal_completion_when_all_browsers_close_;
315 CefRefPtr<CefWaitableEvent> destroy_event_;
317 // Tracks whether DestroyTest() is expected or has been called.
318 bool destroy_test_expected_;
319 bool destroy_test_called_;
321 scoped_ptr<UIThreadHelper> ui_thread_helper_;
323 // Used to track the number of currently existing browser windows.
324 static int browser_count_;
326 DISALLOW_COPY_AND_ASSIGN(TestHandler);
330 // Release |handler| and wait for the destructor to be called.
331 // This function is used to avoid test state leakage and to verify that
332 // all Handler references have been released on test completion.
334 void ReleaseAndWaitForDestructor(CefRefPtr<T>& handler, int delay_ms = 2000) {
335 CefRefPtr<CefWaitableEvent> event =
336 CefWaitableEvent::CreateWaitableEvent(true, false);
337 handler->SetDestroyEvent(event);
338 T* _handler_ptr = handler.get();
340 bool handler_destructed = event->TimedWait(delay_ms);
341 EXPECT_TRUE(handler_destructed);
342 if (!handler_destructed) {
343 // |event| is a stack variable so clear the reference before returning.
344 _handler_ptr->SetDestroyEvent(NULL);
348 // Returns true if the currently running test has failed.
351 // Helper macros for executing checks in a method with a boolean return value.
354 // bool VerifyVals(bool a, bool b) {
357 // V_EXPECT_FALSE(b);
361 // EXPECT_TRUE(VerifyVals(true, false));
363 #define V_DECLARE() \
364 bool __verify = true; \
370 #define V_EXPECT_TRUE(condition) \
371 __result = !!(condition); \
372 __verify &= __result; \
373 GTEST_TEST_BOOLEAN_(__result, #condition, false, true, \
374 GTEST_NONFATAL_FAILURE_)
376 #define V_EXPECT_FALSE(condition) \
377 __result = !!(condition); \
378 __verify &= !__result; \
379 GTEST_TEST_BOOLEAN_(!(__result), #condition, true, false, \
380 GTEST_NONFATAL_FAILURE_)
382 #endif // CEF_TESTS_UNITTESTS_TEST_HANDLER_H_