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 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 5 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
| 8 #include <string> |
8 | 9 |
9 #include "chrome/browser/printing/print_preview_test.h" | 10 #include "chrome/browser/printing/print_preview_test.h" |
10 #include "chrome/browser/printing/print_view_manager.h" | 11 #include "chrome/browser/printing/print_view_manager.h" |
11 #include "chrome/browser/ui/browser_commands.h" | 12 #include "chrome/browser/ui/browser_commands.h" |
12 #include "chrome/browser/ui/browser_finder.h" | 13 #include "chrome/browser/ui/browser_finder.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 15 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 16 #include "content/public/browser/navigation_controller.h" |
15 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/navigation_entry.h" |
16 #include "content/public/browser/web_contents_delegate.h" | 19 #include "content/public/browser/web_contents_delegate.h" |
| 20 #include "content/public/browser/web_contents_observer.h" |
17 #include "content/public/common/url_constants.h" | 21 #include "content/public/common/url_constants.h" |
18 #include "content/public/test/web_contents_tester.h" | 22 #include "content/public/test/web_contents_tester.h" |
19 | 23 |
20 using content::WebContents; | 24 using content::WebContents; |
| 25 using content::WebContentsObserver; |
21 | 26 |
22 namespace { | 27 namespace { |
23 // content::WebContentsDelegate destructor is protected: subclass for testing. | 28 // content::WebContentsDelegate destructor is protected: subclass for testing. |
24 class TestWebContentsDelegate : public content::WebContentsDelegate {}; | 29 class TestWebContentsDelegate : public content::WebContentsDelegate {}; |
| 30 |
| 31 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { |
| 32 public: |
| 33 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) |
| 34 : WebContentsObserver(dialog), |
| 35 dialog_destroyed_(false) { |
| 36 } |
| 37 ~PrintPreviewDialogDestroyedObserver() override {} |
| 38 |
| 39 bool dialog_destroyed() const { return dialog_destroyed_; } |
| 40 |
| 41 private: |
| 42 // content::WebContentsObserver implementation. |
| 43 void WebContentsDestroyed() override { dialog_destroyed_ = true; } |
| 44 |
| 45 bool dialog_destroyed_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); |
| 48 }; |
| 49 |
25 } // namespace | 50 } // namespace |
26 | 51 |
27 namespace printing { | 52 namespace printing { |
28 | 53 |
29 using PrintPreviewDialogControllerUnitTest = PrintPreviewTest; | 54 using PrintPreviewDialogControllerUnitTest = PrintPreviewTest; |
30 | 55 |
31 // Create/Get a preview dialog for initiator. | 56 // Create/Get a preview dialog for initiator. |
32 TEST_F(PrintPreviewDialogControllerUnitTest, GetOrCreatePreviewDialog) { | 57 TEST_F(PrintPreviewDialogControllerUnitTest, GetOrCreatePreviewDialog) { |
33 // Lets start with one window with one tab. | 58 // Lets start with one window with one tab. |
34 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); | 59 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 WebContents* new_preview_dialog = | 187 WebContents* new_preview_dialog = |
163 dialog_controller->GetOrCreatePreviewDialog(initiator); | 188 dialog_controller->GetOrCreatePreviewDialog(initiator); |
164 | 189 |
165 // New print preview dialog is a constrained window, so the number of tabs is | 190 // New print preview dialog is a constrained window, so the number of tabs is |
166 // still 1. | 191 // still 1. |
167 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 192 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
168 // Verify a new print preview dialog has been created. | 193 // Verify a new print preview dialog has been created. |
169 EXPECT_NE(new_preview_dialog, preview_dialog); | 194 EXPECT_NE(new_preview_dialog, preview_dialog); |
170 } | 195 } |
171 | 196 |
| 197 // Test that print preview dialogs close on navigation to new pages |
| 198 // and when navigating to old pages via fwd/back, but that auto navigation |
| 199 // (typed + address bar) to an existing page as occurs in gmail does not cause |
| 200 // the dialogs to close. |
| 201 TEST_F(PrintPreviewDialogControllerUnitTest, CloseDialogOnNavigation) { |
| 202 // Two similar URLs (same webpage, different URL fragments) |
| 203 GURL tiger_barb("https://www.google.com/#q=tiger+barb"); |
| 204 GURL tiger("https://www.google.com/#q=tiger"); |
| 205 |
| 206 // Set up by opening a new tab and getting web contents |
| 207 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
| 208 EXPECT_EQ(0, browser()->tab_strip_model()->count()); |
| 209 chrome::NewTab(browser()); |
| 210 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 211 WebContents* web_contents = |
| 212 browser()->tab_strip_model()->GetActiveWebContents(); |
| 213 ASSERT_TRUE(web_contents); |
| 214 content::NavigationController& nav_controller = web_contents->GetController(); |
| 215 |
| 216 // Navigate to first page |
| 217 nav_controller.LoadURL(tiger, content::Referrer(), |
| 218 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), |
| 219 std::string()); |
| 220 CommitPendingLoad(&nav_controller); |
| 221 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); |
| 222 |
| 223 // Get the preview dialog |
| 224 PrintPreviewDialogController* dialog_controller = |
| 225 PrintPreviewDialogController::GetInstance(); |
| 226 ASSERT_TRUE(dialog_controller); |
| 227 WebContents* tiger_preview_dialog = |
| 228 dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| 229 PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents); |
| 230 manager->PrintPreviewNow(false); |
| 231 |
| 232 // New print preview dialog is a constrained window, so the number of tabs is |
| 233 // still 1. |
| 234 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 235 EXPECT_NE(web_contents, tiger_preview_dialog); |
| 236 PrintPreviewDialogDestroyedObserver tiger_destroyed(tiger_preview_dialog); |
| 237 |
| 238 // Navigate via link to a similar page. |
| 239 nav_controller.LoadURL(tiger_barb, content::Referrer(), |
| 240 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), |
| 241 std::string()); |
| 242 CommitPendingLoad(&nav_controller); |
| 243 |
| 244 // Check navigation was successful |
| 245 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); |
| 246 |
| 247 // Print preview now should return true as the navigation should have closed |
| 248 // |tiger_preview_dialog| and the previous dialog should have closed. |
| 249 EXPECT_TRUE(manager->PrintPreviewNow(false)); |
| 250 WebContents* tiger_barb_preview_dialog = |
| 251 dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| 252 ASSERT_TRUE(tiger_barb_preview_dialog); |
| 253 |
| 254 // Check a new dialog was created - either the pointers should be different or |
| 255 // the previous web contents must have been destroyed. |
| 256 EXPECT_TRUE(tiger_destroyed.dialog_destroyed() || |
| 257 tiger_barb_preview_dialog != tiger_preview_dialog); |
| 258 EXPECT_NE(tiger_barb_preview_dialog, web_contents); |
| 259 PrintPreviewDialogDestroyedObserver tiger_barb_destroyed( |
| 260 tiger_barb_preview_dialog); |
| 261 |
| 262 // Now this returns false as |tiger_barb_preview_dialog| is open. |
| 263 EXPECT_FALSE(manager->PrintPreviewNow(false)); |
| 264 |
| 265 // Navigate with back button or ALT+LEFT ARROW to a similar page. |
| 266 nav_controller.GoBack(); |
| 267 CommitPendingLoad(&nav_controller); |
| 268 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); |
| 269 EXPECT_TRUE(manager->PrintPreviewNow(false)); |
| 270 |
| 271 // Get new dialog |
| 272 WebContents* tiger_preview_dialog_2 = |
| 273 dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| 274 ASSERT_TRUE(tiger_preview_dialog_2); |
| 275 |
| 276 // Verify this is a new dialog. |
| 277 EXPECT_TRUE(tiger_barb_destroyed.dialog_destroyed() || |
| 278 tiger_barb_preview_dialog != tiger_preview_dialog_2); |
| 279 EXPECT_NE(tiger_preview_dialog_2, web_contents); |
| 280 PrintPreviewDialogDestroyedObserver tiger_2_destroyed( |
| 281 tiger_preview_dialog_2); |
| 282 |
| 283 // Try to simulate Gmail navigation: Navigate to an existing page (via |
| 284 // Forward) but modify the navigation type while pending to look like an |
| 285 // address bar + typed transition (like Gmail auto navigation) |
| 286 nav_controller.GoForward(); |
| 287 nav_controller.GetPendingEntry()->SetTransitionType(ui::PageTransitionFromInt( |
| 288 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); |
| 289 CommitPendingLoad(&nav_controller); |
| 290 |
| 291 // Navigation successful |
| 292 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); |
| 293 |
| 294 // Print preview should not have changed due to this navigation type so print |
| 295 // preview now should return false, dialog is still alive, and the dialog |
| 296 // returned by GetOrCreatePreviewDialog should be the same as the earlier |
| 297 // dialog. |
| 298 EXPECT_FALSE(manager->PrintPreviewNow(false)); |
| 299 EXPECT_FALSE(tiger_2_destroyed.dialog_destroyed()); |
| 300 WebContents* tiger_preview_dialog_2b = |
| 301 dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| 302 ASSERT_TRUE(tiger_preview_dialog_2b); |
| 303 EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2); |
| 304 EXPECT_NE(tiger_preview_dialog_2b, web_contents); |
| 305 } |
| 306 |
172 } // namespace printing | 307 } // namespace printing |
OLD | NEW |