Home | History | Annotate | Download | only in extensions
      1 // Copyright 2013 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 "extensions/browser/process_manager.h"
      6 
      7 #include "chrome/browser/extensions/browser_action_test_util.h"
      8 #include "chrome/browser/extensions/extension_browsertest.h"
      9 #include "chrome/browser/extensions/extension_service.h"
     10 #include "chrome/browser/extensions/extension_system.h"
     11 #include "chrome/test/base/in_process_browser_test.h"
     12 #include "content/public/browser/notification_service.h"
     13 #include "content/public/test/test_utils.h"
     14 
     15 namespace extensions {
     16 
     17 // Exists as a browser test because ExtensionHosts are hard to create without
     18 // a real browser.
     19 typedef ExtensionBrowserTest ProcessManagerBrowserTest;
     20 
     21 // Test that basic extension loading creates the appropriate ExtensionHosts
     22 // and background pages.
     23 IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
     24                        ExtensionHostCreation) {
     25   ProcessManager* pm = ExtensionSystem::Get(profile())->process_manager();
     26 
     27   // We start with no background hosts.
     28   ASSERT_EQ(0u, pm->background_hosts().size());
     29   ASSERT_EQ(0u, pm->GetAllViews().size());
     30 
     31   // Load an extension with a background page.
     32   scoped_refptr<const Extension> extension =
     33       LoadExtension(test_data_dir_.AppendASCII("api_test")
     34                         .AppendASCII("browser_action")
     35                         .AppendASCII("none"));
     36   ASSERT_TRUE(extension.get());
     37 
     38   // Process manager gains a background host.
     39   EXPECT_EQ(1u, pm->background_hosts().size());
     40   EXPECT_EQ(1u, pm->GetAllViews().size());
     41   EXPECT_TRUE(pm->GetBackgroundHostForExtension(extension->id()));
     42   EXPECT_TRUE(pm->GetSiteInstanceForURL(extension->url()));
     43   EXPECT_EQ(1u, pm->GetRenderViewHostsForExtension(extension->id()).size());
     44   EXPECT_FALSE(pm->IsBackgroundHostClosing(extension->id()));
     45   EXPECT_EQ(0, pm->GetLazyKeepaliveCount(extension.get()));
     46 
     47   // Unload the extension.
     48   UnloadExtension(extension->id());
     49 
     50   // Background host disappears.
     51   EXPECT_EQ(0u, pm->background_hosts().size());
     52   EXPECT_EQ(0u, pm->GetAllViews().size());
     53   EXPECT_FALSE(pm->GetBackgroundHostForExtension(extension->id()));
     54   EXPECT_TRUE(pm->GetSiteInstanceForURL(extension->url()));
     55   EXPECT_EQ(0u, pm->GetRenderViewHostsForExtension(extension->id()).size());
     56   EXPECT_FALSE(pm->IsBackgroundHostClosing(extension->id()));
     57   EXPECT_EQ(0, pm->GetLazyKeepaliveCount(extension.get()));
     58 }
     59 
     60 // Test that loading an extension with a browser action does not create a
     61 // background page and that clicking on the action creates the appropriate
     62 // ExtensionHost.
     63 // Disabled due to flake, see http://crbug.com/315242
     64 IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest,
     65                        DISABLED_PopupHostCreation) {
     66   ProcessManager* pm = ExtensionSystem::Get(profile())->process_manager();
     67 
     68   // Load an extension with the ability to open a popup but no background
     69   // page.
     70   scoped_refptr<const Extension> popup =
     71       LoadExtension(test_data_dir_.AppendASCII("api_test")
     72                         .AppendASCII("browser_action")
     73                         .AppendASCII("popup"));
     74   ASSERT_TRUE(popup);
     75 
     76   // No background host was added.
     77   EXPECT_EQ(0u, pm->background_hosts().size());
     78   EXPECT_EQ(0u, pm->GetAllViews().size());
     79   EXPECT_FALSE(pm->GetBackgroundHostForExtension(popup->id()));
     80   EXPECT_EQ(0u, pm->GetRenderViewHostsForExtension(popup->id()).size());
     81   EXPECT_TRUE(pm->GetSiteInstanceForURL(popup->url()));
     82   EXPECT_FALSE(pm->IsBackgroundHostClosing(popup->id()));
     83   EXPECT_EQ(0, pm->GetLazyKeepaliveCount(popup.get()));
     84 
     85   // Simulate clicking on the action to open a popup.
     86   BrowserActionTestUtil test_util(browser());
     87   content::WindowedNotificationObserver frame_observer(
     88       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
     89       content::NotificationService::AllSources());
     90   // Open popup in the first extension.
     91   test_util.Press(0);
     92   frame_observer.Wait();
     93   ASSERT_TRUE(test_util.HasPopup());
     94 
     95   // We now have a view, but still no background hosts.
     96   EXPECT_EQ(0u, pm->background_hosts().size());
     97   EXPECT_EQ(1u, pm->GetAllViews().size());
     98   EXPECT_FALSE(pm->GetBackgroundHostForExtension(popup->id()));
     99   EXPECT_EQ(1u, pm->GetRenderViewHostsForExtension(popup->id()).size());
    100   EXPECT_TRUE(pm->GetSiteInstanceForURL(popup->url()));
    101   EXPECT_FALSE(pm->IsBackgroundHostClosing(popup->id()));
    102   EXPECT_EQ(0, pm->GetLazyKeepaliveCount(popup.get()));
    103 }
    104 
    105 }  // namespace extensions
    106