Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / activity_log / uma_policy_unittest.cc
blobe7ba924213ecd90e2e8be2ab57edd675a5230d04
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.
5 #include "base/time/time.h"
6 #include "chrome/browser/extensions/activity_log/activity_action_constants.h"
7 #include "chrome/browser/extensions/activity_log/uma_policy.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "extensions/common/dom_action_types.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions {
15 class UmaPolicyTest : public testing::Test {
16 public:
17 UmaPolicyTest() {
18 profile_.reset(new TestingProfile());
21 protected:
22 content::TestBrowserThreadBundle thread_bundle_;
23 scoped_ptr<TestingProfile> profile_;
26 TEST_F(UmaPolicyTest, Construct) {
27 ActivityLogPolicy* policy = new UmaPolicy(profile_.get());
28 policy->Close();
31 TEST_F(UmaPolicyTest, MatchActionToStatusTest) {
32 UmaPolicy* policy = new UmaPolicy(profile_.get());
34 scoped_refptr<Action> action = new Action(
35 "id", base::Time::Now(), Action::ACTION_API_CALL, "extension.connect");
36 ASSERT_EQ(UmaPolicy::NONE, policy->MatchActionToStatus(action));
38 action = new Action(
39 "id", base::Time::Now(), Action::ACTION_API_CALL, "tabs.executeScript");
40 ASSERT_EQ(
41 (1 << UmaPolicy::CONTENT_SCRIPT), policy->MatchActionToStatus(action));
43 action = new Action(
44 "id", base::Time::Now(), Action::ACTION_CONTENT_SCRIPT, "");
45 ASSERT_TRUE(
46 (1 << UmaPolicy::CONTENT_SCRIPT) & policy->MatchActionToStatus(action));
47 ASSERT_EQ(
48 (1 << UmaPolicy::CONTENT_SCRIPT), policy->MatchActionToStatus(action));
50 action = new Action(
51 "id", base::Time::Now(), Action::ACTION_DOM_ACCESS, "Document.location");
52 action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
53 DomActionType::GETTER);
54 ASSERT_TRUE((1 << UmaPolicy::READ_DOM) & policy->MatchActionToStatus(action));
55 ASSERT_EQ((1 << UmaPolicy::READ_DOM), policy->MatchActionToStatus(action));
57 action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
58 DomActionType::SETTER);
59 ASSERT_TRUE(
60 (1 << UmaPolicy::MODIFIED_DOM) & policy->MatchActionToStatus(action));
61 ASSERT_EQ(
62 (1 << UmaPolicy::MODIFIED_DOM), policy->MatchActionToStatus(action));
64 action = new Action(
65 "id", base::Time::Now(), Action::ACTION_DOM_ACCESS, "HTMLDocument.write");
66 action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
67 DomActionType::METHOD);
68 ASSERT_TRUE(
69 (1 << UmaPolicy::DOCUMENT_WRITE) & policy->MatchActionToStatus(action));
70 ASSERT_TRUE(
71 (1 << UmaPolicy::DOM_METHOD) & policy->MatchActionToStatus(action));
73 action = new Action("id",
74 base::Time::Now(),
75 Action::ACTION_DOM_ACCESS,
76 "Document.createElement");
77 scoped_ptr<base::ListValue> args(new base::ListValue());
78 args->Set(0, new base::StringValue("script"));
79 action->set_args(args.Pass());
80 ASSERT_EQ(UmaPolicy::NONE, policy->MatchActionToStatus(action));
81 action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
82 DomActionType::METHOD);
83 ASSERT_TRUE(
84 (1 << UmaPolicy::CREATED_SCRIPT) & policy->MatchActionToStatus(action));
85 ASSERT_TRUE(
86 (1 << UmaPolicy::DOM_METHOD) & policy->MatchActionToStatus(action));
88 policy->Close();
91 TEST_F(UmaPolicyTest, SiteUrlTest) {
92 UmaPolicy* policy = new UmaPolicy(profile_.get());
94 const std::string site0 = "http://www.zzz.com/";
95 const std::string site1 = "http://www.foo.com/";
96 const std::string site2 = "http://www.google.com#a";
97 const std::string site3 = "http://www.google.com#bb";
99 // Record some opened sites.
100 policy->SetupOpenedPage(site1);
101 policy->SetupOpenedPage(site2);
102 policy->SetupOpenedPage(site2);
103 policy->SetupOpenedPage(site3);
104 policy->SetupOpenedPage(site3);
105 policy->SetupOpenedPage(site3);
107 // Check that site1, site2, and site3 were recorded.
108 ASSERT_EQ(3u, policy->url_status().size());
109 ASSERT_EQ(1, policy->url_status()[site1][UmaPolicy::kNumberOfTabs]);
110 ASSERT_EQ(2, policy->url_status()[site2][UmaPolicy::kNumberOfTabs]);
111 ASSERT_EQ(3, policy->url_status()[site3][UmaPolicy::kNumberOfTabs]);
113 // Remove some sites.
114 policy->CleanupClosedPage(site0, NULL);
115 policy->CleanupClosedPage(site2, NULL);
116 policy->CleanupClosedPage(site2, NULL);
117 policy->CleanupClosedPage(site3, NULL);
119 // Check that the removal worked.
120 ASSERT_EQ(2u, policy->url_status().size());
121 ASSERT_EQ(1, policy->url_status()[site1][UmaPolicy::kNumberOfTabs]);
122 ASSERT_EQ(2, policy->url_status()[site3][UmaPolicy::kNumberOfTabs]);
124 policy->Close();
127 TEST_F(UmaPolicyTest, ProcessActionTest) {
128 const std::string site_a = "http://www.zzz.com/";
129 const std::string site_b = "http://www.foo.com/";
130 const std::string ext_a = "a";
131 const std::string ext_b = "b";
132 UmaPolicy* policy = new UmaPolicy(profile_.get());
134 // Populate with a few different pages.
135 policy->SetupOpenedPage(site_a);
136 policy->SetupOpenedPage(site_b);
138 // Process a few actions for site_a.
139 scoped_refptr<Action> action1 = new Action(
140 ext_a, base::Time::Now(), Action::ACTION_CONTENT_SCRIPT, "");
141 action1->set_page_url(GURL(site_a));
142 policy->ProcessAction(action1);
144 scoped_refptr<Action> action2 = new Action(
145 ext_a, base::Time::Now(), Action::ACTION_CONTENT_SCRIPT, "");
146 action2->set_page_url(GURL(site_a));
147 policy->ProcessAction(action2);
149 scoped_refptr<Action> action3 = new Action(
150 ext_b, base::Time::Now(), Action::ACTION_DOM_ACCESS, "Document.location");
151 action3->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
152 DomActionType::GETTER);
153 action3->set_page_url(GURL(site_a));
154 policy->ProcessAction(action3);
156 // Process an action for site_b.
157 scoped_refptr<Action> action4 = new Action(
158 ext_a, base::Time::Now(), Action::ACTION_DOM_ACCESS, "Document.location");
159 action4->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
160 DomActionType::SETTER);
161 action4->set_page_url(GURL(site_b));
162 policy->ProcessAction(action4);
164 scoped_refptr<Action> action5 = new Action(
165 ext_b, base::Time::Now(), Action::ACTION_DOM_ACCESS, "Document.location");
166 action5->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
167 DomActionType::SETTER);
168 action5->set_page_url(GURL(site_b));
169 policy->ProcessAction(action5);
171 scoped_refptr<Action> action6 = new Action(
172 ext_b, base::Time::Now(), Action::ACTION_API_CALL, "tabs.executeScript");
173 action6->set_arg_url(GURL(site_b));
174 policy->ProcessAction(action6);
176 // Now check what's been recorded.
177 ASSERT_EQ(2u, policy->url_status().size());
179 ASSERT_EQ(3u, policy->url_status()[site_a].size());
180 ASSERT_TRUE(
181 (1 << UmaPolicy::CONTENT_SCRIPT) & policy->url_status()[site_a][ext_a]);
182 ASSERT_FALSE(
183 (1 << UmaPolicy::CONTENT_SCRIPT) & policy->url_status()[site_a][ext_b]);
184 ASSERT_TRUE((1 << UmaPolicy::READ_DOM) & policy->url_status()[site_a][ext_b]);
185 ASSERT_FALSE(
186 (1 << UmaPolicy::READ_DOM) & policy->url_status()[site_a][ext_a]);
188 ASSERT_EQ(3u, policy->url_status()[site_b].size());
189 ASSERT_TRUE(
190 (1 << UmaPolicy::MODIFIED_DOM) & policy->url_status()[site_b][ext_a]);
191 ASSERT_TRUE(
192 (1 << UmaPolicy::MODIFIED_DOM) & policy->url_status()[site_b][ext_b]);
193 ASSERT_TRUE(
194 (1 << UmaPolicy::CONTENT_SCRIPT) & policy->url_status()[site_b][ext_b]);
196 policy->Close();
199 TEST_F(UmaPolicyTest, CleanURLTest) {
200 ASSERT_EQ("http://www.google.com/",
201 UmaPolicy::CleanURL(GURL("http://www.google.com/")));
202 ASSERT_EQ("http://www.google.com/",
203 UmaPolicy::CleanURL(GURL("http://www.google.com")));
204 ASSERT_EQ("http://www.google.com:8080/a.html",
205 UmaPolicy::CleanURL(GURL("http://www.google.com:8080/a.html")));
206 ASSERT_EQ("http://www.google.com/",
207 UmaPolicy::CleanURL(GURL("http://www.google.com/#a")));
208 ASSERT_EQ("http://www.google.com/",
209 UmaPolicy::CleanURL(GURL("http://www.google.com/#aaaa")));
210 ASSERT_EQ("http://www.google.com/?q=a",
211 UmaPolicy::CleanURL(GURL("http://www.google.com/?q=a")));
213 ASSERT_EQ("http://www.cnn.com/",
214 UmaPolicy::CleanURL(GURL("http://www.cnn.com/")));
215 ASSERT_EQ("http://www.cnn.com:8080/a.html",
216 UmaPolicy::CleanURL(GURL("http://www.cnn.com:8080/a.html")));
217 ASSERT_EQ("http://www.cnn.com/",
218 UmaPolicy::CleanURL(GURL("http://www.cnn.com")));
219 ASSERT_EQ("http://www.cnn.com/",
220 UmaPolicy::CleanURL(GURL("http://www.cnn.com/#a")));
221 ASSERT_EQ("http://www.cnn.com/",
222 UmaPolicy::CleanURL(GURL("http://www.cnn.com/#aaaa")));
223 ASSERT_EQ("http://www.cnn.com/?q=a",
224 UmaPolicy::CleanURL(GURL("http://www.cnn.com/?q=a")));
227 } // namespace extensions