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_observer.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "content/public/test/test_navigation_observer.h"
22 #include "content/public/test/test_utils.h"
24 #if defined(OS_WIN) && defined(USE_AURA)
25 #include "content/public/browser/web_contents_view.h"
26 #include "ui/aura/root_window.h"
31 class PrintPreviewTest : public InProcessBrowserTest {
35 #if !defined(GOOGLE_CHROME_BUILD)
36 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
37 command_line->AppendSwitch(switches::kEnablePrintPreview);
42 content::TestNavigationObserver nav_observer(NULL);
43 nav_observer.StartWatchingNewWebContents();
44 chrome::ExecuteCommand(browser(), IDC_PRINT);
46 nav_observer.StopWatchingNewWebContents();
50 IN_PROC_BROWSER_TEST_F(PrintPreviewTest, PrintCommands) {
51 // We start off at about:blank page.
52 // Make sure there is 1 tab and print is enabled.
53 ASSERT_EQ(1, browser()->tab_strip_model()->count());
55 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
57 // Make sure advanced print command (Ctrl+Shift+p) is enabled.
58 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
60 // Create the print preview dialog.
63 // Make sure print is disabled.
64 ASSERT_FALSE(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 content::TestNavigationObserver reload_observer(
70 browser()->tab_strip_model()->GetActiveWebContents());
71 chrome::Reload(browser(), CURRENT_TAB);
72 reload_observer.Wait();
74 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
76 // Make sure advanced print command (Ctrl+Shift+p) is enabled.
77 ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
80 #if defined(OS_WIN) && defined(USE_AURA)
82 BOOL CALLBACK EnumerateChildren(HWND hwnd, LPARAM l_param) {
83 HWND* child = reinterpret_cast<HWND*>(l_param);
85 // The first child window is the plugin, then its children. So stop
86 // enumerating after the first callback.
90 // This test verifies that constrained windows aren't covered by windowed NPAPI
91 // plugins. The code which fixes this is in WebContentsViewAura::WindowObserver.
92 IN_PROC_BROWSER_TEST_F(PrintPreviewTest, WindowedNPAPIPluginHidden) {
93 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize,
96 // First load the page and wait for the NPAPI plugin's window to display.
97 string16 expected_title(ASCIIToUTF16("ready"));
98 content::WebContents* tab =
99 browser()->tab_strip_model()->GetActiveWebContents();
100 content::TitleWatcher title_watcher(tab, expected_title);
102 GURL url = ui_test_utils::GetTestUrl(
103 base::FilePath().AppendASCII("printing"),
104 base::FilePath().AppendASCII("npapi_plugin.html"));
105 ui_test_utils::NavigateToURL(browser(), url);
107 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
109 // Now get the region of the plugin before and after the print preview is
110 // shown. They should be different.
112 tab->GetView()->GetNativeView()->GetDispatcher()->GetAcceleratedWidget();
114 EnumChildWindows(hwnd, EnumerateChildren,reinterpret_cast<LPARAM>(&child));
116 RECT region_before, region_after;
117 int result = GetWindowRgnBox(child, ®ion_before);
118 ASSERT_EQ(result, SIMPLEREGION);
120 // Now print preview.
123 result = GetWindowRgnBox(child, ®ion_after);
124 if (result == NULLREGION) {
125 // Depending on the browser window size, the plugin could be full covered.
129 if (result == COMPLEXREGION) {
130 // Complex region, by definition not equal to the initial region.
134 ASSERT_EQ(result, SIMPLEREGION);
136 region_before.left == region_after.left &&
137 region_before.top == region_after.top &&
138 region_before.right == region_after.right &&
139 region_before.bottom == region_after.bottom;
140 ASSERT_FALSE(rects_equal);