Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(292)

Side by Side Diff: chrome/browser/extensions/activity_log/counting_policy_unittest.cc

Issue 2980503002: [Extensions][TaskScheduler] Update ActivityLog for scheduling migration (Closed)
Patch Set: . Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 base::RunLoop run_loop;
78 thread, FROM_HERE, base::BindOnce(&base::DoNothing), 77 GetActivityLogTaskRunner()->PostTaskAndReply(
79 base::MessageLoop::current()->QuitWhenIdleClosure()); 78 FROM_HERE, base::BindOnce(&base::DoNothing),
80 base::RunLoop().Run(); 79 run_loop.QuitWhenIdleClosure());
80 run_loop.Run();
81 } 81 }
82 82
83 // A wrapper function for CheckReadFilteredData, so that we don't need to 83 // A wrapper function for CheckReadFilteredData, so that we don't need to
84 // enter empty string values for parameters we don't care about. 84 // enter empty string values for parameters we don't care about.
85 void CheckReadData(ActivityLogDatabasePolicy* policy, 85 void CheckReadData(ActivityLogDatabasePolicy* policy,
86 const std::string& extension_id, 86 const std::string& extension_id,
87 int day, 87 int day,
88 const base::Callback<void( 88 const base::Callback<void(
89 std::unique_ptr<Action::ActionVector>)>& checker) { 89 std::unique_ptr<Action::ActionVector>)>& checker) {
90 CheckReadFilteredData( 90 CheckReadFilteredData(
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 "punky", mock_clock->Now(), Action::ACTION_API_CALL, "tabs.create"); 766 "punky", mock_clock->Now(), Action::ACTION_API_CALL, "tabs.create");
767 policy->ProcessAction(action); 767 policy->ProcessAction(action);
768 768
769 // There should now be three strings ("punky", "brewster", "tabs.create") and 769 // There should now be three strings ("punky", "brewster", "tabs.create") and
770 // one URL in the tables. 770 // one URL in the tables.
771 policy->Flush(); 771 policy->Flush();
772 policy->ScheduleAndForget(policy, 772 policy->ScheduleAndForget(policy,
773 &CountingPolicyTest::CheckStringTableSizes, 773 &CountingPolicyTest::CheckStringTableSizes,
774 3, 774 3,
775 1); 775 1);
776 WaitOnThread(BrowserThread::DB); 776 WaitOnActivityLogSequence();
777 777
778 // Trigger a cleaning. The oldest action is expired when we submit a 778 // 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 779 // duplicate of the newer action. After this, there should be two strings
780 // and no URLs. 780 // and no URLs.
781 policy->set_retention_time(base::TimeDelta::FromDays(2)); 781 policy->set_retention_time(base::TimeDelta::FromDays(2));
782 policy->last_database_cleaning_time_ = base::Time(); 782 policy->last_database_cleaning_time_ = base::Time();
783 policy->ProcessAction(action); 783 policy->ProcessAction(action);
784 policy->Flush(); 784 policy->Flush();
785 policy->ScheduleAndForget(policy, 785 policy->ScheduleAndForget(policy,
786 &CountingPolicyTest::CheckStringTableSizes, 786 &CountingPolicyTest::CheckStringTableSizes,
787 2, 787 2,
788 0); 788 0);
789 WaitOnThread(BrowserThread::DB); 789 WaitOnActivityLogSequence();
790 790
791 policy->Close(); 791 policy->Close();
792 } 792 }
793 793
794 // A stress test for memory- and database-based merging of actions. Submit 794 // 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 795 // multiple items, not in chronological order, spanning a few days. Check that
796 // items are merged properly and final timestamps are correct. 796 // items are merged properly and final timestamps are correct.
797 TEST_F(CountingPolicyTest, MoreMerging) { 797 TEST_F(CountingPolicyTest, MoreMerging) {
798 CountingPolicy* policy = new CountingPolicy(profile_.get()); 798 CountingPolicy* policy = new CountingPolicy(profile_.get());
799 policy->Init(); 799 policy->Init();
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 for (int i = 0; i < 500; i++) { 877 for (int i = 0; i < 500; i++) {
878 scoped_refptr<Action> action = 878 scoped_refptr<Action> action =
879 new Action("punky", 879 new Action("punky",
880 base::Time::Now(), 880 base::Time::Now(),
881 Action::ACTION_API_CALL, 881 Action::ACTION_API_CALL,
882 base::StringPrintf("apicall_%d", i)); 882 base::StringPrintf("apicall_%d", i));
883 policy->ProcessAction(action); 883 policy->ProcessAction(action);
884 } 884 }
885 885
886 policy->ScheduleAndForget(policy, &CountingPolicyTest::CheckQueueSize); 886 policy->ScheduleAndForget(policy, &CountingPolicyTest::CheckQueueSize);
887 WaitOnThread(BrowserThread::DB); 887 WaitOnActivityLogSequence();
888 888
889 policy->Close(); 889 policy->Close();
890 } 890 }
891 891
892 TEST_F(CountingPolicyTest, CapReturns) { 892 TEST_F(CountingPolicyTest, CapReturns) {
893 CountingPolicy* policy = new CountingPolicy(profile_.get()); 893 CountingPolicy* policy = new CountingPolicy(profile_.get());
894 policy->Init(); 894 policy->Init();
895 895
896 for (int i = 0; i < 305; i++) { 896 for (int i = 0; i < 305; i++) {
897 scoped_refptr<Action> action = 897 scoped_refptr<Action> action =
898 new Action("punky", 898 new Action("punky",
899 base::Time::Now(), 899 base::Time::Now(),
900 Action::ACTION_API_CALL, 900 Action::ACTION_API_CALL,
901 base::StringPrintf("apicall_%d", i)); 901 base::StringPrintf("apicall_%d", i));
902 policy->ProcessAction(action); 902 policy->ProcessAction(action);
903 } 903 }
904 904
905 policy->Flush(); 905 policy->Flush();
906 WaitOnThread(BrowserThread::DB); 906 WaitOnActivityLogSequence();
907 907
908 CheckReadFilteredData( 908 CheckReadFilteredData(
909 policy, 909 policy,
910 "punky", 910 "punky",
911 Action::ACTION_ANY, 911 Action::ACTION_ANY,
912 "", 912 "",
913 "", 913 "",
914 "", 914 "",
915 -1, 915 -1,
916 base::Bind( 916 base::Bind(
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
1281 1281
1282 action_ids.push_back(2); 1282 action_ids.push_back(2);
1283 CheckRemoveActions( 1283 CheckRemoveActions(
1284 policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted)); 1284 policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted));
1285 action_ids.clear(); 1285 action_ids.clear();
1286 1286
1287 policy->Close(); 1287 policy->Close();
1288 } 1288 }
1289 1289
1290 } // namespace extensions 1290 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698