From d35162497672a6c2d66429e94b7341bc73d3620c Mon Sep 17 00:00:00 2001 From: "rkc@chromium.org" Date: Wed, 2 May 2012 08:54:20 +0000 Subject: Re-implement the screensaver to use WebView instead of ExtensionDialogHost. Use WebView to render the screensaver extension instead of ExtensionDialogHost. Using the RenderViewGone override to detect termination of the renderer process to restart it. Added browser tests. R=ben@chromium.org,sky@chromium.org BUG=chromium-os:28211 TEST=Tested that the screensaver comes up; tested the reload via crashing the extension renderer with SIG_ABRT; also ran browser tests. Review URL: https://chromiumcodereview.appspot.com/10191010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@134878 0039d316-1c4b-4281-b951-d872f2087c98 --- ash/ash.gyp | 9 ++ ash/screensaver/DEPS | 3 + ash/screensaver/screensaver_view.cc | 140 +++++++++++++++++++++ ash/screensaver/screensaver_view.h | 86 +++++++++++++ ash/screensaver/screensaver_view_unittest.cc | 93 ++++++++++++++ ash/shell/shell_delegate_impl.cc | 4 + ash/shell/shell_delegate_impl.h | 1 + ash/shell/window_type_launcher.cc | 17 ++- ash/shell/window_type_launcher.h | 1 + ash/shell_delegate.h | 3 + ash/test/DEPS | 3 + ash/test/test_shell_delegate.cc | 5 + ash/test/test_shell_delegate.h | 1 + .../chromeos/kiosk_mode/kiosk_mode_screensaver.cc | 13 +- .../chromeos/ui/screensaver_extension_dialog.cc | 109 ---------------- .../chromeos/ui/screensaver_extension_dialog.h | 58 --------- .../accessibility_event_router_views_unittest.cc | 6 + .../browser/ui/views/ash/chrome_shell_delegate.cc | 4 + .../browser/ui/views/ash/chrome_shell_delegate.h | 1 + .../ui/views/bookmarks/bookmark_bar_view_test.cc | 6 + chrome/browser/ui/views/chrome_views_delegate.cc | 6 + chrome/browser/ui/views/chrome_views_delegate.h | 4 + chrome/browser/ui/views/menu_model_adapter_test.cc | 6 + chrome/chrome_browser.gypi | 2 - content/content_tests.gypi | 4 + content/test/content_test_suite.cc | 35 ++---- content/test/test_content_client_initializer.cc | 35 ++++++ content/test/test_content_client_initializer.h | 37 ++++++ content/test/test_render_view_host_factory.cc | 39 ++++++ content/test/test_render_view_host_factory.h | 53 ++++++++ content/test/test_renderer_host.cc | 61 +-------- ui/views/controls/webview/webview.cc | 27 +++- ui/views/controls/webview/webview.h | 9 +- ui/views/test/DEPS | 3 + ui/views/test/test_views_delegate.cc | 8 ++ ui/views/test/test_views_delegate.h | 4 + ui/views/test/webview_test_helper.cc | 34 +++++ ui/views/test/webview_test_helper.h | 41 ++++++ ui/views/views.gyp | 33 ++++- ui/views/views_delegate.h | 11 ++ 40 files changed, 745 insertions(+), 270 deletions(-) create mode 100644 ash/screensaver/DEPS create mode 100644 ash/screensaver/screensaver_view.cc create mode 100644 ash/screensaver/screensaver_view.h create mode 100644 ash/screensaver/screensaver_view_unittest.cc create mode 100644 ash/test/DEPS delete mode 100644 chrome/browser/chromeos/ui/screensaver_extension_dialog.cc delete mode 100644 chrome/browser/chromeos/ui/screensaver_extension_dialog.h create mode 100644 content/test/test_content_client_initializer.cc create mode 100644 content/test/test_content_client_initializer.h create mode 100644 content/test/test_render_view_host_factory.cc create mode 100644 content/test/test_render_view_host_factory.h create mode 100644 ui/views/test/DEPS create mode 100644 ui/views/test/webview_test_helper.cc create mode 100644 ui/views/test/webview_test_helper.h diff --git a/ash/ash.gyp b/ash/ash.gyp index 47e6b8a..67090bd 100644 --- a/ash/ash.gyp +++ b/ash/ash.gyp @@ -18,6 +18,8 @@ '../base/base.gyp:base_i18n', '../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', '../build/temp_gyp/googleurl.gyp:googleurl', + '../content/content.gyp:content', + '../content/content.gyp:content_browser', '../net/net.gyp:net', '../skia/skia.gyp:skia', '../third_party/icu/icu.gyp:icui18n', @@ -111,6 +113,8 @@ 'monitor/secondary_monitor_view.h', 'screen_ash.cc', 'screen_ash.h', + 'screensaver/screensaver_view.cc', + 'screensaver/screensaver_view.h', 'screenshot_delegate.h', 'shell.cc', 'shell.h', @@ -323,6 +327,8 @@ '../base/base.gyp:base', '../base/base.gyp:test_support_base', '../chrome/chrome_resources.gyp:packed_resources', + '../content/content.gyp:content_browser', + '../content/content.gyp:test_support_content', '../build/temp_gyp/googleurl.gyp:googleurl', '../skia/skia.gyp:skia', '../testing/gtest.gyp:gtest', @@ -337,6 +343,7 @@ '../ui/ui.gyp:ui_resources', '../ui/ui.gyp:ui_resources_standard', '../ui/views/views.gyp:views', + '../ui/views/views.gyp:test_support_views', 'ash', ], 'sources': [ @@ -356,6 +363,7 @@ 'launcher/launcher_unittest.cc', 'launcher/launcher_view_unittest.cc', 'monitor/multi_monitor_manager_unittest.cc', + 'screensaver/screensaver_view_unittest.cc', 'shell_unittest.cc', 'test/ash_test_base.cc', 'test/ash_test_base.h', @@ -457,6 +465,7 @@ '../ui/ui.gyp:ui_resources_standard', '../ui/views/views.gyp:views', '../ui/views/views.gyp:views_examples_lib', + '../ui/views/views.gyp:test_support_views', 'ash', ], 'sources': [ diff --git a/ash/screensaver/DEPS b/ash/screensaver/DEPS new file mode 100644 index 0000000..1c35d9c --- /dev/null +++ b/ash/screensaver/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+content/public/browser", +] diff --git a/ash/screensaver/screensaver_view.cc b/ash/screensaver/screensaver_view.cc new file mode 100644 index 0000000..858ae9c --- /dev/null +++ b/ash/screensaver/screensaver_view.cc @@ -0,0 +1,140 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ash/screensaver/screensaver_view.h" + +#include "ash/shell.h" +#include "ash/shell_delegate.h" +#include "base/bind.h" +#include "base/logging.h" +#include "content/public/browser/browser_context.h" +#include "content/public/browser/browser_thread.h" +#include "ui/gfx/screen.h" +#include "ui/aura/root_window.h" +#include "ui/views/layout/fill_layout.h" +#include "ui/views/controls/webview/webview.h" +#include "ui/views/widget/widget.h" + +using content::BrowserThread; + +namespace { + +ash::internal::ScreensaverView* g_instance = NULL; + +} // namespace + +namespace ash { + +void ShowScreensaver(const GURL& url) { + internal::ScreensaverView::ShowScreensaver(url); +} + +void CloseScreensaver() { + internal::ScreensaverView::CloseScreensaver(); +} + +namespace internal { + +// static +void ScreensaverView::ShowScreensaver(const GURL& url) { + if (!g_instance) { + g_instance = new ScreensaverView(url); + g_instance->Show(); + } +} + +// static +void ScreensaverView::CloseScreensaver() { + if (g_instance) { + g_instance->Close(); + g_instance = NULL; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// ScreensaverView, views::WidgetDelegateView implementation. +views::View* ScreensaverView::GetContentsView() { + return this; +} + +//////////////////////////////////////////////////////////////////////////////// +// ScreensaverView, content::WebContentsObserver implementation. +void ScreensaverView::RenderViewGone( + base::TerminationStatus status) { + LOG(ERROR) << "Screensaver terminated with status " << status + << ", reloading."; + // Reload the screensaver url into the webcontents. + LoadScreensaver(); +} + +//////////////////////////////////////////////////////////////////////////////// +// ScreensaverView private methods. +ScreensaverView::ScreensaverView(const GURL& url) + : url_(url), + screensaver_webview_(NULL), + container_window_(NULL) { +} + +ScreensaverView::~ScreensaverView() { +} + +void ScreensaverView::Show() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + // Add the WebView to our view. + AddChildWebContents(); + // Show the window. + ShowWindow(); +} + +void ScreensaverView::Close() { + DCHECK(GetWidget()); + GetWidget()->Close(); +} + +void ScreensaverView::AddChildWebContents() { + content::BrowserContext* context = + Shell::GetInstance()->delegate()->GetCurrentBrowserContext(); + screensaver_webview_ = new views::WebView(context); + SetLayoutManager(new views::FillLayout); + AddChildView(screensaver_webview_); + + LoadScreensaver(); + content::WebContentsObserver::Observe( + screensaver_webview_->GetWebContents()); +} + +void ScreensaverView::LoadScreensaver() { + screensaver_webview_->GetWebContents()->GetController().LoadURL( + url_, + content::Referrer(), + content::PAGE_TRANSITION_START_PAGE, + std::string()); +} + +void ScreensaverView::ShowWindow() { + aura::RootWindow* root_window = ash::Shell::GetRootWindow(); + gfx::Rect screen_rect = + gfx::Screen::GetMonitorNearestWindow(root_window).bounds(); + + // We want to be the fullscreen topmost child of the root window. + // There should be nothing ever really that should show up on top of us. + container_window_ = new views::Widget(); + views::Widget::InitParams params( + views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); + params.delegate = this; + params.parent = root_window; + container_window_->Init(params); + + container_window_->StackAtTop(); + container_window_->SetBounds(screen_rect); + container_window_->Show(); +} + +// static +ScreensaverView* ScreensaverView::GetInstance() { + return g_instance; +} + +} // namespace internal +} // namespace ash diff --git a/ash/screensaver/screensaver_view.h b/ash/screensaver/screensaver_view.h new file mode 100644 index 0000000..0835f9b --- /dev/null +++ b/ash/screensaver/screensaver_view.h @@ -0,0 +1,86 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ASH_SCREENSAVER_SCREENSAVER_VIEW_H_ +#define ASH_SCREENSAVER_SCREENSAVER_VIEW_H_ +#pragma once + +#include "base/callback.h" +#include "content/public/browser/web_contents_observer.h" +#include "googleurl/src/gurl.h" +#include "ui/views/widget/widget_delegate.h" + +namespace content { +class BrowserContent; +} + +namespace views { +class WebView; +} + +namespace ash { + +namespace test { +class ScreensaverViewTest; +} + +void ShowScreensaver(const GURL& url); +void CloseScreensaver(); + +typedef + base::Callback WebViewFactory; + +namespace internal { + +// Shows a URL as a screensaver. The screensaver window is fullscreen, +// always on top of every other window and will reload the URL if the +// renderer crashes for any reason. +class ScreensaverView : public views::WidgetDelegateView, + public content::WebContentsObserver { + public: + static void ShowScreensaver(const GURL& url); + static void CloseScreensaver(); + + private: + friend class test::ScreensaverViewTest; + + explicit ScreensaverView(const GURL& url); + virtual ~ScreensaverView(); + + // views::WidgetDelegate overrides. + virtual views::View* GetContentsView() OVERRIDE; + + // content::WebContentsObserver overrides. + virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE; + + void Show(); + void Close(); + + // Creates and adds web contents to our view. + void AddChildWebContents(); + // Load the screensaver in the WebView's webcontent. If the webcontents + // don't exist, they'll be created by WebView. + void LoadScreensaver(); + // Creates and shows a frameless full screen window containing our view. + void ShowWindow(); + + // For testing purposes. + static ScreensaverView* GetInstance(); + + // URL to show in the screensaver. + GURL url_; + + // Host for the extension that implements this dialog. + views::WebView* screensaver_webview_; + + // Window that holds the screensaver webview. + views::Widget* container_window_; + + DISALLOW_COPY_AND_ASSIGN(ScreensaverView); +}; + +} // namespace internal +} // namespace ash + +#endif // ASH_SCREENSAVER_SCREENSAVER_VIEW_H_ diff --git a/ash/screensaver/screensaver_view_unittest.cc b/ash/screensaver/screensaver_view_unittest.cc new file mode 100644 index 0000000..47a19e2 --- /dev/null +++ b/ash/screensaver/screensaver_view_unittest.cc @@ -0,0 +1,93 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ash/screensaver/screensaver_view.h" + +#include "ash/test/ash_test_base.h" +#include "base/bind.h" +#include "content/public/browser/browser_context.h" +#include "ui/views/controls/webview/webview.h" +#include "ui/views/test/test_views_delegate.h" +#include "ui/views/test/webview_test_helper.h" + +namespace ash { +namespace test { + +class ScreensaverViewTest : public ash::test::AshTestBase { + public: + ScreensaverViewTest() { + url_ = GURL("http://www.google.com"); + views_delegate_.reset(new views::TestViewsDelegate); + webview_test_helper_.reset(new views::WebViewTestHelper(message_loop())); + } + + virtual ~ScreensaverViewTest() {} + + virtual void SetUp() OVERRIDE { + AshTestBase::SetUp(); + RunAllPendingInMessageLoop(); + } + + virtual void TearDown() OVERRIDE { + AshTestBase::TearDown(); + } + + void ExpectOpenScreensaver() { + internal::ScreensaverView* screensaver = + internal::ScreensaverView::GetInstance(); + EXPECT_TRUE(screensaver != NULL); + if (!screensaver) return; + + EXPECT_TRUE(screensaver->screensaver_webview_ != NULL); + if (!screensaver->screensaver_webview_) return; + + EXPECT_TRUE(screensaver->screensaver_webview_->web_contents() != NULL); + if (!screensaver->screensaver_webview_->web_contents()) return; + + EXPECT_EQ(screensaver->screensaver_webview_->web_contents()->GetURL(), + url_); + } + + void ExpectClosedScreensaver() { + EXPECT_TRUE(internal::ScreensaverView::GetInstance() == NULL); + } + + protected: + GURL url_; + + private: + scoped_ptr views_delegate_; + scoped_ptr webview_test_helper_; + + DISALLOW_COPY_AND_ASSIGN(ScreensaverViewTest); +}; + +TEST_F(ScreensaverViewTest, ShowScreensaverAndClose) { + ash::ShowScreensaver(url_); + RunAllPendingInMessageLoop(); + ExpectOpenScreensaver(); + + ash::CloseScreensaver(); + ExpectClosedScreensaver(); +} + +TEST_F(ScreensaverViewTest, OutOfOrderMultipleShowAndClose) { + ash::CloseScreensaver(); + ExpectClosedScreensaver(); + + ash::ShowScreensaver(url_); + ExpectOpenScreensaver(); + RunAllPendingInMessageLoop(); + ash::ShowScreensaver(url_); + ExpectOpenScreensaver(); + RunAllPendingInMessageLoop(); + + ash::CloseScreensaver(); + ExpectClosedScreensaver(); + ash::CloseScreensaver(); + ExpectClosedScreensaver(); +} + +} // namespace test +} // namespace ash diff --git a/ash/shell/shell_delegate_impl.cc b/ash/shell/shell_delegate_impl.cc index c2bfdc0..4805957 100644 --- a/ash/shell/shell_delegate_impl.cc +++ b/ash/shell/shell_delegate_impl.cc @@ -75,6 +75,10 @@ void ShellDelegateImpl::OpenCrosh() { void ShellDelegateImpl::OpenMobileSetup() { } +content::BrowserContext* ShellDelegateImpl::GetCurrentBrowserContext() { + return Shell::GetInstance()->browser_context(); +} + ash::AppListViewDelegate* ShellDelegateImpl::CreateAppListViewDelegate() { return ash::shell::CreateAppListViewDelegate(); } diff --git a/ash/shell/shell_delegate_impl.h b/ash/shell/shell_delegate_impl.h index b3905e0..243cfa4 100644 --- a/ash/shell/shell_delegate_impl.h +++ b/ash/shell/shell_delegate_impl.h @@ -33,6 +33,7 @@ class ShellDelegateImpl : public ash::ShellDelegate { virtual void OpenFileManager() OVERRIDE; virtual void OpenCrosh() OVERRIDE; virtual void OpenMobileSetup() OVERRIDE; + virtual content::BrowserContext* GetCurrentBrowserContext() OVERRIDE; virtual ash::AppListViewDelegate* CreateAppListViewDelegate() OVERRIDE; virtual void StartPartialScreenshot( ash::ScreenshotDelegate* screenshot_delegate) OVERRIDE; diff --git a/ash/shell/window_type_launcher.cc b/ash/shell/window_type_launcher.cc index f3abad2..020ec00 100644 --- a/ash/shell/window_type_launcher.cc +++ b/ash/shell/window_type_launcher.cc @@ -4,6 +4,7 @@ #include "ash/shell/window_type_launcher.h" +#include "ash/screensaver/screensaver_view.h" #include "ash/shell.h" #include "ash/shell_delegate.h" #include "ash/shell_window_ids.h" @@ -11,7 +12,10 @@ #include "ash/shell/panel_window.h" #include "ash/shell/toplevel_window.h" #include "ash/wm/shadow_types.h" +#include "base/bind.h" +#include "base/time.h" #include "base/utf_string_conversions.h" +#include "content/public/browser/browser_thread.h" #include "ui/aura/root_window.h" #include "ui/aura/window.h" #include "ui/gfx/canvas.h" @@ -216,7 +220,10 @@ WindowTypeLauncher::WindowTypeLauncher() this, ASCIIToUTF16("Open Views Examples Window")))), ALLOW_THIS_IN_INITIALIZER_LIST(show_hide_window_button_( new views::NativeTextButton( - this, ASCIIToUTF16("Show/Hide a Window")))) { + this, ASCIIToUTF16("Show/Hide a Window")))), + ALLOW_THIS_IN_INITIALIZER_LIST(show_screensaver_( + new views::NativeTextButton( + this, ASCIIToUTF16("Show the Screensaver [for 5 seconds]")))) { views::GridLayout* layout = new views::GridLayout(this); layout->SetInsets(5, 5, 5, 5); SetLayoutManager(layout); @@ -239,6 +246,7 @@ WindowTypeLauncher::WindowTypeLauncher() AddViewToLayout(layout, transient_button_); AddViewToLayout(layout, examples_button_); AddViewToLayout(layout, show_hide_window_button_); + AddViewToLayout(layout, show_screensaver_); #if !defined(OS_MACOSX) set_context_menu_controller(this); #endif @@ -305,6 +313,13 @@ void WindowTypeLauncher::ButtonPressed(views::Button* sender, NonModalTransient::OpenNonModalTransient(GetWidget()->GetNativeView()); } else if (sender == show_hide_window_button_) { NonModalTransient::ToggleNonModalTransient(GetWidget()->GetNativeView()); + } else if (sender == show_screensaver_) { + ash::ShowScreensaver(GURL("http://www.google.com")); + content::BrowserThread::PostDelayedTask(content::BrowserThread::UI, + FROM_HERE, + base::Bind(&ash::CloseScreensaver), + base::TimeDelta::FromSeconds(5)); + } #if !defined(OS_MACOSX) else if (sender == examples_button_) { diff --git a/ash/shell/window_type_launcher.h b/ash/shell/window_type_launcher.h index 3b9807d..4dcc5ab 100644 --- a/ash/shell/window_type_launcher.h +++ b/ash/shell/window_type_launcher.h @@ -77,6 +77,7 @@ class WindowTypeLauncher : public views::WidgetDelegateView, views::NativeTextButton* transient_button_; views::NativeTextButton* examples_button_; views::NativeTextButton* show_hide_window_button_; + views::NativeTextButton* show_screensaver_; #if !defined(OS_MACOSX) scoped_ptr menu_runner_; #endif diff --git a/ash/shell_delegate.h b/ash/shell_delegate.h index 0a86aa3..9cbe304 100644 --- a/ash/shell_delegate.h +++ b/ash/shell_delegate.h @@ -71,6 +71,9 @@ class ASH_EXPORT ShellDelegate { // Invoked when the user needs to set up mobile networking. virtual void OpenMobileSetup() = 0; + // Get the current browser context. This will get us the current profile. + virtual content::BrowserContext* GetCurrentBrowserContext() = 0; + // Invoked to create an AppListViewDelegate. Shell takes the ownership of // the created delegate. virtual AppListViewDelegate* CreateAppListViewDelegate() = 0; diff --git a/ash/test/DEPS b/ash/test/DEPS new file mode 100644 index 0000000..659b105 --- /dev/null +++ b/ash/test/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+content/test", +] diff --git a/ash/test/test_shell_delegate.cc b/ash/test/test_shell_delegate.cc index f3dbea5..ad6f34a 100644 --- a/ash/test/test_shell_delegate.cc +++ b/ash/test/test_shell_delegate.cc @@ -10,6 +10,7 @@ #include "ash/shell.h" #include "ash/shell_window_ids.h" #include "ash/test/test_launcher_delegate.h" +#include "content/test/test_browser_context.h" #include "grit/ui_resources.h" #include "ui/aura/window.h" @@ -59,6 +60,10 @@ void TestShellDelegate::OpenCrosh() { void TestShellDelegate::OpenMobileSetup() { } +content::BrowserContext* TestShellDelegate::GetCurrentBrowserContext() { + return new TestBrowserContext(); +} + AppListViewDelegate* TestShellDelegate::CreateAppListViewDelegate() { return NULL; } diff --git a/ash/test/test_shell_delegate.h b/ash/test/test_shell_delegate.h index 1788a4a..ce04845 100644 --- a/ash/test/test_shell_delegate.h +++ b/ash/test/test_shell_delegate.h @@ -29,6 +29,7 @@ class TestShellDelegate : public ShellDelegate { virtual void OpenFileManager() OVERRIDE; virtual void OpenCrosh() OVERRIDE; virtual void OpenMobileSetup() OVERRIDE; + virtual content::BrowserContext* GetCurrentBrowserContext() OVERRIDE; virtual AppListViewDelegate* CreateAppListViewDelegate() OVERRIDE; virtual void StartPartialScreenshot( ScreenshotDelegate* screenshot_delegate) OVERRIDE; diff --git a/chrome/browser/chromeos/kiosk_mode/kiosk_mode_screensaver.cc b/chrome/browser/chromeos/kiosk_mode/kiosk_mode_screensaver.cc index 9bdfb33..68f725e 100644 --- a/chrome/browser/chromeos/kiosk_mode/kiosk_mode_screensaver.cc +++ b/chrome/browser/chromeos/kiosk_mode/kiosk_mode_screensaver.cc @@ -4,6 +4,7 @@ #include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_screensaver.h" +#include "ash/screensaver/screensaver_view.h" #include "base/bind.h" #include "base/callback.h" #include "base/lazy_instance.h" @@ -11,7 +12,7 @@ #include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h" #include "chrome/browser/chromeos/login/existing_user_controller.h" #include "chrome/browser/chromeos/login/user_manager.h" -#include "chrome/browser/chromeos/ui/screensaver_extension_dialog.h" +#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/extensions/extension.h" @@ -173,7 +174,11 @@ void KioskModeScreensaver::SetupScreensaver( chromeos::DBusThreadManager::Get()-> GetPowerManagerClient()->RequestActiveNotification(); - browser::ShowScreensaverDialog(extension); + // Add the extension to the extension service and display the screensaver. + Profile* default_profile = ProfileManager::GetDefaultProfile(); + default_profile->GetExtensionService()->AddExtension(extension); + + ash::ShowScreensaver(extension->GetFullLaunchURL()); } // NotificationObserver overrides: @@ -188,7 +193,7 @@ void KioskModeScreensaver::Observe( if (power_manager->HasObserver(this)) power_manager->RemoveObserver(this); - browser::CloseScreensaverDialog(); + ash::CloseScreensaver(); ShutdownKioskModeScreensaver(); } @@ -203,7 +208,7 @@ void KioskModeScreensaver::ActiveNotify() { } else { // Remove the screensaver so the user can at least use the underlying // login screen to be able to log in. - browser::CloseScreensaverDialog(); + ash::CloseScreensaver(); } } diff --git a/chrome/browser/chromeos/ui/screensaver_extension_dialog.cc b/chrome/browser/chromeos/ui/screensaver_extension_dialog.cc deleted file mode 100644 index c8bd9e7..0000000 --- a/chrome/browser/chromeos/ui/screensaver_extension_dialog.cc +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/chromeos/ui/screensaver_extension_dialog.h" - -#include "base/bind.h" -#include "base/logging.h" -#include "base/memory/ref_counted.h" -#include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h" -#include "chrome/browser/extensions/extension_service.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/profiles/profile_manager.h" -#include "chrome/browser/sessions/restore_tab_helper.h" -#include "chrome/browser/ui/views/extensions/extension_dialog.h" -#include "chrome/common/chrome_switches.h" -#include "chrome/common/extensions/extension.h" -#include "chrome/common/extensions/extension_file_util.h" - -using content::BrowserThread; - -namespace { - -ScreensaverExtensionDialog* g_instance = NULL; - -} // namespace - -namespace browser { - -void ShowScreensaverDialog(scoped_refptr extension) { - ScreensaverExtensionDialog::ShowScreensaverDialog(extension); -} - -void CloseScreensaverDialog() { - ScreensaverExtensionDialog::CloseScreensaverDialog(); -} - -} // namespace browser - -// static -void ScreensaverExtensionDialog::ShowScreensaverDialog( - scoped_refptr extension) { - if (!g_instance) - g_instance = new ScreensaverExtensionDialog(extension); - g_instance->Show(); -} - -// static -void ScreensaverExtensionDialog::CloseScreensaverDialog() { - if (g_instance) - g_instance->Close(); -} - -ScreensaverExtensionDialog::ScreensaverExtensionDialog( - scoped_refptr extension) - : screensaver_extension_(extension) { -} - -void ScreensaverExtensionDialog::Show() { - if (!screensaver_extension_) - return; - - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - Profile* default_profile = ProfileManager::GetDefaultProfile(); - default_profile->GetExtensionService()->AddExtension(screensaver_extension_); - extension_dialog_ = ExtensionDialog::ShowFullscreen( - screensaver_extension_->GetFullLaunchURL(), - default_profile, - string16(), - this); -} - -void ScreensaverExtensionDialog::Close() { - if (extension_dialog_) { - extension_dialog_->Close(); - extension_dialog_ = NULL; - } -} - -ScreensaverExtensionDialog::~ScreensaverExtensionDialog() { - if (extension_dialog_) - extension_dialog_->ObserverDestroyed(); -} - -void ScreensaverExtensionDialog::ExtensionDialogClosing( - ExtensionDialog* dialog) { - // Release our reference to the dialog to allow it to close. - extension_dialog_ = NULL; -} - -void ScreensaverExtensionDialog::ExtensionTerminated( - ExtensionDialog* dialog) { - // This needs to be run 'slightly' delayed. When we get the extension - // terminated notification, the extension isn't fully unloaded yet. There - // is no good way to get around this. The correct solution will be to - // not need to reload the extension at all - but the current wiring in - // ExtensionViewsHost makes that not possible. - MessageLoop::current()->PostTask(FROM_HERE, - base::Bind(&ScreensaverExtensionDialog::ReloadAndShow, - base::Unretained(this))); - dialog->Close(); -} - -void ScreensaverExtensionDialog::ReloadAndShow() { - ProfileManager::GetDefaultProfile()->GetExtensionService()->ReloadExtension( - screensaver_extension_->id()); - - Show(); -} diff --git a/chrome/browser/chromeos/ui/screensaver_extension_dialog.h b/chrome/browser/chromeos/ui/screensaver_extension_dialog.h deleted file mode 100644 index 6da2683..0000000 --- a/chrome/browser/chromeos/ui/screensaver_extension_dialog.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_CHROMEOS_UI_SCREENSAVER_EXTENSION_DIALOG_H_ -#define CHROME_BROWSER_CHROMEOS_UI_SCREENSAVER_EXTENSION_DIALOG_H_ -#pragma once - -#include "base/memory/ref_counted.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" - -class Extension; -class ExtensionDialog; - -namespace browser { - -void ShowScreensaverDialog(scoped_refptr extension); -void CloseScreensaverDialog(); - -} // namespace browser - -// Shows or hides the screensaver extension in fullscreen mode on -// top of all other windows. -class ScreensaverExtensionDialog : public ExtensionDialogObserver { - public: - static void ShowScreensaverDialog(scoped_refptr extension); - static void CloseScreensaverDialog(); - - // ExtensionDialog::Observer implementation. - virtual void ExtensionDialogClosing(ExtensionDialog* dialog) OVERRIDE; - virtual void ExtensionTerminated(ExtensionDialog* dialog) OVERRIDE; - - protected: - virtual void Show(); - virtual void Close(); - - private: - friend class ScreensaverExtensionDialogBrowserTest; - friend class ScreensaverExtensionDialogTest; - - explicit ScreensaverExtensionDialog(scoped_refptr extension); - virtual ~ScreensaverExtensionDialog(); - - // Reload the screensaver extension and show another screensaver dialog. - void ReloadAndShow(); - - scoped_refptr screensaver_extension_; - // Host for the extension that implements this dialog. - scoped_refptr extension_dialog_; - - // Set while we're loading an extension; only touched from the UI thread. - bool loading_extension_; - - DISALLOW_COPY_AND_ASSIGN(ScreensaverExtensionDialog); -}; - -#endif // CHROME_BROWSER_CHROMEOS_UI_SCREENSAVER_EXTENSION_DIALOG_H_ diff --git a/chrome/browser/ui/views/accessibility_event_router_views_unittest.cc b/chrome/browser/ui/views/accessibility_event_router_views_unittest.cc index 981db9b..82ad589 100644 --- a/chrome/browser/ui/views/accessibility_event_router_views_unittest.cc +++ b/chrome/browser/ui/views/accessibility_event_router_views_unittest.cc @@ -90,6 +90,12 @@ class AccessibilityViewsDelegate : public views::ViewsDelegate { } #endif + virtual content::WebContents* CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) OVERRIDE { + return NULL; + } + DISALLOW_COPY_AND_ASSIGN(AccessibilityViewsDelegate); }; diff --git a/chrome/browser/ui/views/ash/chrome_shell_delegate.cc b/chrome/browser/ui/views/ash/chrome_shell_delegate.cc index 368d798..3e29621 100644 --- a/chrome/browser/ui/views/ash/chrome_shell_delegate.cc +++ b/chrome/browser/ui/views/ash/chrome_shell_delegate.cc @@ -186,6 +186,10 @@ void ChromeShellDelegate::OpenMobileSetup() { #endif } +content::BrowserContext* ChromeShellDelegate::GetCurrentBrowserContext() { + return ProfileManager::GetDefaultProfile(); +} + ash::AppListViewDelegate* ChromeShellDelegate::CreateAppListViewDelegate() { // Shell will own the created delegate. diff --git a/chrome/browser/ui/views/ash/chrome_shell_delegate.h b/chrome/browser/ui/views/ash/chrome_shell_delegate.h index 7a71f49..9415ce4 100644 --- a/chrome/browser/ui/views/ash/chrome_shell_delegate.h +++ b/chrome/browser/ui/views/ash/chrome_shell_delegate.h @@ -42,6 +42,7 @@ class ChromeShellDelegate : public ash::ShellDelegate, virtual void OpenFileManager() OVERRIDE; virtual void OpenCrosh() OVERRIDE; virtual void OpenMobileSetup() OVERRIDE; + virtual content::BrowserContext* GetCurrentBrowserContext() OVERRIDE; virtual ash::AppListViewDelegate* CreateAppListViewDelegate() OVERRIDE; virtual void StartPartialScreenshot( ash::ScreenshotDelegate* screenshot_delegate) OVERRIDE; diff --git a/chrome/browser/ui/views/bookmarks/bookmark_bar_view_test.cc b/chrome/browser/ui/views/bookmarks/bookmark_bar_view_test.cc index 62465f5..cb5d34d 100644 --- a/chrome/browser/ui/views/bookmarks/bookmark_bar_view_test.cc +++ b/chrome/browser/ui/views/bookmarks/bookmark_bar_view_test.cc @@ -128,6 +128,12 @@ class ViewsDelegateImpl : public views::ViewsDelegate { } #endif + virtual content::WebContents* CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) OVERRIDE { + return NULL; + } + private: DISALLOW_COPY_AND_ASSIGN(ViewsDelegateImpl); }; diff --git a/chrome/browser/ui/views/chrome_views_delegate.cc b/chrome/browser/ui/views/chrome_views_delegate.cc index 51c573e..5b6b3e3 100644 --- a/chrome/browser/ui/views/chrome_views_delegate.cc +++ b/chrome/browser/ui/views/chrome_views_delegate.cc @@ -170,3 +170,9 @@ views::NativeWidgetHelperAura* ChromeViewsDelegate::CreateNativeWidgetHelper( #endif } #endif + +content::WebContents* ChromeViewsDelegate::CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) { + return NULL; +} diff --git a/chrome/browser/ui/views/chrome_views_delegate.h b/chrome/browser/ui/views/chrome_views_delegate.h index c7e4b7c..963150d 100644 --- a/chrome/browser/ui/views/chrome_views_delegate.h +++ b/chrome/browser/ui/views/chrome_views_delegate.h @@ -51,6 +51,10 @@ class ChromeViewsDelegate : public views::ViewsDelegate { views::NativeWidgetAura* native_widget) OVERRIDE; #endif + virtual content::WebContents* CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) OVERRIDE; + private: DISALLOW_COPY_AND_ASSIGN(ChromeViewsDelegate); }; diff --git a/chrome/browser/ui/views/menu_model_adapter_test.cc b/chrome/browser/ui/views/menu_model_adapter_test.cc index 740ec42..98a6683 100644 --- a/chrome/browser/ui/views/menu_model_adapter_test.cc +++ b/chrome/browser/ui/views/menu_model_adapter_test.cc @@ -95,6 +95,12 @@ class TestViewsDelegate : public views::ViewsDelegate { } #endif + content::WebContents* CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) OVERRIDE { + return NULL; + } + private: DISALLOW_COPY_AND_ASSIGN(TestViewsDelegate); }; diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 41d7cbd..44fb363 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -845,8 +845,6 @@ 'browser/chromeos/ui/brightness_bubble.h', 'browser/chromeos/ui/idle_logout_dialog_view.cc', 'browser/chromeos/ui/idle_logout_dialog_view.h', - 'browser/chromeos/ui/screensaver_extension_dialog.cc', - 'browser/chromeos/ui/screensaver_extension_dialog.h', 'browser/chromeos/ui/setting_level_bubble.cc', 'browser/chromeos/ui/setting_level_bubble.h', 'browser/chromeos/ui/setting_level_bubble_view.cc', diff --git a/content/content_tests.gypi b/content/content_tests.gypi index df47b5e0..88c7400 100644 --- a/content/content_tests.gypi +++ b/content/content_tests.gypi @@ -90,6 +90,8 @@ 'test/test_browser_thread.h', 'test/test_content_client.cc', 'test/test_content_client.h', + 'test/test_content_client_initializer.cc', + 'test/test_content_client_initializer.h', 'test/test_file_error_injector.cc', 'test/test_file_error_injector.h', 'test/test_navigation_observer.cc', @@ -98,6 +100,8 @@ 'test/test_notification_tracker.h', 'test/test_renderer_host.cc', 'test/test_renderer_host.h', + 'test/test_render_view_host_factory.cc', + 'test/test_render_view_host_factory.h', 'test/test_url_fetcher_factory.cc', 'test/test_url_fetcher_factory.h', 'test/test_web_contents_view.cc', diff --git a/content/test/content_test_suite.cc b/content/test/content_test_suite.cc index 4272a9c..a81b816b 100644 --- a/content/test/content_test_suite.cc +++ b/content/test/content_test_suite.cc @@ -5,13 +5,11 @@ #include "content/test/content_test_suite.h" #include "base/logging.h" -#include "base/memory/scoped_ptr.h" #include "content/browser/mock_content_browser_client.h" -#include "content/browser/notification_service_impl.h" -#include "content/public/common/content_client.h" #include "content/public/common/content_paths.h" #include "content/public/common/url_constants.h" #include "content/test/test_content_client.h" +#include "content/test/test_content_client_initializer.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/base/ui_base_paths.h" @@ -20,40 +18,27 @@ #endif #include "ui/gfx/compositor/compositor_setup.h" + namespace { -class TestContentClientInitializer : public testing::EmptyTestEventListener { +class TestInitializationListener : public testing::EmptyTestEventListener { public: - TestContentClientInitializer() { + TestInitializationListener() : test_content_client_initializer_(NULL) { } virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE { - notification_service_.reset(new NotificationServiceImpl()); - - DCHECK(!content::GetContentClient()); - content_client_.reset(new TestContentClient); - content::SetContentClient(content_client_.get()); - - content_browser_client_.reset(new content::MockContentBrowserClient()); - content_client_->set_browser(content_browser_client_.get()); + test_content_client_initializer_ = + new content::TestContentClientInitializer(); } virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { - notification_service_.reset(); - - DCHECK_EQ(content_client_.get(), content::GetContentClient()); - content::SetContentClient(NULL); - content_client_.reset(); - - content_browser_client_.reset(); + delete test_content_client_initializer_; } private: - scoped_ptr notification_service_; - scoped_ptr content_client_; - scoped_ptr content_browser_client_; + content::TestContentClientInitializer* test_content_client_initializer_; - DISALLOW_COPY_AND_ASSIGN(TestContentClientInitializer); + DISALLOW_COPY_AND_ASSIGN(TestInitializationListener); }; } // namespace @@ -85,6 +70,6 @@ void ContentTestSuite::Initialize() { testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners(); - listeners.Append(new TestContentClientInitializer); + listeners.Append(new TestInitializationListener); } diff --git a/content/test/test_content_client_initializer.cc b/content/test/test_content_client_initializer.cc new file mode 100644 index 0000000..26cd3aa --- /dev/null +++ b/content/test/test_content_client_initializer.cc @@ -0,0 +1,35 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/test/test_content_client_initializer.h" + +#include "content/browser/mock_content_browser_client.h" +#include "content/browser/notification_service_impl.h" +#include "content/public/common/content_client.h" +#include "content/test/test_content_client.h" + +namespace content { + +TestContentClientInitializer::TestContentClientInitializer() { + notification_service_.reset(new NotificationServiceImpl()); + + DCHECK(!content::GetContentClient()); + content_client_.reset(new TestContentClient); + content::SetContentClient(content_client_.get()); + + content_browser_client_.reset(new content::MockContentBrowserClient()); + content_client_->set_browser(content_browser_client_.get()); +} + +TestContentClientInitializer::~TestContentClientInitializer() { + notification_service_.reset(); + + DCHECK_EQ(content_client_.get(), content::GetContentClient()); + content::SetContentClient(NULL); + content_client_.reset(); + + content_browser_client_.reset(); +} + +} // namespace content diff --git a/content/test/test_content_client_initializer.h b/content/test/test_content_client_initializer.h new file mode 100644 index 0000000..fbbdd80 --- /dev/null +++ b/content/test/test_content_client_initializer.h @@ -0,0 +1,37 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_TEST_TEST_CONTENT_CLIENT_INITIALIZER_ +#define CONTENT_TEST_TEST_CONTENT_CLIENT_INITIALIZER_ +#pragma once + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" + +class NotificationServiceImpl; + +namespace content { + +class ContentClient; +class ContentBrowserClient; + +// Initializes various objects needed to run unit tests that use content:: +// objects. Currently this includes setting up the notification service, +// creating and setting the content client and the content browser client. +class TestContentClientInitializer { + public: + TestContentClientInitializer(); + ~TestContentClientInitializer(); + + private: + scoped_ptr notification_service_; + scoped_ptr content_client_; + scoped_ptr content_browser_client_; + + DISALLOW_COPY_AND_ASSIGN(TestContentClientInitializer); +}; + +} // namespace content + +#endif // CONTENT_TEST_TEST_CONTENT_CLIENT_INITIALIZER_ diff --git a/content/test/test_render_view_host_factory.cc b/content/test/test_render_view_host_factory.cc new file mode 100644 index 0000000..429922c --- /dev/null +++ b/content/test/test_render_view_host_factory.cc @@ -0,0 +1,39 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/test/test_render_view_host_factory.h" + +#include "content/browser/renderer_host/test_render_view_host.h" +#include "content/browser/site_instance_impl.h" +#include "content/public/browser/render_process_host_factory.h" + +namespace content { + +TestRenderViewHostFactory::TestRenderViewHostFactory( + content::RenderProcessHostFactory* rph_factory) + : render_process_host_factory_(rph_factory) { + RenderViewHostFactory::RegisterFactory(this); +} + +TestRenderViewHostFactory::~TestRenderViewHostFactory() { + RenderViewHostFactory::UnregisterFactory(); +} + +void TestRenderViewHostFactory::set_render_process_host_factory( + content::RenderProcessHostFactory* rph_factory) { + render_process_host_factory_ = rph_factory; +} + +content::RenderViewHost* TestRenderViewHostFactory::CreateRenderViewHost( + SiteInstance* instance, + RenderViewHostDelegate* delegate, + int routing_id, + SessionStorageNamespace* session_storage) { + // See declaration of render_process_host_factory_ below. + static_cast(instance)-> + set_render_process_host_factory(render_process_host_factory_); + return new TestRenderViewHost(instance, delegate, routing_id); +} + +} // namespace content diff --git a/content/test/test_render_view_host_factory.h b/content/test/test_render_view_host_factory.h new file mode 100644 index 0000000..406d1c2 --- /dev/null +++ b/content/test/test_render_view_host_factory.h @@ -0,0 +1,53 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_TEST_TEST_RENDER_VIEW_HOST_FACTORY_H_ +#define CONTENT_TEST_TEST_RENDER_VIEW_HOST_FACTORY_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "content/browser/renderer_host/render_view_host_factory.h" + +namespace content { + +class SiteInstance; +class RenderViewHostDelegate; +class RenderProcessHostFactory; +class SessionStorageNamespace; + +// Manages creation of the RenderViewHosts using our special subclass. This +// automatically registers itself when it goes in scope, and unregisters itself +// when it goes out of scope. Since you can't have more than one factory +// registered at a time, you can only have one of these objects at a time. +class TestRenderViewHostFactory : public RenderViewHostFactory { + public: + explicit TestRenderViewHostFactory( + content::RenderProcessHostFactory* rph_factory); + virtual ~TestRenderViewHostFactory(); + + virtual void set_render_process_host_factory( + content::RenderProcessHostFactory* rph_factory); + virtual content::RenderViewHost* CreateRenderViewHost( + content::SiteInstance* instance, + content::RenderViewHostDelegate* delegate, + int routing_id, + content::SessionStorageNamespace* session_storage) OVERRIDE; + + private: + // This is a bit of a hack. With the current design of the site instances / + // browsing instances, it's difficult to pass a RenderProcessHostFactory + // around properly. + // + // Instead, we set it right before we create a new RenderViewHost, which + // happens before the RenderProcessHost is created. This way, the instance + // has the correct factory and creates our special RenderProcessHosts. + content::RenderProcessHostFactory* render_process_host_factory_; + + DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); +}; + +} // namespace content + +#endif // CONTENT_TEST_TEST_RENDER_VIEW_HOST_FACTORY_H_ diff --git a/content/test/test_renderer_host.cc b/content/test/test_renderer_host.cc index cd9aa34..0921774 100644 --- a/content/test/test_renderer_host.cc +++ b/content/test/test_renderer_host.cc @@ -12,6 +12,7 @@ #include "content/public/browser/web_contents.h" #include "content/test/mock_render_process_host.h" #include "content/test/test_browser_context.h" +#include "content/test/test_render_view_host_factory.h" #if defined(USE_AURA) #include "ui/aura/env.h" @@ -25,66 +26,6 @@ namespace content { -// Manages creation of the RenderViewHosts using our special subclass. This -// automatically registers itself when it goes in scope, and unregisters itself -// when it goes out of scope. Since you can't have more than one factory -// registered at a time, you can only have one of these objects at a time. -// -// This is an implementation detail of this file and used only via -// RenderViewHostTestEnabler. -class TestRenderViewHostFactory : public RenderViewHostFactory { - public: - explicit TestRenderViewHostFactory( - content::RenderProcessHostFactory* rph_factory); - virtual ~TestRenderViewHostFactory(); - - virtual void set_render_process_host_factory( - content::RenderProcessHostFactory* rph_factory); - virtual content::RenderViewHost* CreateRenderViewHost( - content::SiteInstance* instance, - content::RenderViewHostDelegate* delegate, - int routing_id, - content::SessionStorageNamespace* session_storage) OVERRIDE; - - private: - // This is a bit of a hack. With the current design of the site instances / - // browsing instances, it's difficult to pass a RenderProcessHostFactory - // around properly. - // - // Instead, we set it right before we create a new RenderViewHost, which - // happens before the RenderProcessHost is created. This way, the instance - // has the correct factory and creates our special RenderProcessHosts. - content::RenderProcessHostFactory* render_process_host_factory_; - - DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); -}; - -TestRenderViewHostFactory::TestRenderViewHostFactory( - content::RenderProcessHostFactory* rph_factory) - : render_process_host_factory_(rph_factory) { - RenderViewHostFactory::RegisterFactory(this); -} - -TestRenderViewHostFactory::~TestRenderViewHostFactory() { - RenderViewHostFactory::UnregisterFactory(); -} - -void TestRenderViewHostFactory::set_render_process_host_factory( - content::RenderProcessHostFactory* rph_factory) { - render_process_host_factory_ = rph_factory; -} - -content::RenderViewHost* TestRenderViewHostFactory::CreateRenderViewHost( - SiteInstance* instance, - RenderViewHostDelegate* delegate, - int routing_id, - SessionStorageNamespace* session_storage) { - // See declaration of render_process_host_factory_ below. - static_cast(instance)-> - set_render_process_host_factory(render_process_host_factory_); - return new TestRenderViewHost(instance, delegate, routing_id); -} - // static RenderViewHostTester* RenderViewHostTester::For(RenderViewHost* host) { return static_cast(host); diff --git a/ui/views/controls/webview/webview.cc b/ui/views/controls/webview/webview.cc index eb7846b..b6825d1 100644 --- a/ui/views/controls/webview/webview.cc +++ b/ui/views/controls/webview/webview.cc @@ -17,6 +17,7 @@ #include "ui/base/accessibility/accessibility_types.h" #include "ui/views/controls/native/native_view_host.h" #include "ui/views/focus/focus_manager.h" +#include "ui/views/views_delegate.h" namespace views { @@ -46,11 +47,7 @@ content::WebContents* WebView::GetWebContents() { void WebView::CreateWebContentsWithSiteInstance( content::SiteInstance* site_instance) { if (!web_contents_) { - wc_owner_.reset(content::WebContents::Create(browser_context_, - site_instance, - MSG_ROUTING_NONE, - NULL, - NULL)); + wc_owner_.reset(CreateWebContents(browser_context_, site_instance)); web_contents_ = wc_owner_.get(); web_contents_->SetDelegate(this); AttachWebContents(); @@ -228,4 +225,24 @@ void WebView::WebContentsDestroyed(content::WebContents* web_contents) { SetWebContents(NULL); } +content::WebContents* WebView::CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) { + content::WebContents* contents = NULL; + if (ViewsDelegate::views_delegate) { + contents = ViewsDelegate::views_delegate->CreateWebContents( + browser_context, site_instance); + } + + if (!contents) { + return content::WebContents::Create(browser_context, + site_instance, + MSG_ROUTING_NONE, + NULL, + NULL); + } + + return contents; +} + } // namespace views diff --git a/ui/views/controls/webview/webview.h b/ui/views/controls/webview/webview.h index 5a73dc9..de6fa7c 100644 --- a/ui/views/controls/webview/webview.h +++ b/ui/views/controls/webview/webview.h @@ -102,6 +102,13 @@ class VIEWS_EXPORT WebView : public View, content::RenderViewHost* new_host); void WebContentsDestroyed(content::WebContents* web_contents); + // Create a regular or test web contents (based on whether we're running + // in a unit test or not). + content::WebContents* CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance); + + NativeViewHost* wcv_holder_; scoped_ptr wc_owner_; content::WebContents* web_contents_; @@ -114,4 +121,4 @@ class VIEWS_EXPORT WebView : public View, } // namespace views -#endif // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_ \ No newline at end of file +#endif // UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_ diff --git a/ui/views/test/DEPS b/ui/views/test/DEPS new file mode 100644 index 0000000..659b105 --- /dev/null +++ b/ui/views/test/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+content/test", +] diff --git a/ui/views/test/test_views_delegate.cc b/ui/views/test/test_views_delegate.cc index a8fe511..21ae8ae 100644 --- a/ui/views/test/test_views_delegate.cc +++ b/ui/views/test/test_views_delegate.cc @@ -5,6 +5,7 @@ #include "ui/views/test/test_views_delegate.h" #include "base/logging.h" +#include "content/test/web_contents_tester.h" #include "ui/base/clipboard/clipboard.h" namespace views { @@ -64,4 +65,11 @@ views::NativeWidgetHelperAura* TestViewsDelegate::CreateNativeWidgetHelper( } #endif +content::WebContents* TestViewsDelegate::CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) { + return content::WebContentsTester::CreateTestWebContents(browser_context, + site_instance); +} + } // namespace views diff --git a/ui/views/test/test_views_delegate.h b/ui/views/test/test_views_delegate.h index 1ea5cdf..54ae0be 100644 --- a/ui/views/test/test_views_delegate.h +++ b/ui/views/test/test_views_delegate.h @@ -64,6 +64,10 @@ class TestViewsDelegate : public ViewsDelegate { views::NativeWidgetAura* native_widget) OVERRIDE; #endif + virtual content::WebContents* CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) OVERRIDE; + private: mutable scoped_ptr clipboard_; bool use_transparent_windows_; diff --git a/ui/views/test/webview_test_helper.cc b/ui/views/test/webview_test_helper.cc new file mode 100644 index 0000000..6ff3925 --- /dev/null +++ b/ui/views/test/webview_test_helper.cc @@ -0,0 +1,34 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/views/test/webview_test_helper.h" + +#include "base/message_loop.h" +#include "content/test/mock_render_process_host.h" +#include "content/test/test_browser_thread.h" +#include "content/test/test_content_client_initializer.h" +#include "content/test/test_render_view_host_factory.h" +#include "ui/views/controls/webview/webview.h" + +namespace views { + +WebViewTestHelper::WebViewTestHelper(MessageLoopForUI* ui_loop) { + test_content_client_initializer_.reset( + new content::TestContentClientInitializer); + + // Setup to register a new RenderViewHost factory which manufactures + // mock render process hosts. This ensures that we never create a 'real' + // render view host since support for it doesn't exist in unit tests. + rph_factory_.reset(new content::MockRenderProcessHostFactory()); + rvh_factory_.reset( + new content::TestRenderViewHostFactory(rph_factory_.get())); + + ui_thread_.reset( + new content::TestBrowserThread(content::BrowserThread::UI, ui_loop)); +} + +WebViewTestHelper::~WebViewTestHelper() { +} + +} // namespace views diff --git a/ui/views/test/webview_test_helper.h b/ui/views/test/webview_test_helper.h new file mode 100644 index 0000000..9868bcc --- /dev/null +++ b/ui/views/test/webview_test_helper.h @@ -0,0 +1,41 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_VIEWS_TEST_WEB_VIEW_TEST_HELPER_H_ +#define UI_VIEWS_TEST_WEB_VIEW_TEST_HELPER_H_ +#pragma once + +#include "base/memory/scoped_ptr.h" + +class MessageLoopForUI; + +namespace content { +class TestContentClientInitializer; +class TestBrowserThread; +class MockRenderProcessHostFactory; +class TestRenderViewHostFactory; +} // namespace content + +namespace views { + +class WebViewTestHelper { + public: + explicit WebViewTestHelper(MessageLoopForUI* ui_loop); + virtual ~WebViewTestHelper(); + + private: + scoped_ptr + test_content_client_initializer_; + + scoped_ptr ui_thread_; + + scoped_ptr rph_factory_; + scoped_ptr rvh_factory_; + + DISALLOW_COPY_AND_ASSIGN(WebViewTestHelper); +}; + +} // namespace views + +#endif // UI_VIEWS_TEST_WEB_VIEW_TEST_HELPER_H_ diff --git a/ui/views/views.gyp b/ui/views/views.gyp index 7919b8e..e83e467 100644 --- a/ui/views/views.gyp +++ b/ui/views/views.gyp @@ -451,6 +451,32 @@ ], }, # target_name: views { + 'target_name': 'test_support_views', + 'type': 'static_library', + 'dependencies': [ + '../../base/base.gyp:base', + '../../content/content.gyp:test_support_content', + '../../ipc/ipc.gyp:test_support_ipc', + '../../net/net.gyp:net_test_support', + '../../skia/skia.gyp:skia', + '../../testing/gtest.gyp:gtest', + '../ui.gyp:ui', + 'views', + ], + 'include_dirs': [ + '..', + ], + 'sources': [ + 'test/test_tooltip_client.h', + 'test/test_views_delegate.cc', + 'test/test_views_delegate.h', + 'test/views_test_base.cc', + 'test/views_test_base.h', + 'test/webview_test_helper.cc', + 'test/webview_test_helper.h', + ], + }, # target_name: test_support_views + { 'target_name': 'views_unittests', 'type': 'executable', 'dependencies': [ @@ -473,6 +499,7 @@ '../ui.gyp:ui', '../ui.gyp:ui_resources', '../ui.gyp:ui_resources_standard', + 'test_support_views', 'views', ], 'include_dirs': [ @@ -504,11 +531,6 @@ 'focus/focus_traversal_unittest.cc', 'layout/box_layout_unittest.cc', 'layout/grid_layout_unittest.cc', - 'test/test_tooltip_client.h', - 'test/test_views_delegate.cc', - 'test/test_views_delegate.h', - 'test/views_test_base.cc', - 'test/views_test_base.h', 'view_model_unittest.cc', 'view_model_utils_unittest.cc', 'view_unittest.cc', @@ -655,6 +677,7 @@ '../../chrome/chrome_resources.gyp:packed_resources', '../../content/content.gyp:content_shell_lib', '../../content/content.gyp:content', + '../../content/content.gyp:test_support_content', '../../skia/skia.gyp:skia', '../../third_party/icu/icu.gyp:icui18n', '../../third_party/icu/icu.gyp:icuuc', diff --git a/ui/views/views_delegate.h b/ui/views/views_delegate.h index d1fc739..11e38f5 100644 --- a/ui/views/views_delegate.h +++ b/ui/views/views_delegate.h @@ -17,6 +17,12 @@ #include "ui/base/ui_base_types.h" #include "ui/views/views_export.h" +namespace content { +class WebContents; +class BrowserContext; +class SiteInstance; +} + namespace gfx { class Rect; } @@ -108,6 +114,11 @@ class VIEWS_EXPORT ViewsDelegate { virtual NativeWidgetHelperAura* CreateNativeWidgetHelper( NativeWidgetAura* native_widget) = 0; #endif + + // Creates a web contents. This will return NULL unless overriden. + virtual content::WebContents* CreateWebContents( + content::BrowserContext* browser_context, + content::SiteInstance* site_instance) = 0; }; } // namespace views -- cgit v1.1