| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_TEST_BASE_UI_TEST_UTILS_H_ | 5 #ifndef CHROME_TEST_BASE_UI_TEST_UTILS_H_ |
| 6 #define CHROME_TEST_BASE_UI_TEST_UTILS_H_ | 6 #define CHROME_TEST_BASE_UI_TEST_UTILS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <queue> | 10 #include <queue> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/message_loop.h" | |
| 17 #include "base/process.h" | 16 #include "base/process.h" |
| 17 #include "base/run_loop.h" |
| 18 #include "base/scoped_temp_dir.h" | 18 #include "base/scoped_temp_dir.h" |
| 19 #include "base/string16.h" | 19 #include "base/string16.h" |
| 20 #include "chrome/browser/ui/view_ids.h" | 20 #include "chrome/browser/ui/view_ids.h" |
| 21 #include "chrome/test/automation/dom_element_proxy.h" | 21 #include "chrome/test/automation/dom_element_proxy.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/notification_observer.h" | 23 #include "content/public/browser/notification_observer.h" |
| 24 #include "content/public/browser/notification_registrar.h" | 24 #include "content/public/browser/notification_registrar.h" |
| 25 #include "content/public/browser/notification_details.h" | 25 #include "content/public/browser/notification_details.h" |
| 26 #include "content/public/browser/notification_source.h" | 26 #include "content/public/browser/notification_source.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 BROWSER_TEST_MASK = BROWSER_TEST_WAIT_FOR_BROWSER | | 89 BROWSER_TEST_MASK = BROWSER_TEST_WAIT_FOR_BROWSER | |
| 90 BROWSER_TEST_WAIT_FOR_TAB | | 90 BROWSER_TEST_WAIT_FOR_TAB | |
| 91 BROWSER_TEST_WAIT_FOR_NAVIGATION | 91 BROWSER_TEST_WAIT_FOR_NAVIGATION |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 // Turns on nestable tasks, runs the message loop, then resets nestable tasks | 94 // Turns on nestable tasks, runs the message loop, then resets nestable tasks |
| 95 // to what they were originally. Prefer this over MessageLoop::Run for in | 95 // to what they were originally. Prefer this over MessageLoop::Run for in |
| 96 // process browser tests that need to block until a condition is met. | 96 // process browser tests that need to block until a condition is met. |
| 97 void RunMessageLoop(); | 97 void RunMessageLoop(); |
| 98 | 98 |
| 99 // Variant of RunMessageLoop that takes RunLoop. |
| 100 void RunThisRunLoop(base::RunLoop* run_loop); |
| 101 |
| 102 // Get task to quit the given RunLoop. It allows a few generations of pending |
| 103 // tasks to run as opposed to run_loop->QuitClosure(). |
| 104 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop); |
| 105 |
| 99 // Turns on nestable tasks, runs all pending tasks in the message loop, | 106 // Turns on nestable tasks, runs all pending tasks in the message loop, |
| 100 // then resets nestable tasks to what they were originally. Prefer this | 107 // then resets nestable tasks to what they were originally. Prefer this |
| 101 // over MessageLoop::RunAllPending for in process browser tests to run | 108 // over MessageLoop::RunAllPending for in process browser tests to run |
| 102 // all pending tasks. | 109 // all pending tasks. |
| 103 void RunAllPendingInMessageLoop(); | 110 void RunAllPendingInMessageLoop(); |
| 104 | 111 |
| 105 // Blocks the current thread until all the pending messages in the loop of the | 112 // Blocks the current thread until all the pending messages in the loop of the |
| 106 // thread |thread_id| have been processed. | 113 // thread |thread_id| have been processed. |
| 107 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id); | 114 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id); |
| 108 | 115 |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 const content::NotificationSource& source) | 305 const content::NotificationSource& source) |
| 299 WARN_UNUSED_RESULT; | 306 WARN_UNUSED_RESULT; |
| 300 | 307 |
| 301 // Sends a move event blocking until received. Returns true if the event was | 308 // Sends a move event blocking until received. Returns true if the event was |
| 302 // successfully received. This uses ui_controls::SendMouse***NotifyWhenDone, | 309 // successfully received. This uses ui_controls::SendMouse***NotifyWhenDone, |
| 303 // see it for details. | 310 // see it for details. |
| 304 bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT; | 311 bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT; |
| 305 bool SendMouseEventsSync(ui_controls::MouseButton type, | 312 bool SendMouseEventsSync(ui_controls::MouseButton type, |
| 306 int state) WARN_UNUSED_RESULT; | 313 int state) WARN_UNUSED_RESULT; |
| 307 | 314 |
| 308 // Run a message loop only for the specified amount of time. | 315 // Helper class to Run and Quit the message loop. Run and Quit can only happen |
| 309 class TimedMessageLoopRunner { | 316 // once per instance. Make a new instance for each use. Calling Quit after Run |
| 317 // has returned is safe and has no effect. |
| 318 class MessageLoopRunner |
| 319 : public base::RefCounted<MessageLoopRunner> { |
| 310 public: | 320 public: |
| 311 // Create new MessageLoopForUI and attach to it. | 321 MessageLoopRunner(); |
| 312 TimedMessageLoopRunner(); | |
| 313 | 322 |
| 314 // Attach to an existing message loop. | 323 // Run the current MessageLoop. |
| 315 explicit TimedMessageLoopRunner(MessageLoop* loop) | 324 void Run(); |
| 316 : loop_(loop), owned_(false), quit_loop_invoked_(false) {} | |
| 317 | 325 |
| 318 ~TimedMessageLoopRunner(); | 326 // Quit the matching call to Run (nested MessageLoops are unaffected). |
| 319 | |
| 320 // Run the message loop for ms milliseconds. | |
| 321 void RunFor(int ms); | |
| 322 | |
| 323 // Post Quit task to the message loop. | |
| 324 void Quit(); | 327 void Quit(); |
| 325 | 328 |
| 326 // Post delayed Quit task to the message loop. | 329 // Hand this closure off to code that uses callbacks to notify completion. |
| 327 void QuitAfter(int ms); | 330 // Example: |
| 328 | 331 // scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 329 bool WasTimedOut() const { | 332 // kick_off_some_api(runner.QuitNowClosure()); |
| 330 return !quit_loop_invoked_; | 333 // runner.Run(); |
| 331 } | 334 base::Closure QuitClosure(); |
| 332 | 335 |
| 333 private: | 336 private: |
| 334 MessageLoop* loop_; | 337 friend class base::RefCounted<MessageLoopRunner>; |
| 335 bool owned_; | 338 ~MessageLoopRunner(); |
| 336 bool quit_loop_invoked_; | |
| 337 | 339 |
| 338 DISALLOW_COPY_AND_ASSIGN(TimedMessageLoopRunner); | 340 base::RunLoop run_loop_; |
| 341 |
| 342 DISALLOW_COPY_AND_ASSIGN(MessageLoopRunner); |
| 339 }; | 343 }; |
| 340 | 344 |
| 341 // This is a utility class for running a python websocket server | 345 // This is a utility class for running a python websocket server |
| 342 // during tests. The server is started during the construction of the | 346 // during tests. The server is started during the construction of the |
| 343 // object, and is stopped when the destructor is called. Note that | 347 // object, and is stopped when the destructor is called. Note that |
| 344 // because of the underlying script that is used: | 348 // because of the underlying script that is used: |
| 345 // | 349 // |
| 346 // third_paty/WebKit/Tools/Scripts/new-run-webkit-websocketserver | 350 // third_paty/WebKit/Tools/Scripts/new-run-webkit-websocketserver |
| 347 // | 351 // |
| 348 // Only *_wsh.py handlers found under "http/tests/websocket/tests" from the | 352 // Only *_wsh.py handlers found under "http/tests/websocket/tests" from the |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 const content::NotificationSource& source, | 447 const content::NotificationSource& source, |
| 444 const content::NotificationDetails& details) OVERRIDE; | 448 const content::NotificationDetails& details) OVERRIDE; |
| 445 | 449 |
| 446 private: | 450 private: |
| 447 bool seen_; | 451 bool seen_; |
| 448 bool running_; | 452 bool running_; |
| 449 content::NotificationRegistrar registrar_; | 453 content::NotificationRegistrar registrar_; |
| 450 | 454 |
| 451 content::NotificationSource source_; | 455 content::NotificationSource source_; |
| 452 content::NotificationDetails details_; | 456 content::NotificationDetails details_; |
| 457 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 453 | 458 |
| 454 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); | 459 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); |
| 455 }; | 460 }; |
| 456 | 461 |
| 457 // A WindowedNotificationObserver hard-wired to observe | 462 // A WindowedNotificationObserver hard-wired to observe |
| 458 // chrome::NOTIFICATION_TAB_ADDED. | 463 // chrome::NOTIFICATION_TAB_ADDED. |
| 459 class WindowedTabAddedNotificationObserver | 464 class WindowedTabAddedNotificationObserver |
| 460 : public WindowedNotificationObserver { | 465 : public WindowedNotificationObserver { |
| 461 public: | 466 public: |
| 462 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either | 467 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 | 541 |
| 537 private: | 542 private: |
| 538 // content::NotificationObserver | 543 // content::NotificationObserver |
| 539 virtual void Observe(int type, | 544 virtual void Observe(int type, |
| 540 const content::NotificationSource& source, | 545 const content::NotificationSource& source, |
| 541 const content::NotificationDetails& details) OVERRIDE; | 546 const content::NotificationDetails& details) OVERRIDE; |
| 542 | 547 |
| 543 content::WebContents* web_contents_; | 548 content::WebContents* web_contents_; |
| 544 std::vector<string16> expected_titles_; | 549 std::vector<string16> expected_titles_; |
| 545 content::NotificationRegistrar notification_registrar_; | 550 content::NotificationRegistrar notification_registrar_; |
| 551 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 546 | 552 |
| 547 // The most recently observed expected title, if any. | 553 // The most recently observed expected title, if any. |
| 548 string16 observed_title_; | 554 string16 observed_title_; |
| 549 | 555 |
| 550 bool expected_title_observed_; | 556 bool expected_title_observed_; |
| 551 bool quit_loop_on_observation_; | 557 bool quit_loop_on_observation_; |
| 552 | 558 |
| 553 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); | 559 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); |
| 554 }; | 560 }; |
| 555 | 561 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 | 641 |
| 636 // Overridden content::NotificationObserver methods. | 642 // Overridden content::NotificationObserver methods. |
| 637 virtual void Observe(int type, | 643 virtual void Observe(int type, |
| 638 const content::NotificationSource& source, | 644 const content::NotificationSource& source, |
| 639 const content::NotificationDetails& details) OVERRIDE; | 645 const content::NotificationDetails& details) OVERRIDE; |
| 640 | 646 |
| 641 private: | 647 private: |
| 642 content::NotificationRegistrar registrar_; | 648 content::NotificationRegistrar registrar_; |
| 643 std::queue<std::string> message_queue_; | 649 std::queue<std::string> message_queue_; |
| 644 bool waiting_for_message_; | 650 bool waiting_for_message_; |
| 651 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 645 | 652 |
| 646 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); | 653 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); |
| 647 }; | 654 }; |
| 648 | 655 |
| 649 // Takes a snapshot of the given render widget, rendered at |page_size|. The | 656 // Takes a snapshot of the given render widget, rendered at |page_size|. The |
| 650 // snapshot is set to |bitmap|. Returns true on success. | 657 // snapshot is set to |bitmap|. Returns true on success. |
| 651 bool TakeRenderWidgetSnapshot(content::RenderWidgetHost* rwh, | 658 bool TakeRenderWidgetSnapshot(content::RenderWidgetHost* rwh, |
| 652 const gfx::Size& page_size, | 659 const gfx::Size& page_size, |
| 653 SkBitmap* bitmap) WARN_UNUSED_RESULT; | 660 SkBitmap* bitmap) WARN_UNUSED_RESULT; |
| 654 | 661 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 // ui_controls_linux.cc and ui_controls_mac.cc | 700 // ui_controls_linux.cc and ui_controls_mac.cc |
| 694 void ClickTask(ui_controls::MouseButton button, | 701 void ClickTask(ui_controls::MouseButton button, |
| 695 int state, | 702 int state, |
| 696 const base::Closure& followup); | 703 const base::Closure& followup); |
| 697 | 704 |
| 698 } // namespace internal | 705 } // namespace internal |
| 699 | 706 |
| 700 } // namespace ui_test_utils | 707 } // namespace ui_test_utils |
| 701 | 708 |
| 702 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_ | 709 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_ |
| OLD | NEW |