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

Side by Side Diff: chrome/browser/ui/panels/panel_browser_view_browsertest.cc

Issue 7605009: Implement drag and drop testing in a platform independent way. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed a build error on mac only. Created 9 years, 4 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
OLDNEW
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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/i18n/time_formatting.h" 6 #include "base/i18n/time_formatting.h"
7 #include "chrome/browser/extensions/extension_service.h" 7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_window.h" 10 #include "chrome/browser/ui/browser_window.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 } 88 }
89 89
90 void WaitTillBoundsAnimationFinished(PanelBrowserView* browser_view) { 90 void WaitTillBoundsAnimationFinished(PanelBrowserView* browser_view) {
91 // The timer for the animation will only kick in as async task. 91 // The timer for the animation will only kick in as async task.
92 while (browser_view->bounds_animator_->is_animating()) { 92 while (browser_view->bounds_animator_->is_animating()) {
93 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 93 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
94 MessageLoop::current()->RunAllPending(); 94 MessageLoop::current()->RunAllPending();
95 } 95 }
96 } 96 }
97 97
98 void ValidateDragging(PanelBrowserView** browser_views,
99 size_t num_browser_views,
100 size_t index_to_drag,
101 int delta_x,
102 int delta_y,
103 int* expected_delta_x_after_drag,
104 int* expected_delta_x_after_release,
105 bool cancel_dragging) {
106 // Keep track of the initial bounds for comparison.
107 scoped_array<gfx::Rect> initial_bounds(new gfx::Rect[num_browser_views]);
108 for (size_t i = 0; i < num_browser_views; ++i)
109 initial_bounds[i] = browser_views[i]->panel()->GetRestoredBounds();
110
111 // Trigger the mouse-pressed event.
112 // All panels should remain in their original positions.
113 views::MouseEvent pressed(ui::ET_MOUSE_PRESSED,
114 initial_bounds[index_to_drag].x(),
115 initial_bounds[index_to_drag].y(),
116 ui::EF_LEFT_BUTTON_DOWN);
117 browser_views[index_to_drag]->OnTitleBarMousePressed(pressed);
118 gfx::Rect bounds_after_press =
119 browser_views[index_to_drag]->panel()->GetRestoredBounds();
120 for (size_t i = 0; i < num_browser_views; ++i) {
121 EXPECT_EQ(initial_bounds[i],
122 browser_views[i]->panel()->GetRestoredBounds());
123 }
124
125 // Trigger the mouse_dragged event if delta_x or delta_y is not 0.
126 views::MouseEvent dragged(ui::ET_MOUSE_DRAGGED,
127 initial_bounds[index_to_drag].x() + delta_x,
128 initial_bounds[index_to_drag].y() + delta_y,
129 ui::EF_LEFT_BUTTON_DOWN);
130 if (delta_x || delta_y) {
131 browser_views[index_to_drag]->OnTitleBarMouseDragged(dragged);
132
133 for (size_t i = 0; i < num_browser_views; ++i) {
134 gfx::Rect expected_bounds = initial_bounds[i];
135 expected_bounds.Offset(expected_delta_x_after_drag[i], 0);
136 EXPECT_EQ(expected_bounds,
137 browser_views[i]->panel()->GetRestoredBounds());
138 }
139 }
140
141 // Cancel the dragging if asked.
142 // All panels should stay in their original positions.
143 if (cancel_dragging) {
144 browser_views[index_to_drag]->OnTitleBarMouseCaptureLost();
145 for (size_t i = 0; i < num_browser_views; ++i) {
146 EXPECT_EQ(initial_bounds[i],
147 browser_views[i]->panel()->GetRestoredBounds());
148 }
149 return;
150 }
151
152 // Otherwise, trigger the mouse-released event.
153 views::MouseEvent released(ui::ET_MOUSE_RELEASED, 0, 0, 0);
154 browser_views[index_to_drag]->OnTitleBarMouseReleased(released);
155 for (size_t i = 0; i < num_browser_views; ++i) {
156 gfx::Rect expected_bounds = initial_bounds[i];
157 expected_bounds.Offset(expected_delta_x_after_release[i], 0);
158 EXPECT_EQ(expected_bounds,
159 browser_views[i]->panel()->GetRestoredBounds());
160 }
161
162 // If releasing the drag causes the panels to swap, move these panels back
163 // so that the next test will not be affected.
164 if (initial_bounds[index_to_drag] !=
165 browser_views[index_to_drag]->panel()->GetRestoredBounds()) {
166 // Find the index of the panel to swap with the dragging panel.
167 size_t swapped_index = 0;
168 for (; swapped_index < num_browser_views; ++swapped_index) {
169 if (initial_bounds[index_to_drag] ==
170 browser_views[swapped_index]->panel()->GetRestoredBounds())
171 break;
172 }
173 ASSERT_NE(swapped_index, index_to_drag);
174
175 // Repeat the mouse events to this panel.
176 browser_views[swapped_index]->OnTitleBarMousePressed(pressed);
177 browser_views[swapped_index]->OnTitleBarMouseDragged(dragged);
178 browser_views[swapped_index]->OnTitleBarMouseReleased(released);
179 for (size_t i = 0; i < num_browser_views; ++i) {
180 EXPECT_EQ(initial_bounds[i],
181 browser_views[i]->panel()->GetRestoredBounds());
182 }
183 }
184 }
185
186 void ValidateSettingsMenuItems(ui::SimpleMenuModel* settings_menu_contents, 98 void ValidateSettingsMenuItems(ui::SimpleMenuModel* settings_menu_contents,
187 size_t num_expected_menu_items, 99 size_t num_expected_menu_items,
188 const MenuItem* expected_menu_items) { 100 const MenuItem* expected_menu_items) {
189 ASSERT_TRUE(settings_menu_contents); 101 ASSERT_TRUE(settings_menu_contents);
190 EXPECT_EQ(static_cast<int>(num_expected_menu_items), 102 EXPECT_EQ(static_cast<int>(num_expected_menu_items),
191 settings_menu_contents->GetItemCount()); 103 settings_menu_contents->GetItemCount());
192 for (size_t i = 0; i < num_expected_menu_items; ++i) { 104 for (size_t i = 0; i < num_expected_menu_items; ++i) {
193 if (expected_menu_items[i].id == -1) { 105 if (expected_menu_items[i].id == -1) {
194 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, 106 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR,
195 settings_menu_contents->GetTypeAt(i)); 107 settings_menu_contents->GetTypeAt(i));
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 // the panel or not. 443 // the panel or not.
532 browser_view->panel()->Deactivate(); 444 browser_view->panel()->Deactivate();
533 EXPECT_FALSE(browser_view->panel()->IsActive()); 445 EXPECT_FALSE(browser_view->panel()->IsActive());
534 446
535 mouse_watcher->MoveMouse(true); 447 mouse_watcher->MoveMouse(true);
536 EXPECT_TRUE(frame_view->settings_button_->IsVisible()); 448 EXPECT_TRUE(frame_view->settings_button_->IsVisible());
537 mouse_watcher->MoveMouse(false); 449 mouse_watcher->MoveMouse(false);
538 EXPECT_FALSE(frame_view->settings_button_->IsVisible()); 450 EXPECT_FALSE(frame_view->settings_button_->IsVisible());
539 } 451 }
540 452
541 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, TitleBarMouseEvent) {
542 // TODO(jianli): Move the test to platform-independent PanelManager unittest.
543
544 // Creates the 1st panel.
545 PanelBrowserView* browser_view1 = CreatePanelBrowserView("PanelTest1",
546 SHOW_AS_ACTIVE);
547
548 // The delta is from the pressed location which is the left-top corner of the
549 // panel to drag.
550 int small_negative_delta = -5;
551 int small_positive_delta = 5;
552 int big_negative_delta =
553 -(browser_view1->panel()->GetRestoredBounds().width() * 0.5 + 5);
554 int big_positive_delta =
555 browser_view1->panel()->GetRestoredBounds().width() * 1.5 + 5;
556
557 // Tests dragging a panel in single-panel environment.
558 // We should get back to the original position after the dragging is ended.
559 int expected_single_delta_x_after_drag[] = { big_negative_delta };
560 int expected_single_delta_x_after_release[] = { 0 };
561 ValidateDragging(&browser_view1,
562 1,
563 0,
564 big_negative_delta,
565 big_negative_delta,
566 expected_single_delta_x_after_drag,
567 expected_single_delta_x_after_release,
568 false);
569
570 expected_single_delta_x_after_drag[0] = big_positive_delta;
571 ValidateDragging(&browser_view1,
572 1,
573 0,
574 big_positive_delta,
575 big_positive_delta,
576 expected_single_delta_x_after_drag,
577 expected_single_delta_x_after_release,
578 false);
579
580 // Creates 2 more panels to test dragging a panel in multi-panel environment.
581 PanelBrowserView* browser_view2 = CreatePanelBrowserView("PanelTest2",
582 SHOW_AS_ACTIVE);
583 PanelBrowserView* browser_view3 = CreatePanelBrowserView("PanelTest3",
584 SHOW_AS_ACTIVE);
585 PanelBrowserView* browser_views[] =
586 { browser_view1, browser_view2, browser_view3 };
587 size_t browser_view_count = arraysize(browser_views);
588 int expected_delta_x_without_change[] = { 0, 0, 0 };
589
590 // Tests dragging a panel with small delta.
591 // We should get back to the original position after the dragging is ended.
592 for (size_t i = 0; i < browser_view_count; ++i) {
593 int expected_delta_x_after_drag[] = { 0, 0, 0 };
594
595 expected_delta_x_after_drag[i] = small_negative_delta;
596 ValidateDragging(browser_views,
597 browser_view_count,
598 i,
599 small_negative_delta,
600 small_negative_delta,
601 expected_delta_x_after_drag,
602 expected_delta_x_without_change,
603 false);
604
605 expected_delta_x_after_drag[i] = small_positive_delta;
606 ValidateDragging(browser_views,
607 browser_view_count,
608 i,
609 small_positive_delta,
610 small_positive_delta,
611 expected_delta_x_after_drag,
612 expected_delta_x_without_change,
613 false);
614 }
615
616 // Tests dragging a panel with big delta and then cancelling it.
617 // We should get back to the original position.
618 for (size_t i = 0; i < browser_view_count; ++i) {
619 int expected_delta_x_after_drag[] = { 0, 0, 0 };
620 expected_delta_x_after_drag[i] = big_negative_delta;
621 if (i + 1 < browser_view_count) {
622 expected_delta_x_after_drag[i + 1] =
623 browser_views[i]->panel()->GetRestoredBounds().x() -
624 browser_views[i + 1]->panel()->GetRestoredBounds().x();
625 }
626 ValidateDragging(browser_views,
627 browser_view_count,
628 i,
629 big_negative_delta,
630 big_negative_delta,
631 expected_delta_x_after_drag,
632 expected_delta_x_without_change,
633 true);
634
635 int expected_delta_x_after_drag2[] = { 0, 0, 0 };
636 expected_delta_x_after_drag2[i] = big_positive_delta;
637 if (static_cast<int>(i) - 1 >= 0) {
638 expected_delta_x_after_drag2[i - 1] =
639 browser_views[i]->panel()->GetRestoredBounds().x() -
640 browser_views[i - 1]->panel()->GetRestoredBounds().x();
641 }
642 ValidateDragging(browser_views,
643 browser_view_count,
644 i,
645 big_positive_delta,
646 big_positive_delta,
647 expected_delta_x_after_drag2,
648 expected_delta_x_without_change,
649 true);
650 }
651
652 // Tests dragging a panel with big delta.
653 // We should move to the new position after the dragging is ended.
654 for (size_t i = 0; i < browser_view_count; ++i) {
655 int expected_delta_x_after_drag[] = { 0, 0, 0 };
656 int expected_delta_x_after_release[] = { 0, 0, 0 };
657 expected_delta_x_after_drag[i] = big_negative_delta;
658 if (i + 1 < browser_view_count) {
659 expected_delta_x_after_drag[i + 1] =
660 browser_views[i]->panel()->GetRestoredBounds().x() -
661 browser_views[i + 1]->panel()->GetRestoredBounds().x();
662 expected_delta_x_after_release[i + 1] =
663 expected_delta_x_after_drag[i + 1];
664 expected_delta_x_after_release[i] =
665 browser_views[i + 1]->panel()->GetRestoredBounds().x() -
666 browser_views[i]->panel()->GetRestoredBounds().x();
667 }
668 ValidateDragging(browser_views,
669 browser_view_count,
670 i,
671 big_negative_delta,
672 big_negative_delta,
673 expected_delta_x_after_drag,
674 expected_delta_x_after_release,
675 false);
676
677 int expected_delta_x_after_drag2[] = { 0, 0, 0 };
678 int expected_delta_x_after_release2[] = { 0, 0, 0 };
679 expected_delta_x_after_drag2[i] = big_positive_delta;
680 if (static_cast<int>(i) - 1 >= 0) {
681 expected_delta_x_after_drag2[i - 1] =
682 browser_views[i]->panel()->GetRestoredBounds().x() -
683 browser_views[i - 1]->panel()->GetRestoredBounds().x();
684 expected_delta_x_after_release2[i - 1] =
685 expected_delta_x_after_drag2[i - 1];
686 expected_delta_x_after_release2[i] =
687 browser_views[i - 1]->panel()->GetRestoredBounds().x() -
688 browser_views[i]->panel()->GetRestoredBounds().x();
689 }
690 ValidateDragging(browser_views,
691 browser_view_count,
692 i,
693 big_positive_delta,
694 big_positive_delta,
695 expected_delta_x_after_drag2,
696 expected_delta_x_after_release2,
697 false);
698 }
699
700 // Closes all panels.
701 for (size_t i = 0; i < browser_view_count; ++i) {
702 browser_views[i]->panel()->Close();
703 // We're only verifying that PanelBrowserView::Close has been called when
704 // a panel is closed. This is to make sure the dragging is ended and does
705 // not interfere the closing process.
706 EXPECT_TRUE(browser_views[i]->closed());
707 }
708 }
709
710 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, SetBoundsAnimation) { 453 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, SetBoundsAnimation) {
711 PanelBrowserView* browser_view = CreatePanelBrowserView("PanelTest", 454 PanelBrowserView* browser_view = CreatePanelBrowserView("PanelTest",
712 SHOW_AS_ACTIVE); 455 SHOW_AS_ACTIVE);
713 456
714 // Validate that animation should be triggered when SetBounds is called. 457 // Validate that animation should be triggered when SetBounds is called.
715 gfx::Rect target_bounds(browser_view->GetBounds()); 458 gfx::Rect target_bounds(browser_view->GetBounds());
716 target_bounds.Offset(20, -30); 459 target_bounds.Offset(20, -30);
717 target_bounds.set_width(target_bounds.width() + 100); 460 target_bounds.set_width(target_bounds.width() + 100);
718 target_bounds.set_height(target_bounds.height() + 50); 461 target_bounds.set_height(target_bounds.height() + 50);
719 browser_view->SetBounds(target_bounds); 462 browser_view->SetBounds(target_bounds);
720 ASSERT_TRUE(browser_view->bounds_animator_.get()); 463 ASSERT_TRUE(browser_view->bounds_animator_.get());
721 EXPECT_TRUE(browser_view->bounds_animator_->is_animating()); 464 EXPECT_TRUE(browser_view->bounds_animator_->is_animating());
722 EXPECT_NE(browser_view->GetBounds(), target_bounds); 465 EXPECT_NE(browser_view->GetBounds(), target_bounds);
723 WaitTillBoundsAnimationFinished(browser_view); 466 WaitTillBoundsAnimationFinished(browser_view);
724 EXPECT_EQ(browser_view->GetBounds(), target_bounds); 467 EXPECT_EQ(browser_view->GetBounds(), target_bounds);
725 468
726 // Validates that no animation should be triggered for the panel currently 469 // Validates that no animation should be triggered for the panel currently
727 // being dragged. 470 // being dragged.
728 views::MouseEvent pressed(ui::ET_MOUSE_PRESSED, 471 browser_view->OnTitleBarMousePressed(gfx::Point(
729 target_bounds.x(), 472 target_bounds.x(), target_bounds.y()));
730 target_bounds.y(), 473 browser_view->OnTitleBarMouseDragged(gfx::Point(
731 ui::EF_LEFT_BUTTON_DOWN); 474 target_bounds.x() + 5, target_bounds.y() + 5));
732 browser_view->OnTitleBarMousePressed(pressed);
733
734 views::MouseEvent dragged(ui::ET_MOUSE_DRAGGED,
735 target_bounds.x() + 5,
736 target_bounds.y() + 5,
737 ui::EF_LEFT_BUTTON_DOWN);
738 browser_view->OnTitleBarMouseDragged(dragged);
739 EXPECT_FALSE(browser_view->bounds_animator_->is_animating()); 475 EXPECT_FALSE(browser_view->bounds_animator_->is_animating());
740 browser_view->OnTitleBarMouseCaptureLost(); 476 browser_view->OnTitleBarMouseCaptureLost();
741 477
742 browser_view->panel()->Close(); 478 browser_view->panel()->Close();
743 } 479 }
744 480
745 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, CreateSettingsMenu) { 481 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, CreateSettingsMenu) {
746 TestCreateSettingsMenuForExtension( 482 TestCreateSettingsMenuForExtension(
747 FILE_PATH_LITERAL("extension1"), Extension::EXTERNAL_POLICY_DOWNLOAD, 483 FILE_PATH_LITERAL("extension1"), Extension::EXTERNAL_POLICY_DOWNLOAD,
748 "", ""); 484 "", "");
749 TestCreateSettingsMenuForExtension( 485 TestCreateSettingsMenuForExtension(
750 FILE_PATH_LITERAL("extension2"), Extension::INVALID, 486 FILE_PATH_LITERAL("extension2"), Extension::INVALID,
751 "http://home", "options.html"); 487 "http://home", "options.html");
752 } 488 }
753 489
754 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, MinimizeAndRestore) { 490 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, MinimizeAndRestore) {
755 TestMinimizeAndRestore(); 491 TestMinimizeAndRestore();
756 } 492 }
757 493
758 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, DrawAttention) { 494 IN_PROC_BROWSER_TEST_F(PanelBrowserViewTest, DrawAttention) {
759 TestDrawAttention(); 495 TestDrawAttention();
760 } 496 }
761 #endif 497 #endif
OLDNEW
« no previous file with comments | « chrome/browser/ui/panels/panel_browser_view.cc ('k') | chrome/browser/ui/panels/panel_browser_window_cocoa.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698