This source file includes following definitions.
- TEST
- TEST
#include "chrome/browser/extensions/api/declarative/rules_registry.h"
#include <algorithm>
#include "base/message_loop/message_loop.h"
#include "chrome/browser/extensions/api/declarative/test_rules_registry.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kExtensionId[] = "foobar";
const char kRuleId[] = "foo";
}
namespace extensions {
TEST(RulesRegistryTest, FillOptionalIdentifiers) {
base::MessageLoopForUI message_loop;
content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop);
const RulesRegistry::WebViewKey key(0, 0);
std::string error;
scoped_refptr<RulesRegistry> registry =
new TestRulesRegistry(content::BrowserThread::UI, "" , key);
std::vector<linked_ptr<RulesRegistry::Rule> > add_rules;
add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule));
add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule));
error = registry->AddRules(kExtensionId, add_rules);
EXPECT_TRUE(error.empty()) << error;
std::vector<linked_ptr<RulesRegistry::Rule> > get_rules;
registry->GetAllRules(kExtensionId, &get_rules);
ASSERT_EQ(2u, get_rules.size());
ASSERT_TRUE(get_rules[0]->id.get());
EXPECT_NE("", *get_rules[0]->id);
ASSERT_TRUE(get_rules[1]->id.get());
EXPECT_NE("", *get_rules[1]->id);
EXPECT_NE(*get_rules[0]->id, *get_rules[1]->id);
EXPECT_EQ(1u + 2u ,
registry->GetNumberOfUsedRuleIdentifiersForTesting());
std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_2;
add_rules_2.push_back(make_linked_ptr(new RulesRegistry::Rule));
add_rules_2[0]->id.reset(new std::string(*get_rules[0]->id));
error = registry->AddRules(kExtensionId, add_rules_2);
EXPECT_FALSE(error.empty());
std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_2;
registry->GetAllRules(kExtensionId, &get_rules_2);
ASSERT_EQ(2u, get_rules_2.size());
EXPECT_EQ(1u + 2u ,
registry->GetNumberOfUsedRuleIdentifiersForTesting());
std::vector<std::string> remove_rules_3;
remove_rules_3.push_back(*get_rules[0]->id);
error = registry->RemoveRules(kExtensionId, remove_rules_3);
EXPECT_TRUE(error.empty()) << error;
EXPECT_EQ(1u + 1u ,
registry->GetNumberOfUsedRuleIdentifiersForTesting());
std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_3a;
registry->GetAllRules(kExtensionId, &get_rules_3a);
ASSERT_EQ(1u, get_rules_3a.size());
std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_3;
add_rules_3.push_back(make_linked_ptr(new RulesRegistry::Rule));
add_rules_3[0]->id.reset(new std::string(*get_rules[0]->id));
error = registry->AddRules(kExtensionId, add_rules_3);
EXPECT_TRUE(error.empty()) << error;
EXPECT_EQ(1u + 2u ,
registry->GetNumberOfUsedRuleIdentifiersForTesting());
std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_3b;
registry->GetAllRules(kExtensionId, &get_rules_3b);
ASSERT_EQ(2u, get_rules_3b.size());
error = registry->RemoveAllRules(kExtensionId);
EXPECT_TRUE(error.empty()) << error;
EXPECT_EQ(0u + 0u ,
registry->GetNumberOfUsedRuleIdentifiersForTesting());
std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_4a;
registry->GetAllRules(kExtensionId, &get_rules_4a);
ASSERT_TRUE(get_rules_4a.empty());
std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_4;
add_rules_4.push_back(make_linked_ptr(new RulesRegistry::Rule));
add_rules_4[0]->id.reset(new std::string(kRuleId));
error = registry->AddRules(kExtensionId, add_rules_4);
EXPECT_TRUE(error.empty()) << error;
EXPECT_EQ(1u + 1u ,
registry->GetNumberOfUsedRuleIdentifiersForTesting());
std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_4b;
registry->GetAllRules(kExtensionId, &get_rules_4b);
ASSERT_EQ(1u, get_rules_4b.size());
ASSERT_TRUE(get_rules_4b[0]->id.get());
EXPECT_EQ(kRuleId, *get_rules_4b[0]->id);
registry->OnExtensionUninstalled(kExtensionId);
EXPECT_EQ(0u + 0u ,
registry->GetNumberOfUsedRuleIdentifiersForTesting());
registry = NULL;
message_loop.RunUntilIdle();
}
TEST(RulesRegistryTest, FillOptionalPriority) {
base::MessageLoopForUI message_loop;
content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop);
const RulesRegistry::WebViewKey key(0, 0);
std::string error;
scoped_refptr<RulesRegistry> registry =
new TestRulesRegistry(content::BrowserThread::UI, "" , key);
std::vector<linked_ptr<RulesRegistry::Rule> > add_rules;
add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule));
add_rules[0]->priority.reset(new int(2));
add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule));
error = registry->AddRules(kExtensionId, add_rules);
EXPECT_TRUE(error.empty()) << error;
std::vector<linked_ptr<RulesRegistry::Rule> > get_rules;
registry->GetAllRules(kExtensionId, &get_rules);
ASSERT_EQ(2u, get_rules.size());
ASSERT_TRUE(get_rules[0]->priority.get());
ASSERT_TRUE(get_rules[1]->priority.get());
EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2);
EXPECT_EQ(2, std::min(*get_rules[0]->priority, *get_rules[1]->priority));
EXPECT_EQ(RulesRegistry::DEFAULT_PRIORITY,
std::max(*get_rules[0]->priority, *get_rules[1]->priority));
registry = NULL;
message_loop.RunUntilIdle();
}
}