Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / print_preview / print_preview_ui_browsertest.cc
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.
4
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"
26
27 #if defined(OS_WIN)
28 #include "ui/aura/window.h"
29 #include "ui/aura/window_tree_host.h"
30 #endif
31
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;
37
38 namespace {
39
40 class PrintPreviewTest : public InProcessBrowserTest {
41  public:
42   PrintPreviewTest() {}
43
44 #if !defined(GOOGLE_CHROME_BUILD)
45   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
46     command_line->AppendSwitch(switches::kEnablePrintPreview);
47   }
48 #endif
49
50   void Print() {
51     content::TestNavigationObserver nav_observer(NULL);
52     nav_observer.StartWatchingNewWebContents();
53     chrome::ExecuteCommand(browser(), IDC_PRINT);
54     nav_observer.Wait();
55     nav_observer.StopWatchingNewWebContents();
56   }
57 };
58
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());
63
64   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
65
66   // Make sure advanced print command (Ctrl+Shift+p) is enabled.
67   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
68
69   // Create the print preview dialog.
70   Print();
71
72   // Make sure print is disabled.
73   ASSERT_FALSE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
74
75   // Make sure advanced print command (Ctrl+Shift+p) is enabled.
76   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
77
78   content::TestNavigationObserver reload_observer(
79       browser()->tab_strip_model()->GetActiveWebContents());
80   chrome::Reload(browser(), CURRENT_TAB);
81   reload_observer.Wait();
82
83   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
84
85   // Make sure advanced print command (Ctrl+Shift+p) is enabled.
86   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
87 }
88
89 // Disable the test for mac, see http://crbug/367665.
90 #if defined(OS_MACOSX) && !defined(OS_IOS)
91 #define MAYBE_TaskManagerNewPrintPreview DISABLED_TaskManagerNewPrintPreview
92 #else
93 #define MAYBE_TaskManagerNewPrintPreview TaskManagerNewPrintPreview
94 #endif
95 IN_PROC_BROWSER_TEST_F(PrintPreviewTest, MAYBE_TaskManagerNewPrintPreview) {
96   chrome::ShowTaskManager(browser());  // Show task manager BEFORE print dialog.
97
98   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
99   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
100   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyPrint()));
101
102   // Create the print preview dialog.
103   Print();
104
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")));
109 }
110
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
114 #else
115 #define MAYBE_TaskManagerExistingPrintPreview TaskManagerExistingPrintPreview
116 #endif
117 IN_PROC_BROWSER_TEST_F(PrintPreviewTest,
118                        MAYBE_TaskManagerExistingPrintPreview) {
119   // Create the print preview dialog.
120   Print();
121
122   chrome::ShowTaskManager(browser());  // Show task manager AFTER print dialog.
123
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")));
128 }
129
130 #if defined(OS_WIN)
131
132 BOOL CALLBACK EnumerateChildren(HWND hwnd, LPARAM l_param) {
133   HWND* child = reinterpret_cast<HWND*>(l_param);
134   *child = hwnd;
135   // The first child window is the plugin, then its children. So stop
136   // enumerating after the first callback.
137   return FALSE;
138 }
139
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,
144                                                true);
145
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);
151
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);
156
157   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
158
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();
162   HWND child = NULL;
163   EnumChildWindows(hwnd, EnumerateChildren,reinterpret_cast<LPARAM>(&child));
164
165   RECT region_before, region_after;
166   int result = GetWindowRgnBox(child, &region_before);
167   ASSERT_EQ(result, SIMPLEREGION);
168
169   // Now print preview.
170   Print();
171
172   result = GetWindowRgnBox(child, &region_after);
173   if (result == NULLREGION) {
174     // Depending on the browser window size, the plugin could be full covered.
175     return;
176   }
177
178   if (result == COMPLEXREGION) {
179     // Complex region, by definition not equal to the initial region.
180     return;
181   }
182
183   ASSERT_EQ(result, SIMPLEREGION);
184   bool rects_equal =
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);
190 }
191
192 IN_PROC_BROWSER_TEST_F(PrintPreviewTest, NoCrashOnCloseWithOtherTabs) {
193   // Now print preview.
194   Print();
195
196   ui_test_utils::NavigateToURLWithDisposition(
197       browser(), GURL("about:blank"), NEW_FOREGROUND_TAB,
198       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
199
200   browser()->tab_strip_model()->ActivateTabAt(0, true);
201
202   // Navigate main tab to hide print preview.
203   ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
204
205   browser()->tab_strip_model()->ActivateTabAt(1, true);
206 }
207 #endif
208
209 }  // namespace