OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/browser.h" | 6 #include "chrome/browser/browser.h" |
7 #include "chrome/browser/browser_list.h" | |
8 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
9 #include "chrome/browser/extensions/extension_host.h" | 8 #include "chrome/browser/extensions/extension_host.h" |
10 #include "chrome/browser/extensions/extension_process_manager.h" | 9 #include "chrome/browser/extensions/extension_process_manager.h" |
11 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
12 #include "chrome/browser/renderer_host/render_view_host.h" | 11 #include "chrome/browser/renderer_host/render_view_host.h" |
13 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
14 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/test/ui_test_utils.h" | 14 #include "chrome/test/ui_test_utils.h" |
16 #include "net/base/mock_host_resolver.h" | 15 #include "net/base/mock_host_resolver.h" |
17 | 16 |
18 class AppApiTest : public ExtensionApiTest { | 17 class AppApiTest : public ExtensionApiTest { |
19 }; | 18 }; |
20 | 19 |
21 // Simulates a page calling window.open on an URL, and waits for the navigation. | 20 // Simulates a page calling window.open on an URL, and waits for the navigation. |
22 static void WindowOpenHelper(Browser* browser, | 21 static void WindowOpenHelper(Browser* browser, |
23 RenderViewHost* opener_host, | 22 RenderViewHost* opener_host, const GURL& url) { |
24 const GURL& url, | |
25 bool newtab_process_should_equal_opener) { | |
26 bool result = false; | 23 bool result = false; |
27 ui_test_utils::ExecuteJavaScriptAndExtractBool( | 24 ui_test_utils::ExecuteJavaScriptAndExtractBool( |
28 opener_host, L"", | 25 opener_host, L"", |
29 L"window.open('" + UTF8ToWide(url.spec()) + L"');" | 26 L"window.open('" + UTF8ToWide(url.spec()) + L"');" |
30 L"window.domAutomationController.send(true);", | 27 L"window.domAutomationController.send(true);", |
31 &result); | 28 &result); |
32 ASSERT_TRUE(result); | 29 ASSERT_TRUE(result); |
33 | 30 |
34 // The above window.open call is not user-initiated, it will create | 31 // Now the current tab should be the new tab. |
35 // a popup window instead of a new tab in current window. | 32 TabContents* newtab = browser->GetSelectedTabContents(); |
36 // Now the active tab in last active window should be the new tab. | |
37 Browser* last_active_browser = BrowserList::GetLastActive(); | |
38 EXPECT_TRUE(last_active_browser); | |
39 TabContents* newtab = last_active_browser->GetSelectedTabContents(); | |
40 EXPECT_TRUE(newtab); | |
41 if (!newtab->controller().GetLastCommittedEntry() || | 33 if (!newtab->controller().GetLastCommittedEntry() || |
42 newtab->controller().GetLastCommittedEntry()->url() != url) | 34 newtab->controller().GetLastCommittedEntry()->url() != url) |
43 ui_test_utils::WaitForNavigation(&newtab->controller()); | 35 ui_test_utils::WaitForNavigation(&newtab->controller()); |
44 EXPECT_EQ(url, newtab->controller().GetLastCommittedEntry()->url()); | 36 EXPECT_EQ(url, newtab->controller().GetLastCommittedEntry()->url()); |
45 if (newtab_process_should_equal_opener) | |
46 EXPECT_EQ(opener_host->process(), newtab->render_view_host()->process()); | |
47 else | |
48 EXPECT_NE(opener_host->process(), newtab->render_view_host()->process()); | |
49 } | 37 } |
50 | 38 |
51 // Simulates a page navigating itself to an URL, and waits for the navigation. | 39 // Simulates a page navigating itself to an URL, and waits for the navigation. |
52 static void NavigateTabHelper(TabContents* contents, const GURL& url) { | 40 static void NavigateTabHelper(TabContents* contents, const GURL& url) { |
53 bool result = false; | 41 bool result = false; |
54 ui_test_utils::ExecuteJavaScriptAndExtractBool( | 42 ui_test_utils::ExecuteJavaScriptAndExtractBool( |
55 contents->render_view_host(), L"", | 43 contents->render_view_host(), L"", |
56 L"window.addEventListener('unload', function() {" | 44 L"window.addEventListener('unload', function() {" |
57 L" window.domAutomationController.send(true);" | 45 L" window.domAutomationController.send(true);" |
58 L"}, false);" | 46 L"}, false);" |
59 L"window.location = '" + UTF8ToWide(url.spec()) + L"';", | 47 L"window.location = '" + UTF8ToWide(url.spec()) + L"';", |
60 &result); | 48 &result); |
61 ASSERT_TRUE(result); | 49 ASSERT_TRUE(result); |
62 | 50 |
63 if (!contents->controller().GetLastCommittedEntry() || | 51 if (!contents->controller().GetLastCommittedEntry() || |
64 contents->controller().GetLastCommittedEntry()->url() != url) | 52 contents->controller().GetLastCommittedEntry()->url() != url) |
65 ui_test_utils::WaitForNavigation(&contents->controller()); | 53 ui_test_utils::WaitForNavigation(&contents->controller()); |
66 EXPECT_EQ(url, contents->controller().GetLastCommittedEntry()->url()); | 54 EXPECT_EQ(url, contents->controller().GetLastCommittedEntry()->url()); |
67 } | 55 } |
68 | 56 |
69 IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcess) { | 57 IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcess) { |
70 CommandLine::ForCurrentProcess()->AppendSwitch( | |
71 switches::kDisablePopupBlocking); | |
72 | |
73 host_resolver()->AddRule("*", "127.0.0.1"); | 58 host_resolver()->AddRule("*", "127.0.0.1"); |
74 ASSERT_TRUE(test_server()->Start()); | 59 ASSERT_TRUE(test_server()->Start()); |
75 | 60 |
76 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process"))); | 61 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process"))); |
77 | 62 |
78 // Open two tabs in the app, one outside it. | 63 // Open two tabs in the app, one outside it. |
79 GURL base_url("http://localhost:1337/files/extensions/api_test/app_process/"); | 64 GURL base_url("http://localhost:1337/files/extensions/api_test/app_process/"); |
80 browser()->NewTab(); | 65 browser()->NewTab(); |
81 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html")); | 66 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html")); |
82 browser()->NewTab(); | 67 browser()->NewTab(); |
83 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path2/empty.html")); | 68 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path2/empty.html")); |
84 browser()->NewTab(); | 69 browser()->NewTab(); |
85 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path3/empty.html")); | 70 ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path3/empty.html")); |
86 | 71 |
87 // The extension should have opened 3 new tabs. Including the original blank | 72 // The extension should have opened 3 new tabs. Including the original blank |
88 // tab, we now have 4 tabs. Two should be part of the extension app, and | 73 // tab, we now have 4 tabs. Two should be part of the extension app, and |
89 // grouped in the extension process. | 74 // grouped in the extension process. |
90 ASSERT_EQ(4, browser()->tab_count()); | 75 ASSERT_EQ(4, browser()->tab_count()); |
91 RenderViewHost* host = browser()->GetTabContentsAt(1)->render_view_host(); | 76 RenderViewHost* host = browser()->GetTabContentsAt(1)->render_view_host(); |
92 EXPECT_TRUE(host->is_extension_process()); | 77 EXPECT_TRUE(host->is_extension_process()); |
93 | 78 |
94 EXPECT_EQ(host->process(), | 79 EXPECT_EQ(host->process(), |
95 browser()->GetTabContentsAt(2)->render_view_host()->process()); | 80 browser()->GetTabContentsAt(2)->render_view_host()->process()); |
96 EXPECT_NE(host->process(), | 81 EXPECT_NE(host->process(), |
97 browser()->GetTabContentsAt(3)->render_view_host()->process()); | 82 browser()->GetTabContentsAt(3)->render_view_host()->process()); |
98 | 83 |
99 // Now let's do the same using window.open. The same should happen. | 84 // Now let's do the same using window.open. The same should happen. |
100 ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile())); | |
101 WindowOpenHelper(browser(), host, | 85 WindowOpenHelper(browser(), host, |
102 base_url.Resolve("path1/empty.html"), true); | 86 base_url.Resolve("path1/empty.html")); |
103 WindowOpenHelper(browser(), host, | 87 WindowOpenHelper(browser(), host, |
104 base_url.Resolve("path2/empty.html"), true); | 88 base_url.Resolve("path2/empty.html")); |
105 WindowOpenHelper(browser(), host, | 89 WindowOpenHelper(browser(), host, |
106 base_url.Resolve("path3/empty.html"), false); | 90 base_url.Resolve("path3/empty.html")); |
| 91 |
| 92 ASSERT_EQ(7, browser()->tab_count()); |
| 93 EXPECT_EQ(host->process(), |
| 94 browser()->GetTabContentsAt(4)->render_view_host()->process()); |
| 95 EXPECT_EQ(host->process(), |
| 96 browser()->GetTabContentsAt(5)->render_view_host()->process()); |
| 97 EXPECT_NE(host->process(), |
| 98 browser()->GetTabContentsAt(6)->render_view_host()->process()); |
107 | 99 |
108 // Now let's have these pages navigate, into or out of the extension web | 100 // Now let's have these pages navigate, into or out of the extension web |
109 // extent. They should switch processes. | 101 // extent. They should switch processes. |
110 const GURL& app_url(base_url.Resolve("path1/empty.html")); | 102 const GURL& app_url(base_url.Resolve("path1/empty.html")); |
111 const GURL& non_app_url(base_url.Resolve("path3/empty.html")); | 103 const GURL& non_app_url(base_url.Resolve("path3/empty.html")); |
112 NavigateTabHelper(browser()->GetTabContentsAt(2), non_app_url); | 104 NavigateTabHelper(browser()->GetTabContentsAt(2), non_app_url); |
113 NavigateTabHelper(browser()->GetTabContentsAt(3), app_url); | 105 NavigateTabHelper(browser()->GetTabContentsAt(3), app_url); |
114 EXPECT_NE(host->process(), | 106 EXPECT_NE(host->process(), |
115 browser()->GetTabContentsAt(2)->render_view_host()->process()); | 107 browser()->GetTabContentsAt(2)->render_view_host()->process()); |
116 EXPECT_EQ(host->process(), | 108 EXPECT_EQ(host->process(), |
117 browser()->GetTabContentsAt(3)->render_view_host()->process()); | 109 browser()->GetTabContentsAt(3)->render_view_host()->process()); |
118 | 110 |
119 // Navigate the non-app tab into the browse extent. It should not enter the | 111 // Navigate the non-app tab into the browse extent. It should not enter the |
120 // app process. | 112 // app process. |
121 // Navigate the app tab into the browse extent. It should stay in the app | 113 // Navigate the app tab into the browse extent. It should stay in the app |
122 // process. | 114 // process. |
123 const GURL& browse_url(base_url.Resolve("path4/empty.html")); | 115 const GURL& browse_url(base_url.Resolve("path4/empty.html")); |
124 NavigateTabHelper(browser()->GetTabContentsAt(2), browse_url); | 116 NavigateTabHelper(browser()->GetTabContentsAt(2), browse_url); |
125 NavigateTabHelper(browser()->GetTabContentsAt(3), browse_url); | 117 NavigateTabHelper(browser()->GetTabContentsAt(3), browse_url); |
126 EXPECT_NE(host->process(), | 118 EXPECT_NE(host->process(), |
127 browser()->GetTabContentsAt(2)->render_view_host()->process()); | 119 browser()->GetTabContentsAt(2)->render_view_host()->process()); |
128 EXPECT_EQ(host->process(), | 120 EXPECT_EQ(host->process(), |
129 browser()->GetTabContentsAt(3)->render_view_host()->process()); | 121 browser()->GetTabContentsAt(3)->render_view_host()->process()); |
130 } | 122 } |
OLD | NEW |