OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/cancelable_callback.h" | 5 #include "base/cancelable_callback.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 ASSERT_EQ(expected_api_name, action.api_name()); | 282 ASSERT_EQ(expected_api_name, action.api_name()); |
283 ASSERT_EQ(expected_args_str, | 283 ASSERT_EQ(expected_args_str, |
284 ActivityLogPolicy::Util::Serialize(action.args())); | 284 ActivityLogPolicy::Util::Serialize(action.args())); |
285 ASSERT_EQ(expected_page_url, action.SerializePageUrl()); | 285 ASSERT_EQ(expected_page_url, action.SerializePageUrl()); |
286 ASSERT_EQ(expected_page_title, action.page_title()); | 286 ASSERT_EQ(expected_page_title, action.page_title()); |
287 ASSERT_EQ(expected_arg_url, action.SerializeArgUrl()); | 287 ASSERT_EQ(expected_arg_url, action.SerializeArgUrl()); |
288 ASSERT_EQ(expected_count, action.count()); | 288 ASSERT_EQ(expected_count, action.count()); |
289 ASSERT_NE(-1, action.action_id()); | 289 ASSERT_NE(-1, action.action_id()); |
290 } | 290 } |
291 | 291 |
292 // A helper function initializes the policy with a number of actions, calls | |
293 // RemoveActions on a policy object and then checks the result of the | |
294 // deletion. | |
295 void CheckRemoveActions( | |
296 ActivityLogDatabasePolicy* policy, | |
297 const std::vector<int64>& action_ids, | |
298 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) { | |
299 | |
300 // Record some actions | |
301 scoped_refptr<Action> action = new Action( | |
302 "punky1", base::Time::Now(), Action::ACTION_DOM_ACCESS, "lets1"); | |
303 action->mutable_args()->AppendString("vamoose1"); | |
304 action->set_page_url(GURL("http://www.google1.com")); | |
305 action->set_page_title("Google1"); | |
306 action->set_arg_url(GURL("http://www.args-url1.com")); | |
307 policy->ProcessAction(action); | |
308 // Record the same action twice, so there are multiple entries in the | |
309 // database. | |
310 policy->ProcessAction(action); | |
311 | |
312 action = new Action( | |
313 "punky2", base::Time::Now(), Action::ACTION_API_CALL, "lets2"); | |
314 action->mutable_args()->AppendString("vamoose2"); | |
315 action->set_page_url(GURL("http://www.google2.com")); | |
316 action->set_page_title("Google2"); | |
317 action->set_arg_url(GURL("http://www.args-url2.com")); | |
318 policy->ProcessAction(action); | |
319 // Record the same action twice, so there are multiple entries in the | |
320 // database. | |
321 policy->ProcessAction(action); | |
322 | |
323 // Submit a request to delete actions. | |
324 policy->RemoveActions(action_ids); | |
325 | |
326 // Check the result of the deletion. The checker function gets all | |
327 // activities in the database. | |
328 CheckReadData(policy, "", -1, checker); | |
329 | |
330 // Clean database. | |
331 policy->DeleteDatabase(); | |
332 } | |
333 | |
334 static void AllActionsDeleted(scoped_ptr<Action::ActionVector> actions) { | |
335 ASSERT_EQ(0, static_cast<int>(actions->size())); | |
336 } | |
337 | |
338 static void NoActionsDeleted(scoped_ptr<Action::ActionVector> actions) { | |
339 // These will be in the vector in reverse time order. | |
340 ASSERT_EQ(2, static_cast<int>(actions->size())); | |
341 CheckAction(*actions->at(0), | |
342 "punky2", | |
343 Action::ACTION_API_CALL, | |
344 "lets2", | |
345 "", | |
346 "http://www.google2.com/", | |
347 "Google2", | |
348 "http://www.args-url2.com/", | |
349 2); | |
350 ASSERT_EQ(2, actions->at(0)->action_id()); | |
351 CheckAction(*actions->at(1), | |
352 "punky1", | |
353 Action::ACTION_DOM_ACCESS, | |
354 "lets1", | |
355 "", | |
356 "http://www.google1.com/", | |
357 "Google1", | |
358 "http://www.args-url1.com/", | |
359 2); | |
360 ASSERT_EQ(1, actions->at(1)->action_id()); | |
361 } | |
362 | |
363 static void Action1Deleted(scoped_ptr<Action::ActionVector> actions) { | |
364 // These will be in the vector in reverse time order. | |
365 ASSERT_EQ(1, static_cast<int>(actions->size())); | |
366 CheckAction(*actions->at(0), | |
367 "punky2", | |
368 Action::ACTION_API_CALL, | |
369 "lets2", | |
370 "", | |
371 "http://www.google2.com/", | |
372 "Google2", | |
373 "http://www.args-url2.com/", | |
374 2); | |
375 ASSERT_EQ(2, actions->at(0)->action_id()); | |
376 } | |
377 | |
378 static void Action2Deleted(scoped_ptr<Action::ActionVector> actions) { | |
379 // These will be in the vector in reverse time order. | |
380 ASSERT_EQ(1, static_cast<int>(actions->size())); | |
381 CheckAction(*actions->at(0), | |
382 "punky1", | |
383 Action::ACTION_DOM_ACCESS, | |
384 "lets1", | |
385 "", | |
386 "http://www.google1.com/", | |
387 "Google1", | |
388 "http://www.args-url1.com/", | |
389 2); | |
390 ASSERT_EQ(1, actions->at(0)->action_id()); | |
391 } | |
392 | |
292 protected: | 393 protected: |
293 ExtensionService* extension_service_; | 394 ExtensionService* extension_service_; |
294 scoped_ptr<TestingProfile> profile_; | 395 scoped_ptr<TestingProfile> profile_; |
295 content::TestBrowserThreadBundle thread_bundle_; | 396 content::TestBrowserThreadBundle thread_bundle_; |
296 // Used to preserve a copy of the original command line. | 397 // Used to preserve a copy of the original command line. |
297 // The test framework will do this itself as well. However, by then, | 398 // The test framework will do this itself as well. However, by then, |
298 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in | 399 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in |
299 // TearDown(). | 400 // TearDown(). |
300 CommandLine saved_cmdline_; | 401 CommandLine saved_cmdline_; |
301 | 402 |
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
994 Action::ACTION_ANY, | 1095 Action::ACTION_ANY, |
995 "", | 1096 "", |
996 "", | 1097 "", |
997 "", | 1098 "", |
998 -1, | 1099 -1, |
999 base::Bind( | 1100 base::Bind( |
1000 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); | 1101 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); |
1001 policy->Close(); | 1102 policy->Close(); |
1002 } | 1103 } |
1003 | 1104 |
1004 TEST_F(CountingPolicyTest, DeleteActions) { | 1105 TEST_F(CountingPolicyTest, DeleteDatabase) { |
1005 CountingPolicy* policy = new CountingPolicy(profile_.get()); | 1106 CountingPolicy* policy = new CountingPolicy(profile_.get()); |
1006 policy->Init(); | 1107 policy->Init(); |
1007 // Disable row expiration for this test by setting a time before any actions | 1108 // Disable row expiration for this test by setting a time before any actions |
1008 // we generate. | 1109 // we generate. |
1009 policy->set_retention_time(base::TimeDelta::FromDays(14)); | 1110 policy->set_retention_time(base::TimeDelta::FromDays(14)); |
1010 | 1111 |
1011 // Use a mock clock to ensure that events are not recorded on the wrong day | 1112 // Use a mock clock to ensure that events are not recorded on the wrong day |
1012 // when the test is run close to local midnight. Note: Ownership is passed | 1113 // when the test is run close to local midnight. Note: Ownership is passed |
1013 // to the policy, but we still keep a pointer locally. The policy will take | 1114 // to the policy, but we still keep a pointer locally. The policy will take |
1014 // care of destruction; this is safe since the policy outlives all our | 1115 // care of destruction; this is safe since the policy outlives all our |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1110 policy->ProcessAction(action); | 1211 policy->ProcessAction(action); |
1111 | 1212 |
1112 CheckReadData( | 1213 CheckReadData( |
1113 policy, | 1214 policy, |
1114 "punky", | 1215 "punky", |
1115 0, | 1216 0, |
1116 base::Bind(&CountingPolicyTest::CheckDuplicates)); | 1217 base::Bind(&CountingPolicyTest::CheckDuplicates)); |
1117 policy->Close(); | 1218 policy->Close(); |
1118 } | 1219 } |
1119 | 1220 |
1221 TEST_F(CountingPolicyTest, RemoveActions) { | |
1222 ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get()); | |
1223 policy->Init(); | |
1224 | |
1225 std::vector<int64> action_ids; | |
1226 | |
1227 CheckRemoveActions( | |
1228 policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted)); | |
1229 | |
1230 action_ids.push_back(-1); | |
1231 action_ids.push_back(-10); | |
1232 action_ids.push_back(0); | |
1233 action_ids.push_back(5); | |
1234 action_ids.push_back(10); | |
1235 CheckRemoveActions( | |
1236 policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted)); | |
1237 action_ids.clear(); | |
1238 | |
1239 // policy->RemoveActions() deletes actions in batches of 50 itemis. Test the | |
mvrable
2014/02/11 17:44:35
s/itemis/items/
pmarch
2014/02/12 18:19:04
Done.
| |
1240 // batch size boundary. | |
1241 for (int i = 0; i < 50; i++) { | |
1242 action_ids.push_back(i + 3); | |
1243 } | |
1244 CheckRemoveActions( | |
1245 policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted)); | |
1246 action_ids.clear(); | |
1247 | |
1248 for (int i = 0; i < 51; i++) { | |
1249 action_ids.push_back(i + 3); | |
1250 } | |
1251 CheckRemoveActions( | |
1252 policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted)); | |
1253 action_ids.clear(); | |
1254 | |
1255 for (int i = 0; i < 110; i++) { | |
1256 action_ids.push_back(i + 3); | |
1257 } | |
1258 CheckRemoveActions( | |
1259 policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted)); | |
1260 action_ids.clear(); | |
1261 | |
1262 // CheckRemoveActions pushes two actions to the Activity Log database with IDs | |
1263 // 1 and 2. | |
1264 action_ids.push_back(1); | |
1265 action_ids.push_back(2); | |
1266 CheckRemoveActions( | |
1267 policy, action_ids, base::Bind(&CountingPolicyTest::AllActionsDeleted)); | |
1268 action_ids.clear(); | |
1269 | |
1270 action_ids.push_back(1); | |
1271 CheckRemoveActions( | |
1272 policy, action_ids, base::Bind(&CountingPolicyTest::Action1Deleted)); | |
1273 action_ids.clear(); | |
1274 | |
1275 action_ids.push_back(2); | |
1276 CheckRemoveActions( | |
1277 policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted)); | |
1278 action_ids.clear(); | |
1279 | |
1280 policy->Close(); | |
1281 } | |
1282 | |
1120 } // namespace extensions | 1283 } // namespace extensions |
OLD | NEW |