Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1369)

Side by Side Diff: chrome/test/base/ui_test_utils.h

Issue 10807047: Create content\public\test\test_utils.h to hold common test classes that are used by unit and brows… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/test/base/tracing.cc ('k') | chrome/test/base/ui_test_utils.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 7
8 #include <map> 8 #include <map>
9 #include <queue> 9 #include <queue>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/basictypes.h" 14 #include "base/basictypes.h"
15 #include "base/process.h" 15 #include "base/process.h"
16 #include "base/run_loop.h"
17 #include "base/scoped_temp_dir.h" 16 #include "base/scoped_temp_dir.h"
18 #include "base/string16.h" 17 #include "base/string16.h"
19 #include "chrome/browser/ui/view_ids.h" 18 #include "chrome/browser/ui/view_ids.h"
20 #include "chrome/test/automation/dom_element_proxy.h" 19 #include "chrome/test/automation/dom_element_proxy.h"
21 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/notification_details.h" 21 #include "content/public/browser/notification_details.h"
23 #include "content/public/browser/notification_observer.h" 22 #include "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h" 23 #include "content/public/browser/notification_registrar.h"
25 #include "content/public/browser/notification_source.h" 24 #include "content/public/browser/notification_source.h"
25 #include "content/public/test/test_utils.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
28 #include "ui/base/keycodes/keyboard_codes.h" 28 #include "ui/base/keycodes/keyboard_codes.h"
29 #include "ui/gfx/native_widget_types.h" 29 #include "ui/gfx/native_widget_types.h"
30 #include "ui/ui_controls/ui_controls.h" 30 #include "ui/ui_controls/ui_controls.h"
31 #include "webkit/glue/window_open_disposition.h" 31 #include "webkit/glue/window_open_disposition.h"
32 32
33 #if defined(OS_WIN) 33 #if defined(OS_WIN)
34 #include "base/win/scoped_handle.h" 34 #include "base/win/scoped_handle.h"
35 #endif 35 #endif
(...skipping 15 matching lines...) Expand all
51 class ScopedTempDir; 51 class ScopedTempDir;
52 class SkBitmap; 52 class SkBitmap;
53 class TabContents; 53 class TabContents;
54 class TemplateURLService; 54 class TemplateURLService;
55 55
56 namespace chrome { 56 namespace chrome {
57 struct NavigateParams; 57 struct NavigateParams;
58 } 58 }
59 59
60 namespace content { 60 namespace content {
61 class MessageLoopRunner;
61 class RenderViewHost; 62 class RenderViewHost;
62 class RenderWidgetHost; 63 class RenderWidgetHost;
63 class WebContents; 64 class WebContents;
64 } 65 }
65 66
66 namespace gfx { 67 namespace gfx {
67 class Point; 68 class Point;
68 class Rect; 69 class Rect;
69 class Size; 70 class Size;
70 } 71 }
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 const content::NotificationSource& source) 292 const content::NotificationSource& source)
292 WARN_UNUSED_RESULT; 293 WARN_UNUSED_RESULT;
293 294
294 // Sends a move event blocking until received. Returns true if the event was 295 // Sends a move event blocking until received. Returns true if the event was
295 // successfully received. This uses ui_controls::SendMouse***NotifyWhenDone, 296 // successfully received. This uses ui_controls::SendMouse***NotifyWhenDone,
296 // see it for details. 297 // see it for details.
297 bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT; 298 bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT;
298 bool SendMouseEventsSync(ui_controls::MouseButton type, 299 bool SendMouseEventsSync(ui_controls::MouseButton type,
299 int state) WARN_UNUSED_RESULT; 300 int state) WARN_UNUSED_RESULT;
300 301
301 // Helper class to Run and Quit the message loop. Run and Quit can only happen
302 // once per instance. Make a new instance for each use. Calling Quit after Run
303 // has returned is safe and has no effect.
304 class MessageLoopRunner
305 : public base::RefCounted<MessageLoopRunner> {
306 public:
307 MessageLoopRunner();
308
309 // Run the current MessageLoop.
310 void Run();
311
312 // Quit the matching call to Run (nested MessageLoops are unaffected).
313 void Quit();
314
315 // Hand this closure off to code that uses callbacks to notify completion.
316 // Example:
317 // scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
318 // kick_off_some_api(runner.QuitNowClosure());
319 // runner.Run();
320 base::Closure QuitClosure();
321
322 private:
323 friend class base::RefCounted<MessageLoopRunner>;
324 ~MessageLoopRunner();
325
326 base::RunLoop run_loop_;
327
328 DISALLOW_COPY_AND_ASSIGN(MessageLoopRunner);
329 };
330
331 // This is a utility class for running a python websocket server 302 // This is a utility class for running a python websocket server
332 // during tests. The server is started during the construction of the 303 // during tests. The server is started during the construction of the
333 // object, and is stopped when the destructor is called. Note that 304 // object, and is stopped when the destructor is called. Note that
334 // because of the underlying script that is used: 305 // because of the underlying script that is used:
335 // 306 //
336 // third_paty/WebKit/Tools/Scripts/new-run-webkit-websocketserver 307 // third_paty/WebKit/Tools/Scripts/new-run-webkit-websocketserver
337 // 308 //
338 // Only *_wsh.py handlers found under "http/tests/websocket/tests" from the 309 // Only *_wsh.py handlers found under "http/tests/websocket/tests" from the
339 // |root_directory| will be found and active while running the test 310 // |root_directory| will be found and active while running the test
340 // server. 311 // server.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 356
386 // Holds port number which the python websocket server uses. 357 // Holds port number which the python websocket server uses.
387 int port_; 358 int port_;
388 359
389 // If the python websocket server serves with TLS. 360 // If the python websocket server serves with TLS.
390 bool secure_; 361 bool secure_;
391 362
392 DISALLOW_COPY_AND_ASSIGN(TestWebSocketServer); 363 DISALLOW_COPY_AND_ASSIGN(TestWebSocketServer);
393 }; 364 };
394 365
395 // A WindowedNotificationObserver allows code to watch for a notification
396 // over a window of time. Typically testing code will need to do something
397 // like this:
398 // PerformAction()
399 // WaitForCompletionNotification()
400 // This leads to flakiness as there's a window between PerformAction returning
401 // and the observers getting registered, where a notification will be missed.
402 //
403 // Rather, one can do this:
404 // WindowedNotificationObserver signal(...)
405 // PerformAction()
406 // signal.Wait()
407 class WindowedNotificationObserver : public content::NotificationObserver {
408 public:
409 // Register to listen for notifications of the given type from either a
410 // specific source, or from all sources if |source| is
411 // NotificationService::AllSources().
412 WindowedNotificationObserver(int notification_type,
413 const content::NotificationSource& source);
414 virtual ~WindowedNotificationObserver();
415
416 // Wait until the specified notification occurs. If the notification was
417 // emitted between the construction of this object and this call then it
418 // returns immediately.
419 void Wait();
420
421 // Returns NotificationService::AllSources() if we haven't observed a
422 // notification yet.
423 const content::NotificationSource& source() const {
424 return source_;
425 }
426
427 const content::NotificationDetails& details() const {
428 return details_;
429 }
430
431 // content::NotificationObserver:
432 virtual void Observe(int type,
433 const content::NotificationSource& source,
434 const content::NotificationDetails& details) OVERRIDE;
435
436 private:
437 bool seen_;
438 bool running_;
439 content::NotificationRegistrar registrar_;
440
441 content::NotificationSource source_;
442 content::NotificationDetails details_;
443 scoped_refptr<MessageLoopRunner> message_loop_runner_;
444
445 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver);
446 };
447
448 // A WindowedNotificationObserver hard-wired to observe 366 // A WindowedNotificationObserver hard-wired to observe
449 // chrome::NOTIFICATION_TAB_ADDED. 367 // chrome::NOTIFICATION_TAB_ADDED.
450 class WindowedTabAddedNotificationObserver 368 class WindowedTabAddedNotificationObserver
451 : public WindowedNotificationObserver { 369 : public content::WindowedNotificationObserver {
452 public: 370 public:
453 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either 371 // Register to listen for notifications of NOTIFICATION_TAB_ADDED from either
454 // a specific source, or from all sources if |source| is 372 // a specific source, or from all sources if |source| is
455 // NotificationService::AllSources(). 373 // NotificationService::AllSources().
456 explicit WindowedTabAddedNotificationObserver( 374 explicit WindowedTabAddedNotificationObserver(
457 const content::NotificationSource& source); 375 const content::NotificationSource& source);
458 376
459 // Returns the added tab, or NULL if no notification was observed yet. 377 // Returns the added tab, or NULL if no notification was observed yet.
460 content::WebContents* GetTab() { return added_tab_; } 378 content::WebContents* GetTab() { return added_tab_; }
461 379
462 virtual void Observe(int type, 380 virtual void Observe(int type,
463 const content::NotificationSource& source, 381 const content::NotificationSource& source,
464 const content::NotificationDetails& details) OVERRIDE; 382 const content::NotificationDetails& details) OVERRIDE;
465 383
466 private: 384 private:
467 content::WebContents* added_tab_; 385 content::WebContents* added_tab_;
468 386
469 DISALLOW_COPY_AND_ASSIGN(WindowedTabAddedNotificationObserver); 387 DISALLOW_COPY_AND_ASSIGN(WindowedTabAddedNotificationObserver);
470 }; 388 };
471 389
472 // Similar to WindowedNotificationObserver but also provides a way of retrieving 390 // Similar to WindowedNotificationObserver but also provides a way of retrieving
473 // the details associated with the notification. 391 // the details associated with the notification.
474 // Note that in order to use that class the details class should be copiable, 392 // Note that in order to use that class the details class should be copiable,
475 // which is the case with most notifications. 393 // which is the case with most notifications.
476 template <class U> 394 template <class U>
477 class WindowedNotificationObserverWithDetails 395 class WindowedNotificationObserverWithDetails
478 : public WindowedNotificationObserver { 396 : public content::WindowedNotificationObserver {
479 public: 397 public:
480 WindowedNotificationObserverWithDetails( 398 WindowedNotificationObserverWithDetails(
481 int notification_type, 399 int notification_type,
482 const content::NotificationSource& source) 400 const content::NotificationSource& source)
483 : WindowedNotificationObserver(notification_type, source) {} 401 : content::WindowedNotificationObserver(notification_type, source) {}
484 402
485 // Fills |details| with the details of the notification received for |source|. 403 // Fills |details| with the details of the notification received for |source|.
486 bool GetDetailsFor(uintptr_t source, U* details) { 404 bool GetDetailsFor(uintptr_t source, U* details) {
487 typename std::map<uintptr_t, U>::const_iterator iter = 405 typename std::map<uintptr_t, U>::const_iterator iter =
488 details_.find(source); 406 details_.find(source);
489 if (iter == details_.end()) 407 if (iter == details_.end())
490 return false; 408 return false;
491 *details = iter->second; 409 *details = iter->second;
492 return true; 410 return true;
493 } 411 }
494 412
495 virtual void Observe(int type, 413 virtual void Observe(int type,
496 const content::NotificationSource& source, 414 const content::NotificationSource& source,
497 const content::NotificationDetails& details) { 415 const content::NotificationDetails& details) {
498 const U* details_ptr = content::Details<U>(details).ptr(); 416 const U* details_ptr = content::Details<U>(details).ptr();
499 if (details_ptr) 417 if (details_ptr)
500 details_[source.map_key()] = *details_ptr; 418 details_[source.map_key()] = *details_ptr;
501 WindowedNotificationObserver::Observe(type, source, details); 419 content::WindowedNotificationObserver::Observe(type, source, details);
502 } 420 }
503 421
504 private: 422 private:
505 std::map<uintptr_t, U> details_; 423 std::map<uintptr_t, U> details_;
506 424
507 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserverWithDetails); 425 DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserverWithDetails);
508 }; 426 };
509 427
510 // Watches title changes on a tab, blocking until an expected title is set. 428 // Watches title changes on a tab, blocking until an expected title is set.
511 class TitleWatcher : public content::NotificationObserver { 429 class TitleWatcher : public content::NotificationObserver {
(...skipping 15 matching lines...) Expand all
527 445
528 private: 446 private:
529 // content::NotificationObserver 447 // content::NotificationObserver
530 virtual void Observe(int type, 448 virtual void Observe(int type,
531 const content::NotificationSource& source, 449 const content::NotificationSource& source,
532 const content::NotificationDetails& details) OVERRIDE; 450 const content::NotificationDetails& details) OVERRIDE;
533 451
534 content::WebContents* web_contents_; 452 content::WebContents* web_contents_;
535 std::vector<string16> expected_titles_; 453 std::vector<string16> expected_titles_;
536 content::NotificationRegistrar notification_registrar_; 454 content::NotificationRegistrar notification_registrar_;
537 scoped_refptr<MessageLoopRunner> message_loop_runner_; 455 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
538 456
539 // The most recently observed expected title, if any. 457 // The most recently observed expected title, if any.
540 string16 observed_title_; 458 string16 observed_title_;
541 459
542 bool expected_title_observed_; 460 bool expected_title_observed_;
543 bool quit_loop_on_observation_; 461 bool quit_loop_on_observation_;
544 462
545 DISALLOW_COPY_AND_ASSIGN(TitleWatcher); 463 DISALLOW_COPY_AND_ASSIGN(TitleWatcher);
546 }; 464 };
547 465
548 // Convenience class for waiting for a new browser to be created. 466 // Convenience class for waiting for a new browser to be created.
549 // Like WindowedNotificationObserver, this class provides a safe, non-racey 467 // Like WindowedNotificationObserver, this class provides a safe, non-racey
550 // way to wait for a new browser to be created. 468 // way to wait for a new browser to be created.
551 class BrowserAddedObserver { 469 class BrowserAddedObserver {
552 public: 470 public:
553 BrowserAddedObserver(); 471 BrowserAddedObserver();
554 ~BrowserAddedObserver(); 472 ~BrowserAddedObserver();
555 473
556 // Wait for a new browser to be created, and return a pointer to it. 474 // Wait for a new browser to be created, and return a pointer to it.
557 Browser* WaitForSingleNewBrowser(); 475 Browser* WaitForSingleNewBrowser();
558 476
559 private: 477 private:
560 WindowedNotificationObserver notification_observer_; 478 content::WindowedNotificationObserver notification_observer_;
561 std::set<Browser*> original_browsers_; 479 std::set<Browser*> original_browsers_;
562 480
563 DISALLOW_COPY_AND_ASSIGN(BrowserAddedObserver); 481 DISALLOW_COPY_AND_ASSIGN(BrowserAddedObserver);
564 }; 482 };
565 483
566 // See SendKeyPressAndWait. This function additionally performs a check on the 484 // See SendKeyPressAndWait. This function additionally performs a check on the
567 // NotificationDetails using the provided Details<U>. 485 // NotificationDetails using the provided Details<U>.
568 template <class U> 486 template <class U>
569 bool SendKeyPressAndWaitWithDetails( 487 bool SendKeyPressAndWaitWithDetails(
570 const Browser* browser, 488 const Browser* browser,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 545
628 // Overridden content::NotificationObserver methods. 546 // Overridden content::NotificationObserver methods.
629 virtual void Observe(int type, 547 virtual void Observe(int type,
630 const content::NotificationSource& source, 548 const content::NotificationSource& source,
631 const content::NotificationDetails& details) OVERRIDE; 549 const content::NotificationDetails& details) OVERRIDE;
632 550
633 private: 551 private:
634 content::NotificationRegistrar registrar_; 552 content::NotificationRegistrar registrar_;
635 std::queue<std::string> message_queue_; 553 std::queue<std::string> message_queue_;
636 bool waiting_for_message_; 554 bool waiting_for_message_;
637 scoped_refptr<MessageLoopRunner> message_loop_runner_; 555 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
638 556
639 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue); 557 DISALLOW_COPY_AND_ASSIGN(DOMMessageQueue);
640 }; 558 };
641 559
642 // Takes a snapshot of the given render widget, rendered at |page_size|. The 560 // Takes a snapshot of the given render widget, rendered at |page_size|. The
643 // snapshot is set to |bitmap|. Returns true on success. 561 // snapshot is set to |bitmap|. Returns true on success.
644 bool TakeRenderWidgetSnapshot(content::RenderWidgetHost* rwh, 562 bool TakeRenderWidgetSnapshot(content::RenderWidgetHost* rwh,
645 const gfx::Size& page_size, 563 const gfx::Size& page_size,
646 SkBitmap* bitmap) WARN_UNUSED_RESULT; 564 SkBitmap* bitmap) WARN_UNUSED_RESULT;
647 565
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 // ui_controls_linux.cc and ui_controls_mac.cc 604 // ui_controls_linux.cc and ui_controls_mac.cc
687 void ClickTask(ui_controls::MouseButton button, 605 void ClickTask(ui_controls::MouseButton button,
688 int state, 606 int state,
689 const base::Closure& followup); 607 const base::Closure& followup);
690 608
691 } // namespace internal 609 } // namespace internal
692 610
693 } // namespace ui_test_utils 611 } // namespace ui_test_utils
694 612
695 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_ 613 #endif // CHROME_TEST_BASE_UI_TEST_UTILS_H_
OLDNEW
« no previous file with comments | « chrome/test/base/tracing.cc ('k') | chrome/test/base/ui_test_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698