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.
4
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 "extensions/test/extension_test_message_listener.h"
12#include "ui/gfx/image/image_skia.h"
13
14namespace extensions {
15namespace {
16
17const char kDeclarativeContentManifest[] =
18    "{\n"
19    "  \"name\": \"Declarative Content apitest\",\n"
20    "  \"version\": \"0.1\",\n"
21    "  \"manifest_version\": 2,\n"
22    "  \"description\": \n"
23    "      \"end-to-end browser test for the declarative Content API\",\n"
24    "  \"background\": {\n"
25    "    \"scripts\": [\"background.js\"]\n"
26    "  },\n"
27    "  \"page_action\": {},\n"
28    "  \"permissions\": [\n"
29    "    \"declarativeContent\"\n"
30    "  ]\n"
31    "}\n";
32
33class SetIconAPITest : public ExtensionApiTest {
34 public:
35  SetIconAPITest()
36      // Set the channel to "trunk" since declarativeContent is restricted
37      // to trunk.
38      : current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {
39  }
40  virtual ~SetIconAPITest() {}
41
42  extensions::ScopedCurrentChannel current_channel_;
43  TestExtensionDir ext_dir_;
44};
45
46IN_PROC_BROWSER_TEST_F(SetIconAPITest, Overview) {
47  ext_dir_.WriteManifest(kDeclarativeContentManifest);
48  ext_dir_.WriteFile(
49      FILE_PATH_LITERAL("background.js"),
50      "var declarative = chrome.declarative;\n"
51      "\n"
52      "var PageStateMatcher = chrome.declarativeContent.PageStateMatcher;\n"
53      "var SetIcon = chrome.declarativeContent.SetIcon;\n"
54      "\n"
55      "var canvas = document.createElement(\'canvas\');\n"
56      "var ctx = canvas.getContext(\"2d\");"
57      "var imageData = ctx.createImageData(19,19);\n"
58      "\n"
59      "var rule0 = {\n"
60      "  conditions: [new PageStateMatcher({\n"
61      "                   pageUrl: {hostPrefix: \"test1\"}})],\n"
62      "  actions: [new SetIcon({\"imageData\": imageData})]\n"
63      "}\n"
64      "\n"
65      "var testEvent = chrome.declarativeContent.onPageChanged;\n"
66      "\n"
67      "testEvent.removeRules(undefined, function() {\n"
68      "  testEvent.addRules([rule0], function() {\n"
69      "    chrome.test.sendMessage(\"ready\", function(reply) {\n"
70      "    })\n"
71      "  });\n"
72      "});\n");
73  ExtensionTestMessageListener ready("ready", true);
74  const Extension* extension = LoadExtension(ext_dir_.unpacked_path());
75  ASSERT_TRUE(extension);
76  const ExtensionAction* page_action =
77      ExtensionActionManager::Get(browser()->profile())->
78      GetPageAction(*extension);
79  ASSERT_TRUE(page_action);
80
81  ASSERT_TRUE(ready.WaitUntilSatisfied());
82  content::WebContents* const tab =
83      browser()->tab_strip_model()->GetWebContentsAt(0);
84  const int tab_id = ExtensionTabUtil::GetTabId(tab);
85
86  // There should be no declarative icon until we navigate to a matched page.
87  EXPECT_TRUE(page_action->GetDeclarativeIcon(tab_id).bitmap()->empty());
88  NavigateInRenderer(tab, GURL("http://test1/"));
89  EXPECT_FALSE(page_action->GetDeclarativeIcon(tab_id).bitmap()->empty());
90
91  // Navigating to an unmatched page should reset the icon.
92  NavigateInRenderer(tab, GURL("http://test2/"));
93  EXPECT_TRUE(page_action->GetDeclarativeIcon(tab_id).bitmap()->empty());
94}
95}  // namespace
96}  // namespace extensions
97