- add sources.
[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/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"
23
24 #if defined(OS_WIN) && defined(USE_AURA)
25 #include "content/public/browser/web_contents_view.h"
26 #include "ui/aura/root_window.h"
27 #endif
28
29 namespace {
30
31 class PrintPreviewTest : public InProcessBrowserTest {
32  public:
33   PrintPreviewTest() {}
34
35 #if !defined(GOOGLE_CHROME_BUILD)
36   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
37     command_line->AppendSwitch(switches::kEnablePrintPreview);
38   }
39 #endif
40
41   void Print() {
42     content::TestNavigationObserver nav_observer(NULL);
43     nav_observer.StartWatchingNewWebContents();
44     chrome::ExecuteCommand(browser(), IDC_PRINT);
45     nav_observer.Wait();
46     nav_observer.StopWatchingNewWebContents();
47   }
48 };
49
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());
54
55   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
56
57   // Make sure advanced print command (Ctrl+Shift+p) is enabled.
58   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
59
60   // Create the print preview dialog.
61   Print();
62
63   // Make sure print is disabled.
64   ASSERT_FALSE(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   content::TestNavigationObserver reload_observer(
70       browser()->tab_strip_model()->GetActiveWebContents());
71   chrome::Reload(browser(), CURRENT_TAB);
72   reload_observer.Wait();
73
74   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
75
76   // Make sure advanced print command (Ctrl+Shift+p) is enabled.
77   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
78 }
79
80 #if defined(OS_WIN) && defined(USE_AURA)
81
82 BOOL CALLBACK EnumerateChildren(HWND hwnd, LPARAM l_param) {
83   HWND* child = reinterpret_cast<HWND*>(l_param);
84   *child = hwnd;
85   // The first child window is the plugin, then its children. So stop
86   // enumerating after the first callback.
87   return FALSE;
88 }
89
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,
94                                                true);
95
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);
101
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);
106
107   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
108
109   // Now get the region of the plugin before and after the print preview is
110   // shown. They should be different.
111   HWND hwnd =
112       tab->GetView()->GetNativeView()->GetDispatcher()->GetAcceleratedWidget();
113   HWND child = NULL;
114   EnumChildWindows(hwnd, EnumerateChildren,reinterpret_cast<LPARAM>(&child));
115
116   RECT region_before, region_after;
117   int result = GetWindowRgnBox(child, &region_before);
118   ASSERT_EQ(result, SIMPLEREGION);
119
120   // Now print preview.
121   Print();
122
123   result = GetWindowRgnBox(child, &region_after);
124   if (result == NULLREGION) {
125     // Depending on the browser window size, the plugin could be full covered.
126     return;
127   }
128
129   if (result == COMPLEXREGION) {
130     // Complex region, by definition not equal to the initial region.
131     return;
132   }
133
134   ASSERT_EQ(result, SIMPLEREGION);
135   bool rects_equal =
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);
141 }
142 #endif
143
144 }  // namespace