Rename desktop_cursor_loader_updater_aurax11.
[chromium-blink-merge.git] / chrome / browser / extensions / activity_log / fullstream_ui_policy_unittest.cc
blob3b9695131a75207bc360ca4869d38aaee2b8970f
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
3 // found in the LICENSE file.
5 #include "base/cancelable_callback.h"
6 #include "base/command_line.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/test/simple_test_clock.h"
12 #include "base/test/test_timeouts.h"
13 #include "chrome/browser/extensions/activity_log/activity_log.h"
14 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/test_extension_system.h"
17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/extensions/extension_builder.h"
20 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "sql/statement.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 #if defined(OS_CHROMEOS)
27 #include "chrome/browser/chromeos/login/user_manager.h"
28 #include "chrome/browser/chromeos/settings/cros_settings.h"
29 #include "chrome/browser/chromeos/settings/device_settings_service.h"
30 #endif
32 namespace extensions {
34 class FullStreamUIPolicyTest : public testing::Test {
35 public:
36 FullStreamUIPolicyTest()
37 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
38 saved_cmdline_(CommandLine::NO_PROGRAM) {
39 #if defined OS_CHROMEOS
40 test_user_manager_.reset(new chromeos::ScopedTestUserManager());
41 #endif
42 CommandLine command_line(CommandLine::NO_PROGRAM);
43 saved_cmdline_ = *CommandLine::ForCurrentProcess();
44 profile_.reset(new TestingProfile());
45 CommandLine::ForCurrentProcess()->AppendSwitch(
46 switches::kEnableExtensionActivityLogging);
47 CommandLine::ForCurrentProcess()->AppendSwitch(
48 switches::kEnableExtensionActivityLogTesting);
49 extension_service_ = static_cast<TestExtensionSystem*>(
50 ExtensionSystem::Get(profile_.get()))->CreateExtensionService
51 (&command_line, base::FilePath(), false);
54 virtual ~FullStreamUIPolicyTest() {
55 #if defined OS_CHROMEOS
56 test_user_manager_.reset();
57 #endif
58 base::RunLoop().RunUntilIdle();
59 profile_.reset(NULL);
60 base::RunLoop().RunUntilIdle();
61 // Restore the original command line and undo the affects of SetUp().
62 *CommandLine::ForCurrentProcess() = saved_cmdline_;
65 // A wrapper function for CheckReadFilteredData, so that we don't need to
66 // enter empty string values for parameters we don't care about.
67 void CheckReadData(
68 ActivityLogDatabasePolicy* policy,
69 const std::string& extension_id,
70 int day,
71 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
72 CheckReadFilteredData(
73 policy, extension_id, Action::ACTION_ANY, "", "", "", day, checker);
76 // A helper function to call ReadFilteredData on a policy object and wait for
77 // the results to be processed.
78 void CheckReadFilteredData(
79 ActivityLogDatabasePolicy* policy,
80 const std::string& extension_id,
81 const Action::ActionType type,
82 const std::string& api_name,
83 const std::string& page_url,
84 const std::string& arg_url,
85 const int days_ago,
86 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
87 // Submit a request to the policy to read back some data, and call the
88 // checker function when results are available. This will happen on the
89 // database thread.
90 policy->ReadFilteredData(
91 extension_id,
92 type,
93 api_name,
94 page_url,
95 arg_url,
96 days_ago,
97 base::Bind(&FullStreamUIPolicyTest::CheckWrapper,
98 checker,
99 base::MessageLoop::current()->QuitClosure()));
101 // Set up a timeout for receiving results; if we haven't received anything
102 // when the timeout triggers then assume that the test is broken.
103 base::CancelableClosure timeout(
104 base::Bind(&FullStreamUIPolicyTest::TimeoutCallback));
105 base::MessageLoop::current()->PostDelayedTask(
106 FROM_HERE, timeout.callback(), TestTimeouts::action_timeout());
108 // Wait for results; either the checker or the timeout callbacks should
109 // cause the main loop to exit.
110 base::MessageLoop::current()->Run();
112 timeout.Cancel();
115 static void CheckWrapper(
116 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker,
117 const base::Closure& done,
118 scoped_ptr<Action::ActionVector> results) {
119 checker.Run(results.Pass());
120 done.Run();
123 static void TimeoutCallback() {
124 base::MessageLoop::current()->QuitWhenIdle();
125 FAIL() << "Policy test timed out waiting for results";
128 static void RetrieveActions_LogAndFetchActions(
129 scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
130 ASSERT_EQ(2, static_cast<int>(i->size()));
133 static void RetrieveActions_FetchFilteredActions0(
134 scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
135 ASSERT_EQ(0, static_cast<int>(i->size()));
138 static void RetrieveActions_FetchFilteredActions1(
139 scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
140 ASSERT_EQ(1, static_cast<int>(i->size()));
143 static void RetrieveActions_FetchFilteredActions2(
144 scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
145 ASSERT_EQ(2, static_cast<int>(i->size()));
148 static void RetrieveActions_FetchFilteredActions300(
149 scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
150 ASSERT_EQ(300, static_cast<int>(i->size()));
153 static void Arguments_Present(scoped_ptr<Action::ActionVector> i) {
154 scoped_refptr<Action> last = i->front();
155 CheckAction(*last, "odlameecjipmbmbejkplpemijjgpljce",
156 Action::ACTION_API_CALL, "extension.connect",
157 "[\"hello\",\"world\"]", "", "", "");
160 static void Arguments_GetTodaysActions(
161 scoped_ptr<Action::ActionVector> actions) {
162 ASSERT_EQ(2, static_cast<int>(actions->size()));
163 CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets",
164 "[\"vamoose\"]", "http://www.google.com/", "Page Title",
165 "http://www.arg-url.com/");
166 CheckAction(*actions->at(1), "punky", Action::ACTION_API_CALL, "brewster",
167 "[\"woof\"]", "", "Page Title", "http://www.arg-url.com/");
170 static void Arguments_GetOlderActions(
171 scoped_ptr<Action::ActionVector> actions) {
172 ASSERT_EQ(2, static_cast<int>(actions->size()));
173 CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets",
174 "[\"vamoose\"]", "http://www.google.com/", "", "");
175 CheckAction(*actions->at(1), "punky", Action::ACTION_API_CALL, "brewster",
176 "[\"woof\"]", "", "", "");
179 static void AllURLsRemoved(scoped_ptr<Action::ActionVector> actions) {
180 ASSERT_EQ(2, static_cast<int>(actions->size()));
181 CheckAction(*actions->at(0), "punky", Action::ACTION_API_CALL, "lets",
182 "[\"vamoose\"]", "", "", "");
183 CheckAction(*actions->at(1), "punky", Action::ACTION_DOM_ACCESS, "lets",
184 "[\"vamoose\"]", "", "", "");
187 static void SomeURLsRemoved(scoped_ptr<Action::ActionVector> actions) {
188 // These will be in the vector in reverse time order.
189 ASSERT_EQ(5, static_cast<int>(actions->size()));
190 CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets",
191 "[\"vamoose\"]", "http://www.google.com/", "Google",
192 "http://www.args-url.com/");
193 CheckAction(*actions->at(1), "punky", Action::ACTION_DOM_ACCESS, "lets",
194 "[\"vamoose\"]", "http://www.google.com/", "Google", "");
195 CheckAction(*actions->at(2), "punky", Action::ACTION_DOM_ACCESS, "lets",
196 "[\"vamoose\"]", "", "", "");
197 CheckAction(*actions->at(3), "punky", Action::ACTION_DOM_ACCESS, "lets",
198 "[\"vamoose\"]", "", "", "http://www.google.com/");
199 CheckAction(*actions->at(4), "punky", Action::ACTION_DOM_ACCESS, "lets",
200 "[\"vamoose\"]", "", "", "");
203 static void CheckAction(const Action& action,
204 const std::string& expected_id,
205 const Action::ActionType& expected_type,
206 const std::string& expected_api_name,
207 const std::string& expected_args_str,
208 const std::string& expected_page_url,
209 const std::string& expected_page_title,
210 const std::string& expected_arg_url) {
211 ASSERT_EQ(expected_id, action.extension_id());
212 ASSERT_EQ(expected_type, action.action_type());
213 ASSERT_EQ(expected_api_name, action.api_name());
214 ASSERT_EQ(expected_args_str,
215 ActivityLogPolicy::Util::Serialize(action.args()));
216 ASSERT_EQ(expected_page_url, action.SerializePageUrl());
217 ASSERT_EQ(expected_page_title, action.page_title());
218 ASSERT_EQ(expected_arg_url, action.SerializeArgUrl());
221 protected:
222 ExtensionService* extension_service_;
223 scoped_ptr<TestingProfile> profile_;
224 content::TestBrowserThreadBundle thread_bundle_;
225 // Used to preserve a copy of the original command line.
226 // The test framework will do this itself as well. However, by then,
227 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in
228 // TearDown().
229 CommandLine saved_cmdline_;
231 #if defined OS_CHROMEOS
232 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
233 chromeos::ScopedTestCrosSettings test_cros_settings_;
234 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
235 #endif
238 TEST_F(FullStreamUIPolicyTest, Construct) {
239 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
240 policy->Init();
241 scoped_refptr<const Extension> extension =
242 ExtensionBuilder()
243 .SetManifest(DictionaryBuilder()
244 .Set("name", "Test extension")
245 .Set("version", "1.0.0")
246 .Set("manifest_version", 2))
247 .Build();
248 extension_service_->AddExtension(extension.get());
249 scoped_ptr<base::ListValue> args(new base::ListValue());
250 scoped_refptr<Action> action = new Action(extension->id(),
251 base::Time::Now(),
252 Action::ACTION_API_CALL,
253 "tabs.testMethod");
254 action->set_args(args.Pass());
255 policy->ProcessAction(action);
256 policy->Close();
259 TEST_F(FullStreamUIPolicyTest, LogAndFetchActions) {
260 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
261 policy->Init();
262 scoped_refptr<const Extension> extension =
263 ExtensionBuilder()
264 .SetManifest(DictionaryBuilder()
265 .Set("name", "Test extension")
266 .Set("version", "1.0.0")
267 .Set("manifest_version", 2))
268 .Build();
269 extension_service_->AddExtension(extension.get());
270 GURL gurl("http://www.google.com");
272 // Write some API calls
273 scoped_refptr<Action> action_api = new Action(extension->id(),
274 base::Time::Now(),
275 Action::ACTION_API_CALL,
276 "tabs.testMethod");
277 action_api->set_args(make_scoped_ptr(new base::ListValue()));
278 policy->ProcessAction(action_api);
280 scoped_refptr<Action> action_dom = new Action(extension->id(),
281 base::Time::Now(),
282 Action::ACTION_DOM_ACCESS,
283 "document.write");
284 action_dom->set_args(make_scoped_ptr(new base::ListValue()));
285 action_dom->set_page_url(gurl);
286 policy->ProcessAction(action_dom);
288 CheckReadData(
289 policy,
290 extension->id(),
292 base::Bind(&FullStreamUIPolicyTest::RetrieveActions_LogAndFetchActions));
294 policy->Close();
297 TEST_F(FullStreamUIPolicyTest, LogAndFetchFilteredActions) {
298 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
299 policy->Init();
300 scoped_refptr<const Extension> extension =
301 ExtensionBuilder()
302 .SetManifest(DictionaryBuilder()
303 .Set("name", "Test extension")
304 .Set("version", "1.0.0")
305 .Set("manifest_version", 2))
306 .Build();
307 extension_service_->AddExtension(extension.get());
308 GURL gurl("http://www.google.com");
310 // Write some API calls
311 scoped_refptr<Action> action_api = new Action(extension->id(),
312 base::Time::Now(),
313 Action::ACTION_API_CALL,
314 "tabs.testMethod");
315 action_api->set_args(make_scoped_ptr(new base::ListValue()));
316 policy->ProcessAction(action_api);
318 scoped_refptr<Action> action_dom = new Action(extension->id(),
319 base::Time::Now(),
320 Action::ACTION_DOM_ACCESS,
321 "document.write");
322 action_dom->set_args(make_scoped_ptr(new base::ListValue()));
323 action_dom->set_page_url(gurl);
324 policy->ProcessAction(action_dom);
326 CheckReadFilteredData(
327 policy,
328 extension->id(),
329 Action::ACTION_API_CALL,
330 "tabs.testMethod",
334 base::Bind(
335 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
337 CheckReadFilteredData(
338 policy,
340 Action::ACTION_DOM_ACCESS,
345 base::Bind(
346 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
348 CheckReadFilteredData(
349 policy,
351 Action::ACTION_DOM_ACCESS,
353 "http://www.google.com/",
356 base::Bind(
357 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
359 CheckReadFilteredData(
360 policy,
362 Action::ACTION_DOM_ACCESS,
364 "http://www.google.com",
367 base::Bind(
368 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
370 CheckReadFilteredData(
371 policy,
373 Action::ACTION_DOM_ACCESS,
375 "http://www.goo",
378 base::Bind(
379 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
381 CheckReadFilteredData(
382 policy,
383 extension->id(),
384 Action::ACTION_ANY,
389 base::Bind(
390 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions2));
392 policy->Close();
395 TEST_F(FullStreamUIPolicyTest, LogWithArguments) {
396 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
397 policy->Init();
398 scoped_refptr<const Extension> extension =
399 ExtensionBuilder()
400 .SetManifest(DictionaryBuilder()
401 .Set("name", "Test extension")
402 .Set("version", "1.0.0")
403 .Set("manifest_version", 2))
404 .Build();
405 extension_service_->AddExtension(extension.get());
407 scoped_ptr<base::ListValue> args(new base::ListValue());
408 args->Set(0, new base::StringValue("hello"));
409 args->Set(1, new base::StringValue("world"));
410 scoped_refptr<Action> action = new Action(extension->id(),
411 base::Time::Now(),
412 Action::ACTION_API_CALL,
413 "extension.connect");
414 action->set_args(args.Pass());
416 policy->ProcessAction(action);
417 CheckReadData(policy,
418 extension->id(),
420 base::Bind(&FullStreamUIPolicyTest::Arguments_Present));
421 policy->Close();
424 TEST_F(FullStreamUIPolicyTest, GetTodaysActions) {
425 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
426 policy->Init();
428 // Use a mock clock to ensure that events are not recorded on the wrong day
429 // when the test is run close to local midnight. Note: Ownership is passed
430 // to the policy, but we still keep a pointer locally. The policy will take
431 // care of destruction; this is safe since the policy outlives all our
432 // accesses to the mock clock.
433 base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
434 mock_clock->SetNow(base::Time::Now().LocalMidnight() +
435 base::TimeDelta::FromHours(12));
436 policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
438 // Record some actions
439 scoped_refptr<Action> action =
440 new Action("punky",
441 mock_clock->Now() - base::TimeDelta::FromMinutes(40),
442 Action::ACTION_API_CALL,
443 "brewster");
444 action->mutable_args()->AppendString("woof");
445 action->set_arg_url(GURL("http://www.arg-url.com"));
446 action->set_page_title("Page Title");
447 policy->ProcessAction(action);
449 action =
450 new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
451 action->mutable_args()->AppendString("vamoose");
452 action->set_page_url(GURL("http://www.google.com"));
453 action->set_arg_url(GURL("http://www.arg-url.com"));
454 action->set_page_title("Page Title");
455 policy->ProcessAction(action);
457 action = new Action(
458 "scoobydoo", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
459 action->mutable_args()->AppendString("vamoose");
460 action->set_page_url(GURL("http://www.google.com"));
461 action->set_arg_url(GURL("http://www.arg-url.com"));
462 policy->ProcessAction(action);
464 CheckReadData(
465 policy,
466 "punky",
468 base::Bind(&FullStreamUIPolicyTest::Arguments_GetTodaysActions));
469 policy->Close();
472 // Check that we can read back less recent actions in the db.
473 TEST_F(FullStreamUIPolicyTest, GetOlderActions) {
474 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
475 policy->Init();
477 // Use a mock clock to ensure that events are not recorded on the wrong day
478 // when the test is run close to local midnight.
479 base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
480 mock_clock->SetNow(base::Time::Now().LocalMidnight() +
481 base::TimeDelta::FromHours(12));
482 policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
484 // Record some actions
485 scoped_refptr<Action> action =
486 new Action("punky",
487 mock_clock->Now() - base::TimeDelta::FromDays(3) -
488 base::TimeDelta::FromMinutes(40),
489 Action::ACTION_API_CALL,
490 "brewster");
491 action->mutable_args()->AppendString("woof");
492 policy->ProcessAction(action);
494 action = new Action("punky",
495 mock_clock->Now() - base::TimeDelta::FromDays(3),
496 Action::ACTION_DOM_ACCESS,
497 "lets");
498 action->mutable_args()->AppendString("vamoose");
499 action->set_page_url(GURL("http://www.google.com"));
500 policy->ProcessAction(action);
502 action = new Action("punky",
503 mock_clock->Now(),
504 Action::ACTION_DOM_ACCESS,
505 "lets");
506 action->mutable_args()->AppendString("too new");
507 action->set_page_url(GURL("http://www.google.com"));
508 policy->ProcessAction(action);
510 action = new Action("punky",
511 mock_clock->Now() - base::TimeDelta::FromDays(7),
512 Action::ACTION_DOM_ACCESS,
513 "lets");
514 action->mutable_args()->AppendString("too old");
515 action->set_page_url(GURL("http://www.google.com"));
516 policy->ProcessAction(action);
518 CheckReadData(
519 policy,
520 "punky",
522 base::Bind(&FullStreamUIPolicyTest::Arguments_GetOlderActions));
523 policy->Close();
526 TEST_F(FullStreamUIPolicyTest, RemoveAllURLs) {
527 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
528 policy->Init();
530 // Use a mock clock to ensure that events are not recorded on the wrong day
531 // when the test is run close to local midnight.
532 base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
533 mock_clock->SetNow(base::Time::Now().LocalMidnight() +
534 base::TimeDelta::FromHours(12));
535 policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
537 // Record some actions
538 scoped_refptr<Action> action =
539 new Action("punky", mock_clock->Now(),
540 Action::ACTION_DOM_ACCESS, "lets");
541 action->mutable_args()->AppendString("vamoose");
542 action->set_page_url(GURL("http://www.google.com"));
543 action->set_page_title("Google");
544 action->set_arg_url(GURL("http://www.google.com"));
545 policy->ProcessAction(action);
547 mock_clock->Advance(base::TimeDelta::FromSeconds(1));
548 action = new Action(
549 "punky", mock_clock->Now(), Action::ACTION_API_CALL, "lets");
550 action->mutable_args()->AppendString("vamoose");
551 action->set_page_url(GURL("http://www.google2.com"));
552 action->set_page_title("Google");
553 // Deliberately no arg url set to make sure it still works when there is no
554 // arg url.
555 policy->ProcessAction(action);
557 // Clean all the URLs.
558 std::vector<GURL> no_url_restrictions;
559 policy->RemoveURLs(no_url_restrictions);
561 CheckReadData(
562 policy,
563 "punky",
565 base::Bind(&FullStreamUIPolicyTest::AllURLsRemoved));
566 policy->Close();
569 TEST_F(FullStreamUIPolicyTest, RemoveSpecificURLs) {
570 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
571 policy->Init();
573 // Use a mock clock to ensure that events are not recorded on the wrong day
574 // when the test is run close to local midnight.
575 base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
576 mock_clock->SetNow(base::Time::Now().LocalMidnight() +
577 base::TimeDelta::FromHours(12));
578 policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
580 // Record some actions
581 // This should have the page url and args url cleared.
582 scoped_refptr<Action> action = new Action("punky", mock_clock->Now(),
583 Action::ACTION_DOM_ACCESS, "lets");
584 action->mutable_args()->AppendString("vamoose");
585 action->set_page_url(GURL("http://www.google1.com"));
586 action->set_page_title("Google");
587 action->set_arg_url(GURL("http://www.google1.com"));
588 policy->ProcessAction(action);
590 // This should have the page url cleared but not args url.
591 mock_clock->Advance(base::TimeDelta::FromSeconds(1));
592 action = new Action(
593 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
594 action->mutable_args()->AppendString("vamoose");
595 action->set_page_url(GURL("http://www.google1.com"));
596 action->set_page_title("Google");
597 action->set_arg_url(GURL("http://www.google.com"));
598 policy->ProcessAction(action);
600 // This should have the page url cleared. The args url is deliberately not set
601 // to make sure this doesn't cause any issues.
602 mock_clock->Advance(base::TimeDelta::FromSeconds(1));
603 action = new Action(
604 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
605 action->mutable_args()->AppendString("vamoose");
606 action->set_page_url(GURL("http://www.google2.com"));
607 action->set_page_title("Google");
608 policy->ProcessAction(action);
610 // This should have the args url cleared but not the page url or page title.
611 mock_clock->Advance(base::TimeDelta::FromSeconds(1));
612 action = new Action(
613 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
614 action->mutable_args()->AppendString("vamoose");
615 action->set_page_url(GURL("http://www.google.com"));
616 action->set_page_title("Google");
617 action->set_arg_url(GURL("http://www.google1.com"));
618 policy->ProcessAction(action);
620 // This should have neither cleared.
621 mock_clock->Advance(base::TimeDelta::FromSeconds(1));
622 action = new Action(
623 "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
624 action->mutable_args()->AppendString("vamoose");
625 action->set_page_url(GURL("http://www.google.com"));
626 action->set_page_title("Google");
627 action->set_arg_url(GURL("http://www.args-url.com"));
628 policy->ProcessAction(action);
630 // Clean some URLs.
631 std::vector<GURL> urls;
632 urls.push_back(GURL("http://www.google1.com"));
633 urls.push_back(GURL("http://www.google2.com"));
634 urls.push_back(GURL("http://www.url_not_in_db.com"));
635 policy->RemoveURLs(urls);
637 CheckReadData(
638 policy,
639 "punky",
641 base::Bind(&FullStreamUIPolicyTest::SomeURLsRemoved));
642 policy->Close();
645 TEST_F(FullStreamUIPolicyTest, RemoveExtensionData) {
646 FullStreamUIPolicy* policy = new FullStreamUIPolicy(profile_.get());
647 policy->Init();
649 // Use a mock clock to ensure that events are not recorded on the wrong day
650 // when the test is run close to local midnight.
651 base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
652 mock_clock->SetNow(base::Time::Now().LocalMidnight() +
653 base::TimeDelta::FromHours(12));
654 policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
656 // Record some actions
657 scoped_refptr<Action> action = new Action("deleteextensiondata",
658 mock_clock->Now(),
659 Action::ACTION_DOM_ACCESS,
660 "lets");
661 action->mutable_args()->AppendString("vamoose");
662 action->set_page_title("Google");
663 action->set_arg_url(GURL("http://www.google.com"));
664 policy->ProcessAction(action);
665 policy->ProcessAction(action);
666 policy->ProcessAction(action);
668 scoped_refptr<Action> action2 = new Action("dontdelete",
669 mock_clock->Now(),
670 Action::ACTION_DOM_ACCESS,
671 "lets");
672 action->mutable_args()->AppendString("vamoose");
673 action->set_page_title("Google");
674 action->set_arg_url(GURL("http://www.google.com"));
675 policy->ProcessAction(action2);
677 policy->Flush();
678 policy->RemoveExtensionData("deleteextensiondata");
680 CheckReadFilteredData(
681 policy,
682 "deleteextensiondata",
683 Action::ACTION_ANY,
688 base::Bind(
689 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions0));
691 CheckReadFilteredData(
692 policy,
693 "dontdelete",
694 Action::ACTION_ANY,
699 base::Bind(
700 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
701 policy->Close();
704 TEST_F(FullStreamUIPolicyTest, CapReturns) {
705 FullStreamUIPolicy* policy = new FullStreamUIPolicy(profile_.get());
706 policy->Init();
708 for (int i = 0; i < 305; i++) {
709 scoped_refptr<Action> action =
710 new Action("punky",
711 base::Time::Now(),
712 Action::ACTION_API_CALL,
713 base::StringPrintf("apicall_%d", i));
714 policy->ProcessAction(action);
717 policy->Flush();
718 BrowserThread::PostTaskAndReply(
719 BrowserThread::DB,
720 FROM_HERE,
721 base::Bind(&base::DoNothing),
722 base::MessageLoop::current()->QuitClosure());
723 base::MessageLoop::current()->Run();
725 CheckReadFilteredData(
726 policy,
727 "punky",
728 Action::ACTION_ANY,
733 base::Bind(
734 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions300));
735 policy->Close();
738 TEST_F(FullStreamUIPolicyTest, DeleteActions) {
739 ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
740 policy->Init();
741 scoped_refptr<const Extension> extension =
742 ExtensionBuilder()
743 .SetManifest(DictionaryBuilder()
744 .Set("name", "Test extension")
745 .Set("version", "1.0.0")
746 .Set("manifest_version", 2))
747 .Build();
748 extension_service_->AddExtension(extension.get());
749 GURL gurl("http://www.google.com");
751 // Write some API calls.
752 scoped_refptr<Action> action_api = new Action(extension->id(),
753 base::Time::Now(),
754 Action::ACTION_API_CALL,
755 "tabs.testMethod");
756 action_api->set_args(make_scoped_ptr(new base::ListValue()));
757 policy->ProcessAction(action_api);
759 scoped_refptr<Action> action_dom = new Action(extension->id(),
760 base::Time::Now(),
761 Action::ACTION_DOM_ACCESS,
762 "document.write");
763 action_dom->set_args(make_scoped_ptr(new base::ListValue()));
764 action_dom->set_page_url(gurl);
765 policy->ProcessAction(action_dom);
767 CheckReadData(
768 policy,
769 extension->id(),
771 base::Bind(&FullStreamUIPolicyTest::RetrieveActions_LogAndFetchActions));
773 // Now delete them.
774 policy->DeleteDatabase();
776 CheckReadFilteredData(
777 policy,
779 Action::ACTION_ANY,
784 base::Bind(
785 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions0));
787 policy->Close();
790 } // namespace extensions