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

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

Issue 11414223: Move the test functions that deal with focus to interactive_ui_tets_utils.h and into the interactiv… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years 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
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 #include "chrome/test/base/ui_test_utils.h" 5 #include "chrome/test/base/ui_test_utils.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #endif 9 #endif
10 10
(...skipping 27 matching lines...) Expand all
38 #include "chrome/browser/ui/browser_finder.h" 38 #include "chrome/browser/ui/browser_finder.h"
39 #include "chrome/browser/ui/browser_list.h" 39 #include "chrome/browser/ui/browser_list.h"
40 #include "chrome/browser/ui/browser_navigator.h" 40 #include "chrome/browser/ui/browser_navigator.h"
41 #include "chrome/browser/ui/browser_tabstrip.h" 41 #include "chrome/browser/ui/browser_tabstrip.h"
42 #include "chrome/browser/ui/browser_window.h" 42 #include "chrome/browser/ui/browser_window.h"
43 #include "chrome/browser/ui/find_bar/find_notification_details.h" 43 #include "chrome/browser/ui/find_bar/find_notification_details.h"
44 #include "chrome/browser/ui/find_bar/find_tab_helper.h" 44 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
45 #include "chrome/browser/ui/host_desktop.h" 45 #include "chrome/browser/ui/host_desktop.h"
46 #include "chrome/browser/ui/omnibox/location_bar.h" 46 #include "chrome/browser/ui/omnibox/location_bar.h"
47 #include "chrome/browser/ui/omnibox/omnibox_view.h" 47 #include "chrome/browser/ui/omnibox/omnibox_view.h"
48 #include "chrome/browser/ui/window_snapshot/window_snapshot.h"
48 #include "chrome/common/chrome_notification_types.h" 49 #include "chrome/common/chrome_notification_types.h"
49 #include "chrome/common/chrome_paths.h" 50 #include "chrome/common/chrome_paths.h"
50 #include "chrome/common/pref_names.h" 51 #include "chrome/common/pref_names.h"
51 #include "chrome/test/base/bookmark_load_observer.h" 52 #include "chrome/test/base/bookmark_load_observer.h"
52 #include "content/public/browser/browser_thread.h" 53 #include "content/public/browser/browser_thread.h"
53 #include "content/public/browser/dom_operation_notification_details.h" 54 #include "content/public/browser/dom_operation_notification_details.h"
54 #include "content/public/browser/download_item.h" 55 #include "content/public/browser/download_item.h"
55 #include "content/public/browser/download_manager.h" 56 #include "content/public/browser/download_manager.h"
56 #include "content/public/browser/geolocation.h" 57 #include "content/public/browser/geolocation.h"
57 #include "content/public/browser/navigation_controller.h" 58 #include "content/public/browser/navigation_controller.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 int number_of_matches_; 144 int number_of_matches_;
144 gfx::Rect selection_rect_; 145 gfx::Rect selection_rect_;
145 // The id of the current find request, obtained from WebContents. Allows us 146 // The id of the current find request, obtained from WebContents. Allows us
146 // to monitor when the search completes. 147 // to monitor when the search completes.
147 int current_find_request_id_; 148 int current_find_request_id_;
148 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 149 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
149 150
150 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver); 151 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver);
151 }; 152 };
152 153
154 const char kSnapshotBaseName[] = "ChromiumSnapshot";
155 const char kSnapshotExtension[] = ".png";
156
157 FilePath GetSnapshotFileName(const FilePath& snapshot_directory) {
158 base::Time::Exploded the_time;
159
160 base::Time::Now().LocalExplode(&the_time);
161 std::string filename(StringPrintf("%s%04d%02d%02d%02d%02d%02d%s",
162 kSnapshotBaseName, the_time.year, the_time.month, the_time.day_of_month,
163 the_time.hour, the_time.minute, the_time.second, kSnapshotExtension));
164
165 FilePath snapshot_file = snapshot_directory.AppendASCII(filename);
166 if (file_util::PathExists(snapshot_file)) {
167 int index = 0;
168 std::string suffix;
169 FilePath trial_file;
170 do {
171 suffix = StringPrintf(" (%d)", ++index);
172 trial_file = snapshot_file.InsertBeforeExtensionASCII(suffix);
173 } while (file_util::PathExists(trial_file));
174 snapshot_file = trial_file;
175 }
176 return snapshot_file;
177 }
178
153 } // namespace 179 } // namespace
154 180
155 bool GetCurrentTabTitle(const Browser* browser, string16* title) { 181 bool GetCurrentTabTitle(const Browser* browser, string16* title) {
156 WebContents* web_contents = chrome::GetActiveWebContents(browser); 182 WebContents* web_contents = chrome::GetActiveWebContents(browser);
157 if (!web_contents) 183 if (!web_contents)
158 return false; 184 return false;
159 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry(); 185 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry();
160 if (!last_entry) 186 if (!last_entry)
161 return false; 187 return false;
162 title->assign(last_entry->GetTitleForDisplay("")); 188 title->assign(last_entry->GetTitleForDisplay(""));
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 omnibox->SetUserText(ASCIIToUTF16(input)); 472 omnibox->SetUserText(ASCIIToUTF16(input));
447 location_bar->AcceptInput(); 473 location_bar->AcceptInput();
448 while (!omnibox->model()->autocomplete_controller()->done()) { 474 while (!omnibox->model()->autocomplete_controller()->done()) {
449 content::WindowedNotificationObserver observer( 475 content::WindowedNotificationObserver observer(
450 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY, 476 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
451 content::NotificationService::AllSources()); 477 content::NotificationService::AllSources());
452 observer.Wait(); 478 observer.Wait();
453 } 479 }
454 } 480 }
455 481
456 bool GetNativeWindow(const Browser* browser, gfx::NativeWindow* native_window) {
457 BrowserWindow* window = browser->window();
458 if (!window)
459 return false;
460
461 *native_window = window->GetNativeWindow();
462 return *native_window;
463 }
464
465 bool BringBrowserWindowToFront(const Browser* browser) {
466 gfx::NativeWindow window = NULL;
467 if (!GetNativeWindow(browser, &window))
468 return false;
469
470 return ui_test_utils::ShowAndFocusNativeWindow(window);
471 }
472
473 Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers) { 482 Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers) {
474 for (BrowserList::const_iterator iter = BrowserList::begin(); 483 for (BrowserList::const_iterator iter = BrowserList::begin();
475 iter != BrowserList::end(); 484 iter != BrowserList::end();
476 ++iter) { 485 ++iter) {
477 if (excluded_browsers.find(*iter) == excluded_browsers.end()) 486 if (excluded_browsers.find(*iter) == excluded_browsers.end())
478 return *iter; 487 return *iter;
479 } 488 }
480 489
481 return NULL; 490 return NULL;
482 } 491 }
483 492
484 bool SendKeyPressSync(const Browser* browser,
485 ui::KeyboardCode key,
486 bool control,
487 bool shift,
488 bool alt,
489 bool command) {
490 gfx::NativeWindow window = NULL;
491 if (!GetNativeWindow(browser, &window))
492 return false;
493 scoped_refptr<content::MessageLoopRunner> runner =
494 new content::MessageLoopRunner;
495 bool result;
496 result = ui_controls::SendKeyPressNotifyWhenDone(
497 window, key, control, shift, alt, command, runner->QuitClosure());
498 #if defined(OS_WIN)
499 if (!result && BringBrowserWindowToFront(browser)) {
500 result = ui_controls::SendKeyPressNotifyWhenDone(
501 window, key, control, shift, alt, command, runner->QuitClosure());
502 }
503 #endif
504 if (!result) {
505 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
506 return false;
507 }
508
509 // Run the message loop. It'll stop running when either the key was received
510 // or the test timed out (in which case testing::Test::HasFatalFailure should
511 // be set).
512 runner->Run();
513 return !testing::Test::HasFatalFailure();
514 }
515
516 bool SendKeyPressAndWait(const Browser* browser,
517 ui::KeyboardCode key,
518 bool control,
519 bool shift,
520 bool alt,
521 bool command,
522 int type,
523 const content::NotificationSource& source) {
524 content::WindowedNotificationObserver observer(type, source);
525
526 if (!SendKeyPressSync(browser, key, control, shift, alt, command))
527 return false;
528
529 observer.Wait();
530 return !testing::Test::HasFatalFailure();
531 }
532
533 bool SendMouseMoveSync(const gfx::Point& location) {
534 scoped_refptr<content::MessageLoopRunner> runner =
535 new content::MessageLoopRunner;
536 if (!ui_controls::SendMouseMoveNotifyWhenDone(
537 location.x(), location.y(), runner->QuitClosure())) {
538 return false;
539 }
540 runner->Run();
541 return !testing::Test::HasFatalFailure();
542 }
543
544 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) {
545 scoped_refptr<content::MessageLoopRunner> runner =
546 new content::MessageLoopRunner;
547 if (!ui_controls::SendMouseEventsNotifyWhenDone(
548 type, state, runner->QuitClosure())) {
549 return false;
550 }
551 runner->Run();
552 return !testing::Test::HasFatalFailure();
553 }
554
555 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver( 493 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver(
556 const content::NotificationSource& source) 494 const content::NotificationSource& source)
557 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source), 495 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source),
558 added_tab_(NULL) { 496 added_tab_(NULL) {
559 } 497 }
560 498
561 void WindowedTabAddedNotificationObserver::Observe( 499 void WindowedTabAddedNotificationObserver::Observe(
562 int type, 500 int type,
563 const content::NotificationSource& source, 501 const content::NotificationSource& source,
564 const content::NotificationDetails& details) { 502 const content::NotificationDetails& details) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 SnapshotTaker taker; 611 SnapshotTaker taker;
674 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 612 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
675 } 613 }
676 614
677 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 615 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
678 DCHECK(bitmap); 616 DCHECK(bitmap);
679 SnapshotTaker taker; 617 SnapshotTaker taker;
680 return taker.TakeEntirePageSnapshot(rvh, bitmap); 618 return taker.TakeEntirePageSnapshot(rvh, bitmap);
681 } 619 }
682 620
621 #if defined(OS_WIN)
622
623 bool SaveScreenSnapshotToDirectory(const FilePath& directory,
624 FilePath* screenshot_path) {
625 bool succeeded = false;
626 FilePath out_path(GetSnapshotFileName(directory));
627
628 MONITORINFO monitor_info = {};
629 monitor_info.cbSize = sizeof(monitor_info);
630 HMONITOR main_monitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
631 if (GetMonitorInfo(main_monitor, &monitor_info)) {
632 RECT& rect = monitor_info.rcMonitor;
633
634 std::vector<unsigned char> png_data;
635 gfx::Rect bounds(
636 gfx::Size(rect.right - rect.left, rect.bottom - rect.top));
637 if (chrome::internal::GrabWindowSnapshot(NULL, &png_data, bounds) &&
638 png_data.size() <= INT_MAX) {
639 int bytes = static_cast<int>(png_data.size());
640 int written = file_util::WriteFile(
641 out_path, reinterpret_cast<char*>(&png_data[0]), bytes);
642 succeeded = (written == bytes);
643 }
644 }
645
646 if (succeeded && screenshot_path != NULL)
647 *screenshot_path = out_path;
648
649 return succeeded;
650 }
651
652 bool SaveScreenSnapshotToDesktop(FilePath* screenshot_path) {
653 FilePath desktop;
654
655 return PathService::Get(base::DIR_USER_DESKTOP, &desktop) &&
656 SaveScreenSnapshotToDirectory(desktop, screenshot_path);
657 }
658
659 #endif // defined(OS_WIN)
660
683 void OverrideGeolocation(double latitude, double longitude) { 661 void OverrideGeolocation(double latitude, double longitude) {
684 content::Geoposition position; 662 content::Geoposition position;
685 position.latitude = latitude; 663 position.latitude = latitude;
686 position.longitude = longitude; 664 position.longitude = longitude;
687 position.altitude = 0.; 665 position.altitude = 0.;
688 position.accuracy = 0.; 666 position.accuracy = 0.;
689 position.timestamp = base::Time::Now(); 667 position.timestamp = base::Time::Now();
690 scoped_refptr<content::MessageLoopRunner> runner = 668 scoped_refptr<content::MessageLoopRunner> runner =
691 new content::MessageLoopRunner; 669 new content::MessageLoopRunner;
692 content::OverrideLocationForTesting(position, runner->QuitClosure()); 670 content::OverrideLocationForTesting(position, runner->QuitClosure());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 void HistoryEnumerator::HistoryQueryComplete( 704 void HistoryEnumerator::HistoryQueryComplete(
727 const base::Closure& quit_task, 705 const base::Closure& quit_task,
728 HistoryService::Handle request_handle, 706 HistoryService::Handle request_handle,
729 history::QueryResults* results) { 707 history::QueryResults* results) {
730 for (size_t i = 0; i < results->size(); ++i) 708 for (size_t i = 0; i < results->size(); ++i)
731 urls_.push_back((*results)[i].url()); 709 urls_.push_back((*results)[i].url());
732 quit_task.Run(); 710 quit_task.Run();
733 } 711 }
734 712
735 } // namespace ui_test_utils 713 } // namespace ui_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698