]> git.sesse.net Git - casparcg/blob - dependencies64/cef/linux/tests/ceftests/test_handler.h
6ddae4ffdaaefaa04cb123be916c857e23a3a298
[casparcg] / dependencies64 / cef / linux / tests / ceftests / test_handler.h
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 #ifndef CEF_TESTS_UNITTESTS_TEST_HANDLER_H_
6 #define CEF_TESTS_UNITTESTS_TEST_HANDLER_H_
7 #pragma once
8
9 #include <list>
10 #include <map>
11 #include <string>
12 #include <utility>
13
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"
23
24 class TrackCallback {
25  public:
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_; }
31  protected:
32   bool gotit_;
33 };
34
35 class ResourceContent {
36  public:
37    typedef std::multimap<std::string, std::string> HeaderMap;
38
39    ResourceContent(const std::string& content,
40                    const std::string& mime_type,
41                    const HeaderMap& header_map)
42     : content_(content),
43       mime_type_(mime_type),
44       header_map_(header_map)
45   {}
46
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_; }
50
51  private:
52   std::string content_;
53   std::string mime_type_;
54   HeaderMap header_map_;
55 };
56
57
58 // Base implementation of CefClient for unit tests. Add new interfaces as needed
59 // by test cases.
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 {
70  public:
71   // Tracks the completion state of related test runs.
72   class CompletionState {
73    public:
74     // |total| is the number of times that TestComplete() must be called before
75     // WaitForTests() will return.
76     explicit CompletionState(int total);
77
78     // Call this method to indicate that a test has completed.
79     void TestComplete();
80
81     // This method blocks until TestComplete() has been called the required
82     // number of times.
83     void WaitForTests();
84
85     int total() const { return total_; }
86     int count() const { return count_; }
87
88    private:
89     int total_;
90     int count_;
91
92     // Handle used to notify when the test is complete
93     CefRefPtr<CefWaitableEvent> event_;
94   };
95
96   // Represents a collection of related tests that need to be run
97   // simultaniously.
98   class Collection {
99    public:
100     // The |completion_state| object must outlive this class.
101     explicit Collection(CompletionState* completion_state);
102
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);
106
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().
114     void ExecuteTests();
115
116    private:
117     CompletionState* completion_state_;
118
119     typedef std::list<TestHandler*> TestHandlerList;
120     TestHandlerList handler_list_;
121   };
122
123   typedef std::map<int, CefRefPtr<CefBrowser> > BrowserMap;
124
125   // Helper for executing methods using WeakPtr references to TestHandler.
126   class UIThreadHelper {
127    public:
128     UIThreadHelper();
129
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.
132     // For example:
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);
137
138    private:
139     void TaskHelper(const base::Closure& task);
140
141     // Must be the last member.
142     base::WeakPtrFactory<UIThreadHelper> weak_ptr_factory_;
143   };
144
145   // The |completion_state| object if specified must outlive this class.
146   explicit TestHandler(CompletionState* completion_state = NULL);
147   ~TestHandler() override;
148
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() {}
152
153   // Implement this method to run the test. Call DestroyTest() once the test is
154   // complete.
155   virtual void RunTest() =0;
156
157   // CefClient methods. Add new methods as needed by test cases.
158   CefRefPtr<CefDialogHandler> GetDialogHandler() override {
159     return this;
160   }
161   CefRefPtr<CefDisplayHandler> GetDisplayHandler() override {
162     return this;
163   }
164   CefRefPtr<CefDownloadHandler> GetDownloadHandler() override {
165     return this;
166   }
167   CefRefPtr<CefDragHandler> GetDragHandler() override {
168     return this;
169   }
170   CefRefPtr<CefGeolocationHandler> GetGeolocationHandler() override {
171     return this;
172   }
173   CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() override {
174     return this;
175   }
176   CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override {
177     return this;
178   }
179   CefRefPtr<CefLoadHandler> GetLoadHandler() override {
180     return this;
181   }
182   CefRefPtr<CefRequestHandler> GetRequestHandler() override {
183     return this;
184   }
185
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 {}
192
193   // CefDragHandler methods
194   void OnDraggableRegionsChanged(
195       CefRefPtr<CefBrowser> browser,
196       const std::vector<CefDraggableRegion>& regions) override {}
197
198   // CefLifeSpanHandler methods
199   void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
200   void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
201
202   // CefRequestHandler methods
203   CefRefPtr<CefResourceHandler> GetResourceHandler(
204       CefRefPtr<CefBrowser> browser,
205       CefRefPtr<CefFrame> frame,
206       CefRefPtr<CefRequest> request) override;
207
208   // These methods should only be used if at most one non-popup browser exists.
209   CefRefPtr<CefBrowser> GetBrowser();
210   int GetBrowserId();
211
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);
215
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.
220   void ExecuteTest();
221
222   // Event that will be signaled from the TestHandler destructor.
223   // Used by ReleaseAndWaitForDestructor.
224   void SetDestroyEvent(CefRefPtr<CefWaitableEvent> event) {
225     destroy_event_ = event;
226   }
227
228   // If a test will not call DestroyTest() indicate so using this method.
229   void SetDestroyTestExpected(bool expected) {
230     destroy_test_expected_ = expected;
231   }
232
233   // Returns true if a browser currently exists.
234   static bool HasBrowser() { return browser_count_ > 0; }
235
236  protected:
237   // Indicate that test setup is complete. Only used in combination with a
238   // Collection.
239   virtual void SetupComplete();
240
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();
247
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);
251
252   // Called from CreateBrowser() to optionally set per-browser settings.
253   virtual void PopulateBrowserSettings(CefBrowserSettings* settings) {}
254
255   void CreateBrowser(const CefString& url,
256                      CefRefPtr<CefRequestContext> request_context = NULL);
257   static void CloseBrowser(CefRefPtr<CefBrowser> browser,
258                            bool force_close);
259
260   void AddResource(const std::string& url,
261                    const std::string& content,
262                    const std::string& mime_type);
263
264   void AddResource(const std::string& url,
265                    const std::string& content,
266                    const std::string& mime_type,
267                    const ResourceContent::HeaderMap& header_map);
268
269   void AddResourceEx(const std::string& url, const ResourceContent& content);
270
271   void ClearResources();
272
273   void SetSignalCompletionWhenAllBrowsersClose(bool val) {
274     signal_completion_when_all_browsers_close_ = val;
275   }
276   bool SignalCompletionWhenAllBrowsersClose() const {
277     return signal_completion_when_all_browsers_close_;
278   }
279
280   // Call OnTestTimeout() after the specified amount of time.
281   void SetTestTimeout(int timeout_ms = 5000, bool treat_as_error = true);
282
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.
287   void TestComplete();
288
289   // Returns the single UIThreadHelper instance, creating it if necessary. Must
290   // be called on the UI thread.
291   UIThreadHelper* GetUIThreadHelper();
292
293  private:
294   // Used to notify when the test is complete. Can be accessed on any thread.
295   CompletionState* completion_state_;
296   bool completion_state_owned_;
297
298   // Map browser ID to browser object for non-popup browsers. Only accessed on
299   // the UI thread.
300   BrowserMap browser_map_;
301
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_;
306
307   // Map of resources that can be automatically loaded. Only accessed on the
308   // IO thread.
309   typedef std::map<std::string, ResourceContent > ResourceMap;
310   ResourceMap resource_map_;
311
312   // If true test completion will be signaled when all browsers have closed.
313   bool signal_completion_when_all_browsers_close_;
314
315   CefRefPtr<CefWaitableEvent> destroy_event_;
316
317   // Tracks whether DestroyTest() is expected or has been called.
318   bool destroy_test_expected_;
319   bool destroy_test_called_;
320
321   scoped_ptr<UIThreadHelper> ui_thread_helper_;
322
323   // Used to track the number of currently existing browser windows.
324   static int browser_count_;
325
326   DISALLOW_COPY_AND_ASSIGN(TestHandler);
327 };
328
329
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.
333 template<typename T>
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();
339   handler = NULL;
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);
345   }
346 }
347
348 // Returns true if the currently running test has failed.
349 bool TestFailed();
350
351 // Helper macros for executing checks in a method with a boolean return value.
352 // For example:
353 //
354 // bool VerifyVals(bool a, bool b) {
355 //   V_DECLARE();
356 //   V_EXPECT_TRUE(a);
357 //   V_EXPECT_FALSE(b);
358 //   V_RETURN();
359 // }
360 //
361 // EXPECT_TRUE(VerifyVals(true, false));
362
363 #define V_DECLARE() \
364     bool __verify = true; \
365     bool __result
366
367 #define V_RETURN() \
368     return __verify
369
370 #define V_EXPECT_TRUE(condition) \
371     __result = !!(condition); \
372     __verify &= __result; \
373     GTEST_TEST_BOOLEAN_(__result, #condition, false, true, \
374                         GTEST_NONFATAL_FAILURE_)
375
376 #define V_EXPECT_FALSE(condition) \
377     __result = !!(condition); \
378     __verify &= !__result; \
379     GTEST_TEST_BOOLEAN_(!(__result), #condition, true, false, \
380                         GTEST_NONFATAL_FAILURE_)
381
382 #endif  // CEF_TESTS_UNITTESTS_TEST_HANDLER_H_