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 "base/prefs/pref_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/content_settings/host_content_settings_map.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_commands.h"
14 #include "chrome/browser/ui/browser_dialogs.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/test_navigation_observer.h"
25 #include "content/public/test/test_utils.h"
28 #include "ui/aura/window.h"
29 #include "ui/aura/window_tree_host.h"
32 using task_manager::browsertest_util::MatchAboutBlankTab
;
33 using task_manager::browsertest_util::MatchAnyPrint
;
34 using task_manager::browsertest_util::MatchAnyTab
;
35 using task_manager::browsertest_util::MatchPrint
;
36 using task_manager::browsertest_util::WaitForTaskManagerRows
;
40 class PrintPreviewTest
: public InProcessBrowserTest
{
44 #if !defined(GOOGLE_CHROME_BUILD)
45 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
46 command_line
->AppendSwitch(switches::kEnablePrintPreview
);
51 content::TestNavigationObserver
nav_observer(NULL
);
52 nav_observer
.StartWatchingNewWebContents();
53 chrome::ExecuteCommand(browser(), IDC_PRINT
);
55 nav_observer
.StopWatchingNewWebContents();
59 IN_PROC_BROWSER_TEST_F(PrintPreviewTest
, PrintCommands
) {
60 // We start off at about:blank page.
61 // Make sure there is 1 tab and print is enabled.
62 ASSERT_EQ(1, browser()->tab_strip_model()->count());
64 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT
));
66 // Make sure advanced print command (Ctrl+Shift+p) is enabled.
67 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT
));
69 // Create the print preview dialog.
72 // Make sure print is disabled.
73 ASSERT_FALSE(chrome::IsCommandEnabled(browser(), IDC_PRINT
));
75 // Make sure advanced print command (Ctrl+Shift+p) is enabled.
76 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT
));
78 content::TestNavigationObserver
reload_observer(
79 browser()->tab_strip_model()->GetActiveWebContents());
80 chrome::Reload(browser(), CURRENT_TAB
);
81 reload_observer
.Wait();
83 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT
));
85 // Make sure advanced print command (Ctrl+Shift+p) is enabled.
86 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT
));
89 // Disable the test for mac, see http://crbug/367665.
90 #if defined(OS_MACOSX) && !defined(OS_IOS)
91 #define MAYBE_TaskManagerNewPrintPreview DISABLED_TaskManagerNewPrintPreview
93 #define MAYBE_TaskManagerNewPrintPreview TaskManagerNewPrintPreview
95 IN_PROC_BROWSER_TEST_F(PrintPreviewTest
, MAYBE_TaskManagerNewPrintPreview
) {
96 chrome::ShowTaskManager(browser()); // Show task manager BEFORE print dialog.
98 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
99 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
100 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyPrint()));
102 // Create the print preview dialog.
105 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
106 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
107 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyPrint()));
108 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchPrint("about:blank")));
111 // Disable the test for mac as it started being flaky, see http://crbug/367665.
112 #if defined(OS_MACOSX) && !defined(OS_IOS)
113 #define MAYBE_TaskManagerExistingPrintPreview DISABLED_TaskManagerExistingPrintPreview
115 #define MAYBE_TaskManagerExistingPrintPreview TaskManagerExistingPrintPreview
117 IN_PROC_BROWSER_TEST_F(PrintPreviewTest
,
118 MAYBE_TaskManagerExistingPrintPreview
) {
119 // Create the print preview dialog.
122 chrome::ShowTaskManager(browser()); // Show task manager AFTER print dialog.
124 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
125 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
126 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyPrint()));
127 ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchPrint("about:blank")));
132 BOOL CALLBACK
EnumerateChildren(HWND hwnd
, LPARAM l_param
) {
133 HWND
* child
= reinterpret_cast<HWND
*>(l_param
);
135 // The first child window is the plugin, then its children. So stop
136 // enumerating after the first callback.
140 // This test verifies that constrained windows aren't covered by windowed NPAPI
141 // plugins. The code which fixes this is in WebContentsViewAura::WindowObserver.
142 IN_PROC_BROWSER_TEST_F(PrintPreviewTest
, WindowedNPAPIPluginHidden
) {
143 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize
,
146 // First load the page and wait for the NPAPI plugin's window to display.
147 base::string16
expected_title(base::ASCIIToUTF16("ready"));
148 content::WebContents
* tab
=
149 browser()->tab_strip_model()->GetActiveWebContents();
150 content::TitleWatcher
title_watcher(tab
, expected_title
);
152 GURL url
= ui_test_utils::GetTestUrl(
153 base::FilePath().AppendASCII("printing"),
154 base::FilePath().AppendASCII("npapi_plugin.html"));
155 ui_test_utils::NavigateToURL(browser(), url
);
157 EXPECT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
159 // Now get the region of the plugin before and after the print preview is
160 // shown. They should be different.
161 HWND hwnd
= tab
->GetNativeView()->GetHost()->GetAcceleratedWidget();
163 EnumChildWindows(hwnd
, EnumerateChildren
,reinterpret_cast<LPARAM
>(&child
));
165 RECT region_before
, region_after
;
166 int result
= GetWindowRgnBox(child
, ®ion_before
);
167 ASSERT_EQ(result
, SIMPLEREGION
);
169 // Now print preview.
172 result
= GetWindowRgnBox(child
, ®ion_after
);
173 if (result
== NULLREGION
) {
174 // Depending on the browser window size, the plugin could be full covered.
178 if (result
== COMPLEXREGION
) {
179 // Complex region, by definition not equal to the initial region.
183 ASSERT_EQ(result
, SIMPLEREGION
);
185 region_before
.left
== region_after
.left
&&
186 region_before
.top
== region_after
.top
&&
187 region_before
.right
== region_after
.right
&&
188 region_before
.bottom
== region_after
.bottom
;
189 ASSERT_FALSE(rects_equal
);
192 IN_PROC_BROWSER_TEST_F(PrintPreviewTest
, NoCrashOnCloseWithOtherTabs
) {
193 // Now print preview.
196 ui_test_utils::NavigateToURLWithDisposition(
197 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB
,
198 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
200 browser()->tab_strip_model()->ActivateTabAt(0, true);
202 // Navigate main tab to hide print preview.
203 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
205 browser()->tab_strip_model()->ActivateTabAt(1, true);