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/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 ASSERT_EQ(expected_type, action.action_type()); | 214 ASSERT_EQ(expected_type, action.action_type()); |
215 ASSERT_EQ(expected_api_name, action.api_name()); | 215 ASSERT_EQ(expected_api_name, action.api_name()); |
216 ASSERT_EQ(expected_args_str, | 216 ASSERT_EQ(expected_args_str, |
217 ActivityLogPolicy::Util::Serialize(action.args())); | 217 ActivityLogPolicy::Util::Serialize(action.args())); |
218 ASSERT_EQ(expected_page_url, action.SerializePageUrl()); | 218 ASSERT_EQ(expected_page_url, action.SerializePageUrl()); |
219 ASSERT_EQ(expected_page_title, action.page_title()); | 219 ASSERT_EQ(expected_page_title, action.page_title()); |
220 ASSERT_EQ(expected_arg_url, action.SerializeArgUrl()); | 220 ASSERT_EQ(expected_arg_url, action.SerializeArgUrl()); |
221 ASSERT_NE(-1, action.action_id()); | 221 ASSERT_NE(-1, action.action_id()); |
222 } | 222 } |
223 | 223 |
| 224 // A helper function initializes the policy with a number of actions, calls |
| 225 // RemoveActions on a policy object and then checks the result of the |
| 226 // deletion. |
| 227 void CheckRemoveActions( |
| 228 ActivityLogDatabasePolicy* policy, |
| 229 const std::vector<int64>& action_ids, |
| 230 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) { |
| 231 |
| 232 // Record some actions |
| 233 scoped_refptr<Action> action = new Action( |
| 234 "punky1", base::Time::Now(), Action::ACTION_DOM_ACCESS, "lets1"); |
| 235 action->mutable_args()->AppendString("vamoose1"); |
| 236 action->set_page_url(GURL("http://www.google1.com")); |
| 237 action->set_page_title("Google1"); |
| 238 action->set_arg_url(GURL("http://www.args-url1.com")); |
| 239 policy->ProcessAction(action); |
| 240 // Record the same action twice, so there are multiple entries in the |
| 241 // database. |
| 242 policy->ProcessAction(action); |
| 243 |
| 244 action = new Action( |
| 245 "punky2", base::Time::Now(), Action::ACTION_API_CALL, "lets2"); |
| 246 action->mutable_args()->AppendString("vamoose2"); |
| 247 action->set_page_url(GURL("http://www.google2.com")); |
| 248 action->set_page_title("Google2"); |
| 249 action->set_arg_url(GURL("http://www.args-url2.com")); |
| 250 policy->ProcessAction(action); |
| 251 // Record the same action twice, so there are multiple entries in the |
| 252 // database. |
| 253 policy->ProcessAction(action); |
| 254 |
| 255 // Submit a request to delete actions. |
| 256 policy->RemoveActions(action_ids); |
| 257 |
| 258 // Check the result of the deletion. The checker function gets all |
| 259 // activities in the database. |
| 260 CheckReadData(policy, "", -1, checker); |
| 261 |
| 262 // Clean database. |
| 263 policy->DeleteDatabase(); |
| 264 } |
| 265 |
| 266 static void AllActionsDeleted(scoped_ptr<Action::ActionVector> actions) { |
| 267 ASSERT_EQ(0, static_cast<int>(actions->size())); |
| 268 } |
| 269 |
| 270 static void NoActionsDeleted(scoped_ptr<Action::ActionVector> actions) { |
| 271 // These will be in the vector in reverse time order. |
| 272 ASSERT_EQ(4, static_cast<int>(actions->size())); |
| 273 CheckAction(*actions->at(0), |
| 274 "punky2", |
| 275 Action::ACTION_API_CALL, |
| 276 "lets2", |
| 277 "[\"vamoose2\"]", |
| 278 "http://www.google2.com/", |
| 279 "Google2", |
| 280 "http://www.args-url2.com/"); |
| 281 ASSERT_EQ(3, actions->at(0)->action_id()); |
| 282 CheckAction(*actions->at(1), |
| 283 "punky2", |
| 284 Action::ACTION_API_CALL, |
| 285 "lets2", |
| 286 "[\"vamoose2\"]", |
| 287 "http://www.google2.com/", |
| 288 "Google2", |
| 289 "http://www.args-url2.com/"); |
| 290 ASSERT_EQ(4, actions->at(1)->action_id()); |
| 291 CheckAction(*actions->at(2), |
| 292 "punky1", |
| 293 Action::ACTION_DOM_ACCESS, |
| 294 "lets1", |
| 295 "[\"vamoose1\"]", |
| 296 "http://www.google1.com/", |
| 297 "Google1", |
| 298 "http://www.args-url1.com/"); |
| 299 ASSERT_EQ(1, actions->at(2)->action_id()); |
| 300 CheckAction(*actions->at(3), |
| 301 "punky1", |
| 302 Action::ACTION_DOM_ACCESS, |
| 303 "lets1", |
| 304 "[\"vamoose1\"]", |
| 305 "http://www.google1.com/", |
| 306 "Google1", |
| 307 "http://www.args-url1.com/"); |
| 308 ASSERT_EQ(2, actions->at(3)->action_id()); |
| 309 } |
| 310 |
| 311 static void Action1Deleted(scoped_ptr<Action::ActionVector> actions) { |
| 312 // These will be in the vector in reverse time order. |
| 313 ASSERT_EQ(2, static_cast<int>(actions->size())); |
| 314 CheckAction(*actions->at(0), |
| 315 "punky2", |
| 316 Action::ACTION_API_CALL, |
| 317 "lets2", |
| 318 "[\"vamoose2\"]", |
| 319 "http://www.google2.com/", |
| 320 "Google2", |
| 321 "http://www.args-url2.com/"); |
| 322 ASSERT_EQ(3, actions->at(0)->action_id()); |
| 323 CheckAction(*actions->at(1), |
| 324 "punky2", |
| 325 Action::ACTION_API_CALL, |
| 326 "lets2", |
| 327 "[\"vamoose2\"]", |
| 328 "http://www.google2.com/", |
| 329 "Google2", |
| 330 "http://www.args-url2.com/"); |
| 331 ASSERT_EQ(4, actions->at(1)->action_id()); |
| 332 } |
| 333 |
| 334 static void Action2Deleted(scoped_ptr<Action::ActionVector> actions) { |
| 335 // These will be in the vector in reverse time order. |
| 336 ASSERT_EQ(2, static_cast<int>(actions->size())); |
| 337 CheckAction(*actions->at(0), |
| 338 "punky1", |
| 339 Action::ACTION_DOM_ACCESS, |
| 340 "lets1", |
| 341 "[\"vamoose1\"]", |
| 342 "http://www.google1.com/", |
| 343 "Google1", |
| 344 "http://www.args-url1.com/"); |
| 345 ASSERT_EQ(1, actions->at(0)->action_id()); |
| 346 CheckAction(*actions->at(1), |
| 347 "punky1", |
| 348 Action::ACTION_DOM_ACCESS, |
| 349 "lets1", |
| 350 "[\"vamoose1\"]", |
| 351 "http://www.google1.com/", |
| 352 "Google1", |
| 353 "http://www.args-url1.com/"); |
| 354 ASSERT_EQ(2, actions->at(1)->action_id()); |
| 355 } |
| 356 |
224 protected: | 357 protected: |
225 ExtensionService* extension_service_; | 358 ExtensionService* extension_service_; |
226 scoped_ptr<TestingProfile> profile_; | 359 scoped_ptr<TestingProfile> profile_; |
227 content::TestBrowserThreadBundle thread_bundle_; | 360 content::TestBrowserThreadBundle thread_bundle_; |
228 // Used to preserve a copy of the original command line. | 361 // Used to preserve a copy of the original command line. |
229 // The test framework will do this itself as well. However, by then, | 362 // The test framework will do this itself as well. However, by then, |
230 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in | 363 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in |
231 // TearDown(). | 364 // TearDown(). |
232 CommandLine saved_cmdline_; | 365 CommandLine saved_cmdline_; |
233 | 366 |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 Action::ACTION_ANY, | 864 Action::ACTION_ANY, |
732 "", | 865 "", |
733 "", | 866 "", |
734 "", | 867 "", |
735 -1, | 868 -1, |
736 base::Bind( | 869 base::Bind( |
737 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions300)); | 870 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions300)); |
738 policy->Close(); | 871 policy->Close(); |
739 } | 872 } |
740 | 873 |
741 TEST_F(FullStreamUIPolicyTest, DeleteActions) { | 874 TEST_F(FullStreamUIPolicyTest, DeleteDatabase) { |
742 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get()); | 875 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get()); |
743 policy->Init(); | 876 policy->Init(); |
744 scoped_refptr<const Extension> extension = | 877 scoped_refptr<const Extension> extension = |
745 ExtensionBuilder() | 878 ExtensionBuilder() |
746 .SetManifest(DictionaryBuilder() | 879 .SetManifest(DictionaryBuilder() |
747 .Set("name", "Test extension") | 880 .Set("name", "Test extension") |
748 .Set("version", "1.0.0") | 881 .Set("version", "1.0.0") |
749 .Set("manifest_version", 2)) | 882 .Set("manifest_version", 2)) |
750 .Build(); | 883 .Build(); |
751 extension_service_->AddExtension(extension.get()); | 884 extension_service_->AddExtension(extension.get()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 "", | 916 "", |
784 "", | 917 "", |
785 "", | 918 "", |
786 -1, | 919 -1, |
787 base::Bind( | 920 base::Bind( |
788 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions0)); | 921 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions0)); |
789 | 922 |
790 policy->Close(); | 923 policy->Close(); |
791 } | 924 } |
792 | 925 |
| 926 TEST_F(FullStreamUIPolicyTest, RemoveActions) { |
| 927 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get()); |
| 928 policy->Init(); |
| 929 |
| 930 std::vector<int64> action_ids; |
| 931 |
| 932 CheckRemoveActions(policy, |
| 933 action_ids, |
| 934 base::Bind(&FullStreamUIPolicyTest::NoActionsDeleted)); |
| 935 |
| 936 action_ids.push_back(-1); |
| 937 action_ids.push_back(-10); |
| 938 action_ids.push_back(0); |
| 939 action_ids.push_back(5); |
| 940 action_ids.push_back(10); |
| 941 CheckRemoveActions(policy, |
| 942 action_ids, |
| 943 base::Bind(&FullStreamUIPolicyTest::NoActionsDeleted)); |
| 944 action_ids.clear(); |
| 945 |
| 946 for (int i = 0; i < 50; i++) { |
| 947 action_ids.push_back(i + 5); |
| 948 } |
| 949 CheckRemoveActions(policy, |
| 950 action_ids, |
| 951 base::Bind(&FullStreamUIPolicyTest::NoActionsDeleted)); |
| 952 action_ids.clear(); |
| 953 |
| 954 // CheckRemoveActions pushes four actions to the Activity Log database with |
| 955 // IDs 1, 2, 3, and 4. |
| 956 action_ids.push_back(1); |
| 957 action_ids.push_back(2); |
| 958 action_ids.push_back(3); |
| 959 action_ids.push_back(4); |
| 960 CheckRemoveActions(policy, |
| 961 action_ids, |
| 962 base::Bind(&FullStreamUIPolicyTest::AllActionsDeleted)); |
| 963 action_ids.clear(); |
| 964 |
| 965 action_ids.push_back(1); |
| 966 action_ids.push_back(2); |
| 967 CheckRemoveActions( |
| 968 policy, action_ids, base::Bind(&FullStreamUIPolicyTest::Action1Deleted)); |
| 969 action_ids.clear(); |
| 970 |
| 971 action_ids.push_back(3); |
| 972 action_ids.push_back(4); |
| 973 CheckRemoveActions( |
| 974 policy, action_ids, base::Bind(&FullStreamUIPolicyTest::Action2Deleted)); |
| 975 action_ids.clear(); |
| 976 |
| 977 policy->Close(); |
| 978 } |
| 979 |
793 } // namespace extensions | 980 } // namespace extensions |
OLD | NEW |