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/common/extensions/dom_action_types.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions
{
14 class UmaPolicyTest
: public testing::Test
{
17 profile_
.reset(new TestingProfile());
21 scoped_ptr
<TestingProfile
> profile_
;
24 TEST_F(UmaPolicyTest
, Construct
) {
25 ActivityLogPolicy
* policy
= new UmaPolicy(profile_
.get());
29 TEST_F(UmaPolicyTest
, MatchActionToStatusTest
) {
30 UmaPolicy
* policy
= new UmaPolicy(profile_
.get());
32 scoped_refptr
<Action
> action
= new Action(
33 "id", base::Time::Now(), Action::ACTION_API_CALL
, "extension.connect");
34 ASSERT_EQ(UmaPolicy::NONE
, policy
->MatchActionToStatus(action
));
37 "id", base::Time::Now(), Action::ACTION_API_CALL
, "tabs.executeScript");
39 (1 << UmaPolicy::CONTENT_SCRIPT
), policy
->MatchActionToStatus(action
));
42 "id", base::Time::Now(), Action::ACTION_CONTENT_SCRIPT
, "");
44 (1 << UmaPolicy::CONTENT_SCRIPT
) & policy
->MatchActionToStatus(action
));
46 (1 << UmaPolicy::CONTENT_SCRIPT
), policy
->MatchActionToStatus(action
));
49 "id", base::Time::Now(), Action::ACTION_DOM_ACCESS
, "Document.location");
50 action
->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb
,
51 DomActionType::GETTER
);
52 ASSERT_TRUE((1 << UmaPolicy::READ_DOM
) & policy
->MatchActionToStatus(action
));
53 ASSERT_EQ((1 << UmaPolicy::READ_DOM
), policy
->MatchActionToStatus(action
));
55 action
->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb
,
56 DomActionType::SETTER
);
58 (1 << UmaPolicy::MODIFIED_DOM
) & policy
->MatchActionToStatus(action
));
60 (1 << UmaPolicy::MODIFIED_DOM
), policy
->MatchActionToStatus(action
));
63 "id", base::Time::Now(), Action::ACTION_DOM_ACCESS
, "HTMLDocument.write");
64 action
->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb
,
65 DomActionType::METHOD
);
67 (1 << UmaPolicy::DOCUMENT_WRITE
) & policy
->MatchActionToStatus(action
));
69 (1 << UmaPolicy::DOM_METHOD
) & policy
->MatchActionToStatus(action
));
71 action
= new Action("id",
73 Action::ACTION_DOM_ACCESS
,
74 "Document.createElement");
75 scoped_ptr
<base::ListValue
> args(new base::ListValue());
76 args
->Set(0, new base::StringValue("script"));
77 action
->set_args(args
.Pass());
78 ASSERT_EQ(UmaPolicy::NONE
, policy
->MatchActionToStatus(action
));
79 action
->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb
,
80 DomActionType::METHOD
);
82 (1 << UmaPolicy::CREATED_SCRIPT
) & policy
->MatchActionToStatus(action
));
84 (1 << UmaPolicy::DOM_METHOD
) & policy
->MatchActionToStatus(action
));
89 TEST_F(UmaPolicyTest
, SiteUrlTest
) {
90 UmaPolicy
* policy
= new UmaPolicy(profile_
.get());
92 const std::string site0
= "http://www.zzz.com";
93 const std::string site1
= "http://www.foo.com";
94 const std::string site2
= "http://www.google.com#a";
95 const std::string site3
= "http://www.google.com#bb";
97 // Record some opened sites.
98 policy
->SetupOpenedPage(site1
);
99 policy
->SetupOpenedPage(site2
);
100 policy
->SetupOpenedPage(site2
);
101 policy
->SetupOpenedPage(site3
);
102 policy
->SetupOpenedPage(site3
);
103 policy
->SetupOpenedPage(site3
);
105 // Check that site1, site2, and site3 were recorded.
106 ASSERT_EQ(3u, policy
->url_status().size());
107 ASSERT_EQ(1, policy
->url_status()[site1
][UmaPolicy::kNumberOfTabs
]);
108 ASSERT_EQ(2, policy
->url_status()[site2
][UmaPolicy::kNumberOfTabs
]);
109 ASSERT_EQ(3, policy
->url_status()[site3
][UmaPolicy::kNumberOfTabs
]);
111 // Remove some sites.
112 policy
->CleanupClosedPage(site0
);
113 policy
->CleanupClosedPage(site2
);
114 policy
->CleanupClosedPage(site2
);
115 policy
->CleanupClosedPage(site3
);
117 // Check that the removal worked.
118 ASSERT_EQ(2u, policy
->url_status().size());
119 ASSERT_EQ(1, policy
->url_status()[site1
][UmaPolicy::kNumberOfTabs
]);
120 ASSERT_EQ(2, policy
->url_status()[site3
][UmaPolicy::kNumberOfTabs
]);
125 TEST_F(UmaPolicyTest
, ProcessActionTest
) {
126 const std::string site_a
= "http://www.zzz.com/";
127 const std::string site_b
= "http://www.foo.com/";
128 const std::string ext_a
= "a";
129 const std::string ext_b
= "b";
130 UmaPolicy
* policy
= new UmaPolicy(profile_
.get());
132 // Populate with a few different pages.
133 policy
->SetupOpenedPage(site_a
);
134 policy
->SetupOpenedPage(site_b
);
136 // Process a few actions for site_a.
137 scoped_refptr
<Action
> action1
= new Action(
138 ext_a
, base::Time::Now(), Action::ACTION_CONTENT_SCRIPT
, "");
139 action1
->set_page_url(GURL(site_a
));
140 policy
->ProcessAction(action1
);
142 scoped_refptr
<Action
> action2
= new Action(
143 ext_a
, base::Time::Now(), Action::ACTION_CONTENT_SCRIPT
, "");
144 action2
->set_page_url(GURL(site_a
));
145 policy
->ProcessAction(action2
);
147 scoped_refptr
<Action
> action3
= new Action(
148 ext_b
, base::Time::Now(), Action::ACTION_DOM_ACCESS
, "Document.location");
149 action3
->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb
,
150 DomActionType::GETTER
);
151 action3
->set_page_url(GURL(site_a
));
152 policy
->ProcessAction(action3
);
154 // Process an action for site_b.
155 scoped_refptr
<Action
> action4
= new Action(
156 ext_a
, base::Time::Now(), Action::ACTION_DOM_ACCESS
, "Document.location");
157 action4
->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb
,
158 DomActionType::SETTER
);
159 action4
->set_page_url(GURL(site_b
));
160 policy
->ProcessAction(action4
);
162 scoped_refptr
<Action
> action5
= new Action(
163 ext_b
, base::Time::Now(), Action::ACTION_DOM_ACCESS
, "Document.location");
164 action5
->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb
,
165 DomActionType::SETTER
);
166 action5
->set_page_url(GURL(site_b
));
167 policy
->ProcessAction(action5
);
169 scoped_refptr
<Action
> action6
= new Action(
170 ext_b
, base::Time::Now(), Action::ACTION_API_CALL
, "tabs.executeScript");
171 action6
->set_arg_url(GURL(site_b
));
172 policy
->ProcessAction(action6
);
174 // Now check what's been recorded.
175 ASSERT_EQ(2u, policy
->url_status().size());
177 ASSERT_EQ(3u, policy
->url_status()[site_a
].size());
179 (1 << UmaPolicy::CONTENT_SCRIPT
) & policy
->url_status()[site_a
][ext_a
]);
181 (1 << UmaPolicy::CONTENT_SCRIPT
) & policy
->url_status()[site_a
][ext_b
]);
182 ASSERT_TRUE((1 << UmaPolicy::READ_DOM
) & policy
->url_status()[site_a
][ext_b
]);
184 (1 << UmaPolicy::READ_DOM
) & policy
->url_status()[site_a
][ext_a
]);
186 ASSERT_EQ(3u, policy
->url_status()[site_b
].size());
188 (1 << UmaPolicy::MODIFIED_DOM
) & policy
->url_status()[site_b
][ext_a
]);
190 (1 << UmaPolicy::MODIFIED_DOM
) & policy
->url_status()[site_b
][ext_b
]);
192 (1 << UmaPolicy::CONTENT_SCRIPT
) & policy
->url_status()[site_b
][ext_b
]);
197 TEST_F(UmaPolicyTest
, CleanURLTest
) {
198 ASSERT_EQ("http://www.google.com/",
199 UmaPolicy::CleanURL(GURL("http://www.google.com/")));
200 ASSERT_EQ("http://www.google.com/",
201 UmaPolicy::CleanURL(GURL("http://www.google.com")));
202 ASSERT_EQ("http://www.google.com:8080/a.html",
203 UmaPolicy::CleanURL(GURL("http://www.google.com:8080/a.html")));
204 ASSERT_EQ("http://www.google.com/",
205 UmaPolicy::CleanURL(GURL("http://www.google.com/#a")));
206 ASSERT_EQ("http://www.google.com/",
207 UmaPolicy::CleanURL(GURL("http://www.google.com/#aaaa")));
208 ASSERT_EQ("http://www.google.com/?q=a",
209 UmaPolicy::CleanURL(GURL("http://www.google.com/?q=a")));
211 ASSERT_EQ("http://www.cnn.com/",
212 UmaPolicy::CleanURL(GURL("http://www.cnn.com/")));
213 ASSERT_EQ("http://www.cnn.com:8080/a.html",
214 UmaPolicy::CleanURL(GURL("http://www.cnn.com:8080/a.html")));
215 ASSERT_EQ("http://www.cnn.com/",
216 UmaPolicy::CleanURL(GURL("http://www.cnn.com")));
217 ASSERT_EQ("http://www.cnn.com/",
218 UmaPolicy::CleanURL(GURL("http://www.cnn.com/#a")));
219 ASSERT_EQ("http://www.cnn.com/",
220 UmaPolicy::CleanURL(GURL("http://www.cnn.com/#aaaa")));
221 ASSERT_EQ("http://www.cnn.com/?q=a",
222 UmaPolicy::CleanURL(GURL("http://www.cnn.com/?q=a")));
225 } // namespace extensions