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"
32 namespace extensions
{
34 class FullStreamUIPolicyTest
: public testing::Test
{
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());
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();
58 base::RunLoop().RunUntilIdle();
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.
68 ActivityLogDatabasePolicy
* policy
,
69 const std::string
& extension_id
,
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
,
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
90 policy
->ReadFilteredData(
97 base::Bind(&FullStreamUIPolicyTest::CheckWrapper
,
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();
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());
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());
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
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_
;
238 TEST_F(FullStreamUIPolicyTest
, Construct
) {
239 ActivityLogDatabasePolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
241 scoped_refptr
<const Extension
> extension
=
243 .SetManifest(DictionaryBuilder()
244 .Set("name", "Test extension")
245 .Set("version", "1.0.0")
246 .Set("manifest_version", 2))
248 extension_service_
->AddExtension(extension
.get());
249 scoped_ptr
<base::ListValue
> args(new base::ListValue());
250 scoped_refptr
<Action
> action
= new Action(extension
->id(),
252 Action::ACTION_API_CALL
,
254 action
->set_args(args
.Pass());
255 policy
->ProcessAction(action
);
259 TEST_F(FullStreamUIPolicyTest
, LogAndFetchActions
) {
260 ActivityLogDatabasePolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
262 scoped_refptr
<const Extension
> extension
=
264 .SetManifest(DictionaryBuilder()
265 .Set("name", "Test extension")
266 .Set("version", "1.0.0")
267 .Set("manifest_version", 2))
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(),
275 Action::ACTION_API_CALL
,
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(),
282 Action::ACTION_DOM_ACCESS
,
284 action_dom
->set_args(make_scoped_ptr(new base::ListValue()));
285 action_dom
->set_page_url(gurl
);
286 policy
->ProcessAction(action_dom
);
292 base::Bind(&FullStreamUIPolicyTest::RetrieveActions_LogAndFetchActions
));
297 TEST_F(FullStreamUIPolicyTest
, LogAndFetchFilteredActions
) {
298 ActivityLogDatabasePolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
300 scoped_refptr
<const Extension
> extension
=
302 .SetManifest(DictionaryBuilder()
303 .Set("name", "Test extension")
304 .Set("version", "1.0.0")
305 .Set("manifest_version", 2))
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(),
313 Action::ACTION_API_CALL
,
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(),
320 Action::ACTION_DOM_ACCESS
,
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(
329 Action::ACTION_API_CALL
,
335 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1
));
337 CheckReadFilteredData(
340 Action::ACTION_DOM_ACCESS
,
346 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1
));
348 CheckReadFilteredData(
351 Action::ACTION_DOM_ACCESS
,
353 "http://www.google.com/",
357 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1
));
359 CheckReadFilteredData(
362 Action::ACTION_DOM_ACCESS
,
364 "http://www.google.com",
368 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1
));
370 CheckReadFilteredData(
373 Action::ACTION_DOM_ACCESS
,
379 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1
));
381 CheckReadFilteredData(
390 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions2
));
395 TEST_F(FullStreamUIPolicyTest
, LogWithArguments
) {
396 ActivityLogDatabasePolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
398 scoped_refptr
<const Extension
> extension
=
400 .SetManifest(DictionaryBuilder()
401 .Set("name", "Test extension")
402 .Set("version", "1.0.0")
403 .Set("manifest_version", 2))
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(),
412 Action::ACTION_API_CALL
,
413 "extension.connect");
414 action
->set_args(args
.Pass());
416 policy
->ProcessAction(action
);
417 CheckReadData(policy
,
420 base::Bind(&FullStreamUIPolicyTest::Arguments_Present
));
424 TEST_F(FullStreamUIPolicyTest
, GetTodaysActions
) {
425 ActivityLogDatabasePolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
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
=
441 mock_clock
->Now() - base::TimeDelta::FromMinutes(40),
442 Action::ACTION_API_CALL
,
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
);
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
);
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
);
468 base::Bind(&FullStreamUIPolicyTest::Arguments_GetTodaysActions
));
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());
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
=
487 mock_clock
->Now() - base::TimeDelta::FromDays(3) -
488 base::TimeDelta::FromMinutes(40),
489 Action::ACTION_API_CALL
,
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
,
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",
504 Action::ACTION_DOM_ACCESS
,
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
,
514 action
->mutable_args()->AppendString("too old");
515 action
->set_page_url(GURL("http://www.google.com"));
516 policy
->ProcessAction(action
);
522 base::Bind(&FullStreamUIPolicyTest::Arguments_GetOlderActions
));
526 TEST_F(FullStreamUIPolicyTest
, RemoveAllURLs
) {
527 ActivityLogDatabasePolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
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));
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
555 policy
->ProcessAction(action
);
557 // Clean all the URLs.
558 std::vector
<GURL
> no_url_restrictions
;
559 policy
->RemoveURLs(no_url_restrictions
);
565 base::Bind(&FullStreamUIPolicyTest::AllURLsRemoved
));
569 TEST_F(FullStreamUIPolicyTest
, RemoveSpecificURLs
) {
570 ActivityLogDatabasePolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
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));
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));
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));
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));
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
);
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
);
641 base::Bind(&FullStreamUIPolicyTest::SomeURLsRemoved
));
645 TEST_F(FullStreamUIPolicyTest
, RemoveExtensionData
) {
646 FullStreamUIPolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
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",
659 Action::ACTION_DOM_ACCESS
,
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",
670 Action::ACTION_DOM_ACCESS
,
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
);
678 policy
->RemoveExtensionData("deleteextensiondata");
680 CheckReadFilteredData(
682 "deleteextensiondata",
689 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions0
));
691 CheckReadFilteredData(
700 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1
));
704 TEST_F(FullStreamUIPolicyTest
, CapReturns
) {
705 FullStreamUIPolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
708 for (int i
= 0; i
< 305; i
++) {
709 scoped_refptr
<Action
> action
=
712 Action::ACTION_API_CALL
,
713 base::StringPrintf("apicall_%d", i
));
714 policy
->ProcessAction(action
);
718 BrowserThread::PostTaskAndReply(
721 base::Bind(&base::DoNothing
),
722 base::MessageLoop::current()->QuitClosure());
723 base::MessageLoop::current()->Run();
725 CheckReadFilteredData(
734 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions300
));
738 TEST_F(FullStreamUIPolicyTest
, DeleteActions
) {
739 ActivityLogDatabasePolicy
* policy
= new FullStreamUIPolicy(profile_
.get());
741 scoped_refptr
<const Extension
> extension
=
743 .SetManifest(DictionaryBuilder()
744 .Set("name", "Test extension")
745 .Set("version", "1.0.0")
746 .Set("manifest_version", 2))
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(),
754 Action::ACTION_API_CALL
,
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(),
761 Action::ACTION_DOM_ACCESS
,
763 action_dom
->set_args(make_scoped_ptr(new base::ListValue()));
764 action_dom
->set_page_url(gurl
);
765 policy
->ProcessAction(action_dom
);
771 base::Bind(&FullStreamUIPolicyTest::RetrieveActions_LogAndFetchActions
));
774 policy
->DeleteDatabase();
776 CheckReadFilteredData(
785 &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions0
));
790 } // namespace extensions