| OLD | NEW |
| (Empty) | |
| 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/initializing_rules_registry.
h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/extensions/api/declarative/test_rules_registry.h" |
| 11 #include "content/test/test_browser_thread.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 const char kExtensionId[] = "foobar"; |
| 16 const char kRuleId[] = "foo"; |
| 17 } // namespace |
| 18 |
| 19 namespace extensions { |
| 20 |
| 21 TEST(InitializingRulesRegistryTest, FillOptionalIdentifiers) { |
| 22 MessageLoopForUI message_loop; |
| 23 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| 24 |
| 25 std::string error; |
| 26 scoped_refptr<RulesRegistry> registry = |
| 27 new InitializingRulesRegistry(new TestRulesRegistry); |
| 28 |
| 29 // Add rules and check that their identifiers are filled and unique. |
| 30 |
| 31 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; |
| 32 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 33 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 34 error = registry->AddRules(kExtensionId, add_rules); |
| 35 EXPECT_TRUE(error.empty()); |
| 36 |
| 37 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules; |
| 38 error = registry->GetAllRules(kExtensionId, &get_rules); |
| 39 EXPECT_TRUE(error.empty()); |
| 40 |
| 41 ASSERT_EQ(2u, get_rules.size()); |
| 42 |
| 43 ASSERT_TRUE(get_rules[0]->id.get()); |
| 44 EXPECT_NE("", *get_rules[0]->id); |
| 45 |
| 46 ASSERT_TRUE(get_rules[1]->id.get()); |
| 47 EXPECT_NE("", *get_rules[1]->id); |
| 48 |
| 49 EXPECT_NE(*get_rules[0]->id, *get_rules[1]->id); |
| 50 |
| 51 // Check that we cannot add a new rule with the same ID. |
| 52 |
| 53 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_2; |
| 54 add_rules_2.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 55 add_rules_2[0]->id.reset(new std::string(*get_rules[0]->id)); |
| 56 error = registry->AddRules(kExtensionId, add_rules_2); |
| 57 EXPECT_FALSE(error.empty()); |
| 58 |
| 59 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_2; |
| 60 error = registry->GetAllRules(kExtensionId, &get_rules_2); |
| 61 EXPECT_TRUE(error.empty()); |
| 62 ASSERT_EQ(2u, get_rules_2.size()); |
| 63 |
| 64 // Check that we can register the old rule IDs once they were unregistered. |
| 65 |
| 66 std::vector<std::string> remove_rules_3; |
| 67 remove_rules_3.push_back(*get_rules[0]->id); |
| 68 error = registry->RemoveRules(kExtensionId, remove_rules_3); |
| 69 EXPECT_TRUE(error.empty()); |
| 70 |
| 71 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_3a; |
| 72 error = registry->GetAllRules(kExtensionId, &get_rules_3a); |
| 73 EXPECT_TRUE(error.empty()); |
| 74 ASSERT_EQ(1u, get_rules_3a.size()); |
| 75 |
| 76 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_3; |
| 77 add_rules_3.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 78 add_rules_3[0]->id.reset(new std::string(*get_rules[0]->id)); |
| 79 error = registry->AddRules(kExtensionId, add_rules_3); |
| 80 EXPECT_TRUE(error.empty()); |
| 81 |
| 82 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_3b; |
| 83 error = registry->GetAllRules(kExtensionId, &get_rules_3b); |
| 84 EXPECT_TRUE(error.empty()); |
| 85 ASSERT_EQ(2u, get_rules_3b.size()); |
| 86 |
| 87 // Check that we can register a rule with an ID that is not modified. |
| 88 |
| 89 error = registry->RemoveAllRules(kExtensionId); |
| 90 EXPECT_TRUE(error.empty()); |
| 91 |
| 92 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_4a; |
| 93 error = registry->GetAllRules(kExtensionId, &get_rules_4a); |
| 94 EXPECT_TRUE(error.empty()); |
| 95 ASSERT_TRUE(get_rules_4a.empty()); |
| 96 |
| 97 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_4; |
| 98 add_rules_4.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 99 add_rules_4[0]->id.reset(new std::string(kRuleId)); |
| 100 error = registry->AddRules(kExtensionId, add_rules_4); |
| 101 EXPECT_TRUE(error.empty()); |
| 102 |
| 103 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_4b; |
| 104 error = registry->GetAllRules(kExtensionId, &get_rules_4b); |
| 105 EXPECT_TRUE(error.empty()); |
| 106 |
| 107 ASSERT_EQ(1u, get_rules_4b.size()); |
| 108 |
| 109 ASSERT_TRUE(get_rules_4b[0]->id.get()); |
| 110 EXPECT_EQ(kRuleId, *get_rules_4b[0]->id); |
| 111 |
| 112 // Make sure that deletion traits of registry are executed. |
| 113 registry = NULL; |
| 114 message_loop.RunAllPending(); |
| 115 } |
| 116 |
| 117 TEST(InitializingRulesRegistryTest, FillOptionalPriority) { |
| 118 MessageLoopForUI message_loop; |
| 119 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| 120 |
| 121 std::string error; |
| 122 scoped_refptr<RulesRegistry> registry = |
| 123 new InitializingRulesRegistry(new TestRulesRegistry); |
| 124 |
| 125 // Add rules and check that their priorities are filled if they are empty. |
| 126 |
| 127 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; |
| 128 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 129 add_rules[0]->priority.reset(new int(2)); |
| 130 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 131 error = registry->AddRules(kExtensionId, add_rules); |
| 132 EXPECT_TRUE(error.empty()); |
| 133 |
| 134 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules; |
| 135 error = registry->GetAllRules(kExtensionId, &get_rules); |
| 136 EXPECT_TRUE(error.empty()); |
| 137 |
| 138 ASSERT_EQ(2u, get_rules.size()); |
| 139 |
| 140 ASSERT_TRUE(get_rules[0]->priority.get()); |
| 141 ASSERT_TRUE(get_rules[1]->priority.get()); |
| 142 |
| 143 // Verify the precondition so that the following EXPECT_EQ statements work. |
| 144 EXPECT_GT(InitializingRulesRegistry::DEFAULT_PRIORITY, 2); |
| 145 EXPECT_EQ(2, std::min(*get_rules[0]->priority, *get_rules[1]->priority)); |
| 146 EXPECT_EQ(InitializingRulesRegistry::DEFAULT_PRIORITY, |
| 147 std::max(*get_rules[0]->priority, *get_rules[1]->priority)); |
| 148 |
| 149 // Make sure that deletion traits of registry are executed. |
| 150 registry = NULL; |
| 151 message_loop.RunAllPending(); |
| 152 } |
| 153 |
| 154 } // namespace extensions |
| OLD | NEW |