OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/ui_test_utils.h" | 5 #include "chrome/test/ui_test_utils.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "chrome/browser/ui/browser_list.h" | 28 #include "chrome/browser/ui/browser_list.h" |
29 #include "chrome/browser/ui/browser_window.h" | 29 #include "chrome/browser/ui/browser_window.h" |
30 #include "chrome/browser/ui/find_bar/find_notification_details.h" | 30 #include "chrome/browser/ui/find_bar/find_notification_details.h" |
31 #include "chrome/browser/ui/find_bar/find_tab_helper.h" | 31 #include "chrome/browser/ui/find_bar/find_tab_helper.h" |
32 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 32 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
33 #include "chrome/common/chrome_notification_types.h" | 33 #include "chrome/common/chrome_notification_types.h" |
34 #include "chrome/common/chrome_paths.h" | 34 #include "chrome/common/chrome_paths.h" |
35 #include "chrome/common/extensions/extension_action.h" | 35 #include "chrome/common/extensions/extension_action.h" |
36 #include "chrome/test/automation/javascript_execution_controller.h" | 36 #include "chrome/test/automation/javascript_execution_controller.h" |
37 #include "chrome/test/bookmark_load_observer.h" | 37 #include "chrome/test/bookmark_load_observer.h" |
| 38 #include "chrome/test/test_navigation_observer.h" |
38 #include "content/browser/renderer_host/render_process_host.h" | 39 #include "content/browser/renderer_host/render_process_host.h" |
39 #include "content/browser/renderer_host/render_view_host.h" | 40 #include "content/browser/renderer_host/render_view_host.h" |
40 #include "content/browser/tab_contents/navigation_controller.h" | 41 #include "content/browser/tab_contents/navigation_controller.h" |
41 #include "content/browser/tab_contents/navigation_entry.h" | 42 #include "content/browser/tab_contents/navigation_entry.h" |
42 #include "content/browser/tab_contents/tab_contents.h" | 43 #include "content/browser/tab_contents/tab_contents.h" |
43 #include "googleurl/src/gurl.h" | 44 #include "googleurl/src/gurl.h" |
44 #include "net/base/net_util.h" | 45 #include "net/base/net_util.h" |
45 #include "testing/gtest/include/gtest/gtest.h" | 46 #include "testing/gtest/include/gtest/gtest.h" |
46 #include "third_party/skia/include/core/SkBitmap.h" | 47 #include "third_party/skia/include/core/SkBitmap.h" |
47 #include "third_party/skia/include/core/SkColor.h" | 48 #include "third_party/skia/include/core/SkColor.h" |
48 #include "ui/gfx/size.h" | 49 #include "ui/gfx/size.h" |
49 | 50 |
50 #if defined(TOOLKIT_VIEWS) | 51 #if defined(TOOLKIT_VIEWS) |
51 #include "views/focus/accelerator_handler.h" | 52 #include "views/focus/accelerator_handler.h" |
52 #endif | 53 #endif |
53 | 54 |
54 namespace ui_test_utils { | 55 namespace ui_test_utils { |
55 | 56 |
56 namespace { | 57 namespace { |
57 | 58 |
58 // Used to block until a navigation completes. | |
59 class NavigationNotificationObserver : public NotificationObserver { | |
60 public: | |
61 NavigationNotificationObserver(NavigationController* controller, | |
62 int number_of_navigations) | |
63 : navigation_started_(false), | |
64 navigations_completed_(0), | |
65 number_of_navigations_(number_of_navigations), | |
66 running_(false), | |
67 done_(false) { | |
68 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
69 Source<NavigationController>(controller)); | |
70 registrar_.Add(this, content::NOTIFICATION_LOAD_START, | |
71 Source<NavigationController>(controller)); | |
72 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, | |
73 Source<NavigationController>(controller)); | |
74 } | |
75 | |
76 void Run() { | |
77 if (!done_) { | |
78 running_ = true; | |
79 RunMessageLoop(); | |
80 } | |
81 } | |
82 | |
83 virtual void Observe(int type, | |
84 const NotificationSource& source, | |
85 const NotificationDetails& details) { | |
86 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED || | |
87 type == content::NOTIFICATION_LOAD_START) { | |
88 navigation_started_ = true; | |
89 } else if (type == content::NOTIFICATION_LOAD_STOP) { | |
90 if (navigation_started_ && | |
91 ++navigations_completed_ == number_of_navigations_) { | |
92 navigation_started_ = false; | |
93 done_ = true; | |
94 if (running_) | |
95 MessageLoopForUI::current()->Quit(); | |
96 } | |
97 } | |
98 } | |
99 | |
100 private: | |
101 NotificationRegistrar registrar_; | |
102 | |
103 // If true the navigation has started. | |
104 bool navigation_started_; | |
105 | |
106 // The number of navigations that have been completed. | |
107 int navigations_completed_; | |
108 | |
109 // The number of navigations to wait for. | |
110 int number_of_navigations_; | |
111 | |
112 // Calls to Observe() can happen early, before the user calls Run(), or | |
113 // after. When we've seen all the navigations we're looking for, we set | |
114 // done_ to true; then when Run() is called we'll never need to run the | |
115 // event loop. Also, we don't need to quit the event loop when we're | |
116 // done if we never had to start an event loop. | |
117 bool running_; | |
118 bool done_; | |
119 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); | |
120 }; | |
121 | |
122 class DOMOperationObserver : public NotificationObserver { | 59 class DOMOperationObserver : public NotificationObserver { |
123 public: | 60 public: |
124 explicit DOMOperationObserver(RenderViewHost* render_view_host) | 61 explicit DOMOperationObserver(RenderViewHost* render_view_host) |
125 : did_respond_(false) { | 62 : did_respond_(false) { |
126 registrar_.Add(this, chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, | 63 registrar_.Add(this, chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, |
127 Source<RenderViewHost>(render_view_host)); | 64 Source<RenderViewHost>(render_view_host)); |
128 ui_test_utils::RunMessageLoop(); | 65 ui_test_utils::RunMessageLoop(); |
129 } | 66 } |
130 | 67 |
131 virtual void Observe(int type, | 68 virtual void Observe(int type, |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 WaitForNavigations(&tab_contents->controller(), number_of_navigations); | 267 WaitForNavigations(&tab_contents->controller(), number_of_navigations); |
331 return true; | 268 return true; |
332 } | 269 } |
333 | 270 |
334 void WaitForNavigation(NavigationController* controller) { | 271 void WaitForNavigation(NavigationController* controller) { |
335 WaitForNavigations(controller, 1); | 272 WaitForNavigations(controller, 1); |
336 } | 273 } |
337 | 274 |
338 void WaitForNavigations(NavigationController* controller, | 275 void WaitForNavigations(NavigationController* controller, |
339 int number_of_navigations) { | 276 int number_of_navigations) { |
340 NavigationNotificationObserver observer(controller, number_of_navigations); | 277 TestNavigationObserver observer(controller, NULL, number_of_navigations); |
341 observer.Run(); | 278 observer.WaitForObservation(); |
342 } | 279 } |
343 | 280 |
344 void WaitForNewTab(Browser* browser) { | 281 void WaitForNewTab(Browser* browser) { |
345 TestNotificationObserver observer; | 282 TestNotificationObserver observer; |
346 RegisterAndWait(&observer, content::NOTIFICATION_TAB_ADDED, | 283 RegisterAndWait(&observer, content::NOTIFICATION_TAB_ADDED, |
347 Source<Browser>(browser)); | 284 Source<Browser>(browser)); |
348 } | 285 } |
349 | 286 |
350 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { | 287 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { |
351 TestNotificationObserver observer; | 288 TestNotificationObserver observer; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 // Navigates the specified tab (via |disposition|) of |browser| to |url|, | 334 // Navigates the specified tab (via |disposition|) of |browser| to |url|, |
398 // blocking until the |number_of_navigations| specified complete. | 335 // blocking until the |number_of_navigations| specified complete. |
399 // |disposition| indicates what tab the download occurs in, and | 336 // |disposition| indicates what tab the download occurs in, and |
400 // |browser_test_flags| controls what to wait for before continuing. | 337 // |browser_test_flags| controls what to wait for before continuing. |
401 static void NavigateToURLWithDispositionBlockUntilNavigationsComplete( | 338 static void NavigateToURLWithDispositionBlockUntilNavigationsComplete( |
402 Browser* browser, | 339 Browser* browser, |
403 const GURL& url, | 340 const GURL& url, |
404 int number_of_navigations, | 341 int number_of_navigations, |
405 WindowOpenDisposition disposition, | 342 WindowOpenDisposition disposition, |
406 int browser_test_flags) { | 343 int browser_test_flags) { |
407 NavigationNotificationObserver | 344 TestNavigationObserver |
408 same_tab_observer(&browser->GetSelectedTabContents()->controller(), | 345 same_tab_observer(&browser->GetSelectedTabContents()->controller(), |
409 number_of_navigations); | 346 NULL, number_of_navigations); |
410 | 347 |
411 std::set<Browser*> initial_browsers; | 348 std::set<Browser*> initial_browsers; |
412 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin(); | 349 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin(); |
413 iter != BrowserList::end(); | 350 iter != BrowserList::end(); |
414 ++iter) { | 351 ++iter) { |
415 initial_browsers.insert(*iter); | 352 initial_browsers.insert(*iter); |
416 } | 353 } |
417 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); | 354 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); |
418 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER) | 355 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER) |
419 browser = WaitForBrowserNotInSet(initial_browsers); | 356 browser = WaitForBrowserNotInSet(initial_browsers); |
(...skipping 11 matching lines...) Expand all Loading... |
431 << " Unable to wait for navigation to \"" << url.spec() | 368 << " Unable to wait for navigation to \"" << url.spec() |
432 << "\" because the new tab is not available yet"; | 369 << "\" because the new tab is not available yet"; |
433 return; | 370 return; |
434 } else if ((disposition == CURRENT_TAB) || | 371 } else if ((disposition == CURRENT_TAB) || |
435 (disposition == NEW_FOREGROUND_TAB) || | 372 (disposition == NEW_FOREGROUND_TAB) || |
436 (disposition == SINGLETON_TAB)) { | 373 (disposition == SINGLETON_TAB)) { |
437 // The currently selected tab is the right one. | 374 // The currently selected tab is the right one. |
438 tab_contents = browser->GetSelectedTabContents(); | 375 tab_contents = browser->GetSelectedTabContents(); |
439 } | 376 } |
440 if (disposition == CURRENT_TAB) { | 377 if (disposition == CURRENT_TAB) { |
441 same_tab_observer.Run(); | 378 same_tab_observer.WaitForObservation(); |
442 return; | 379 return; |
443 } else if (tab_contents) { | 380 } else if (tab_contents) { |
444 NavigationController* controller = &tab_contents->controller(); | 381 NavigationController* controller = &tab_contents->controller(); |
445 WaitForNavigations(controller, number_of_navigations); | 382 WaitForNavigations(controller, number_of_navigations); |
446 return; | 383 return; |
447 } | 384 } |
448 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \"" | 385 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \"" |
449 << url.spec() << "\"" | 386 << url.spec() << "\"" |
450 << " because we can't get the tab contents"; | 387 << " because we can't get the tab contents"; |
451 } | 388 } |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1033 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); | 970 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); |
1034 } | 971 } |
1035 | 972 |
1036 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { | 973 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { |
1037 DCHECK(bitmap); | 974 DCHECK(bitmap); |
1038 SnapshotTaker taker; | 975 SnapshotTaker taker; |
1039 return taker.TakeEntirePageSnapshot(rvh, bitmap); | 976 return taker.TakeEntirePageSnapshot(rvh, bitmap); |
1040 } | 977 } |
1041 | 978 |
1042 } // namespace ui_test_utils | 979 } // namespace ui_test_utils |
OLD | NEW |