12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/api/declarative_content/content_rules_registry.h" 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string> 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/test/values_test_util.h" 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/extension_tab_util.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/test_extension_environment.h" 12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/test/base/testing_profile.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_details.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/frame_navigate_params.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h" 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions { 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using base::test::ParseJson; 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using testing::HasSubstr; 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using content::WebContents; 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Must be outside the anonymous namespace to be a friend of 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ContentRulesRegistry. 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class DeclarativeContentRulesRegistryTest : public testing::Test { 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected: 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static const std::map<int, std::set<ContentRule*> >& active_rules( 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const ContentRulesRegistry& registry) { 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return registry.active_rules_; 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace { 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(DeclarativeContentRulesRegistryTest, ActiveRulesDoesntGrow) { 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TestExtensionEnvironment env; 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_refptr<ContentRulesRegistry> registry( 410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) new ContentRulesRegistry(env.profile(), NULL)); 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EXPECT_EQ(0u, active_rules(*registry.get()).size()); 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::LoadCommittedDetails load_details; 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::FrameNavigateParams navigate_params; 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_ptr<WebContents> tab = env.MakeTab(); 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); 49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EXPECT_EQ(0u, active_rules(*registry.get()).size()); 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Add a rule. 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) RulesRegistry::Rule::Populate( 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *ParseJson( 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "{\n" 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"id\": \"rule1\",\n" 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"priority\": 100,\n" 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"conditions\": [\n" 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " {\n" 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"css\": [\"input\"]\n" 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " }],\n" 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"actions\": [\n" 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n" 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " ]\n" 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "}"), 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule.get()); 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules; 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rules.push_back(rule); 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const Extension* extension = env.MakeExtension(*ParseJson( 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "{\"page_action\": {}}")); 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->AddRulesImpl(extension->id(), rules); 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EXPECT_EQ(0u, active_rules(*registry.get()).size()); 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<std::string> css_selectors; 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) css_selectors.push_back("input"); 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->Apply(tab.get(), css_selectors); 81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EXPECT_EQ(1u, active_rules(*registry.get()).size()); 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Closing the tab should erase its entry from active_rules_. 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) tab.reset(); 85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EXPECT_EQ(0u, active_rules(*registry.get()).size()); 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) tab = env.MakeTab(); 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->Apply(tab.get(), css_selectors); 89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EXPECT_EQ(1u, active_rules(*registry.get()).size()); 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Navigating the tab should erase its entry from active_rules_ if 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // it no longer matches. 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EXPECT_EQ(0u, active_rules(*registry.get()).size()); 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace extensions 98