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_action_manager.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/extensions/extension_tab_util.h"
8 #include "chrome/browser/extensions/test_extension_dir.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/common/extensions/features/feature_channel.h"
11 #include "components/version_info/version_info.h"
12 #include "extensions/test/extension_test_message_listener.h"
13 #include "ui/gfx/image/image.h"
15 namespace extensions
{
18 const char kDeclarativeContentManifest
[] =
20 " \"name\": \"Declarative Content apitest\",\n"
21 " \"version\": \"0.1\",\n"
22 " \"manifest_version\": 2,\n"
23 " \"description\": \n"
24 " \"end-to-end browser test for the declarative Content API\",\n"
25 " \"background\": {\n"
26 " \"scripts\": [\"background.js\"]\n"
28 " \"page_action\": {},\n"
29 " \"permissions\": [\n"
30 " \"declarativeContent\"\n"
34 class SetIconAPITest
: public ExtensionApiTest
{
37 // Set the channel to "trunk" since declarativeContent is restricted
39 : current_channel_(version_info::Channel::UNKNOWN
) {
41 ~SetIconAPITest() override
{}
43 extensions::ScopedCurrentChannel current_channel_
;
44 TestExtensionDir ext_dir_
;
47 IN_PROC_BROWSER_TEST_F(SetIconAPITest
, Overview
) {
48 ext_dir_
.WriteManifest(kDeclarativeContentManifest
);
50 FILE_PATH_LITERAL("background.js"),
51 "var declarative = chrome.declarative;\n"
53 "var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
54 "var SetIcon = chrome.declarativeContent.SetIcon;\n"
56 "var canvas = document.createElement(\'canvas\');\n"
57 "var ctx = canvas.getContext(\"2d\");"
58 "var imageData = ctx.createImageData(19,19);\n"
61 " conditions: [new PageStateMatcher({\n"
62 " pageUrl: {hostPrefix: \"test1\"}})],\n"
63 " actions: [new SetIcon({\"imageData\": imageData})]\n"
66 "var testEvent = chrome.declarativeContent.onPageChanged;\n"
68 "testEvent.removeRules(undefined, function() {\n"
69 " testEvent.addRules([rule0], function() {\n"
70 " chrome.test.sendMessage(\"ready\", function(reply) {\n"
74 ExtensionTestMessageListener
ready("ready", true);
75 const Extension
* extension
= LoadExtension(ext_dir_
.unpacked_path());
76 ASSERT_TRUE(extension
);
77 const ExtensionAction
* page_action
=
78 ExtensionActionManager::Get(browser()->profile())->
79 GetPageAction(*extension
);
80 ASSERT_TRUE(page_action
);
82 ASSERT_TRUE(ready
.WaitUntilSatisfied());
83 content::WebContents
* const tab
=
84 browser()->tab_strip_model()->GetWebContentsAt(0);
85 const int tab_id
= ExtensionTabUtil::GetTabId(tab
);
87 // There should be no declarative icon until we navigate to a matched page.
88 EXPECT_TRUE(page_action
->GetDeclarativeIcon(tab_id
).IsEmpty());
89 NavigateInRenderer(tab
, GURL("http://test1/"));
90 EXPECT_FALSE(page_action
->GetDeclarativeIcon(tab_id
).IsEmpty());
92 // Navigating to an unmatched page should reset the icon.
93 NavigateInRenderer(tab
, GURL("http://test2/"));
94 EXPECT_TRUE(page_action
->GetDeclarativeIcon(tab_id
).IsEmpty());
97 } // namespace extensions