| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // Wait for the task queue for the specified thread to empty. | 63 // Wait for the task queue for the specified thread to empty. |
| 64 void WaitOnThread(const content::BrowserThread::ID& thread) { | 64 void WaitOnThread(const content::BrowserThread::ID& thread) { |
| 65 BrowserThread::PostTaskAndReply( | 65 BrowserThread::PostTaskAndReply( |
| 66 thread, | 66 thread, |
| 67 FROM_HERE, | 67 FROM_HERE, |
| 68 base::Bind(&base::DoNothing), | 68 base::Bind(&base::DoNothing), |
| 69 base::MessageLoop::current()->QuitClosure()); | 69 base::MessageLoop::current()->QuitClosure()); |
| 70 base::MessageLoop::current()->Run(); | 70 base::MessageLoop::current()->Run(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // A helper function to call ReadData on a policy object and wait for the | 73 // A wrapper function for CheckReadFilteredData, so that we don't need to |
| 74 // results to be processed. | 74 // enter empty string values for parameters we don't care about. |
| 75 void CheckReadData( | 75 void CheckReadData( |
| 76 ActivityLogPolicy* policy, | 76 ActivityLogPolicy* policy, |
| 77 const std::string& extension_id, | 77 const std::string& extension_id, |
| 78 int day, | 78 int day, |
| 79 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) { | 79 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) { |
| 80 // Submit a request to the policy to read back some data, and call the | 80 CheckReadFilteredData( |
| 81 // checker function when results are available. This will happen on the | 81 policy, extension_id, Action::ACTION_ANY, "", "", "", day, checker); |
| 82 // database thread. | |
| 83 policy->ReadData( | |
| 84 extension_id, | |
| 85 day, | |
| 86 base::Bind(&CountingPolicyTest::CheckWrapper, | |
| 87 checker, | |
| 88 base::MessageLoop::current()->QuitClosure())); | |
| 89 | |
| 90 // Set up a timeout that will trigger after 8 seconds; if we haven't | |
| 91 // received any results by then assume that the test is broken. | |
| 92 base::CancelableClosure timeout( | |
| 93 base::Bind(&CountingPolicyTest::TimeoutCallback)); | |
| 94 base::MessageLoop::current()->PostDelayedTask( | |
| 95 FROM_HERE, timeout.callback(), base::TimeDelta::FromSeconds(8)); | |
| 96 | |
| 97 // Wait for results; either the checker or the timeout callbacks should | |
| 98 // cause the main loop to exit. | |
| 99 base::MessageLoop::current()->Run(); | |
| 100 | |
| 101 timeout.Cancel(); | |
| 102 } | 82 } |
| 103 | 83 |
| 104 // A helper function to call ReadFilteredData on a policy object and wait for | 84 // A helper function to call ReadFilteredData on a policy object and wait for |
| 105 // the results to be processed. | 85 // the results to be processed. |
| 106 void CheckReadFilteredData( | 86 void CheckReadFilteredData( |
| 107 ActivityLogPolicy* policy, | 87 ActivityLogPolicy* policy, |
| 108 const std::string& extension_id, | 88 const std::string& extension_id, |
| 109 const Action::ActionType type, | 89 const Action::ActionType type, |
| 110 const std::string& api_name, | 90 const std::string& api_name, |
| 111 const std::string& page_url, | 91 const std::string& page_url, |
| 112 const std::string& arg_url, | 92 const std::string& arg_url, |
| 93 int day, |
| 113 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) { | 94 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) { |
| 114 // Submit a request to the policy to read back some data, and call the | 95 // Submit a request to the policy to read back some data, and call the |
| 115 // checker function when results are available. This will happen on the | 96 // checker function when results are available. This will happen on the |
| 116 // database thread. | 97 // database thread. |
| 117 policy->ReadFilteredData( | 98 policy->ReadFilteredData( |
| 118 extension_id, | 99 extension_id, |
| 119 type, | 100 type, |
| 120 api_name, | 101 api_name, |
| 121 page_url, | 102 page_url, |
| 122 arg_url, | 103 arg_url, |
| 104 day, |
| 123 base::Bind(&CountingPolicyTest::CheckWrapper, | 105 base::Bind(&CountingPolicyTest::CheckWrapper, |
| 124 checker, | 106 checker, |
| 125 base::MessageLoop::current()->QuitClosure())); | 107 base::MessageLoop::current()->QuitClosure())); |
| 126 | 108 |
| 127 // Set up a timeout that will trigger after 8 seconds; if we haven't | 109 // Set up a timeout that will trigger after 8 seconds; if we haven't |
| 128 // received any results by then assume that the test is broken. | 110 // received any results by then assume that the test is broken. |
| 129 base::CancelableClosure timeout( | 111 base::CancelableClosure timeout( |
| 130 base::Bind(&CountingPolicyTest::TimeoutCallback)); | 112 base::Bind(&CountingPolicyTest::TimeoutCallback)); |
| 131 base::MessageLoop::current()->PostDelayedTask( | 113 base::MessageLoop::current()->PostDelayedTask( |
| 132 FROM_HERE, timeout.callback(), base::TimeDelta::FromSeconds(8)); | 114 FROM_HERE, timeout.callback(), base::TimeDelta::FromSeconds(8)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 static void Arguments_Stripped(scoped_ptr<Action::ActionVector> i) { | 181 static void Arguments_Stripped(scoped_ptr<Action::ActionVector> i) { |
| 200 scoped_refptr<Action> last = i->front(); | 182 scoped_refptr<Action> last = i->front(); |
| 201 CheckAction(*last, "odlameecjipmbmbejkplpemijjgpljce", | 183 CheckAction(*last, "odlameecjipmbmbejkplpemijjgpljce", |
| 202 Action::ACTION_API_CALL, "extension.connect", | 184 Action::ACTION_API_CALL, "extension.connect", |
| 203 "[\"hello\",\"world\"]", "", "", "", 1); | 185 "[\"hello\",\"world\"]", "", "", "", 1); |
| 204 } | 186 } |
| 205 | 187 |
| 206 static void Arguments_GetTodaysActions( | 188 static void Arguments_GetTodaysActions( |
| 207 scoped_ptr<Action::ActionVector> actions) { | 189 scoped_ptr<Action::ActionVector> actions) { |
| 208 ASSERT_EQ(3, static_cast<int>(actions->size())); | 190 ASSERT_EQ(3, static_cast<int>(actions->size())); |
| 209 CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets", | 191 CheckAction(*actions->at(0), "punky", Action::ACTION_API_CALL, "brewster", |
| 192 "", "", "", "", 2); |
| 193 CheckAction(*actions->at(1), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 210 "[\"vamoose\"]", "http://www.google.com/", "", "", 1); | 194 "[\"vamoose\"]", "http://www.google.com/", "", "", 1); |
| 211 CheckAction(*actions->at(1), "punky", Action::ACTION_API_CALL, | 195 CheckAction(*actions->at(2), "punky", Action::ACTION_API_CALL, |
| 212 "extension.sendMessage", "[\"not\",\"stripped\"]", "", "", "", | 196 "extension.sendMessage", "[\"not\",\"stripped\"]", "", "", "", |
| 213 1); | 197 1); |
| 214 CheckAction(*actions->at(2), "punky", Action::ACTION_API_CALL, "brewster", | |
| 215 "", "", "", "", 2); | |
| 216 } | 198 } |
| 217 | 199 |
| 218 static void Arguments_GetOlderActions( | 200 static void Arguments_GetOlderActions( |
| 219 scoped_ptr<Action::ActionVector> actions) { | 201 scoped_ptr<Action::ActionVector> actions) { |
| 220 ASSERT_EQ(2, static_cast<int>(actions->size())); | 202 ASSERT_EQ(2, static_cast<int>(actions->size())); |
| 221 CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets", | 203 CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets", |
| 222 "[\"vamoose\"]", "http://www.google.com/", "", "", 1); | 204 "[\"vamoose\"]", "http://www.google.com/", "", "", 1); |
| 223 CheckAction(*actions->at(1), "punky", Action::ACTION_API_CALL, "brewster", | 205 CheckAction(*actions->at(1), "punky", Action::ACTION_API_CALL, "brewster", |
| 224 "", "", "", "", 1); | 206 "", "", "", "", 1); |
| 225 } | 207 } |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 action_dom->set_page_url(gurl); | 484 action_dom->set_page_url(gurl); |
| 503 policy->ProcessAction(action_dom); | 485 policy->ProcessAction(action_dom); |
| 504 | 486 |
| 505 CheckReadFilteredData( | 487 CheckReadFilteredData( |
| 506 policy, | 488 policy, |
| 507 extension->id(), | 489 extension->id(), |
| 508 Action::ACTION_API_CALL, | 490 Action::ACTION_API_CALL, |
| 509 "tabs.testMethod", | 491 "tabs.testMethod", |
| 510 "", | 492 "", |
| 511 "", | 493 "", |
| 494 -1, |
| 512 base::Bind( | 495 base::Bind( |
| 513 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); | 496 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); |
| 514 | 497 |
| 515 CheckReadFilteredData( | 498 CheckReadFilteredData( |
| 516 policy, | 499 policy, |
| 517 "", | 500 "", |
| 518 Action::ACTION_DOM_ACCESS, | 501 Action::ACTION_DOM_ACCESS, |
| 519 "", | 502 "", |
| 520 "", | 503 "", |
| 521 "", | 504 "", |
| 505 -1, |
| 522 base::Bind( | 506 base::Bind( |
| 523 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); | 507 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); |
| 524 | 508 |
| 525 CheckReadFilteredData( | 509 CheckReadFilteredData( |
| 526 policy, | 510 policy, |
| 527 "", | 511 "", |
| 528 Action::ACTION_DOM_ACCESS, | 512 Action::ACTION_DOM_ACCESS, |
| 529 "", | 513 "", |
| 530 "http://www.google.com/", | 514 "http://www.google.com/", |
| 531 "", | 515 "", |
| 516 -1, |
| 532 base::Bind( | 517 base::Bind( |
| 533 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); | 518 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); |
| 534 | 519 |
| 535 CheckReadFilteredData( | 520 CheckReadFilteredData( |
| 536 policy, | 521 policy, |
| 537 "", | 522 "", |
| 538 Action::ACTION_DOM_ACCESS, | 523 Action::ACTION_DOM_ACCESS, |
| 539 "", | 524 "", |
| 540 "http://www.google.com", | 525 "http://www.google.com", |
| 541 "", | 526 "", |
| 527 -1, |
| 542 base::Bind( | 528 base::Bind( |
| 543 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); | 529 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); |
| 544 | 530 |
| 545 CheckReadFilteredData( | 531 CheckReadFilteredData( |
| 546 policy, | 532 policy, |
| 547 "", | 533 "", |
| 548 Action::ACTION_DOM_ACCESS, | 534 Action::ACTION_DOM_ACCESS, |
| 549 "", | 535 "", |
| 550 "http://www.goo", | 536 "http://www.goo", |
| 551 "", | 537 "", |
| 538 -1, |
| 552 base::Bind( | 539 base::Bind( |
| 553 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); | 540 &CountingPolicyTest::RetrieveActions_FetchFilteredActions1)); |
| 554 | 541 |
| 555 CheckReadFilteredData( | 542 CheckReadFilteredData( |
| 556 policy, | 543 policy, |
| 557 extension->id(), | 544 extension->id(), |
| 558 Action::ACTION_ANY, | 545 Action::ACTION_ANY, |
| 559 "", | 546 "", |
| 560 "", | 547 "", |
| 561 "", | 548 "", |
| 549 -1, |
| 562 base::Bind( | 550 base::Bind( |
| 563 &CountingPolicyTest::RetrieveActions_FetchFilteredActions2)); | 551 &CountingPolicyTest::RetrieveActions_FetchFilteredActions2)); |
| 564 | 552 |
| 565 policy->Close(); | 553 policy->Close(); |
| 566 } | 554 } |
| 567 | 555 |
| 568 // Check that merging of actions only occurs within the same day, not across | 556 // Check that merging of actions only occurs within the same day, not across |
| 569 // days, and that old data can be expired from the database. | 557 // days, and that old data can be expired from the database. |
| 570 TEST_F(CountingPolicyTest, MergingAndExpiring) { | 558 TEST_F(CountingPolicyTest, MergingAndExpiring) { |
| 571 CountingPolicy* policy = new CountingPolicy(profile_.get()); | 559 CountingPolicy* policy = new CountingPolicy(profile_.get()); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 policy->Flush(); | 786 policy->Flush(); |
| 799 WaitOnThread(BrowserThread::DB); | 787 WaitOnThread(BrowserThread::DB); |
| 800 | 788 |
| 801 CheckReadFilteredData( | 789 CheckReadFilteredData( |
| 802 policy, | 790 policy, |
| 803 "punky", | 791 "punky", |
| 804 Action::ACTION_ANY, | 792 Action::ACTION_ANY, |
| 805 "", | 793 "", |
| 806 "", | 794 "", |
| 807 "", | 795 "", |
| 796 -1, |
| 808 base::Bind( | 797 base::Bind( |
| 809 &CountingPolicyTest::RetrieveActions_FetchFilteredActions300)); | 798 &CountingPolicyTest::RetrieveActions_FetchFilteredActions300)); |
| 810 policy->Close(); | 799 policy->Close(); |
| 811 } | 800 } |
| 812 | 801 |
| 813 TEST_F(CountingPolicyTest, RemoveAllURLs) { | 802 TEST_F(CountingPolicyTest, RemoveAllURLs) { |
| 814 ActivityLogPolicy* policy = new CountingPolicy(profile_.get()); | 803 ActivityLogPolicy* policy = new CountingPolicy(profile_.get()); |
| 815 | 804 |
| 816 // Use a mock clock to ensure that events are not recorded on the wrong day | 805 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 817 // when the test is run close to local midnight. | 806 // when the test is run close to local midnight. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 policy->ProcessAction(action); | 892 policy->ProcessAction(action); |
| 904 | 893 |
| 905 // This should have neither cleared. | 894 // This should have neither cleared. |
| 906 mock_clock->Advance(base::TimeDelta::FromSeconds(1)); | 895 mock_clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 907 action = new Action( | 896 action = new Action( |
| 908 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets"); | 897 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets"); |
| 909 action->mutable_args()->AppendString("vamoose"); | 898 action->mutable_args()->AppendString("vamoose"); |
| 910 action->set_page_url(GURL("http://www.google.com")); | 899 action->set_page_url(GURL("http://www.google.com")); |
| 911 action->set_page_title("Google"); | 900 action->set_page_title("Google"); |
| 912 action->set_arg_url(GURL("http://www.args-url.com")); | 901 action->set_arg_url(GURL("http://www.args-url.com")); |
| 902 action->set_count(5); |
| 913 policy->ProcessAction(action); | 903 policy->ProcessAction(action); |
| 914 | 904 |
| 915 // Clean some URLs. | 905 // Clean some URLs. |
| 916 std::vector<GURL> urls; | 906 std::vector<GURL> urls; |
| 917 urls.push_back(GURL("http://www.google1.com")); | 907 urls.push_back(GURL("http://www.google1.com")); |
| 918 urls.push_back(GURL("http://www.google2.com")); | 908 urls.push_back(GURL("http://www.google2.com")); |
| 919 urls.push_back(GURL("http://www.url_not_in_db.com")); | 909 urls.push_back(GURL("http://www.url_not_in_db.com")); |
| 920 policy->RemoveURLs(urls); | 910 policy->RemoveURLs(urls); |
| 921 | 911 |
| 922 CheckReadData( | 912 CheckReadData( |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 | 977 |
| 988 policy->DeleteDatabase(); | 978 policy->DeleteDatabase(); |
| 989 | 979 |
| 990 CheckReadFilteredData( | 980 CheckReadFilteredData( |
| 991 policy, | 981 policy, |
| 992 "", | 982 "", |
| 993 Action::ACTION_ANY, | 983 Action::ACTION_ANY, |
| 994 "", | 984 "", |
| 995 "", | 985 "", |
| 996 "", | 986 "", |
| 987 -1, |
| 997 base::Bind( | 988 base::Bind( |
| 998 &CountingPolicyTest::RetrieveActions_FetchFilteredActions0)); | 989 &CountingPolicyTest::RetrieveActions_FetchFilteredActions0)); |
| 999 | 990 |
| 1000 policy->Close(); | 991 policy->Close(); |
| 1001 } | 992 } |
| 1002 | 993 |
| 1003 } // namespace extensions | 994 } // namespace extensions |
| OLD | NEW |