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

Side by Side Diff: chrome/browser/ui/panels/panel_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: More code review feedback. 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 "chrome/browser/download/download_manager.h" 6 #include "chrome/browser/download/download_manager.h"
7 #include "chrome/browser/extensions/extension_service.h" 7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/prefs/pref_service.h" 8 #include "chrome/browser/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_list.h" 11 #include "chrome/browser/ui/browser_list.h"
12 #include "chrome/browser/ui/browser_window.h" 12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/find_bar/find_bar.h" 13 #include "chrome/browser/ui/find_bar/find_bar.h"
14 #include "chrome/browser/ui/find_bar/find_bar_controller.h" 14 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
15 #include "chrome/browser/ui/panels/native_panel.h"
15 #include "chrome/browser/ui/panels/panel.h" 16 #include "chrome/browser/ui/panels/panel.h"
16 #include "chrome/browser/ui/panels/panel_manager.h" 17 #include "chrome/browser/ui/panels/panel_manager.h"
17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
18 #include "chrome/browser/web_applications/web_app.h" 19 #include "chrome/browser/web_applications/web_app.h"
19 #include "chrome/common/chrome_notification_types.h" 20 #include "chrome/common/chrome_notification_types.h"
20 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/extensions/extension.h" 22 #include "chrome/common/extensions/extension.h"
22 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
23 #include "chrome/test/base/in_process_browser_test.h" 24 #include "chrome/test/base/in_process_browser_test.h"
24 #include "chrome/test/base/ui_test_utils.h" 25 #include "chrome/test/base/ui_test_utils.h"
(...skipping 24 matching lines...) Expand all
49 bounds, 50 bounds,
50 browser()->profile()); 51 browser()->profile());
51 EXPECT_TRUE(panel_browser->is_type_panel()); 52 EXPECT_TRUE(panel_browser->is_type_panel());
52 53
53 TabContentsWrapper* tab_contents = 54 TabContentsWrapper* tab_contents =
54 new TabContentsWrapper(new TestTabContents(browser()->profile(), NULL)); 55 new TabContentsWrapper(new TestTabContents(browser()->profile(), NULL));
55 panel_browser->AddTab(tab_contents, PageTransition::LINK); 56 panel_browser->AddTab(tab_contents, PageTransition::LINK);
56 57
57 Panel* panel = static_cast<Panel*>(panel_browser->window()); 58 Panel* panel = static_cast<Panel*>(panel_browser->window());
58 panel->Show(); 59 panel->Show();
60 MessageLoopForUI::current()->RunAllPending();
61
59 return panel; 62 return panel;
60 } 63 }
61 64
62 // Creates a testing extension. 65 // Creates a testing extension.
63 scoped_refptr<Extension> CreateExtension(const FilePath::StringType& path) { 66 scoped_refptr<Extension> CreateExtension(const FilePath::StringType& path) {
64 #if defined(OS_WIN) 67 #if defined(OS_WIN)
65 FilePath full_path(FILE_PATH_LITERAL("c:\\")); 68 FilePath full_path(FILE_PATH_LITERAL("c:\\"));
66 #else 69 #else
67 FilePath full_path(FILE_PATH_LITERAL("/")); 70 FilePath full_path(FILE_PATH_LITERAL("/"));
68 #endif 71 #endif
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 web_app::GenerateApplicationNameFromExtensionId(extension3->id()), 146 web_app::GenerateApplicationNameFromExtensionId(extension3->id()),
144 gfx::Rect(0, 0, 500, 200)); 147 gfx::Rect(0, 0, 500, 200));
145 signal3.Wait(); 148 signal3.Wait();
146 signal4.Wait(); 149 signal4.Wait();
147 ASSERT_EQ(2, panel_manager->num_panels()); 150 ASSERT_EQ(2, panel_manager->num_panels());
148 EXPECT_LT(panel6->GetBounds().right(), panel1->GetBounds().x()); 151 EXPECT_LT(panel6->GetBounds().right(), panel1->GetBounds().x());
149 152
150 panel1->Close(); 153 panel1->Close();
151 panel6->Close(); 154 panel6->Close();
152 } 155 }
156
157 struct DragTestData {
158 DragTestData(int drag_delta_x,
159 int drag_delta_y,
160 bool is_big_delta,
161 bool should_cancel_drag)
162 : drag_delta_x(drag_delta_x), drag_delta_y(drag_delta_y),
163 is_big_delta(is_big_delta), should_cancel_drag(should_cancel_drag) {}
164 int drag_delta_x;
165 int drag_delta_y;
166 bool is_big_delta; // Drag big enough to cause shuffling.
167 bool should_cancel_drag;
168 };
169
170 void TestDragging(std::vector<Panel*>* panels,
171 const DragTestData& drag_test_data) {
172 // Test dragging each panel in the list.
173 for (size_t drag_index = 0; drag_index < panels->size(); ++drag_index) {
174 std::vector<int> expected_delta_x_after_drag;
175 std::vector<int> expected_delta_x_after_finish;
176
177 for (size_t j = 0; j < panels->size(); ++j) {
jianli 2011/08/11 20:52:09 Probably more efficient to say: size_t num_panel
prasadt 2011/08/11 21:07:56 Done.
178 expected_delta_x_after_drag.push_back(0);
179 expected_delta_x_after_finish.push_back(0);
180 }
181
182 expected_delta_x_after_drag[drag_index] = drag_test_data.drag_delta_x;
183 size_t swap_index = drag_index;
184 if (drag_test_data.is_big_delta) {
185 if (drag_test_data.drag_delta_x > 0 && drag_index != 0) {
186 // Dragged to right.
187 swap_index = drag_index - 1;
188 } else if (drag_test_data.drag_delta_x < 0 &&
189 drag_index != panels->size() - 1) {
190 // Dragged to left.
191 swap_index = drag_index + 1;
192 }
193 }
194 if (swap_index != drag_index) {
195 expected_delta_x_after_drag[swap_index] =
196 (*panels)[drag_index]->GetRestoredBounds().x() -
197 (*panels)[swap_index]->GetRestoredBounds().x();
198 expected_delta_x_after_finish[swap_index] =
199 expected_delta_x_after_drag[swap_index];
200 expected_delta_x_after_finish[drag_index] =
201 -expected_delta_x_after_drag[swap_index];
202 }
203 ValidateDragging(*panels, drag_index, drag_test_data.drag_delta_x,
204 drag_test_data.drag_delta_y, expected_delta_x_after_drag,
205 expected_delta_x_after_finish, drag_test_data.should_cancel_drag);
206
207 if (swap_index != drag_index && !drag_test_data.should_cancel_drag) {
208 // Swap the panels in the vector so they reflect their true relative
209 // positions.
210 Panel* tmp_panel = (*panels)[swap_index];
211 (*panels)[swap_index] = (*panels)[drag_index];
212 (*panels)[drag_index] = tmp_panel;
213 }
214 }
215 }
216
217 void ValidateDragging(const std::vector<Panel*>& panels,
218 int index_to_drag,
219 int delta_x,
220 int delta_y,
221 const std::vector<int>& expected_delta_x_after_drag,
222 const std::vector<int>& expected_delta_x_after_finish,
223 bool should_cancel_drag) {
224 // Keep track of the initial bounds for comparison.
225 std::vector<gfx::Rect> initial_bounds(panels.size());
226 for (size_t i = 0; i < panels.size(); ++i)
227 initial_bounds[i] = panels[i]->GetRestoredBounds();
228
229 // Trigger the mouse-pressed event.
230 // All panels should remain in their original positions.
231 NativePanel* panel_to_drag = panels[index_to_drag]->native_panel();
232 NativePanelTesting* panel_testing_to_drag =
233 NativePanelTesting::Create(panel_to_drag);
234 gfx::Point button_press_point(initial_bounds[index_to_drag].x(),
235 initial_bounds[index_to_drag].y());
236 panel_testing_to_drag->PressLeftMouseButtonTitlebar(button_press_point);
237 for (size_t i = 0; i < panels.size(); ++i)
238 EXPECT_EQ(initial_bounds[i], panels[i]->GetRestoredBounds());
239
240 if (delta_x || delta_y) {
jianli 2011/08/11 20:52:09 We can remove this if test since we do not have th
prasadt 2011/08/11 21:07:56 Done.
241 // Trigger the drag.
242 panel_testing_to_drag->DragTitlebar(delta_x, delta_y);
243
244 for (size_t i = 0; i < panels.size(); ++i) {
245 gfx::Rect expected_bounds = initial_bounds[i];
246 expected_bounds.Offset(expected_delta_x_after_drag[i], 0);
247 EXPECT_EQ(expected_bounds, panels[i]->GetRestoredBounds());
248 }
249 }
250
251 // Cancel the dragging if asked.
jianli 2011/08/11 20:52:09 To be consistent with other comment: dragging => d
prasadt 2011/08/11 21:07:56 Done.
252 // All panels should stay in their original positions.
253 if (should_cancel_drag) {
254 panel_testing_to_drag->CancelDragTitlebar();
255 for (size_t i = 0; i < panels.size(); ++i)
256 EXPECT_EQ(initial_bounds[i], panels[i]->GetRestoredBounds());
257 return;
258 }
259
260 // Otherwise finish the drag.
261 panel_testing_to_drag->FinishDragTitlebar();
262 for (size_t i = 0; i < panels.size(); ++i) {
263 gfx::Rect expected_bounds = initial_bounds[i];
264 expected_bounds.Offset(expected_delta_x_after_finish[i], 0);
265 EXPECT_EQ(expected_bounds, panels[i]->GetRestoredBounds());
266 }
267 delete panel_testing_to_drag;
jianli 2011/08/11 20:52:09 Better to use scoped_ptr, instead of self-deleting
prasadt 2011/08/11 21:07:56 Done.
268 }
153 }; 269 };
154 270
155 IN_PROC_BROWSER_TEST_F(PanelBrowserTest, CreatePanel) { 271 IN_PROC_BROWSER_TEST_F(PanelBrowserTest, CreatePanel) {
156 PanelManager* panel_manager = PanelManager::GetInstance(); 272 PanelManager* panel_manager = PanelManager::GetInstance();
157 EXPECT_EQ(0, panel_manager->num_panels()); // No panels initially. 273 EXPECT_EQ(0, panel_manager->num_panels()); // No panels initially.
158 274
159 Panel* panel = CreatePanel("PanelTest", gfx::Rect()); 275 Panel* panel = CreatePanel("PanelTest", gfx::Rect());
160 EXPECT_EQ(1, panel_manager->num_panels()); 276 EXPECT_EQ(1, panel_manager->num_panels());
161 277
162 gfx::Rect bounds = panel->GetBounds(); 278 gfx::Rect bounds = panel->GetBounds();
(...skipping 18 matching lines...) Expand all
181 #ifdef OS_MACOSX 297 #ifdef OS_MACOSX
182 #define MAYBE_CreatePanelOnOverflow DISABLED_CreatePanelOnOverflow 298 #define MAYBE_CreatePanelOnOverflow DISABLED_CreatePanelOnOverflow
183 #else 299 #else
184 #define MAYBE_CreatePanelOnOverflow CreatePanelOnOverflow 300 #define MAYBE_CreatePanelOnOverflow CreatePanelOnOverflow
185 #endif 301 #endif
186 302
187 IN_PROC_BROWSER_TEST_F(PanelBrowserTest, MAYBE_CreatePanelOnOverflow) { 303 IN_PROC_BROWSER_TEST_F(PanelBrowserTest, MAYBE_CreatePanelOnOverflow) {
188 TestCreatePanelOnOverflow(); 304 TestCreatePanelOnOverflow();
189 } 305 }
190 306
307 #ifdef OS_MACOSX
308 #define MAYBE_DragPanels DISABLED_DragPanels
309 #else
310 #define MAYBE_DragPanels DragPanels
311 #endif
312
313 IN_PROC_BROWSER_TEST_F(PanelBrowserTest, MAYBE_DragPanels) {
314 std::vector<Panel*> panels;
315 panels.push_back(CreatePanel("PanelTest0", gfx::Rect()));
316
317 int small_delta = 5;
318 int big_delta = panels[0]->GetRestoredBounds().width() * 0.5 + 5;
319
320 // Setup test data.
321 // Template - DragTestData(delta_x, delta_y, is_big_delta, should_cancel_drag)
322 std::vector<DragTestData> drag_test_data;
323 drag_test_data.push_back(DragTestData(
324 small_delta, small_delta, false, false));
325 drag_test_data.push_back(DragTestData(
326 -small_delta, -small_delta, false, false));
327 drag_test_data.push_back(DragTestData(big_delta, big_delta, true, false));
328 drag_test_data.push_back(DragTestData(big_delta, small_delta, true, false));
329 drag_test_data.push_back(DragTestData(-big_delta, -big_delta, true, false));
330 drag_test_data.push_back(DragTestData(-big_delta, 0, true, false));
331 drag_test_data.push_back(DragTestData(big_delta, big_delta, true, true));
332 drag_test_data.push_back(DragTestData(-big_delta, -big_delta, true, true));
333
334 for (int num_panels = 1; num_panels <= 3; ++num_panels) {
335 if (num_panels > 1)
336 panels.push_back(CreatePanel("PanelTest", gfx::Rect()));
337 for (size_t j = 0; j < drag_test_data.size(); ++j) {
338 // Test for each combination of drag test data and number of panels.
339 TestDragging(&panels, drag_test_data[j]);
340 }
341 }
342
343 for (size_t i = 0; i < panels.size(); ++i)
344 panels[i]->Close();
345 }
346
191 class PanelDownloadTest : public PanelBrowserTest { 347 class PanelDownloadTest : public PanelBrowserTest {
192 public: 348 public:
193 PanelDownloadTest() : PanelBrowserTest() { } 349 PanelDownloadTest() : PanelBrowserTest() { }
194 350
195 // Creates a temporary directory for downloads that is auto-deleted 351 // Creates a temporary directory for downloads that is auto-deleted
196 // on destruction. 352 // on destruction.
197 bool CreateDownloadDirectory(Profile* profile) { 353 bool CreateDownloadDirectory(Profile* profile) {
198 bool created = downloads_directory_.CreateUniqueTempDir(); 354 bool created = downloads_directory_.CreateUniqueTempDir();
199 if (!created) 355 if (!created)
200 return false; 356 return false;
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 EXPECT_EQ(1, tabbed_browser->tab_count()); 501 EXPECT_EQ(1, tabbed_browser->tab_count());
346 ASSERT_TRUE(tabbed_browser->window()->IsDownloadShelfVisible()); 502 ASSERT_TRUE(tabbed_browser->window()->IsDownloadShelfVisible());
347 tabbed_browser->CloseWindow(); 503 tabbed_browser->CloseWindow();
348 #endif 504 #endif
349 505
350 EXPECT_EQ(1, panel_browser->tab_count()); 506 EXPECT_EQ(1, panel_browser->tab_count());
351 ASSERT_FALSE(panel_browser->window()->IsDownloadShelfVisible()); 507 ASSERT_FALSE(panel_browser->window()->IsDownloadShelfVisible());
352 508
353 panel_browser->CloseWindow(); 509 panel_browser->CloseWindow();
354 } 510 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698