Chromium Code Reviews| Index: chrome/browser/apps/guest_view/app_view_browsertest.cc |
| diff --git a/chrome/browser/apps/guest_view/app_view_browsertest.cc b/chrome/browser/apps/guest_view/app_view_browsertest.cc |
| index 0278507d3b520be64442f238bf2ee3cfe3dab465..894d386c62e477c9be21d69d4a85e5754a54b960 100644 |
| --- a/chrome/browser/apps/guest_view/app_view_browsertest.cc |
| +++ b/chrome/browser/apps/guest_view/app_view_browsertest.cc |
| @@ -7,10 +7,15 @@ |
| #include "components/guest_view/browser/guest_view_manager.h" |
| #include "components/guest_view/browser/guest_view_manager_factory.h" |
| #include "components/guest_view/browser/test_guest_view_manager.h" |
| +#include "content/public/browser/notification_observer.h" |
| #include "content/public/browser/notification_service.h" |
| #include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/render_process_host_observer.h" |
| #include "content/public/test/browser_test_utils.h" |
| #include "content/public/test/test_utils.h" |
| +#include "extensions/browser/app_window/app_window_registry.h" |
| +#include "extensions/browser/guest_view/app_view/app_view_guest.h" |
| +#include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h" |
| #include "extensions/common/switches.h" |
| #include "extensions/test/extension_test_message_listener.h" |
| #include "net/test/embedded_test_server/embedded_test_server.h" |
| @@ -20,6 +25,50 @@ |
| using guest_view::GuestViewManager; |
| using guest_view::TestGuestViewManagerFactory; |
| +namespace { |
| + |
| +class RenderProcessHostObserverForExit |
| + : public content::RenderProcessHostObserver { |
| + public: |
| + RenderProcessHostObserverForExit(content::RenderProcessHost* observed_host) |
| + : render_process_host_exited_(false), waiting_(false) { |
| + observed_host->AddObserver(this); |
| + LOG(INFO) << "Listening for RPH Exit (ID = " << observed_host->GetID() |
| + << ")"; |
| + } |
| + |
| + bool WaitUntilRenderProcessHostKilled() { |
| + if (!render_process_host_exited_) { |
| + if (!waiting_) { |
| + waiting_ = true; |
| + content::RunMessageLoop(); |
| + } |
| + } |
| + return render_process_host_exited_; |
| + } |
| + |
| + private: |
| + void RenderProcessHostDestroyed(content::RenderProcessHost* host) override { |
|
lfg
2015/06/12 21:43:46
Use RenderProcessHostExited and check the terminat
EhsanK
2015/06/25 16:19:50
Done.
|
| + LOG(INFO) << "RPH Exiting (ID = " << host->GetID() << ")"; |
| + host->RemoveObserver(this); |
|
lfg
2015/06/12 21:43:46
The host was destroyed, is this needed/does it wor
EhsanK
2015/06/25 16:19:50
I copied the pattern from other similar observers.
lfg
2015/06/26 21:46:05
Acknowledged.
EhsanK
2015/06/30 16:53:10
Done.
EhsanK
2015/06/30 16:53:10
Done.
|
| + render_process_host_exited_ = true; |
| + if (waiting_) { |
| + waiting_ = false; |
| + base::MessageLoopForUI::current()->Quit(); |
| + } |
| + } |
| + |
| + bool render_process_host_exited_; |
| + bool waiting_; |
| +}; |
| + |
| +// This is a mock callback function used for the tests "TestKill*". |
| +void OnWebContentsCreated(content::WebContents* webcontents) { |
| + LOG(ERROR) << "The RPH was not killed."; |
| +} |
| + |
| +} // namespace |
| + |
| class AppViewTest : public extensions::PlatformAppBrowserTest { |
| public: |
| AppViewTest() { |
| @@ -59,8 +108,7 @@ class AppViewTest : public extensions::PlatformAppBrowserTest { |
| done_listener.set_failure_message("TEST_FAILED"); |
| if (!content::ExecuteScript( |
| embedder_web_contents, |
| - base::StringPrintf("runTest('%s', '%s')", |
| - test_name.c_str(), |
| + base::StringPrintf("runTest('%s', '%s')", test_name.c_str(), |
| app_to_embed.c_str()))) { |
| LOG(ERROR) << "UNABLE TO START TEST."; |
| return; |
| @@ -68,6 +116,22 @@ class AppViewTest : public extensions::PlatformAppBrowserTest { |
| ASSERT_TRUE(done_listener.WaitUntilSatisfied()); |
| } |
| + guest_view::TestGuestViewManager* GetGuestViewManager() { |
| + guest_view::TestGuestViewManager* manager = |
| + static_cast<guest_view::TestGuestViewManager*>( |
| + guest_view::TestGuestViewManager::FromBrowserContext( |
| + browser()->profile())); |
| + if (!manager) { |
| + manager = static_cast<guest_view::TestGuestViewManager*>( |
| + GuestViewManager::CreateWithDelegate( |
| + browser()->profile(), |
| + scoped_ptr<guest_view::GuestViewManagerDelegate>( |
| + new extensions::ExtensionsGuestViewManagerDelegate( |
| + browser()->profile())))); |
| + } |
| + return manager; |
| + } |
| + |
| private: |
| void SetUpCommandLine(base::CommandLine* command_line) override { |
| extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); |
| @@ -125,3 +189,76 @@ IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewEmbedSelfShouldFail) { |
| skeleton_app->id(), |
| NO_TEST_SERVER); |
| } |
| + |
| +IN_PROC_BROWSER_TEST_F(AppViewTest, TestKillGuestWithInvalidInstanceID) { |
| + const extensions::Extension* mock_bad_app = |
| + LoadAndLaunchPlatformApp("app_view/bad_app", "AppViewTest.LAUNCHED"); |
| + |
| + content::RenderProcessHost* bad_app_render_process_host = |
| + extensions::AppWindowRegistry::Get(browser()->profile()) |
| + ->GetCurrentAppWindowForApp(mock_bad_app->id()) |
| + ->web_contents() |
| + ->GetRenderProcessHost(); |
| + |
| + // Monitor |mock_bad_app|'s "RenderProcessHost" for its exiting. |
| + RenderProcessHostObserverForExit exit_observer(bad_app_render_process_host); |
| + |
| + // Get/Create the instance of "GuestViewManager". |
| + guest_view::GuestViewManager* guest_view_manager = GetGuestViewManager(); |
| + EXPECT_TRUE(guest_view_manager != NULL); |
| + |
| + // Choosing a |guest_instance_id| which does not exist. |
| + int invalid_guest_instance_id = guest_view_manager->GetNextInstanceID(); |
| + LOG(INFO) << "Guest instance ID: " << invalid_guest_instance_id; |
| + |
| + // Call the desired function to verify that the |mock_bad_app| gets killed if |
| + // the provided |guest_instance_id| is not mapped to any "GuestView"'s. |
| + extensions::AppViewGuest::CompletePendingRequest( |
| + browser()->profile(), GURL("about:blank"), invalid_guest_instance_id, |
| + mock_bad_app->id(), bad_app_render_process_host->GetID()); |
| + ASSERT_TRUE(exit_observer.WaitUntilRenderProcessHostKilled()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(AppViewTest, |
| + TestKillGuestCommunicatingWithWrongAppView) { |
| + const extensions::Extension* mock_host_app = |
| + LoadAndLaunchPlatformApp("app_view/host_app", "AppViewTest.LAUNCHED"); |
| + const extensions::Extension* mock_guest_extension = |
| + InstallPlatformApp("app_view/guest_app"); |
| + const extensions::Extension* mock_bad_app = |
| + LoadAndLaunchPlatformApp("app_view/bad_app", "AppViewTest.LAUNCHED"); |
| + |
| + extensions::AppWindowRegistry* app_window_registry = |
| + extensions::AppWindowRegistry::Get(browser()->profile()); |
| + guest_view::GuestViewManager* guest_view_manager = GetGuestViewManager(); |
| + EXPECT_TRUE(guest_view_manager != NULL); |
| + content::WebContents* host_contents = |
| + app_window_registry->GetCurrentAppWindowForApp(mock_host_app->id()) |
| + ->web_contents(); |
| + extensions::AppViewGuest* app_view = |
| + extensions::AppViewGuest::Create(host_contents) |
| + ->As<extensions::AppViewGuest>(); |
| + |
| + guest_view::GuestViewBase::WebContentsCreatedCallback callback = |
| + base::Bind(OnWebContentsCreated); |
| + |
| + int guest_instance_id = GetGuestViewManager()->GetNextInstanceID(); |
| + LOG(INFO) << "Guest instance ID: " << guest_instance_id; |
| + |
| + // Add an entry for some guest extension and an <appview>. |
| + extensions::AppViewGuest::AddFakeRequestInfoForTesting( |
| + mock_guest_extension, app_view, callback, guest_instance_id); |
| + // Observe |mock_bad_app|'s "RenderProcessHost". |
| + content::RenderProcessHost* bad_app_render_process_host = |
| + app_window_registry->GetCurrentAppWindowForApp(mock_bad_app->id()) |
| + ->web_contents() |
| + ->GetRenderProcessHost(); |
| + |
| + RenderProcessHostObserverForExit exit_observer(bad_app_render_process_host); |
| + |
| + // Now call "AppViewGuest::CompletePendingRequest" form the "mock_bad_app". |
| + extensions::AppViewGuest::CompletePendingRequest( |
| + browser()->profile(), GURL("about:blank"), guest_instance_id, |
| + mock_bad_app->id(), bad_app_render_process_host->GetID()); |
| + ASSERT_TRUE(exit_observer.WaitUntilRenderProcessHostKilled()); |
| +} |