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.
5 #include "extensions/browser/process_manager.h"
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/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/test/test_utils.h"
16 #include "net/dns/mock_host_resolver.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
19 namespace extensions
{
21 // Exists as a browser test because ExtensionHosts are hard to create without
23 typedef ExtensionBrowserTest ProcessManagerBrowserTest
;
25 // Test that basic extension loading creates the appropriate ExtensionHosts
26 // and background pages.
27 IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest
,
28 ExtensionHostCreation
) {
29 ProcessManager
* pm
= ProcessManager::Get(profile());
31 // We start with no background hosts.
32 ASSERT_EQ(0u, pm
->background_hosts().size());
33 ASSERT_EQ(0u, pm
->GetAllViews().size());
35 // Load an extension with a background page.
36 scoped_refptr
<const Extension
> extension
=
37 LoadExtension(test_data_dir_
.AppendASCII("api_test")
38 .AppendASCII("browser_action")
39 .AppendASCII("none"));
40 ASSERT_TRUE(extension
.get());
42 // Process manager gains a background host.
43 EXPECT_EQ(1u, pm
->background_hosts().size());
44 EXPECT_EQ(1u, pm
->GetAllViews().size());
45 EXPECT_TRUE(pm
->GetBackgroundHostForExtension(extension
->id()));
46 EXPECT_TRUE(pm
->GetSiteInstanceForURL(extension
->url()));
47 EXPECT_EQ(1u, pm
->GetRenderViewHostsForExtension(extension
->id()).size());
48 EXPECT_FALSE(pm
->IsBackgroundHostClosing(extension
->id()));
49 EXPECT_EQ(0, pm
->GetLazyKeepaliveCount(extension
.get()));
51 // Unload the extension.
52 UnloadExtension(extension
->id());
54 // Background host disappears.
55 EXPECT_EQ(0u, pm
->background_hosts().size());
56 EXPECT_EQ(0u, pm
->GetAllViews().size());
57 EXPECT_FALSE(pm
->GetBackgroundHostForExtension(extension
->id()));
58 EXPECT_TRUE(pm
->GetSiteInstanceForURL(extension
->url()));
59 EXPECT_EQ(0u, pm
->GetRenderViewHostsForExtension(extension
->id()).size());
60 EXPECT_FALSE(pm
->IsBackgroundHostClosing(extension
->id()));
61 EXPECT_EQ(0, pm
->GetLazyKeepaliveCount(extension
.get()));
64 // Test that loading an extension with a browser action does not create a
65 // background page and that clicking on the action creates the appropriate
67 // Disabled due to flake, see http://crbug.com/315242
68 IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest
,
69 DISABLED_PopupHostCreation
) {
70 ProcessManager
* pm
= ProcessManager::Get(profile());
72 // Load an extension with the ability to open a popup but no background
74 scoped_refptr
<const Extension
> popup
=
75 LoadExtension(test_data_dir_
.AppendASCII("api_test")
76 .AppendASCII("browser_action")
77 .AppendASCII("popup"));
78 ASSERT_TRUE(popup
.get());
80 // No background host was added.
81 EXPECT_EQ(0u, pm
->background_hosts().size());
82 EXPECT_EQ(0u, pm
->GetAllViews().size());
83 EXPECT_FALSE(pm
->GetBackgroundHostForExtension(popup
->id()));
84 EXPECT_EQ(0u, pm
->GetRenderViewHostsForExtension(popup
->id()).size());
85 EXPECT_TRUE(pm
->GetSiteInstanceForURL(popup
->url()));
86 EXPECT_FALSE(pm
->IsBackgroundHostClosing(popup
->id()));
87 EXPECT_EQ(0, pm
->GetLazyKeepaliveCount(popup
.get()));
89 // Simulate clicking on the action to open a popup.
90 BrowserActionTestUtil
test_util(browser());
91 content::WindowedNotificationObserver
frame_observer(
92 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
93 content::NotificationService::AllSources());
94 // Open popup in the first extension.
96 frame_observer
.Wait();
97 ASSERT_TRUE(test_util
.HasPopup());
99 // We now have a view, but still no background hosts.
100 EXPECT_EQ(0u, pm
->background_hosts().size());
101 EXPECT_EQ(1u, pm
->GetAllViews().size());
102 EXPECT_FALSE(pm
->GetBackgroundHostForExtension(popup
->id()));
103 EXPECT_EQ(1u, pm
->GetRenderViewHostsForExtension(popup
->id()).size());
104 EXPECT_TRUE(pm
->GetSiteInstanceForURL(popup
->url()));
105 EXPECT_FALSE(pm
->IsBackgroundHostClosing(popup
->id()));
106 EXPECT_EQ(0, pm
->GetLazyKeepaliveCount(popup
.get()));
109 // Content loaded from http://hlogonemlfkgpejgnedahbkiabcdhnnn should not
110 // interact with an installed extension with that ID. Regression test
112 IN_PROC_BROWSER_TEST_F(ProcessManagerBrowserTest
, HttpHostMatchingExtensionId
) {
113 ProcessManager
* pm
= ProcessManager::Get(profile());
115 // We start with no background hosts.
116 ASSERT_EQ(0u, pm
->background_hosts().size());
117 ASSERT_EQ(0u, pm
->GetAllViews().size());
119 // Load an extension with a background page.
120 scoped_refptr
<const Extension
> extension
=
121 LoadExtension(test_data_dir_
.AppendASCII("api_test")
122 .AppendASCII("browser_action")
123 .AppendASCII("none"));
125 // Set up a test server running at http://[extension-id]
126 ASSERT_TRUE(extension
.get());
127 const std::string
& aliased_host
= extension
->id();
128 host_resolver()->AddRule(aliased_host
, "127.0.0.1");
129 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
131 embedded_test_server()->GetURL("/extensions/test_file_with_body.html");
132 GURL::Replacements replace_host
;
133 replace_host
.SetHostStr(aliased_host
);
134 url
= url
.ReplaceComponents(replace_host
);
136 // Load a page from the test host in a new tab.
137 ui_test_utils::NavigateToURLWithDisposition(
141 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
143 // Sanity check that there's no bleeding between the extension and the tab.
144 content::WebContents
* tab_web_contents
=
145 browser()->tab_strip_model()->GetActiveWebContents();
146 EXPECT_EQ(url
, tab_web_contents
->GetVisibleURL());
147 EXPECT_TRUE(NULL
== pm
->GetExtensionForRenderViewHost(
148 tab_web_contents
->GetRenderViewHost()))
149 << "Non-extension content must not have an associated extension";
150 ASSERT_EQ(1u, pm
->GetRenderViewHostsForExtension(extension
->id()).size());
151 content::WebContents
* extension_web_contents
=
152 content::WebContents::FromRenderViewHost(
153 *pm
->GetRenderViewHostsForExtension(extension
->id()).begin());
154 EXPECT_TRUE(extension_web_contents
->GetSiteInstance() !=
155 tab_web_contents
->GetSiteInstance());
156 EXPECT_TRUE(pm
->GetSiteInstanceForURL(extension
->url()) !=
157 tab_web_contents
->GetSiteInstance());
158 EXPECT_TRUE(pm
->GetBackgroundHostForExtension(extension
->id()));
161 } // namespace extensions