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/api/declarative_content/chrome_content_rules_registry.h"
9 #include "base/test/values_test_util.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/browser/extensions/test_extension_environment.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "components/bookmarks/test/bookmark_test_helpers.h"
15 #include "content/public/browser/navigation_details.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/frame_navigate_params.h"
18 #include "extensions/common/extension.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace extensions
{
24 class DeclarativeChromeContentRulesRegistryTest
: public testing::Test
{
26 DeclarativeChromeContentRulesRegistryTest() {
27 env_
.profile()->CreateBookmarkModel(true);
28 bookmarks::test::WaitForBookmarkModelToLoad(
29 BookmarkModelFactory::GetForProfile(env_
.profile()));
33 TestExtensionEnvironment
* env() { return &env_
; }
36 TestExtensionEnvironment env_
;
38 DISALLOW_COPY_AND_ASSIGN(DeclarativeChromeContentRulesRegistryTest
);
41 TEST_F(DeclarativeChromeContentRulesRegistryTest
, ActiveRulesDoesntGrow
) {
42 scoped_refptr
<ChromeContentRulesRegistry
> registry(
43 new ChromeContentRulesRegistry(env()->profile(), NULL
));
45 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
47 scoped_ptr
<content::WebContents
> tab
= env()->MakeTab();
48 registry
->MonitorWebContentsForRuleEvaluation(tab
.get());
49 registry
->DidNavigateMainFrame(tab
.get(), content::LoadCommittedDetails(),
50 content::FrameNavigateParams());
51 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
54 linked_ptr
<api::events::Rule
> rule(new api::events::Rule
);
55 api::events::Rule::Populate(
56 *base::test::ParseJson(
58 " \"id\": \"rule1\",\n"
59 " \"priority\": 100,\n"
60 " \"conditions\": [\n"
62 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
63 " \"css\": [\"input\"]\n"
66 " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n"
70 std::vector
<linked_ptr
<api::events::Rule
>> rules
;
71 rules
.push_back(rule
);
73 const Extension
* extension
= env()->MakeExtension(*base::test::ParseJson(
74 "{\"page_action\": {}}"));
75 registry
->AddRulesImpl(extension
->id(), rules
);
77 registry
->DidNavigateMainFrame(tab
.get(), content::LoadCommittedDetails(),
78 content::FrameNavigateParams());
79 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
81 std::vector
<std::string
> css_selectors
;
82 css_selectors
.push_back("input");
83 registry
->UpdateMatchingCssSelectorsForTesting(tab
.get(), css_selectors
);
84 EXPECT_EQ(1u, registry
->GetActiveRulesCountForTesting());
86 // Closing the tab should erase its entry from active_rules_.
88 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
90 tab
= env()->MakeTab();
91 registry
->MonitorWebContentsForRuleEvaluation(tab
.get());
92 registry
->UpdateMatchingCssSelectorsForTesting(tab
.get(), css_selectors
);
93 EXPECT_EQ(1u, registry
->GetActiveRulesCountForTesting());
94 // Navigating the tab should erase its entry from active_rules_ if
95 // it no longer matches.
96 registry
->DidNavigateMainFrame(tab
.get(), content::LoadCommittedDetails(),
97 content::FrameNavigateParams());
98 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
101 } // namespace extensions