summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/declarative/rules_registry_service_unittest.cc
blob: b2ff97edb62741a0a744613c2237936d248172ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/declarative/rules_registry_service.h"

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
#include "extensions/browser/api/declarative/test_rules_registry.h"
#include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
const char kExtensionId[] = "foo";

void InsertRule(scoped_refptr<extensions::RulesRegistry> registry,
                const std::string& id) {
  std::vector<linked_ptr<extensions::RulesRegistry::Rule> > add_rules;
  add_rules.push_back(make_linked_ptr(new extensions::RulesRegistry::Rule));
  add_rules[0]->id.reset(new std::string(id));
  std::string error = registry->AddRules(kExtensionId, add_rules);
  EXPECT_TRUE(error.empty());
}

void VerifyNumberOfRules(scoped_refptr<extensions::RulesRegistry> registry,
                         size_t expected_number_of_rules) {
  std::vector<linked_ptr<extensions::RulesRegistry::Rule> > get_rules;
  registry->GetAllRules(kExtensionId, &get_rules);
  EXPECT_EQ(expected_number_of_rules, get_rules.size());
}

}  // namespace

namespace extensions {

class RulesRegistryServiceTest : public testing::Test {
 public:
  RulesRegistryServiceTest()
      : ui_(content::BrowserThread::UI, &message_loop_),
        io_(content::BrowserThread::IO, &message_loop_) {}

  virtual ~RulesRegistryServiceTest() {}

  virtual void TearDown() override {
    // Make sure that deletion traits of all registries are executed.
    message_loop_.RunUntilIdle();
  }

 protected:
  base::MessageLoop message_loop_;
  content::TestBrowserThread ui_;
  content::TestBrowserThread io_;
};

TEST_F(RulesRegistryServiceTest, TestConstructionAndMultiThreading) {
  const RulesRegistry::WebViewKey key(0, 0);
  TestRulesRegistry* ui_registry =
      new TestRulesRegistry(content::BrowserThread::UI, "ui", key);

  TestRulesRegistry* io_registry =
      new TestRulesRegistry(content::BrowserThread::IO, "io", key);

  // Test registration.

  RulesRegistryService registry_service(NULL);
  registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry));
  registry_service.RegisterRulesRegistry(make_scoped_refptr(io_registry));

  EXPECT_TRUE(registry_service.GetRulesRegistry(key, "ui").get());
  EXPECT_TRUE(registry_service.GetRulesRegistry(key, "io").get());
  EXPECT_FALSE(registry_service.GetRulesRegistry(key, "foo").get());

  content::BrowserThread::PostTask(
      content::BrowserThread::UI, FROM_HERE,
      base::Bind(&InsertRule, registry_service.GetRulesRegistry(key, "ui"),
                 "ui_task"));

  content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::Bind(&InsertRule, registry_service.GetRulesRegistry(key, "io"),
                   "io_task"));

  content::BrowserThread::PostTask(
        content::BrowserThread::UI, FROM_HERE,
        base::Bind(&VerifyNumberOfRules,
                   registry_service.GetRulesRegistry(key, "ui"), 1));

  content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::Bind(&VerifyNumberOfRules,
                   registry_service.GetRulesRegistry(key, "io"), 1));

  message_loop_.RunUntilIdle();

  // Test extension uninstalling.

  registry_service.SimulateExtensionUninstalled(kExtensionId);

  content::BrowserThread::PostTask(
        content::BrowserThread::UI, FROM_HERE,
        base::Bind(&VerifyNumberOfRules,
                   registry_service.GetRulesRegistry(key, "ui"), 0));

  content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::Bind(&VerifyNumberOfRules,
                   registry_service.GetRulesRegistry(key, "io"), 0));

  message_loop_.RunUntilIdle();
}

// This test verifies that removing rules registries by process ID works as
// intended. This test ensures that removing registries associated with one
// Webview embedder process does not remove registries associated with the
// other.
TEST_F(RulesRegistryServiceTest, TestWebViewKey) {
  const int kEmbedderProcessID1 = 1;
  const int kEmbedderProcessID2 = 2;
  const int kWebViewInstanceID = 1;

  const RulesRegistry::WebViewKey key1(kEmbedderProcessID1, kWebViewInstanceID);
  const RulesRegistry::WebViewKey key2(kEmbedderProcessID2, kWebViewInstanceID);

  TestRulesRegistry* ui_registry_key1 =
      new TestRulesRegistry(content::BrowserThread::UI, "ui", key1);
  TestRulesRegistry* ui_registry_key2 =
      new TestRulesRegistry(content::BrowserThread::UI, "ui", key2);

  RulesRegistryService registry_service(NULL);
  registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry_key1));
  registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry_key2));

  content::BrowserThread::PostTask(
      content::BrowserThread::UI, FROM_HERE,
      base::Bind(&InsertRule, registry_service.GetRulesRegistry(key1, "ui"),
                 "ui_task"));
  content::BrowserThread::PostTask(
      content::BrowserThread::UI, FROM_HERE,
      base::Bind(&InsertRule, registry_service.GetRulesRegistry(key2, "ui"),
                 "ui_task"));
  message_loop_.RunUntilIdle();

  registry_service.RemoveWebViewRulesRegistries(kEmbedderProcessID1);
  EXPECT_FALSE(registry_service.GetRulesRegistry(key1, "ui").get());
  EXPECT_TRUE(registry_service.GetRulesRegistry(key2, "ui").get());
}

TEST_F(RulesRegistryServiceTest, TestWebViewWebRequestRegistryHasNoCache) {
  const int kEmbedderProcessID = 1;
  const int kWebViewInstanceID = 1;
  const RulesRegistry::WebViewKey key(kEmbedderProcessID, kWebViewInstanceID);
  TestingProfile profile;
  RulesRegistryService registry_service(&profile);
  RulesRegistry* registry =
      registry_service.GetRulesRegistry(
          key,
          declarative_webrequest_constants::kOnRequest).get();
  EXPECT_TRUE(registry);
  EXPECT_FALSE(registry->rules_cache_delegate_for_testing());
}

}  // namespace extensions