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/ui/browser.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "content/public/test/test_navigation_observer.h"
23 #include "content/public/test/test_utils.h"
25 #if defined(OS_WIN) && defined(USE_AURA)
26 #include "content/public/browser/web_contents_view.h"
27 #include "ui/aura/root_window.h"
28 #include "ui/aura/window.h"
33 class PrintPreviewTest
: public InProcessBrowserTest
{
37 #if !defined(GOOGLE_CHROME_BUILD)
38 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
39 command_line
->AppendSwitch(switches::kEnablePrintPreview
);
44 content::TestNavigationObserver
nav_observer(NULL
);
45 nav_observer
.StartWatchingNewWebContents();
46 chrome::ExecuteCommand(browser(), IDC_PRINT
);
48 nav_observer
.StopWatchingNewWebContents();
52 IN_PROC_BROWSER_TEST_F(PrintPreviewTest
, PrintCommands
) {
53 // We start off at about:blank page.
54 // Make sure there is 1 tab and print is enabled.
55 ASSERT_EQ(1, browser()->tab_strip_model()->count());
57 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT
));
59 // Make sure advanced print command (Ctrl+Shift+p) is enabled.
60 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT
));
62 // Create the print preview dialog.
65 // Make sure print is disabled.
66 ASSERT_FALSE(chrome::IsCommandEnabled(browser(), IDC_PRINT
));
68 // Make sure advanced print command (Ctrl+Shift+p) is enabled.
69 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT
));
71 content::TestNavigationObserver
reload_observer(
72 browser()->tab_strip_model()->GetActiveWebContents());
73 chrome::Reload(browser(), CURRENT_TAB
);
74 reload_observer
.Wait();
76 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT
));
78 // Make sure advanced print command (Ctrl+Shift+p) is enabled.
79 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT
));
82 #if defined(OS_WIN) && defined(USE_AURA)
84 BOOL CALLBACK
EnumerateChildren(HWND hwnd
, LPARAM l_param
) {
85 HWND
* child
= reinterpret_cast<HWND
*>(l_param
);
87 // The first child window is the plugin, then its children. So stop
88 // enumerating after the first callback.
92 // This test verifies that constrained windows aren't covered by windowed NPAPI
93 // plugins. The code which fixes this is in WebContentsViewAura::WindowObserver.
94 IN_PROC_BROWSER_TEST_F(PrintPreviewTest
, WindowedNPAPIPluginHidden
) {
95 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize
,
98 // First load the page and wait for the NPAPI plugin's window to display.
99 base::string16
expected_title(base::ASCIIToUTF16("ready"));
100 content::WebContents
* tab
=
101 browser()->tab_strip_model()->GetActiveWebContents();
102 content::TitleWatcher
title_watcher(tab
, expected_title
);
104 GURL url
= ui_test_utils::GetTestUrl(
105 base::FilePath().AppendASCII("printing"),
106 base::FilePath().AppendASCII("npapi_plugin.html"));
107 ui_test_utils::NavigateToURL(browser(), url
);
109 EXPECT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
111 // Now get the region of the plugin before and after the print preview is
112 // shown. They should be different.
113 HWND hwnd
= tab
->GetView()->GetNativeView()->GetDispatcher()->host()->
114 GetAcceleratedWidget();
116 EnumChildWindows(hwnd
, EnumerateChildren
,reinterpret_cast<LPARAM
>(&child
));
118 RECT region_before
, region_after
;
119 int result
= GetWindowRgnBox(child
, ®ion_before
);
120 ASSERT_EQ(result
, SIMPLEREGION
);
122 // Now print preview.
125 result
= GetWindowRgnBox(child
, ®ion_after
);
126 if (result
== NULLREGION
) {
127 // Depending on the browser window size, the plugin could be full covered.
131 if (result
== COMPLEXREGION
) {
132 // Complex region, by definition not equal to the initial region.
136 ASSERT_EQ(result
, SIMPLEREGION
);
138 region_before
.left
== region_after
.left
&&
139 region_before
.top
== region_after
.top
&&
140 region_before
.right
== region_after
.right
&&
141 region_before
.bottom
== region_after
.bottom
;
142 ASSERT_FALSE(rects_equal
);
145 IN_PROC_BROWSER_TEST_F(PrintPreviewTest
, NoCrashOnCloseWithOtherTabs
) {
146 // Now print preview.
149 ui_test_utils::NavigateToURLWithDisposition(
150 browser(), GURL("about:blank"), NEW_FOREGROUND_TAB
,
151 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
153 browser()->tab_strip_model()->ActivateTabAt(0, true);
155 // Navigate main tab to hide print preview.
156 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
158 browser()->tab_strip_model()->ActivateTabAt(1, true);