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