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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "chrome/browser/printing/print_preview_dialog_controller.h"
7 #include "chrome/browser/printing/print_view_manager.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "googleurl/src/gurl.h"
20 using content::WebContents
;
22 class PrintPreviewDialogControllerBrowserTest
: public InProcessBrowserTest
{
24 PrintPreviewDialogControllerBrowserTest() {}
25 virtual ~PrintPreviewDialogControllerBrowserTest() {}
27 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
28 #if !defined(GOOGLE_CHROME_BUILD)
29 command_line
->AppendSwitch(switches::kEnablePrintPreview
);
34 class PrintPreviewDialogDestroyedObserver
35 : public content::WebContentsObserver
{
37 explicit PrintPreviewDialogDestroyedObserver(WebContents
* dialog
)
38 : content::WebContentsObserver(dialog
),
39 dialog_destroyed_(false) {
41 virtual ~PrintPreviewDialogDestroyedObserver() {}
43 bool dialog_destroyed() { return dialog_destroyed_
; }
46 virtual void WebContentsDestroyed(WebContents
* contents
) OVERRIDE
{
47 dialog_destroyed_
= true;
50 bool dialog_destroyed_
;
53 // Test to verify that when a initiator tab navigates, we can create a new
54 // preview dialog for the new tab contents.
55 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest
,
56 NavigateFromInitiatorTab
) {
57 // Create a reference to initiator tab contents.
58 WebContents
* initiator_tab
=
59 browser()->tab_strip_model()->GetActiveWebContents();
60 ASSERT_TRUE(initiator_tab
);
62 printing::PrintPreviewDialogController
* dialog_controller
=
63 printing::PrintPreviewDialogController::GetInstance();
64 ASSERT_TRUE(dialog_controller
);
66 // Get the preview dialog for the initiator tab.
67 printing::PrintViewManager
* print_view_manager
=
68 printing::PrintViewManager::FromWebContents(initiator_tab
);
69 print_view_manager
->PrintPreviewNow(false);
70 WebContents
* preview_dialog
=
71 dialog_controller
->GetOrCreatePreviewDialog(initiator_tab
);
73 // Check a new print preview dialog got created.
74 ASSERT_TRUE(preview_dialog
);
75 ASSERT_NE(initiator_tab
, preview_dialog
);
77 // Navigate in the initiator tab. Make sure navigating destroys the print
79 PrintPreviewDialogDestroyedObserver
observer(preview_dialog
);
80 GURL
url(chrome::kChromeUINewTabURL
);
81 ui_test_utils::NavigateToURL(browser(), url
);
82 ASSERT_TRUE(observer
.dialog_destroyed());
84 // Get the print preview dialog for the initiator tab.
85 print_view_manager
->PrintPreviewNow(false);
86 WebContents
* new_preview_dialog
=
87 dialog_controller
->GetOrCreatePreviewDialog(initiator_tab
);
89 // Check a new preview dialog got created.
90 EXPECT_TRUE(new_preview_dialog
);
93 // Test to verify that after reloading the initiator tab, it creates a new
94 // print preview dialog.
95 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest
,
97 // Create a reference to initiator tab contents.
98 WebContents
* initiator_tab
=
99 browser()->tab_strip_model()->GetActiveWebContents();
100 ASSERT_TRUE(initiator_tab
);
102 printing::PrintPreviewDialogController
* dialog_controller
=
103 printing::PrintPreviewDialogController::GetInstance();
104 ASSERT_TRUE(dialog_controller
);
106 // Create a preview dialog for the initiator tab.
107 printing::PrintViewManager
* print_view_manager
=
108 printing::PrintViewManager::FromWebContents(initiator_tab
);
109 print_view_manager
->PrintPreviewNow(false);
110 WebContents
* preview_dialog
=
111 dialog_controller
->GetOrCreatePreviewDialog(initiator_tab
);
113 // Check a new print preview dialog got created.
114 ASSERT_TRUE(preview_dialog
);
115 ASSERT_NE(initiator_tab
, preview_dialog
);
117 // Reload the initiator tab. Make sure reloading destroys the print preview
119 PrintPreviewDialogDestroyedObserver
dialog_destroyed_observer(preview_dialog
);
120 content::WindowedNotificationObserver
notification_observer(
121 content::NOTIFICATION_LOAD_STOP
,
122 content::NotificationService::AllSources());
123 chrome::Reload(browser(), CURRENT_TAB
);
124 notification_observer
.Wait();
125 ASSERT_TRUE(dialog_destroyed_observer
.dialog_destroyed());
127 // Create a preview dialog for the initiator tab.
128 print_view_manager
->PrintPreviewNow(false);
129 WebContents
* new_preview_dialog
=
130 dialog_controller
->GetOrCreatePreviewDialog(initiator_tab
);
132 EXPECT_TRUE(new_preview_dialog
);