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 "chrome/browser/extensions/activity_log/counting_policy.h" | 5 #include "chrome/browser/extensions/activity_log/counting_policy.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
22 #include "base/synchronization/waitable_event.h" | 22 #include "base/synchronization/waitable_event.h" |
23 #include "base/test/simple_test_clock.h" | 23 #include "base/test/simple_test_clock.h" |
24 #include "base/test/test_timeouts.h" | 24 #include "base/test/test_timeouts.h" |
25 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
26 #include "base/values.h" | 26 #include "base/values.h" |
27 #include "build/build_config.h" | 27 #include "build/build_config.h" |
28 #include "chrome/browser/extensions/activity_log/activity_log.h" | 28 #include "chrome/browser/extensions/activity_log/activity_log.h" |
29 #include "chrome/browser/extensions/activity_log/activity_log_task_runner.h" | |
29 #include "chrome/browser/extensions/extension_service.h" | 30 #include "chrome/browser/extensions/extension_service.h" |
30 #include "chrome/browser/extensions/test_extension_system.h" | 31 #include "chrome/browser/extensions/test_extension_system.h" |
31 #include "chrome/common/chrome_constants.h" | 32 #include "chrome/common/chrome_constants.h" |
32 #include "chrome/common/chrome_switches.h" | 33 #include "chrome/common/chrome_switches.h" |
33 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 34 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
34 #include "chrome/test/base/testing_profile.h" | 35 #include "chrome/test/base/testing_profile.h" |
35 #include "content/public/test/test_browser_thread_bundle.h" | 36 #include "content/public/test/test_browser_thread_bundle.h" |
36 #include "extensions/common/extension_builder.h" | 37 #include "extensions/common/extension_builder.h" |
37 #include "sql/statement.h" | 38 #include "sql/statement.h" |
38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
39 | 40 |
40 #if defined(OS_CHROMEOS) | 41 #if defined(OS_CHROMEOS) |
41 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" | 42 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
42 #include "chrome/browser/chromeos/settings/cros_settings.h" | 43 #include "chrome/browser/chromeos/settings/cros_settings.h" |
43 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 44 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
44 #endif | 45 #endif |
45 | 46 |
46 using content::BrowserThread; | |
47 | |
48 namespace extensions { | 47 namespace extensions { |
49 | 48 |
50 class CountingPolicyTest : public testing::Test { | 49 class CountingPolicyTest : public testing::Test { |
51 public: | 50 public: |
52 CountingPolicyTest() | 51 CountingPolicyTest() |
53 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 52 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
54 #if defined OS_CHROMEOS | 53 #if defined OS_CHROMEOS |
55 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); | 54 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); |
56 #endif | 55 #endif |
57 profile_.reset(new TestingProfile()); | 56 profile_.reset(new TestingProfile()); |
58 base::CommandLine::ForCurrentProcess()-> | 57 base::CommandLine::ForCurrentProcess()-> |
59 AppendSwitch(switches::kEnableExtensionActivityLogging); | 58 AppendSwitch(switches::kEnableExtensionActivityLogging); |
60 base::CommandLine no_program_command_line(base::CommandLine::NO_PROGRAM); | 59 base::CommandLine no_program_command_line(base::CommandLine::NO_PROGRAM); |
61 extension_service_ = static_cast<TestExtensionSystem*>( | 60 extension_service_ = static_cast<TestExtensionSystem*>( |
62 ExtensionSystem::Get(profile_.get()))->CreateExtensionService | 61 ExtensionSystem::Get(profile_.get()))->CreateExtensionService |
63 (&no_program_command_line, base::FilePath(), false); | 62 (&no_program_command_line, base::FilePath(), false); |
64 } | 63 } |
65 | 64 |
66 ~CountingPolicyTest() override { | 65 ~CountingPolicyTest() override { |
67 #if defined OS_CHROMEOS | 66 #if defined OS_CHROMEOS |
68 test_user_manager_.reset(); | 67 test_user_manager_.reset(); |
69 #endif | 68 #endif |
70 base::RunLoop().RunUntilIdle(); | 69 base::RunLoop().RunUntilIdle(); |
71 profile_.reset(NULL); | 70 profile_.reset(NULL); |
72 base::RunLoop().RunUntilIdle(); | 71 base::RunLoop().RunUntilIdle(); |
73 } | 72 } |
74 | 73 |
75 // Wait for the task queue for the specified thread to empty. | 74 // Waits for the task queue for the activity log sequence to empty. |
76 void WaitOnThread(const BrowserThread::ID& thread) { | 75 void WaitOnActivityLogSequence() { |
77 BrowserThread::PostTaskAndReply( | 76 GetActivityLogTaskRunner()->PostTaskAndReply( |
78 thread, FROM_HERE, base::BindOnce(&base::DoNothing), | 77 FROM_HERE, base::BindOnce(&base::DoNothing), |
79 base::MessageLoop::current()->QuitWhenIdleClosure()); | 78 base::MessageLoop::current()->QuitWhenIdleClosure()); |
gab
2017/07/27 18:34:53
While you're here, this is a deprecated paradigm,
Devlin
2017/07/28 14:41:12
Sure, done.
| |
80 base::RunLoop().Run(); | 79 base::RunLoop().Run(); |
81 } | 80 } |
82 | 81 |
83 // A wrapper function for CheckReadFilteredData, so that we don't need to | 82 // A wrapper function for CheckReadFilteredData, so that we don't need to |
84 // enter empty string values for parameters we don't care about. | 83 // enter empty string values for parameters we don't care about. |
85 void CheckReadData(ActivityLogDatabasePolicy* policy, | 84 void CheckReadData(ActivityLogDatabasePolicy* policy, |
86 const std::string& extension_id, | 85 const std::string& extension_id, |
87 int day, | 86 int day, |
88 const base::Callback<void( | 87 const base::Callback<void( |
89 std::unique_ptr<Action::ActionVector>)>& checker) { | 88 std::unique_ptr<Action::ActionVector>)>& checker) { |
(...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
766 "punky", mock_clock->Now(), Action::ACTION_API_CALL, "tabs.create"); | 765 "punky", mock_clock->Now(), Action::ACTION_API_CALL, "tabs.create"); |
767 policy->ProcessAction(action); | 766 policy->ProcessAction(action); |
768 | 767 |
769 // There should now be three strings ("punky", "brewster", "tabs.create") and | 768 // There should now be three strings ("punky", "brewster", "tabs.create") and |
770 // one URL in the tables. | 769 // one URL in the tables. |
771 policy->Flush(); | 770 policy->Flush(); |
772 policy->ScheduleAndForget(policy, | 771 policy->ScheduleAndForget(policy, |
773 &CountingPolicyTest::CheckStringTableSizes, | 772 &CountingPolicyTest::CheckStringTableSizes, |
774 3, | 773 3, |
775 1); | 774 1); |
776 WaitOnThread(BrowserThread::DB); | 775 WaitOnActivityLogSequence(); |
777 | 776 |
778 // Trigger a cleaning. The oldest action is expired when we submit a | 777 // Trigger a cleaning. The oldest action is expired when we submit a |
779 // duplicate of the newer action. After this, there should be two strings | 778 // duplicate of the newer action. After this, there should be two strings |
780 // and no URLs. | 779 // and no URLs. |
781 policy->set_retention_time(base::TimeDelta::FromDays(2)); | 780 policy->set_retention_time(base::TimeDelta::FromDays(2)); |
782 policy->last_database_cleaning_time_ = base::Time(); | 781 policy->last_database_cleaning_time_ = base::Time(); |
783 policy->ProcessAction(action); | 782 policy->ProcessAction(action); |
784 policy->Flush(); | 783 policy->Flush(); |
785 policy->ScheduleAndForget(policy, | 784 policy->ScheduleAndForget(policy, |
786 &CountingPolicyTest::CheckStringTableSizes, | 785 &CountingPolicyTest::CheckStringTableSizes, |
787 2, | 786 2, |
788 0); | 787 0); |
789 WaitOnThread(BrowserThread::DB); | 788 WaitOnActivityLogSequence(); |
790 | 789 |
791 policy->Close(); | 790 policy->Close(); |
792 } | 791 } |
793 | 792 |
794 // A stress test for memory- and database-based merging of actions. Submit | 793 // A stress test for memory- and database-based merging of actions. Submit |
795 // multiple items, not in chronological order, spanning a few days. Check that | 794 // multiple items, not in chronological order, spanning a few days. Check that |
796 // items are merged properly and final timestamps are correct. | 795 // items are merged properly and final timestamps are correct. |
797 TEST_F(CountingPolicyTest, MoreMerging) { | 796 TEST_F(CountingPolicyTest, MoreMerging) { |
798 CountingPolicy* policy = new CountingPolicy(profile_.get()); | 797 CountingPolicy* policy = new CountingPolicy(profile_.get()); |
799 policy->Init(); | 798 policy->Init(); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
877 for (int i = 0; i < 500; i++) { | 876 for (int i = 0; i < 500; i++) { |
878 scoped_refptr<Action> action = | 877 scoped_refptr<Action> action = |
879 new Action("punky", | 878 new Action("punky", |
880 base::Time::Now(), | 879 base::Time::Now(), |
881 Action::ACTION_API_CALL, | 880 Action::ACTION_API_CALL, |
882 base::StringPrintf("apicall_%d", i)); | 881 base::StringPrintf("apicall_%d", i)); |
883 policy->ProcessAction(action); | 882 policy->ProcessAction(action); |
884 } | 883 } |
885 | 884 |
886 policy->ScheduleAndForget(policy, &CountingPolicyTest::CheckQueueSize); | 885 policy->ScheduleAndForget(policy, &CountingPolicyTest::CheckQueueSize); |
887 WaitOnThread(BrowserThread::DB); | 886 WaitOnActivityLogSequence(); |
888 | 887 |
889 policy->Close(); | 888 policy->Close(); |
890 } | 889 } |
891 | 890 |
892 TEST_F(CountingPolicyTest, CapReturns) { | 891 TEST_F(CountingPolicyTest, CapReturns) { |
893 CountingPolicy* policy = new CountingPolicy(profile_.get()); | 892 CountingPolicy* policy = new CountingPolicy(profile_.get()); |
894 policy->Init(); | 893 policy->Init(); |
895 | 894 |
896 for (int i = 0; i < 305; i++) { | 895 for (int i = 0; i < 305; i++) { |
897 scoped_refptr<Action> action = | 896 scoped_refptr<Action> action = |
898 new Action("punky", | 897 new Action("punky", |
899 base::Time::Now(), | 898 base::Time::Now(), |
900 Action::ACTION_API_CALL, | 899 Action::ACTION_API_CALL, |
901 base::StringPrintf("apicall_%d", i)); | 900 base::StringPrintf("apicall_%d", i)); |
902 policy->ProcessAction(action); | 901 policy->ProcessAction(action); |
903 } | 902 } |
904 | 903 |
905 policy->Flush(); | 904 policy->Flush(); |
906 WaitOnThread(BrowserThread::DB); | 905 WaitOnActivityLogSequence(); |
907 | 906 |
908 CheckReadFilteredData( | 907 CheckReadFilteredData( |
909 policy, | 908 policy, |
910 "punky", | 909 "punky", |
911 Action::ACTION_ANY, | 910 Action::ACTION_ANY, |
912 "", | 911 "", |
913 "", | 912 "", |
914 "", | 913 "", |
915 -1, | 914 -1, |
916 base::Bind( | 915 base::Bind( |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1281 | 1280 |
1282 action_ids.push_back(2); | 1281 action_ids.push_back(2); |
1283 CheckRemoveActions( | 1282 CheckRemoveActions( |
1284 policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted)); | 1283 policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted)); |
1285 action_ids.clear(); | 1284 action_ids.clear(); |
1286 | 1285 |
1287 policy->Close(); | 1286 policy->Close(); |
1288 } | 1287 } |
1289 | 1288 |
1290 } // namespace extensions | 1289 } // namespace extensions |
OLD | NEW |