Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / test / base / ui_test_utils.h
blob6cd40dc46611fa6b0ef83d17c045f67eb5d5a782
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 "chrome/browser/history/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 // Opens |url| in an incognito browser window with the incognito profile of
78 // |profile|, blocking until the navigation finishes. This will create a new
79 // browser if a browser with the incognito profile does not exist. Returns the
80 // incognito window Browser.
81 Browser* OpenURLOffTheRecord(Profile* profile, const GURL& url);
83 // Performs the provided navigation process, blocking until the navigation
84 // finishes. May change the params in some cases (i.e. if the navigation
85 // opens a new browser window). Uses chrome::Navigate.
86 void NavigateToURL(chrome::NavigateParams* params);
88 // Navigates the selected tab of |browser| to |url|, blocking until the
89 // navigation finishes. Simulates a POST and uses chrome::Navigate.
90 void NavigateToURLWithPost(Browser* browser, const GURL& url);
92 // Navigates the selected tab of |browser| to |url|, blocking until the
93 // navigation finishes. Uses Browser::OpenURL --> chrome::Navigate.
94 void NavigateToURL(Browser* browser, const GURL& url);
96 // Navigates the specified tab of |browser| to |url|, blocking until the
97 // navigation finishes.
98 // |disposition| indicates what tab the navigation occurs in, and
99 // |browser_test_flags| controls what to wait for before continuing.
100 void NavigateToURLWithDisposition(Browser* browser,
101 const GURL& url,
102 WindowOpenDisposition disposition,
103 int browser_test_flags);
105 // Navigates the selected tab of |browser| to |url|, blocking until the
106 // number of navigations specified complete.
107 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
108 const GURL& url,
109 int number_of_navigations);
111 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
112 // blocking until the |number_of_navigations| specified complete.
113 // |disposition| indicates what tab the download occurs in, and
114 // |browser_test_flags| controls what to wait for before continuing.
115 void NavigateToURLWithDispositionBlockUntilNavigationsComplete(
116 Browser* browser,
117 const GURL& url,
118 int number_of_navigations,
119 WindowOpenDisposition disposition,
120 int browser_test_flags);
122 // Generate the file path for testing a particular test.
123 // The file for the tests is all located in
124 // test_root_directory/dir/<file>
125 // The returned path is base::FilePath format.
126 base::FilePath GetTestFilePath(const base::FilePath& dir,
127 const base::FilePath& file);
129 // Generate the URL for testing a particular test.
130 // HTML for the tests is all located in
131 // test_root_directory/dir/<file>
132 // The returned path is GURL format.
133 GURL GetTestUrl(const base::FilePath& dir, const base::FilePath& file);
135 // Generate the path of the build directory, relative to the source root.
136 bool GetRelativeBuildDirectory(base::FilePath* build_dir);
138 // Blocks until an application modal dialog is showns and returns it.
139 app_modal::AppModalDialog* WaitForAppModalDialog();
141 // Performs a find in the page of the specified tab. Returns the number of
142 // matches found. |ordinal| is an optional parameter which is set to the index
143 // of the current match. |selection_rect| is an optional parameter which is set
144 // to the location of the current match.
145 int FindInPage(content::WebContents* tab,
146 const base::string16& search_string,
147 bool forward,
148 bool case_sensitive,
149 int* ordinal,
150 gfx::Rect* selection_rect);
152 // Blocks until |service| finishes loading.
153 void WaitForTemplateURLServiceToLoad(TemplateURLService* service);
155 // Blocks until the |history_service|'s history finishes loading.
156 void WaitForHistoryToLoad(HistoryService* history_service);
158 // Download the given file and waits for the download to complete.
159 void DownloadURL(Browser* browser, const GURL& download_url);
161 // Send the given text to the omnibox and wait until it's updated.
162 void SendToOmniboxAndSubmit(LocationBar* location_bar,
163 const std::string& input);
165 // Gets the first browser that is not in the specified set.
166 Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers);
168 // Gets the size and value of the cookie string for |url| in the given tab.
169 // Can be called from any thread.
170 void GetCookies(const GURL& url,
171 content::WebContents* contents,
172 int* value_size,
173 std::string* value);
175 // A WindowedNotificationObserver hard-wired to observe
176 // chrome::NOTIFICATION_TAB_ADDED.
177 class WindowedTabAddedNotificationObserver
178 : public content::WindowedNotificationObserver {
179 public:
180 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either
181 // a specific source, or from all sources if |source| is
182 // NotificationService::AllSources().
183 explicit WindowedTabAddedNotificationObserver(
184 const content::NotificationSource& source);
186 // Returns the added tab, or NULL if no notification was observed yet.
187 content::WebContents* GetTab() { return added_tab_; }
189 void Observe(int type,
190 const content::NotificationSource& source,
191 const content::NotificationDetails& details) override;
193 private:
194 content::WebContents* added_tab_;
196 DISALLOW_COPY_AND_ASSIGN(WindowedTabAddedNotificationObserver);
199 // Similar to WindowedNotificationObserver but also provides a way of retrieving
200 // the details associated with the notification.
201 // Note that in order to use that class the details class should be copiable,
202 // which is the case with most notifications.
203 template <class U>
204 class WindowedNotificationObserverWithDetails
205 : public content::WindowedNotificationObserver {
206 public:
207 WindowedNotificationObserverWithDetails(
208 int notification_type,
209 const content::NotificationSource& source)
210 : content::WindowedNotificationObserver(notification_type, source) {}
212 // Fills |details| with the details of the notification received for |source|.
213 bool GetDetailsFor(uintptr_t source, U* details) {
214 typename std::map<uintptr_t, U>::const_iterator iter =
215 details_.find(source);
216 if (iter == details_.end())
217 return false;
218 *details = iter->second;
219 return true;
222 virtual void Observe(int type,
223 const content::NotificationSource& source,
224 const content::NotificationDetails& details) override {
225 const U* details_ptr = content::Details<U>(details).ptr();
226 if (details_ptr)
227 details_[source.map_key()] = *details_ptr;
228 content::WindowedNotificationObserver::Observe(type, source, details);
231 private:
232 std::map<uintptr_t, U> details_;
234 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserverWithDetails);
237 // Notification observer which waits for navigation events and blocks until
238 // a specific URL is loaded. The URL must be an exact match.
239 class UrlLoadObserver : public content::WindowedNotificationObserver {
240 public:
241 // Register to listen for notifications of the given type from either a
242 // specific source, or from all sources if |source| is
243 // NotificationService::AllSources().
244 UrlLoadObserver(const GURL& url, const content::NotificationSource& source);
245 ~UrlLoadObserver() override;
247 // content::NotificationObserver:
248 void Observe(int type,
249 const content::NotificationSource& source,
250 const content::NotificationDetails& details) override;
252 private:
253 GURL url_;
255 DISALLOW_COPY_AND_ASSIGN(UrlLoadObserver);
258 // Convenience class for waiting for a new browser to be created.
259 // Like WindowedNotificationObserver, this class provides a safe, non-racey
260 // way to wait for a new browser to be created.
261 class BrowserAddedObserver {
262 public:
263 BrowserAddedObserver();
264 ~BrowserAddedObserver();
266 // Wait for a new browser to be created, and return a pointer to it.
267 Browser* WaitForSingleNewBrowser();
269 private:
270 content::WindowedNotificationObserver notification_observer_;
271 std::set<Browser*> original_browsers_;
273 DISALLOW_COPY_AND_ASSIGN(BrowserAddedObserver);
276 // Configures the geolocation provider to always return the given position.
277 void OverrideGeolocation(double latitude, double longitude);
279 // Enumerates all history contents on the backend thread. Returns them in
280 // descending order by time.
281 class HistoryEnumerator {
282 public:
283 explicit HistoryEnumerator(Profile* profile);
284 ~HistoryEnumerator();
286 std::vector<GURL>& urls() { return urls_; }
288 private:
289 void HistoryQueryComplete(
290 const base::Closure& quit_task,
291 history::QueryResults* results);
293 std::vector<GURL> urls_;
295 base::CancelableTaskTracker tracker_;
297 DISALLOW_COPY_AND_ASSIGN(HistoryEnumerator);
300 } // namespace ui_test_utils
302 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_