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/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
10 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
11 #include "base/test/simple_test_clock.h" | 12 #include "base/test/simple_test_clock.h" |
12 #include "chrome/browser/extensions/activity_log/activity_log.h" | 13 #include "chrome/browser/extensions/activity_log/activity_log.h" |
13 #include "chrome/browser/extensions/activity_log/counting_policy.h" | 14 #include "chrome/browser/extensions/activity_log/counting_policy.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/extensions/test_extension_system.h" | 16 #include "chrome/browser/extensions/test_extension_system.h" |
16 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/extensions/extension_builder.h" | 19 #include "chrome/common/extensions/extension_builder.h" |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 "ID=punky CATEGORY=api_call API=brewster COUNT=%d", count); | 243 "ID=punky CATEGORY=api_call API=brewster COUNT=%d", count); |
243 if (count > 0) { | 244 if (count > 0) { |
244 ASSERT_EQ(1u, actions->size()); | 245 ASSERT_EQ(1u, actions->size()); |
245 ASSERT_EQ(api_print, actions->at(0)->PrintForDebug()); | 246 ASSERT_EQ(api_print, actions->at(0)->PrintForDebug()); |
246 ASSERT_EQ(time, actions->at(0)->time()); | 247 ASSERT_EQ(time, actions->at(0)->time()); |
247 } else { | 248 } else { |
248 ASSERT_EQ(0u, actions->size()); | 249 ASSERT_EQ(0u, actions->size()); |
249 } | 250 } |
250 } | 251 } |
251 | 252 |
| 253 static void AllURLsRemoved(scoped_ptr<Action::ActionVector> actions) { |
| 254 ASSERT_EQ(2, static_cast<int>(actions->size())); |
| 255 CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 256 "[\"vamoose\"]", "", "", ""); |
| 257 CheckAction(*actions->at(1), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 258 "[\"vamoose\"]", "", "", ""); |
| 259 } |
| 260 |
| 261 static void SomeURLsRemoved(scoped_ptr<Action::ActionVector> actions) { |
| 262 // These will be in the vector in reverse time order. |
| 263 ASSERT_EQ(5, static_cast<int>(actions->size())); |
| 264 CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 265 "[\"vamoose\"]", "http://www.google.com/", "Google", |
| 266 "http://www.args-url.com/"); |
| 267 CheckAction(*actions->at(1), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 268 "[\"vamoose\"]", "http://www.google.com/", "Google", ""); |
| 269 CheckAction(*actions->at(2), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 270 "[\"vamoose\"]", "", "", ""); |
| 271 CheckAction(*actions->at(3), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 272 "[\"vamoose\"]", "", "", "http://www.google.com/"); |
| 273 CheckAction(*actions->at(4), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 274 "[\"vamoose\"]", "", "", ""); |
| 275 } |
| 276 |
| 277 // TODO(karenlees): add checking for the count value. |
| 278 static void CheckAction(const Action& action, |
| 279 const std::string& expected_id, |
| 280 const Action::ActionType& expected_type, |
| 281 const std::string& expected_api_name, |
| 282 const std::string& expected_args_str, |
| 283 const std::string& expected_page_url, |
| 284 const std::string& expected_page_title, |
| 285 const std::string& expected_arg_url) { |
| 286 ASSERT_EQ(expected_id, action.extension_id()); |
| 287 ASSERT_EQ(expected_type, action.action_type()); |
| 288 ASSERT_EQ(expected_api_name, action.api_name()); |
| 289 ASSERT_EQ(expected_args_str, |
| 290 ActivityLogPolicy::Util::Serialize(action.args())); |
| 291 ASSERT_EQ(expected_page_url, action.SerializePageUrl()); |
| 292 ASSERT_EQ(expected_page_title, action.page_title()); |
| 293 ASSERT_EQ(expected_arg_url, action.SerializeArgUrl()); |
| 294 } |
| 295 |
252 protected: | 296 protected: |
253 ExtensionService* extension_service_; | 297 ExtensionService* extension_service_; |
254 scoped_ptr<TestingProfile> profile_; | 298 scoped_ptr<TestingProfile> profile_; |
255 content::TestBrowserThreadBundle thread_bundle_; | 299 content::TestBrowserThreadBundle thread_bundle_; |
256 // Used to preserve a copy of the original command line. | 300 // Used to preserve a copy of the original command line. |
257 // The test framework will do this itself as well. However, by then, | 301 // The test framework will do this itself as well. However, by then, |
258 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in | 302 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in |
259 // TearDown(). | 303 // TearDown(). |
260 CommandLine saved_cmdline_; | 304 CommandLine saved_cmdline_; |
261 | 305 |
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 base::StringPrintf("apicall_%d", i)); | 775 base::StringPrintf("apicall_%d", i)); |
732 policy->ProcessAction(action); | 776 policy->ProcessAction(action); |
733 } | 777 } |
734 | 778 |
735 policy->ScheduleAndForget(policy, &CountingPolicyTest::CheckQueueSize); | 779 policy->ScheduleAndForget(policy, &CountingPolicyTest::CheckQueueSize); |
736 WaitOnThread(BrowserThread::DB); | 780 WaitOnThread(BrowserThread::DB); |
737 | 781 |
738 policy->Close(); | 782 policy->Close(); |
739 } | 783 } |
740 | 784 |
| 785 TEST_F(CountingPolicyTest, RemoveAllURLs) { |
| 786 ActivityLogPolicy* policy = new CountingPolicy(profile_.get()); |
| 787 |
| 788 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 789 // when the test is run close to local midnight. |
| 790 base::SimpleTestClock* mock_clock = new base::SimpleTestClock(); |
| 791 mock_clock->SetNow(base::Time::Now().LocalMidnight() + |
| 792 base::TimeDelta::FromHours(12)); |
| 793 policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock)); |
| 794 |
| 795 // Record some actions |
| 796 scoped_refptr<Action> action = |
| 797 new Action("punky", mock_clock->Now(), |
| 798 Action::ACTION_DOM_ACCESS, "lets"); |
| 799 action->mutable_args()->AppendString("vamoose"); |
| 800 action->set_page_url(GURL("http://www.google.com")); |
| 801 action->set_page_title("Google"); |
| 802 action->set_arg_url(GURL("http://www.args-url.com")); |
| 803 policy->ProcessAction(action); |
| 804 |
| 805 mock_clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 806 action = new Action( |
| 807 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets"); |
| 808 action->mutable_args()->AppendString("vamoose"); |
| 809 action->set_page_url(GURL("http://www.google2.com")); |
| 810 action->set_page_title("Google"); |
| 811 // Deliberately no arg url set to make sure it stills works if there is no arg |
| 812 // url. |
| 813 policy->ProcessAction(action); |
| 814 |
| 815 // Clean all the URLs. |
| 816 std::vector<GURL> no_url_restrictions; |
| 817 policy->RemoveURLs(no_url_restrictions); |
| 818 |
| 819 CheckReadData( |
| 820 policy, |
| 821 "punky", |
| 822 0, |
| 823 base::Bind(&CountingPolicyTest::AllURLsRemoved)); |
| 824 policy->Close(); |
| 825 } |
| 826 |
| 827 TEST_F(CountingPolicyTest, RemoveSpecificURLs) { |
| 828 ActivityLogPolicy* policy = new CountingPolicy(profile_.get()); |
| 829 |
| 830 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 831 // when the test is run close to local midnight. |
| 832 base::SimpleTestClock* mock_clock = new base::SimpleTestClock(); |
| 833 mock_clock->SetNow(base::Time::Now().LocalMidnight() + |
| 834 base::TimeDelta::FromHours(12)); |
| 835 policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock)); |
| 836 |
| 837 // Record some actions |
| 838 // This should have the page url and args url cleared. |
| 839 scoped_refptr<Action> action = new Action("punky", mock_clock->Now(), |
| 840 Action::ACTION_DOM_ACCESS, "lets"); |
| 841 action->mutable_args()->AppendString("vamoose"); |
| 842 action->set_page_url(GURL("http://www.google1.com")); |
| 843 action->set_page_title("Google"); |
| 844 action->set_arg_url(GURL("http://www.google1.com")); |
| 845 policy->ProcessAction(action); |
| 846 |
| 847 // This should have the page url cleared but not args url. |
| 848 mock_clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 849 action = new Action( |
| 850 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets"); |
| 851 action->mutable_args()->AppendString("vamoose"); |
| 852 action->set_page_url(GURL("http://www.google1.com")); |
| 853 action->set_page_title("Google"); |
| 854 action->set_arg_url(GURL("http://www.google.com")); |
| 855 policy->ProcessAction(action); |
| 856 |
| 857 // This should have the page url cleared. The args url is deliberately not |
| 858 // set to make sure this doesn't cause any issues. |
| 859 mock_clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 860 action = new Action( |
| 861 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets"); |
| 862 action->mutable_args()->AppendString("vamoose"); |
| 863 action->set_page_url(GURL("http://www.google2.com")); |
| 864 action->set_page_title("Google"); |
| 865 policy->ProcessAction(action); |
| 866 |
| 867 // This should have the args url cleared but not the page url or page title. |
| 868 mock_clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 869 action = new Action( |
| 870 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets"); |
| 871 action->mutable_args()->AppendString("vamoose"); |
| 872 action->set_page_url(GURL("http://www.google.com")); |
| 873 action->set_page_title("Google"); |
| 874 action->set_arg_url(GURL("http://www.google1.com")); |
| 875 policy->ProcessAction(action); |
| 876 |
| 877 // This should have neither cleared. |
| 878 mock_clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 879 action = new Action( |
| 880 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets"); |
| 881 action->mutable_args()->AppendString("vamoose"); |
| 882 action->set_page_url(GURL("http://www.google.com")); |
| 883 action->set_page_title("Google"); |
| 884 action->set_arg_url(GURL("http://www.args-url.com")); |
| 885 policy->ProcessAction(action); |
| 886 |
| 887 // Clean some URLs. |
| 888 std::vector<GURL> urls; |
| 889 urls.push_back(GURL("http://www.google1.com")); |
| 890 urls.push_back(GURL("http://www.google2.com")); |
| 891 urls.push_back(GURL("http://www.url_not_in_db.com")); |
| 892 policy->RemoveURLs(urls); |
| 893 |
| 894 CheckReadData( |
| 895 policy, |
| 896 "punky", |
| 897 0, |
| 898 base::Bind(&CountingPolicyTest::SomeURLsRemoved)); |
| 899 policy->Close(); |
| 900 } |
| 901 |
741 } // namespace extensions | 902 } // namespace extensions |
OLD | NEW |