[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/browser.h" |
| 6 | #include "chrome/browser/extensions/extension_apitest.h" |
| 7 | #include "chrome/browser/extensions/extension_host.h" |
| 8 | #include "chrome/browser/extensions/extension_process_manager.h" |
| 9 | #include "chrome/browser/profile.h" |
| 10 | #include "chrome/browser/renderer_host/render_view_host.h" |
| 11 | #include "chrome/browser/tab_contents/tab_contents.h" |
| 12 | #include "chrome/common/chrome_switches.h" |
| 13 | #include "chrome/test/ui_test_utils.h" |
| 14 | #include "net/base/mock_host_resolver.h" |
| 15 | |
| 16 | class AppApiTest : public ExtensionApiTest { |
| 17 | public: |
| 18 | void SetUpCommandLine(CommandLine* command_line) { |
| 19 | ExtensionApiTest::SetUpCommandLine(command_line); |
| 20 | command_line->AppendSwitch(switches::kEnableExtensionApps); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | // Simulates a page calling window.open on an URL, and waits for the navigation. |
| 25 | static void WindowOpenHelper(Browser* browser, |
| 26 | RenderViewHost* opener_host, const GURL& url) { |
| 27 | bool result = false; |
| 28 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 29 | opener_host, L"", |
| 30 | L"window.open('" + UTF8ToWide(url.spec()) + L"');" |
| 31 | L"window.domAutomationController.send(true);", |
| 32 | &result); |
| 33 | ASSERT_TRUE(result); |
| 34 | |
| 35 | // Now the current tab should be the new tab. |
| 36 | TabContents* newtab = browser->GetSelectedTabContents(); |
| 37 | if (!newtab->controller().GetLastCommittedEntry() || |
| 38 | newtab->controller().GetLastCommittedEntry()->url() != url) |
| 39 | ui_test_utils::WaitForNavigation(&newtab->controller()); |
| 40 | EXPECT_EQ(url, newtab->controller().GetLastCommittedEntry()->url()); |
| 41 | } |
| 42 | |
| 43 | // Simulates a page navigating itself to an URL, and waits for the navigation. |
| 44 | static void NavigateTabHelper(TabContents* contents, const GURL& url) { |
| 45 | bool result = false; |
| 46 | ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 47 | contents->render_view_host(), L"", |
| 48 | L"window.location = '" + UTF8ToWide(url.spec()) + L"';" |
| 49 | L"window.domAutomationController.send(true);", |
| 50 | &result); |
| 51 | ASSERT_TRUE(result); |
| 52 | |
| 53 | if (contents->controller().GetLastCommittedEntry() || |
| 54 | contents->controller().GetLastCommittedEntry()->url() != url) |
| 55 | ui_test_utils::WaitForNavigation(&contents->controller()); |
| 56 | EXPECT_EQ(url, contents->controller().GetLastCommittedEntry()->url()); |
| 57 | } |
| 58 | |
[email protected] | 109fe870 | 2010-04-25 02:31:51 | [diff] [blame] | 59 | // This test is flaky, see bug 42497. |
[email protected] | 9a1e6d4 | 2010-04-26 22:29:36 | [diff] [blame^] | 60 | IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcess) { |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 61 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 62 | StartHTTPServer(); |
| 63 | |
| 64 | ASSERT_TRUE(RunExtensionTest("app_process")) << message_; |
| 65 | |
| 66 | Extension* extension = GetSingleLoadedExtension(); |
| 67 | ExtensionHost* host = browser()->profile()->GetExtensionProcessManager()-> |
| 68 | GetBackgroundHostForExtension(extension); |
| 69 | ASSERT_TRUE(host); |
| 70 | |
| 71 | // The extension should have opened 3 new tabs. Including the original blank |
| 72 | // tab, we now have 4 tabs. Two should be part of the extension app, and |
| 73 | // grouped in the extension process. |
| 74 | ASSERT_EQ(4, browser()->tab_count()); |
| 75 | EXPECT_EQ(host->render_process_host(), |
| 76 | browser()->GetTabContentsAt(1)->render_view_host()->process()); |
| 77 | EXPECT_EQ(host->render_process_host(), |
| 78 | browser()->GetTabContentsAt(2)->render_view_host()->process()); |
| 79 | EXPECT_NE(host->render_process_host(), |
| 80 | browser()->GetTabContentsAt(3)->render_view_host()->process()); |
| 81 | |
| 82 | // Now let's do the same using window.open. The same should happen. |
[email protected] | 9a1e6d4 | 2010-04-26 22:29:36 | [diff] [blame^] | 83 | GURL base_url("http://localhost:1337/files/extensions/api_test/app_process/"); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 84 | WindowOpenHelper(browser(), host->render_view_host(), |
[email protected] | 9a1e6d4 | 2010-04-26 22:29:36 | [diff] [blame^] | 85 | base_url.Resolve("path1/empty.html")); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 86 | WindowOpenHelper(browser(), host->render_view_host(), |
[email protected] | 9a1e6d4 | 2010-04-26 22:29:36 | [diff] [blame^] | 87 | base_url.Resolve("path2/empty.html")); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 88 | WindowOpenHelper(browser(), host->render_view_host(), |
[email protected] | 9a1e6d4 | 2010-04-26 22:29:36 | [diff] [blame^] | 89 | base_url.Resolve("path3/empty.html")); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 90 | |
| 91 | ASSERT_EQ(7, browser()->tab_count()); |
| 92 | EXPECT_EQ(host->render_process_host(), |
| 93 | browser()->GetTabContentsAt(4)->render_view_host()->process()); |
| 94 | EXPECT_EQ(host->render_process_host(), |
| 95 | browser()->GetTabContentsAt(5)->render_view_host()->process()); |
| 96 | EXPECT_NE(host->render_process_host(), |
| 97 | browser()->GetTabContentsAt(6)->render_view_host()->process()); |
| 98 | |
| 99 | // Now let's have these pages navigate, into or out of the extension web |
| 100 | // extent. They should switch processes. |
[email protected] | 9a1e6d4 | 2010-04-26 22:29:36 | [diff] [blame^] | 101 | const GURL& app_url(base_url.Resolve("path1/empty.html")); |
| 102 | const GURL& non_app_url(base_url.Resolve("path3/empty.html")); |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 103 | NavigateTabHelper(browser()->GetTabContentsAt(1), non_app_url); |
| 104 | NavigateTabHelper(browser()->GetTabContentsAt(3), app_url); |
| 105 | EXPECT_NE(host->render_process_host(), |
| 106 | browser()->GetTabContentsAt(1)->render_view_host()->process()); |
| 107 | EXPECT_EQ(host->render_process_host(), |
| 108 | browser()->GetTabContentsAt(3)->render_view_host()->process()); |
| 109 | } |