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 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 ASSERT_EQ(expected_api_name, action.api_name()); | 289 ASSERT_EQ(expected_api_name, action.api_name()); |
290 ASSERT_EQ(expected_args_str, | 290 ASSERT_EQ(expected_args_str, |
291 ActivityLogPolicy::Util::Serialize(action.args())); | 291 ActivityLogPolicy::Util::Serialize(action.args())); |
292 ASSERT_EQ(expected_page_url, action.SerializePageUrl()); | 292 ASSERT_EQ(expected_page_url, action.SerializePageUrl()); |
293 ASSERT_EQ(expected_page_title, action.page_title()); | 293 ASSERT_EQ(expected_page_title, action.page_title()); |
294 ASSERT_EQ(expected_arg_url, action.SerializeArgUrl()); | 294 ASSERT_EQ(expected_arg_url, action.SerializeArgUrl()); |
295 ASSERT_EQ(expected_count, action.count()); | 295 ASSERT_EQ(expected_count, action.count()); |
296 ASSERT_NE(-1, action.action_id()); | 296 ASSERT_NE(-1, action.action_id()); |
297 } | 297 } |
298 | 298 |
| 299 // A helper function initializes the policy with a number of actions, calls |
| 300 // RemoveActions on a policy object and then checks the result of the |
| 301 // deletion. |
| 302 void CheckRemoveActions( |
| 303 ActivityLogDatabasePolicy* policy, |
| 304 const std::vector<int64>& action_ids, |
| 305 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) { |
| 306 |
| 307 // Record some actions |
| 308 scoped_refptr<Action> action = new Action( |
| 309 "punky1", base::Time::Now(), Action::ACTION_DOM_ACCESS, "lets1"); |
| 310 action->mutable_args()->AppendString("vamoose1"); |
| 311 action->set_page_url(GURL("http://www.google1.com")); |
| 312 action->set_page_title("Google1"); |
| 313 action->set_arg_url(GURL("http://www.args-url1.com")); |
| 314 policy->ProcessAction(action); |
| 315 // Record the same action twice, so there are multiple entries in the |
| 316 // database. |
| 317 policy->ProcessAction(action); |
| 318 |
| 319 action = new Action( |
| 320 "punky2", base::Time::Now(), Action::ACTION_API_CALL, "lets2"); |
| 321 action->mutable_args()->AppendString("vamoose2"); |
| 322 action->set_page_url(GURL("http://www.google2.com")); |
| 323 action->set_page_title("Google2"); |
| 324 action->set_arg_url(GURL("http://www.args-url2.com")); |
| 325 policy->ProcessAction(action); |
| 326 // Record the same action twice, so there are multiple entries in the |
| 327 // database. |
| 328 policy->ProcessAction(action); |
| 329 |
| 330 // Submit a request to delete actions. |
| 331 policy->RemoveActions(action_ids); |
| 332 |
| 333 // Check the result of the deletion. The checker function gets all |
| 334 // activities in the database. |
| 335 CheckReadData(policy, "", -1, checker); |
| 336 |
| 337 // Clean database. |
| 338 policy->DeleteDatabase(); |
| 339 } |
| 340 |
| 341 static void AllActionsDeleted(scoped_ptr<Action::ActionVector> actions) { |
| 342 ASSERT_EQ(0, static_cast<int>(actions->size())); |
| 343 } |
| 344 |
| 345 static void NoActionsDeleted(scoped_ptr<Action::ActionVector> actions) { |
| 346 // These will be in the vector in reverse time order. |
| 347 ASSERT_EQ(2, static_cast<int>(actions->size())); |
| 348 CheckAction(*actions->at(0), |
| 349 "punky2", |
| 350 Action::ACTION_API_CALL, |
| 351 "lets2", |
| 352 "", |
| 353 "http://www.google2.com/", |
| 354 "Google2", |
| 355 "http://www.args-url2.com/", |
| 356 2); |
| 357 ASSERT_EQ(2, actions->at(0)->action_id()); |
| 358 CheckAction(*actions->at(1), |
| 359 "punky1", |
| 360 Action::ACTION_DOM_ACCESS, |
| 361 "lets1", |
| 362 "", |
| 363 "http://www.google1.com/", |
| 364 "Google1", |
| 365 "http://www.args-url1.com/", |
| 366 2); |
| 367 ASSERT_EQ(1, actions->at(1)->action_id()); |
| 368 } |
| 369 |
| 370 static void Action1Deleted(scoped_ptr<Action::ActionVector> actions) { |
| 371 // These will be in the vector in reverse time order. |
| 372 ASSERT_EQ(1, static_cast<int>(actions->size())); |
| 373 CheckAction(*actions->at(0), |
| 374 "punky2", |
| 375 Action::ACTION_API_CALL, |
| 376 "lets2", |
| 377 "", |
| 378 "http://www.google2.com/", |
| 379 "Google2", |
| 380 "http://www.args-url2.com/", |
| 381 2); |
| 382 ASSERT_EQ(2, actions->at(0)->action_id()); |
| 383 } |
| 384 |
| 385 static void Action2Deleted(scoped_ptr<Action::ActionVector> actions) { |
| 386 // These will be in the vector in reverse time order. |
| 387 ASSERT_EQ(1, static_cast<int>(actions->size())); |
| 388 CheckAction(*actions->at(0), |
| 389 "punky1", |
| 390 Action::ACTION_DOM_ACCESS, |
| 391 "lets1", |
| 392 "", |
| 393 "http://www.google1.com/", |
| 394 "Google1", |
| 395 "http://www.args-url1.com/", |
| 396 2); |
| 397 ASSERT_EQ(1, actions->at(0)->action_id()); |
| 398 } |
| 399 |
299 protected: | 400 protected: |
300 ExtensionService* extension_service_; | 401 ExtensionService* extension_service_; |
301 scoped_ptr<TestingProfile> profile_; | 402 scoped_ptr<TestingProfile> profile_; |
302 content::TestBrowserThreadBundle thread_bundle_; | 403 content::TestBrowserThreadBundle thread_bundle_; |
303 // Used to preserve a copy of the original command line. | 404 // Used to preserve a copy of the original command line. |
304 // The test framework will do this itself as well. However, by then, | 405 // The test framework will do this itself as well. However, by then, |
305 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in | 406 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in |
306 // TearDown(). | 407 // TearDown(). |
307 CommandLine saved_cmdline_; | 408 CommandLine saved_cmdline_; |
308 | 409 |
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1001 Action::ACTION_ANY, | 1102 Action::ACTION_ANY, |
1002 "", | 1103 "", |
1003 "", | 1104 "", |
1004 "", | 1105 "", |
1005 -1, | 1106 -1, |
1006 base::Bind( | 1107 base::Bind( |
1007 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); | 1108 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); |
1008 policy->Close(); | 1109 policy->Close(); |
1009 } | 1110 } |
1010 | 1111 |
1011 TEST_F(CountingPolicyTest, DeleteActions) { | 1112 TEST_F(CountingPolicyTest, DeleteDatabase) { |
1012 CountingPolicy* policy = new CountingPolicy(profile_.get()); | 1113 CountingPolicy* policy = new CountingPolicy(profile_.get()); |
1013 policy->Init(); | 1114 policy->Init(); |
1014 // Disable row expiration for this test by setting a time before any actions | 1115 // Disable row expiration for this test by setting a time before any actions |
1015 // we generate. | 1116 // we generate. |
1016 policy->set_retention_time(base::TimeDelta::FromDays(14)); | 1117 policy->set_retention_time(base::TimeDelta::FromDays(14)); |
1017 | 1118 |
1018 // Use a mock clock to ensure that events are not recorded on the wrong day | 1119 // Use a mock clock to ensure that events are not recorded on the wrong day |
1019 // when the test is run close to local midnight. Note: Ownership is passed | 1120 // when the test is run close to local midnight. Note: Ownership is passed |
1020 // to the policy, but we still keep a pointer locally. The policy will take | 1121 // to the policy, but we still keep a pointer locally. The policy will take |
1021 // care of destruction; this is safe since the policy outlives all our | 1122 // care of destruction; this is safe since the policy outlives all our |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 policy->ProcessAction(action); | 1246 policy->ProcessAction(action); |
1146 | 1247 |
1147 CheckReadData( | 1248 CheckReadData( |
1148 policy, | 1249 policy, |
1149 "punky", | 1250 "punky", |
1150 0, | 1251 0, |
1151 base::Bind(&CountingPolicyTest::CheckDuplicates)); | 1252 base::Bind(&CountingPolicyTest::CheckDuplicates)); |
1152 policy->Close(); | 1253 policy->Close(); |
1153 } | 1254 } |
1154 | 1255 |
| 1256 TEST_F(CountingPolicyTest, RemoveActions) { |
| 1257 ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get()); |
| 1258 policy->Init(); |
| 1259 |
| 1260 std::vector<int64> action_ids; |
| 1261 |
| 1262 CheckRemoveActions( |
| 1263 policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted)); |
| 1264 |
| 1265 action_ids.push_back(-1); |
| 1266 action_ids.push_back(-10); |
| 1267 action_ids.push_back(0); |
| 1268 action_ids.push_back(5); |
| 1269 action_ids.push_back(10); |
| 1270 CheckRemoveActions( |
| 1271 policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted)); |
| 1272 action_ids.clear(); |
| 1273 |
| 1274 for (int i = 0; i < 50; i++) { |
| 1275 action_ids.push_back(i + 3); |
| 1276 } |
| 1277 CheckRemoveActions( |
| 1278 policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted)); |
| 1279 action_ids.clear(); |
| 1280 |
| 1281 // CheckRemoveActions pushes two actions to the Activity Log database with IDs |
| 1282 // 1 and 2. |
| 1283 action_ids.push_back(1); |
| 1284 action_ids.push_back(2); |
| 1285 CheckRemoveActions( |
| 1286 policy, action_ids, base::Bind(&CountingPolicyTest::AllActionsDeleted)); |
| 1287 action_ids.clear(); |
| 1288 |
| 1289 action_ids.push_back(1); |
| 1290 CheckRemoveActions( |
| 1291 policy, action_ids, base::Bind(&CountingPolicyTest::Action1Deleted)); |
| 1292 action_ids.clear(); |
| 1293 |
| 1294 action_ids.push_back(2); |
| 1295 CheckRemoveActions( |
| 1296 policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted)); |
| 1297 action_ids.clear(); |
| 1298 |
| 1299 policy->Close(); |
| 1300 } |
| 1301 |
1155 } // namespace extensions | 1302 } // namespace extensions |
OLD | NEW |