Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / declarative_content / chrome_content_rules_registry_unittest.cc
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/api/declarative_content/chrome_content_rules_registry.h"
6
7 #include <string>
8
9 #include "base/test/values_test_util.h"
10 #include "chrome/browser/extensions/extension_tab_util.h"
11 #include "chrome/browser/extensions/test_extension_environment.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/browser/navigation_details.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/frame_navigate_params.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace extensions {
20
21 using base::test::ParseJson;
22 using testing::HasSubstr;
23 using content::WebContents;
24
25 // Must be outside the anonymous namespace to be a friend of
26 // ContentRulesRegistry.
27 class DeclarativeChromeContentRulesRegistryTest : public testing::Test {
28  protected:
29   static const std::map<int, std::set<ContentRule*> >& active_rules(
30       const ChromeContentRulesRegistry& registry) {
31     return registry.active_rules_;
32   }
33 };
34
35 namespace {
36
37 TEST_F(DeclarativeChromeContentRulesRegistryTest, ActiveRulesDoesntGrow) {
38   TestExtensionEnvironment env;
39
40   scoped_refptr<ChromeContentRulesRegistry> registry(
41       new ChromeContentRulesRegistry(env.profile(), NULL));
42
43   EXPECT_EQ(0u, active_rules(*registry.get()).size());
44
45   content::LoadCommittedDetails load_details;
46   content::FrameNavigateParams navigate_params;
47   scoped_ptr<WebContents> tab = env.MakeTab();
48   registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params);
49   EXPECT_EQ(0u, active_rules(*registry.get()).size());
50
51   // Add a rule.
52   linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule);
53   RulesRegistry::Rule::Populate(
54       *ParseJson(
55           "{\n"
56           "  \"id\": \"rule1\",\n"
57           "  \"priority\": 100,\n"
58           "  \"conditions\": [\n"
59           "    {\n"
60           "      \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
61           "      \"css\": [\"input\"]\n"
62           "    }],\n"
63           "  \"actions\": [\n"
64           "    { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n"
65           "  ]\n"
66           "}"),
67       rule.get());
68   std::vector<linked_ptr<RulesRegistry::Rule> > rules;
69   rules.push_back(rule);
70
71   const Extension* extension = env.MakeExtension(*ParseJson(
72       "{\"page_action\": {}}"));
73   registry->AddRulesImpl(extension->id(), rules);
74
75   registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params);
76   EXPECT_EQ(0u, active_rules(*registry.get()).size());
77
78   std::vector<std::string> css_selectors;
79   css_selectors.push_back("input");
80   registry->Apply(tab.get(), css_selectors);
81   EXPECT_EQ(1u, active_rules(*registry.get()).size());
82
83   // Closing the tab should erase its entry from active_rules_.
84   tab.reset();
85   EXPECT_EQ(0u, active_rules(*registry.get()).size());
86
87   tab = env.MakeTab();
88   registry->Apply(tab.get(), css_selectors);
89   EXPECT_EQ(1u, active_rules(*registry.get()).size());
90   // Navigating the tab should erase its entry from active_rules_ if
91   // it no longer matches.
92   registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params);
93   EXPECT_EQ(0u, active_rules(*registry.get()).size());
94 }
95
96 }  // namespace
97 }  // namespace extensions