Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / base / ui_test_utils.h
blobe5a359a10c8760192178ea1939d9200e65302350
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_TEST_BASE_UI_TEST_UTILS_H_
6 #define CHROME_TEST_BASE_UI_TEST_UTILS_H_
8 #include <map>
9 #include <queue>
10 #include <set>
11 #include <string>
12 #include <vector>
14 #include "base/basictypes.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/strings/string16.h"
17 #include "components/history/core/browser/history_service.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_source.h"
22 #include "content/public/test/test_utils.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/base/window_open_disposition.h"
25 #include "ui/events/keycodes/keyboard_codes.h"
26 #include "ui/gfx/native_widget_types.h"
27 #include "url/gurl.h"
29 class Browser;
30 class LocationBar;
31 class Profile;
32 class TemplateURLService;
34 namespace app_modal {
35 class AppModalDialog;
38 namespace base {
39 class FilePath;
42 namespace chrome {
43 struct NavigateParams;
46 namespace content {
47 class WebContents;
50 namespace gfx {
51 class Rect;
54 // A collections of functions designed for use with InProcessBrowserTest.
55 namespace ui_test_utils {
57 // Flags to indicate what to wait for in a navigation test.
58 // They can be ORed together.
59 // The order in which the waits happen when more than one is selected, is:
60 // Browser
61 // Tab
62 // Navigation
63 enum BrowserTestWaitFlags {
64 BROWSER_TEST_NONE = 0, // Don't wait for anything.
65 BROWSER_TEST_WAIT_FOR_BROWSER = 1 << 0, // Wait for a new browser.
66 BROWSER_TEST_WAIT_FOR_TAB = 1 << 1, // Wait for a new tab.
67 BROWSER_TEST_WAIT_FOR_NAVIGATION = 1 << 2, // Wait for navigation to finish.
69 BROWSER_TEST_MASK = BROWSER_TEST_WAIT_FOR_BROWSER |
70 BROWSER_TEST_WAIT_FOR_TAB |
71 BROWSER_TEST_WAIT_FOR_NAVIGATION
74 // Puts the current tab title in |title|. Returns true on success.
75 bool GetCurrentTabTitle(const Browser* browser, base::string16* title);
77 // Performs the provided navigation process, blocking until the navigation
78 // finishes. May change the params in some cases (i.e. if the navigation
79 // opens a new browser window). Uses chrome::Navigate.
80 void NavigateToURL(chrome::NavigateParams* params);
82 // Navigates the selected tab of |browser| to |url|, blocking until the
83 // navigation finishes. Simulates a POST and uses chrome::Navigate.
84 void NavigateToURLWithPost(Browser* browser, const GURL& url);
86 // Navigates the selected tab of |browser| to |url|, blocking until the
87 // navigation finishes. Uses Browser::OpenURL --> chrome::Navigate.
88 void NavigateToURL(Browser* browser, const GURL& url);
90 // Navigates the specified tab of |browser| to |url|, blocking until the
91 // navigation finishes.
92 // |disposition| indicates what tab the navigation occurs in, and
93 // |browser_test_flags| controls what to wait for before continuing.
94 void NavigateToURLWithDisposition(Browser* browser,
95 const GURL& url,
96 WindowOpenDisposition disposition,
97 int browser_test_flags);
99 // Navigates the selected tab of |browser| to |url|, blocking until the
100 // number of navigations specified complete.
101 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
102 const GURL& url,
103 int number_of_navigations);
105 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
106 // blocking until the |number_of_navigations| specified complete.
107 // |disposition| indicates what tab the download occurs in, and
108 // |browser_test_flags| controls what to wait for before continuing.
109 void NavigateToURLWithDispositionBlockUntilNavigationsComplete(
110 Browser* browser,
111 const GURL& url,
112 int number_of_navigations,
113 WindowOpenDisposition disposition,
114 int browser_test_flags);
116 // Generate the file path for testing a particular test.
117 // The file for the tests is all located in
118 // test_root_directory/dir/<file>
119 // The returned path is base::FilePath format.
120 base::FilePath GetTestFilePath(const base::FilePath& dir,
121 const base::FilePath& file);
123 // Generate the URL for testing a particular test.
124 // HTML for the tests is all located in
125 // test_root_directory/dir/<file>
126 // The returned path is GURL format.
127 GURL GetTestUrl(const base::FilePath& dir, const base::FilePath& file);
129 // Generate the path of the build directory, relative to the source root.
130 bool GetRelativeBuildDirectory(base::FilePath* build_dir);
132 // Blocks until an application modal dialog is showns and returns it.
133 app_modal::AppModalDialog* WaitForAppModalDialog();
135 // Performs a find in the page of the specified tab. Returns the number of
136 // matches found. |ordinal| is an optional parameter which is set to the index
137 // of the current match. |selection_rect| is an optional parameter which is set
138 // to the location of the current match.
139 int FindInPage(content::WebContents* tab,
140 const base::string16& search_string,
141 bool forward,
142 bool case_sensitive,
143 int* ordinal,
144 gfx::Rect* selection_rect);
146 // Blocks until |service| finishes loading.
147 void WaitForTemplateURLServiceToLoad(TemplateURLService* service);
149 // Blocks until the |history_service|'s history finishes loading.
150 void WaitForHistoryToLoad(history::HistoryService* history_service);
152 // Download the given file and waits for the download to complete.
153 void DownloadURL(Browser* browser, const GURL& download_url);
155 // Send the given text to the omnibox and wait until it's updated.
156 void SendToOmniboxAndSubmit(LocationBar* location_bar,
157 const std::string& input);
159 // Gets the first browser that is not in the specified set.
160 Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers);
162 // Gets the size and value of the cookie string for |url| in the given tab.
163 // Can be called from any thread.
164 void GetCookies(const GURL& url,
165 content::WebContents* contents,
166 int* value_size,
167 std::string* value);
169 // A WindowedNotificationObserver hard-wired to observe
170 // chrome::NOTIFICATION_TAB_ADDED.
171 class WindowedTabAddedNotificationObserver
172 : public content::WindowedNotificationObserver {
173 public:
174 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either
175 // a specific source, or from all sources if |source| is
176 // NotificationService::AllSources().
177 explicit WindowedTabAddedNotificationObserver(
178 const content::NotificationSource& source);
180 // Returns the added tab, or NULL if no notification was observed yet.
181 content::WebContents* GetTab() { return added_tab_; }
183 void Observe(int type,
184 const content::NotificationSource& source,
185 const content::NotificationDetails& details) override;
187 private:
188 content::WebContents* added_tab_;
190 DISALLOW_COPY_AND_ASSIGN(WindowedTabAddedNotificationObserver);
193 // Similar to WindowedNotificationObserver but also provides a way of retrieving
194 // the details associated with the notification.
195 // Note that in order to use that class the details class should be copiable,
196 // which is the case with most notifications.
197 template <class U>
198 class WindowedNotificationObserverWithDetails
199 : public content::WindowedNotificationObserver {
200 public:
201 WindowedNotificationObserverWithDetails(
202 int notification_type,
203 const content::NotificationSource& source)
204 : content::WindowedNotificationObserver(notification_type, source) {}
206 // Fills |details| with the details of the notification received for |source|.
207 bool GetDetailsFor(uintptr_t source, U* details) {
208 typename std::map<uintptr_t, U>::const_iterator iter =
209 details_.find(source);
210 if (iter == details_.end())
211 return false;
212 *details = iter->second;
213 return true;
216 void Observe(int type,
217 const content::NotificationSource& source,
218 const content::NotificationDetails& details) override {
219 const U* details_ptr = content::Details<U>(details).ptr();
220 if (details_ptr)
221 details_[source.map_key()] = *details_ptr;
222 content::WindowedNotificationObserver::Observe(type, source, details);
225 private:
226 std::map<uintptr_t, U> details_;
228 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserverWithDetails);
231 // Notification observer which waits for navigation events and blocks until
232 // a specific URL is loaded. The URL must be an exact match.
233 class UrlLoadObserver : public content::WindowedNotificationObserver {
234 public:
235 // Register to listen for notifications of the given type from either a
236 // specific source, or from all sources if |source| is
237 // NotificationService::AllSources().
238 UrlLoadObserver(const GURL& url, const content::NotificationSource& source);
239 ~UrlLoadObserver() override;
241 // content::NotificationObserver:
242 void Observe(int type,
243 const content::NotificationSource& source,
244 const content::NotificationDetails& details) override;
246 private:
247 GURL url_;
249 DISALLOW_COPY_AND_ASSIGN(UrlLoadObserver);
252 // Convenience class for waiting for a new browser to be created.
253 // Like WindowedNotificationObserver, this class provides a safe, non-racey
254 // way to wait for a new browser to be created.
255 class BrowserAddedObserver {
256 public:
257 BrowserAddedObserver();
258 ~BrowserAddedObserver();
260 // Wait for a new browser to be created, and return a pointer to it.
261 Browser* WaitForSingleNewBrowser();
263 private:
264 content::WindowedNotificationObserver notification_observer_;
265 std::set<Browser*> original_browsers_;
267 DISALLOW_COPY_AND_ASSIGN(BrowserAddedObserver);
270 // Configures the geolocation provider to always return the given position.
271 void OverrideGeolocation(double latitude, double longitude);
273 // Enumerates all history contents on the backend thread. Returns them in
274 // descending order by time.
275 class HistoryEnumerator {
276 public:
277 explicit HistoryEnumerator(Profile* profile);
278 ~HistoryEnumerator();
280 std::vector<GURL>& urls() { return urls_; }
282 private:
283 void HistoryQueryComplete(
284 const base::Closure& quit_task,
285 history::QueryResults* results);
287 std::vector<GURL> urls_;
289 base::CancelableTaskTracker tracker_;
291 DISALLOW_COPY_AND_ASSIGN(HistoryEnumerator);
294 } // namespace ui_test_utils
296 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_