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 "chrome/browser/extensions/extension_view_host_factory.h" 6 7 #include "chrome/browser/extensions/extension_browsertest.h" 8 #include "chrome/browser/extensions/extension_view_host.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/ui/browser.h" 11 #include "chrome/test/base/ui_test_utils.h" 12 #include "extensions/common/view_type.h" 13 14 namespace extensions { 15 16 typedef ExtensionBrowserTest ExtensionViewHostFactoryTest; 17 18 // Tests that ExtensionHosts are created with the correct type and profiles. 19 IN_PROC_BROWSER_TEST_F(ExtensionViewHostFactoryTest, CreateExtensionHosts) { 20 // Load a very simple extension with just a background page. 21 scoped_refptr<const Extension> extension = 22 LoadExtension(test_data_dir_.AppendASCII("api_test") 23 .AppendASCII("browser_action") 24 .AppendASCII("none")); 25 ASSERT_TRUE(extension.get()); 26 27 content::BrowserContext* browser_context = browser()->profile(); 28 { 29 // Popup hosts are created with the correct type and profile. 30 scoped_ptr<ExtensionViewHost> host( 31 ExtensionViewHostFactory::CreatePopupHost(extension->url(), browser())); 32 EXPECT_EQ(extension.get(), host->extension()); 33 EXPECT_EQ(browser_context, host->browser_context()); 34 EXPECT_EQ(VIEW_TYPE_EXTENSION_POPUP, host->extension_host_type()); 35 EXPECT_TRUE(host->view()); 36 } 37 38 { 39 // Infobar hosts are created with the correct type and profile. 40 scoped_ptr<ExtensionViewHost> host( 41 ExtensionViewHostFactory::CreateInfobarHost(extension->url(), 42 browser())); 43 EXPECT_EQ(extension.get(), host->extension()); 44 EXPECT_EQ(browser_context, host->browser_context()); 45 EXPECT_EQ(VIEW_TYPE_EXTENSION_INFOBAR, host->extension_host_type()); 46 EXPECT_TRUE(host->view()); 47 } 48 49 { 50 // Dialog hosts are created with the correct type and profile. 51 scoped_ptr<ExtensionViewHost> host( 52 ExtensionViewHostFactory::CreateDialogHost(extension->url(), 53 browser()->profile())); 54 EXPECT_EQ(extension.get(), host->extension()); 55 EXPECT_EQ(browser_context, host->browser_context()); 56 EXPECT_EQ(VIEW_TYPE_EXTENSION_DIALOG, host->extension_host_type()); 57 EXPECT_TRUE(host->view()); 58 } 59 } 60 61 // Tests that extensions loaded in incognito mode have the correct profiles 62 // for split-mode and non-split-mode. 63 // Crashing intermittently on multiple platforms. http://crbug.com/316334 64 IN_PROC_BROWSER_TEST_F(ExtensionViewHostFactoryTest, 65 DISABLED_IncognitoExtensionHosts) { 66 // Open an incognito browser. 67 Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord( 68 browser()->profile(), GURL("about:blank")); 69 70 // Load a non-split-mode extension, enabled in incognito. 71 scoped_refptr<const Extension> regular_extension = 72 LoadExtensionIncognito(test_data_dir_.AppendASCII("api_test") 73 .AppendASCII("browser_action") 74 .AppendASCII("none")); 75 ASSERT_TRUE(regular_extension.get()); 76 77 // The ExtensionHost for a regular extension in an incognito window is 78 // associated with the original window's profile. 79 scoped_ptr<ExtensionHost> regular_host( 80 ExtensionViewHostFactory::CreatePopupHost( 81 regular_extension->url(), incognito_browser)); 82 content::BrowserContext* browser_context = browser()->profile(); 83 EXPECT_EQ(browser_context, regular_host->browser_context()); 84 85 // Load a split-mode incognito extension. 86 scoped_refptr<const Extension> split_mode_extension = 87 LoadExtensionIncognito(test_data_dir_.AppendASCII("api_test") 88 .AppendASCII("browser_action") 89 .AppendASCII("split_mode")); 90 ASSERT_TRUE(split_mode_extension.get()); 91 92 // The ExtensionHost for a split-mode extension is associated with the 93 // incognito profile. 94 scoped_ptr<ExtensionHost> split_mode_host( 95 ExtensionViewHostFactory::CreatePopupHost( 96 split_mode_extension->url(), incognito_browser)); 97 content::BrowserContext* incognito_context = incognito_browser->profile(); 98 EXPECT_EQ(incognito_context, split_mode_host->browser_context()); 99 } 100 101 } // namespace extensions 102