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 "chrome/browser/printing/print_preview_test.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_iterator.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/test_browser_window.h"
13 #include "components/web_modal/web_contents_modal_dialog_host.h"
14 #include "content/public/browser/plugin_service.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_view.h"
18 using web_modal::WebContentsModalDialogHost
;
19 using web_modal::ModalDialogHostObserver
;
23 // Use a custom test browser window to provide a parent view to the print
25 class PrintPreviewTestBrowserWindow
26 : public TestBrowserWindow
,
27 public WebContentsModalDialogHost
{
29 PrintPreviewTestBrowserWindow() {}
31 // BrowserWindow overrides
32 virtual WebContentsModalDialogHost
* GetWebContentsModalDialogHost() OVERRIDE
{
36 // WebContentsModalDialogHost overrides
38 // The web contents modal dialog must be parented to *something*; use the
39 // WebContents window since there is no true browser window for unit tests.
40 virtual gfx::NativeView
GetHostView() const OVERRIDE
{
41 return FindBrowser()->tab_strip_model()->GetActiveWebContents()->GetView()->
45 virtual gfx::Point
GetDialogPosition(const gfx::Size
& size
) OVERRIDE
{
49 virtual gfx::Size
GetMaximumDialogSize() OVERRIDE
{
53 virtual void AddObserver(
54 ModalDialogHostObserver
* observer
) OVERRIDE
{}
55 virtual void RemoveObserver(
56 ModalDialogHostObserver
* observer
) OVERRIDE
{}
59 Browser
* FindBrowser() const {
60 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
61 Browser
* browser
= *it
;
62 if (browser
->window() == this)
69 DISALLOW_COPY_AND_ASSIGN(PrintPreviewTestBrowserWindow
);
73 PrintPreviewTest::PrintPreviewTest() {}
74 PrintPreviewTest::~PrintPreviewTest() {}
76 void PrintPreviewTest::SetUp() {
77 BrowserWithTestWindowTest::SetUp();
79 // The PluginService will be destroyed at the end of the test (due to the
80 // ShadowingAtExitManager in our base class).
81 content::PluginService::GetInstance()->Init();
82 content::PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
84 profile()->GetPrefs()->SetBoolean(prefs::kPrintPreviewDisabled
, false);
87 BrowserWindow
* PrintPreviewTest::CreateBrowserWindow() {
88 return new PrintPreviewTestBrowserWindow
;