1 // Copyright (c) 2012 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 "chrome/browser/extensions/extension_browsertest.h"
6 #include "chrome/browser/extensions/test_extension_dir.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/common/url_constants.h"
10 #include "chrome/test/base/ui_test_utils.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/test/browser_test_utils.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/value_builder.h"
16 #include "extensions/test/extension_test_message_listener.h"
18 namespace extensions
{
20 // Used to simulate a click on the first element named 'Options'.
21 static const char kScriptClickOptionButton
[] =
23 " var button = document.querySelector('.options-link');"
27 // Test that an extension with an options page makes an 'Options' button appear
28 // on chrome://extensions, and that clicking the button opens a new tab with the
29 // extension's options page.
30 // Disabled because of flakiness. See http://crbug.com/174934.
31 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, DISABLED_OptionsPage
) {
32 ExtensionRegistry
* registry
= ExtensionRegistry::Get(browser()->profile());
33 size_t installed_extensions
= registry
->enabled_extensions().size();
34 // Install an extension with an options page.
35 const Extension
* extension
=
36 InstallExtension(test_data_dir_
.AppendASCII("options.crx"), 1);
37 ASSERT_TRUE(extension
);
38 EXPECT_EQ(installed_extensions
+ 1, registry
->enabled_extensions().size());
40 // Go to the Extension Settings page and click the Options button.
41 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIExtensionsURL
));
42 TabStripModel
* tab_strip
= browser()->tab_strip_model();
43 ui_test_utils::WindowedTabAddedNotificationObserver
observer(
44 content::NotificationService::AllSources());
45 // NOTE: Currently the above script needs to execute in an iframe. The
46 // selector for that iframe may break if the layout of the extensions
48 content::RenderFrameHost
* frame
= content::FrameMatchingPredicate(
49 tab_strip
->GetActiveWebContents(),
50 base::Bind(&content::FrameHasSourceUrl
,
51 GURL(chrome::kChromeUIExtensionsFrameURL
)));
52 EXPECT_TRUE(content::ExecuteScript(
54 kScriptClickOptionButton
));
56 EXPECT_EQ(2, tab_strip
->count());
58 EXPECT_EQ(extension
->GetResourceURL("options.html"),
59 tab_strip
->GetWebContentsAt(1)->GetURL());
62 // Tests that navigating directly to chrome://extensions?options=<id> to an
63 // extension with an embedded options page loads that extension's options page.
64 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
,
65 LoadChromeExtensionsWithOptionsParamWhenEmbedded
) {
66 TestExtensionDir extension_dir
;
67 extension_dir
.WriteFile(FILE_PATH_LITERAL("options.html"),
68 "<script src=\"options.js\"></script>\n");
69 extension_dir
.WriteFile(
70 FILE_PATH_LITERAL("options.js"),
71 "chrome.tabs.getCurrent(function(tab) {\n"
72 " chrome.test.sendMessage(tab ? 'tab' : 'embedded');\n"
74 extension_dir
.WriteManifest(
76 .Set("manifest_version", 2)
77 .Set("name", "Extension for options param test")
78 .Set("options_ui", DictionaryBuilder().Set("page", "options.html"))
82 ExtensionTestMessageListener
listener(false /* will_reply */);
83 scoped_refptr
<const Extension
> extension
=
84 InstallExtension(extension_dir
.Pack(), 1);
85 ASSERT_TRUE(extension
.get());
86 ui_test_utils::NavigateToURL(
87 browser(), GURL("chrome://extensions?options=" + extension
->id()));
88 ASSERT_TRUE(listener
.WaitUntilSatisfied());
89 ASSERT_EQ("embedded", listener
.message());
92 } // namespace extensions