Home | History | Annotate | Download | only in print_preview
      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/prefs/pref_service.h"
      6 #include "base/strings/utf_string_conversions.h"
      7 #include "chrome/app/chrome_command_ids.h"
      8 #include "chrome/browser/content_settings/host_content_settings_map.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
     11 #include "chrome/browser/ui/browser.h"
     12 #include "chrome/browser/ui/browser_commands.h"
     13 #include "chrome/browser/ui/browser_dialogs.h"
     14 #include "chrome/browser/ui/tabs/tab_strip_model.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"
     24 #include "url/url_constants.h"
     25 
     26 #if defined(OS_WIN)
     27 #include "ui/aura/window.h"
     28 #include "ui/aura/window_tree_host.h"
     29 #endif
     30 
     31 using task_manager::browsertest_util::MatchAboutBlankTab;
     32 using task_manager::browsertest_util::MatchAnyPrint;
     33 using task_manager::browsertest_util::MatchAnyTab;
     34 using task_manager::browsertest_util::MatchPrint;
     35 using task_manager::browsertest_util::WaitForTaskManagerRows;
     36 
     37 namespace {
     38 
     39 class PrintPreviewTest : public InProcessBrowserTest {
     40  public:
     41   PrintPreviewTest() {}
     42 
     43   void Print() {
     44     content::TestNavigationObserver nav_observer(NULL);
     45     nav_observer.StartWatchingNewWebContents();
     46     chrome::ExecuteCommand(browser(), IDC_PRINT);
     47     nav_observer.Wait();
     48     nav_observer.StopWatchingNewWebContents();
     49   }
     50 };
     51 
     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());
     56 
     57   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
     58 
     59   // Make sure advanced print command (Ctrl+Shift+p) is enabled.
     60   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
     61 
     62   // Create the print preview dialog.
     63   Print();
     64 
     65   // Make sure print is disabled.
     66   ASSERT_FALSE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
     67 
     68   // Make sure advanced print command (Ctrl+Shift+p) is enabled.
     69   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
     70 
     71   content::TestNavigationObserver reload_observer(
     72       browser()->tab_strip_model()->GetActiveWebContents());
     73   chrome::Reload(browser(), CURRENT_TAB);
     74   reload_observer.Wait();
     75 
     76   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_PRINT));
     77 
     78   // Make sure advanced print command (Ctrl+Shift+p) is enabled.
     79   ASSERT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ADVANCED_PRINT));
     80 }
     81 
     82 // Disable the test for mac, see http://crbug/367665.
     83 #if defined(OS_MACOSX) && !defined(OS_IOS)
     84 #define MAYBE_TaskManagerNewPrintPreview DISABLED_TaskManagerNewPrintPreview
     85 #else
     86 #define MAYBE_TaskManagerNewPrintPreview TaskManagerNewPrintPreview
     87 #endif
     88 IN_PROC_BROWSER_TEST_F(PrintPreviewTest, MAYBE_TaskManagerNewPrintPreview) {
     89   chrome::ShowTaskManager(browser());  // Show task manager BEFORE print dialog.
     90 
     91   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
     92   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
     93   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(0, MatchAnyPrint()));
     94 
     95   // Create the print preview dialog.
     96   Print();
     97 
     98   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
     99   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
    100   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyPrint()));
    101   ASSERT_NO_FATAL_FAILURE(
    102       WaitForTaskManagerRows(1, MatchPrint(url::kAboutBlankURL)));
    103 }
    104 
    105 // Disable the test for mac as it started being flaky, see http://crbug/367665.
    106 #if defined(OS_MACOSX) && !defined(OS_IOS)
    107 #define MAYBE_TaskManagerExistingPrintPreview DISABLED_TaskManagerExistingPrintPreview
    108 #else
    109 #define MAYBE_TaskManagerExistingPrintPreview TaskManagerExistingPrintPreview
    110 #endif
    111 IN_PROC_BROWSER_TEST_F(PrintPreviewTest,
    112                        MAYBE_TaskManagerExistingPrintPreview) {
    113   // Create the print preview dialog.
    114   Print();
    115 
    116   chrome::ShowTaskManager(browser());  // Show task manager AFTER print dialog.
    117 
    118   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAboutBlankTab()));
    119   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyTab()));
    120   ASSERT_NO_FATAL_FAILURE(WaitForTaskManagerRows(1, MatchAnyPrint()));
    121   ASSERT_NO_FATAL_FAILURE(
    122       WaitForTaskManagerRows(1, MatchPrint(url::kAboutBlankURL)));
    123 }
    124 
    125 #if defined(OS_WIN)
    126 BOOL CALLBACK EnumerateChildren(HWND hwnd, LPARAM l_param) {
    127   HWND* child = reinterpret_cast<HWND*>(l_param);
    128   *child = hwnd;
    129   // The first child window is the plugin, then its children. So stop
    130   // enumerating after the first callback.
    131   return FALSE;
    132 }
    133 
    134 // This test verifies that constrained windows aren't covered by windowed NPAPI
    135 // plugins. The code which fixes this is in WebContentsViewAura::WindowObserver.
    136 IN_PROC_BROWSER_TEST_F(PrintPreviewTest, WindowedNPAPIPluginHidden) {
    137   browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize,
    138                                                true);
    139 
    140   // First load the page and wait for the NPAPI plugin's window to display.
    141   base::string16 expected_title(base::ASCIIToUTF16("ready"));
    142   content::WebContents* tab =
    143       browser()->tab_strip_model()->GetActiveWebContents();
    144   content::TitleWatcher title_watcher(tab, expected_title);
    145 
    146   GURL url = ui_test_utils::GetTestUrl(
    147       base::FilePath().AppendASCII("printing"),
    148       base::FilePath().AppendASCII("npapi_plugin.html"));
    149   ui_test_utils::NavigateToURL(browser(), url);
    150 
    151   EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
    152 
    153   // Now get the region of the plugin before and after the print preview is
    154   // shown. They should be different.
    155   HWND hwnd = tab->GetNativeView()->GetHost()->GetAcceleratedWidget();
    156   HWND child = NULL;
    157   EnumChildWindows(hwnd, EnumerateChildren,reinterpret_cast<LPARAM>(&child));
    158 
    159   RECT region_before, region_after;
    160   int result = GetWindowRgnBox(child, &region_before);
    161   ASSERT_EQ(result, SIMPLEREGION);
    162 
    163   // Now print preview.
    164   Print();
    165 
    166   result = GetWindowRgnBox(child, &region_after);
    167   if (result == NULLREGION) {
    168     // Depending on the browser window size, the plugin could be full covered.
    169     return;
    170   }
    171 
    172   if (result == COMPLEXREGION) {
    173     // Complex region, by definition not equal to the initial region.
    174     return;
    175   }
    176 
    177   ASSERT_EQ(result, SIMPLEREGION);
    178   bool rects_equal =
    179       region_before.left == region_after.left &&
    180       region_before.top == region_after.top &&
    181       region_before.right == region_after.right &&
    182       region_before.bottom == region_after.bottom;
    183   ASSERT_FALSE(rects_equal);
    184 }
    185 
    186 IN_PROC_BROWSER_TEST_F(PrintPreviewTest, NoCrashOnCloseWithOtherTabs) {
    187   // Now print preview.
    188   Print();
    189 
    190   ui_test_utils::NavigateToURLWithDisposition(
    191       browser(), GURL("about:blank"), NEW_FOREGROUND_TAB,
    192       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
    193 
    194   browser()->tab_strip_model()->ActivateTabAt(0, true);
    195 
    196   // Navigate main tab to hide print preview.
    197   ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
    198 
    199   browser()->tab_strip_model()->ActivateTabAt(1, true);
    200 }
    201 #endif  // defined(OS_WIN)
    202 
    203 }  // namespace
    204