17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/cancelable_callback.h"
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/command_line.h"
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/run_loop.h"
958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/strings/stringprintf.h"
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/synchronization/waitable_event.h"
11a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/test/simple_test_clock.h"
1268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "base/test/test_timeouts.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/extensions/activity_log/activity_log.h"
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h"
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/extensions/test_extension_system.h"
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/test/base/chrome_render_view_host_test_harness.h"
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension_builder.h"
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "sql/statement.h"
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#if defined(OS_CHROMEOS)
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/chromeos/settings/cros_settings.h"
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/chromeos/settings/device_settings_service.h"
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using content::BrowserThread;
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace extensions {
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class FullStreamUIPolicyTest : public testing::Test {
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FullStreamUIPolicyTest()
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        saved_cmdline_(CommandLine::NO_PROGRAM) {
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#if defined OS_CHROMEOS
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    test_user_manager_.reset(new chromeos::ScopedTestUserManager());
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif
447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    CommandLine command_line(CommandLine::NO_PROGRAM);
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    saved_cmdline_ = *CommandLine::ForCurrentProcess();
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    profile_.reset(new TestingProfile());
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    CommandLine::ForCurrentProcess()->AppendSwitch(
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        switches::kEnableExtensionActivityLogging);
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    CommandLine::ForCurrentProcess()->AppendSwitch(
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        switches::kEnableExtensionActivityLogTesting);
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    extension_service_ = static_cast<TestExtensionSystem*>(
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        ExtensionSystem::Get(profile_.get()))->CreateExtensionService
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            (&command_line, base::FilePath(), false);
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ~FullStreamUIPolicyTest() {
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#if defined OS_CHROMEOS
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    test_user_manager_.reset();
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    profile_.reset(NULL);
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Restore the original command line and undo the affects of SetUp().
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    *CommandLine::ForCurrentProcess() = saved_cmdline_;
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
6758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // A wrapper function for CheckReadFilteredData, so that we don't need to
6858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // enter empty string values for parameters we don't care about.
69a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void CheckReadData(
7068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      ActivityLogDatabasePolicy* policy,
71a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      const std::string& extension_id,
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      int day,
73a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CheckReadFilteredData(
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        policy, extension_id, Action::ACTION_ANY, "", "", "", day, checker);
76a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
77a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
78424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // A helper function to call ReadFilteredData on a policy object and wait for
79424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // the results to be processed.
80424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void CheckReadFilteredData(
8168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      ActivityLogDatabasePolicy* policy,
82424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const std::string& extension_id,
83424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const Action::ActionType type,
84424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const std::string& api_name,
85424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const std::string& page_url,
86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const std::string& arg_url,
8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const int days_ago,
88424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
89424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Submit a request to the policy to read back some data, and call the
90424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // checker function when results are available.  This will happen on the
91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // database thread.
92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    policy->ReadFilteredData(
93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        extension_id,
94424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        type,
95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        api_name,
96424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        page_url,
97424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        arg_url,
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        days_ago,
99424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        base::Bind(&FullStreamUIPolicyTest::CheckWrapper,
100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   checker,
101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   base::MessageLoop::current()->QuitClosure()));
102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
10368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // Set up a timeout for receiving results; if we haven't received anything
10468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // when the timeout triggers then assume that the test is broken.
105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    base::CancelableClosure timeout(
106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        base::Bind(&FullStreamUIPolicyTest::TimeoutCallback));
107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    base::MessageLoop::current()->PostDelayedTask(
10868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        FROM_HERE, timeout.callback(), TestTimeouts::action_timeout());
109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Wait for results; either the checker or the timeout callbacks should
111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // cause the main loop to exit.
112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    base::MessageLoop::current()->Run();
113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    timeout.Cancel();
115424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
117a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static void CheckWrapper(
118a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker,
119a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      const base::Closure& done,
120a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      scoped_ptr<Action::ActionVector> results) {
121a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    checker.Run(results.Pass());
122a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    done.Run();
123a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
124a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
125a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static void TimeoutCallback() {
126a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    base::MessageLoop::current()->QuitWhenIdle();
127a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    FAIL() << "Policy test timed out waiting for results";
128a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
129a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static void RetrieveActions_LogAndFetchActions(
1317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
1327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(i->size()));
1337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  static void RetrieveActions_FetchFilteredActions0(
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(0, static_cast<int>(i->size()));
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void RetrieveActions_FetchFilteredActions1(
141424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(1, static_cast<int>(i->size()));
143424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
144424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void RetrieveActions_FetchFilteredActions2(
146424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
147424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(i->size()));
148424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
15058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  static void RetrieveActions_FetchFilteredActions300(
15158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
15258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(300, static_cast<int>(i->size()));
15358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
15458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
155a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static void Arguments_Present(scoped_ptr<Action::ActionVector> i) {
1567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    scoped_refptr<Action> last = i->front();
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*last.get(),
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "odlameecjipmbmbejkplpemijjgpljce",
1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_API_CALL,
1601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "extension.connect",
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"hello\",\"world\"]",
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "");
1657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
167a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static void Arguments_GetTodaysActions(
168a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      scoped_ptr<Action::ActionVector> actions) {
169a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
1701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
1711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
1731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
1761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "Page Title",
1771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.arg-url.com/");
1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_API_CALL,
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "brewster",
1821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"woof\"]",
1831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
1841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "Page Title",
18558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "http://www.arg-url.com/");
186a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
187a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
188a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static void Arguments_GetOlderActions(
189a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      scoped_ptr<Action::ActionVector> actions) {
190a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
1911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
1921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
1931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
1941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
1951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
1961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
1971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
1981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "");
1991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
2001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_API_CALL,
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "brewster",
2031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"woof\"]",
2041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "");
207a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
208a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
209424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void AllURLsRemoved(scoped_ptr<Action::ActionVector> actions) {
210424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
2111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
2121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_API_CALL,
2141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
2161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "");
2191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
2201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
2241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "");
227424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
228424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
229424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void SomeURLsRemoved(scoped_ptr<Action::ActionVector> actions) {
230424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // These will be in the vector in reverse time order.
231424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(5, static_cast<int>(actions->size()));
2321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
2331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
2371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
2381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "Google",
239424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                "http://www.args-url.com/");
2401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
2411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
2451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
2461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "Google",
2471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "");
2481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(2).get(),
2491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
2531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "");
2561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(3).get(),
2571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
2611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/");
2641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(4).get(),
2651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"vamoose\"]",
2691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "");
272424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
273424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
274424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void CheckAction(const Action& action,
275424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_id,
276424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const Action::ActionType& expected_type,
277424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_api_name,
278424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_args_str,
279424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_page_url,
280424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_page_title,
281424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_arg_url) {
282424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_id, action.extension_id());
283424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_type, action.action_type());
284424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_api_name, action.api_name());
285424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_args_str,
286424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              ActivityLogPolicy::Util::Serialize(action.args()));
287424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_page_url, action.SerializePageUrl());
288424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_page_title, action.page_title());
289424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_arg_url, action.SerializeArgUrl());
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_NE(-1, action.action_id());
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A helper function initializes the policy with a number of actions, calls
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // RemoveActions on a policy object and then checks the result of the
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // deletion.
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void CheckRemoveActions(
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ActivityLogDatabasePolicy* policy,
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<int64>& action_ids,
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Use a mock clock to ensure that events are not recorded on the wrong day
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // when the test is run close to local midnight.
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mock_clock->SetNow(base::Time::Now().LocalMidnight() +
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       base::TimeDelta::FromHours(12));
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record some actions
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<Action> action =
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new Action("punky1",
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   mock_clock->Now() - base::TimeDelta::FromMinutes(40),
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   Action::ACTION_DOM_ACCESS,
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   "lets1");
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->mutable_args()->AppendString("vamoose1");
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_url(GURL("http://www.google1.com"));
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_title("Google1");
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_arg_url(GURL("http://www.args-url1.com"));
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record the same action twice, so there are multiple entries in the
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // database.
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action = new Action("punky2",
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        mock_clock->Now() - base::TimeDelta::FromMinutes(30),
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        Action::ACTION_API_CALL,
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        "lets2");
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->mutable_args()->AppendString("vamoose2");
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_url(GURL("http://www.google2.com"));
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_title("Google2");
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_arg_url(GURL("http://www.args-url2.com"));
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record the same action twice, so there are multiple entries in the
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // database.
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Submit a request to delete actions.
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->RemoveActions(action_ids);
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Check the result of the deletion. The checker function gets all
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // activities in the database.
3415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckReadData(policy, "", -1, checker);
3425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Clean database.
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->DeleteDatabase();
3455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void AllActionsDeleted(scoped_ptr<Action::ActionVector> actions) {
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(0, static_cast<int>(actions->size()));
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void NoActionsDeleted(scoped_ptr<Action::ActionVector> actions) {
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
3535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(4, static_cast<int>(actions->size()));
3541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
3555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky2",
3565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_API_CALL,
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets2",
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "[\"vamoose2\"]",
3595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google2.com/",
3605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google2",
3615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url2.com/");
3625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(3, actions->at(0)->action_id());
3631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky2",
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_API_CALL,
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets2",
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "[\"vamoose2\"]",
3685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google2.com/",
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google2",
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url2.com/");
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(4, actions->at(1)->action_id());
3721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(2).get(),
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky1",
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_DOM_ACCESS,
3755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets1",
3765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "[\"vamoose1\"]",
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google1.com/",
3785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google1",
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url1.com/");
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, actions->at(2)->action_id());
3811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(3).get(),
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky1",
3835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_DOM_ACCESS,
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets1",
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "[\"vamoose1\"]",
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google1.com/",
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google1",
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url1.com/");
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, actions->at(3)->action_id());
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void Action1Deleted(scoped_ptr<Action::ActionVector> actions) {
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
3951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky2",
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_API_CALL,
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets2",
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "[\"vamoose2\"]",
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google2.com/",
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google2",
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url2.com/");
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(3, actions->at(0)->action_id());
4041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky2",
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_API_CALL,
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets2",
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "[\"vamoose2\"]",
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google2.com/",
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google2",
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url2.com/");
4125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(4, actions->at(1)->action_id());
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void Action2Deleted(scoped_ptr<Action::ActionVector> actions) {
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
4181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky1",
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_DOM_ACCESS,
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets1",
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "[\"vamoose1\"]",
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google1.com/",
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google1",
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url1.com/");
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, actions->at(0)->action_id());
4271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky1",
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_DOM_ACCESS,
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets1",
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "[\"vamoose1\"]",
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google1.com/",
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google1",
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url1.com/");
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, actions->at(1)->action_id());
436424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
437424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
4387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) protected:
4397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ExtensionService* extension_service_;
4407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_ptr<TestingProfile> profile_;
4417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  content::TestBrowserThreadBundle thread_bundle_;
4427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Used to preserve a copy of the original command line.
4437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // The test framework will do this itself as well. However, by then,
4447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in
4457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // TearDown().
4467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  CommandLine saved_cmdline_;
4477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#if defined OS_CHROMEOS
4497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
4507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  chromeos::ScopedTestCrosSettings test_cros_settings_;
4517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
4527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif
4537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
4547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, Construct) {
45668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
4574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
4587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_refptr<const Extension> extension =
4597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      ExtensionBuilder()
4607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          .SetManifest(DictionaryBuilder()
4617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("name", "Test extension")
4627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("version", "1.0.0")
4637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("manifest_version", 2))
4647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          .Build();
465eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  extension_service_->AddExtension(extension.get());
466eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  scoped_ptr<base::ListValue> args(new base::ListValue());
467558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  scoped_refptr<Action> action = new Action(extension->id(),
468558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                            base::Time::Now(),
469558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                            Action::ACTION_API_CALL,
470558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                            "tabs.testMethod");
471558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  action->set_args(args.Pass());
472558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  policy->ProcessAction(action);
4737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  policy->Close();
4747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
4757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, LogAndFetchActions) {
47768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
4784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
4797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_refptr<const Extension> extension =
4807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      ExtensionBuilder()
4817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          .SetManifest(DictionaryBuilder()
4827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("name", "Test extension")
4837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("version", "1.0.0")
4847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("manifest_version", 2))
4857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          .Build();
486eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  extension_service_->AddExtension(extension.get());
4877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  GURL gurl("http://www.google.com");
4887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Write some API calls
490558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  scoped_refptr<Action> action_api = new Action(extension->id(),
491558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                                base::Time::Now(),
492558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                                Action::ACTION_API_CALL,
493558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                                "tabs.testMethod");
494558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  action_api->set_args(make_scoped_ptr(new base::ListValue()));
495558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  policy->ProcessAction(action_api);
496558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
497558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  scoped_refptr<Action> action_dom = new Action(extension->id(),
498558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                                base::Time::Now(),
499558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                                Action::ACTION_DOM_ACCESS,
500558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                                "document.write");
501558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  action_dom->set_args(make_scoped_ptr(new base::ListValue()));
502558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  action_dom->set_page_url(gurl);
503558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  policy->ProcessAction(action_dom);
504558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
505a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  CheckReadData(
506a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      policy,
507a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      extension->id(),
508a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      0,
509a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&FullStreamUIPolicyTest::RetrieveActions_LogAndFetchActions));
5107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
5117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  policy->Close();
5127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
5137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
514424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, LogAndFetchFilteredActions) {
51568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
5164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
517424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<const Extension> extension =
518424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      ExtensionBuilder()
519424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          .SetManifest(DictionaryBuilder()
520424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("name", "Test extension")
521424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("version", "1.0.0")
522424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("manifest_version", 2))
523424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          .Build();
524424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  extension_service_->AddExtension(extension.get());
525424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GURL gurl("http://www.google.com");
526424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
527424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Write some API calls
528424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action_api = new Action(extension->id(),
529424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                base::Time::Now(),
530424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                Action::ACTION_API_CALL,
531424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                "tabs.testMethod");
532424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_api->set_args(make_scoped_ptr(new base::ListValue()));
533424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action_api);
534424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
535424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action_dom = new Action(extension->id(),
536424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                base::Time::Now(),
537424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                Action::ACTION_DOM_ACCESS,
538424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                "document.write");
539424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_dom->set_args(make_scoped_ptr(new base::ListValue()));
540424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_dom->set_page_url(gurl);
541424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action_dom);
542424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
543424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
544424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
545424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      extension->id(),
546424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_API_CALL,
547424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "tabs.testMethod",
548424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
549424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
55058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
551424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
552424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
553424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
554424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
555424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
556424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
557424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
558424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
559424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
560424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
56158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
562424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
563424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
564424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
565424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
566424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
567424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
568424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
569424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
570424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.google.com/",
571424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
57258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
573424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
574424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
575424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
576424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
577424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
578424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
579424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
580424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
581424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.google.com",
582424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
58358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
584424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
585424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
586424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
587424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
588424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
589424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
590424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
591424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
592424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.goo",
593424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
59458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
595424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
596424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
597424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
598424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
599424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
600424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      extension->id(),
601424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_ANY,
602424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
603424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
604424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
60558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
606424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
607424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions2));
608424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
609424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
610424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
611424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
6127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, LogWithArguments) {
61368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
6144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
6157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_refptr<const Extension> extension =
6167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      ExtensionBuilder()
6177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          .SetManifest(DictionaryBuilder()
6187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("name", "Test extension")
6197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("version", "1.0.0")
6207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                       .Set("manifest_version", 2))
6217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          .Build();
622eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  extension_service_->AddExtension(extension.get());
623558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
624eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  scoped_ptr<base::ListValue> args(new base::ListValue());
6257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  args->Set(0, new base::StringValue("hello"));
6267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  args->Set(1, new base::StringValue("world"));
627558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  scoped_refptr<Action> action = new Action(extension->id(),
628558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                            base::Time::Now(),
629558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                            Action::ACTION_API_CALL,
630558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                            "extension.connect");
631558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  action->set_args(args.Pass());
632558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
633558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  policy->ProcessAction(action);
634a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  CheckReadData(policy,
635a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                extension->id(),
636a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                0,
637a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                base::Bind(&FullStreamUIPolicyTest::Arguments_Present));
638a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->Close();
639a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
640a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
641a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, GetTodaysActions) {
64268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
6434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
644a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
645a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
646ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // when the test is run close to local midnight.  Note: Ownership is passed
647ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // to the policy, but we still keep a pointer locally.  The policy will take
648ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // care of destruction; this is safe since the policy outlives all our
649ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // accesses to the mock clock.
650ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
651ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
652ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromHours(12));
653ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
654a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
655a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Record some actions
656a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_refptr<Action> action =
657a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      new Action("punky",
658ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 mock_clock->Now() - base::TimeDelta::FromMinutes(40),
659a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 Action::ACTION_API_CALL,
660a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 "brewster");
661a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->mutable_args()->AppendString("woof");
662424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.arg-url.com"));
663424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Page Title");
664a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->ProcessAction(action);
665a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
666a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action =
667ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
668a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
669a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
670424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.arg-url.com"));
671424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Page Title");
672a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->ProcessAction(action);
673a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
674a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action = new Action(
675ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      "scoobydoo", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
676a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
677a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
678424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.arg-url.com"));
679a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->ProcessAction(action);
680a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
681a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  CheckReadData(
682a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      policy,
683a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      "punky",
684a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      0,
685a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&FullStreamUIPolicyTest::Arguments_GetTodaysActions));
686a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->Close();
687a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
688a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
689a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Check that we can read back less recent actions in the db.
690a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, GetOlderActions) {
69168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
6924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
693a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
694a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
695a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // when the test is run close to local midnight.
696ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
697ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
698ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromHours(12));
699ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
700a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
701a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Record some actions
702a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  scoped_refptr<Action> action =
703a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      new Action("punky",
704ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 mock_clock->Now() - base::TimeDelta::FromDays(3) -
705a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                     base::TimeDelta::FromMinutes(40),
706a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 Action::ACTION_API_CALL,
707a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 "brewster");
708a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->mutable_args()->AppendString("woof");
709a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->ProcessAction(action);
710a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
711a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action = new Action("punky",
712ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(3),
713a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                      Action::ACTION_DOM_ACCESS,
714a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                      "lets");
715a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
716a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
717a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->ProcessAction(action);
718a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
719a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action = new Action("punky",
720ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now(),
721a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                      Action::ACTION_DOM_ACCESS,
722a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                      "lets");
723a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->mutable_args()->AppendString("too new");
724a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
725a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->ProcessAction(action);
726a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
727a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action = new Action("punky",
728ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(7),
729a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                      Action::ACTION_DOM_ACCESS,
730a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                      "lets");
731a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->mutable_args()->AppendString("too old");
732a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
733a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  policy->ProcessAction(action);
734a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
735a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  CheckReadData(
736a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      policy,
737a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      "punky",
738a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      3,
739a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      base::Bind(&FullStreamUIPolicyTest::Arguments_GetOlderActions));
7407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  policy->Close();
7417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
7427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
743424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, RemoveAllURLs) {
74468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
7454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
746424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
747424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
748424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // when the test is run close to local midnight.
749424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
750424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
751424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                     base::TimeDelta::FromHours(12));
752424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
753424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
754424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Record some actions
755424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action =
756424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      new Action("punky", mock_clock->Now(),
757424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                 Action::ACTION_DOM_ACCESS, "lets");
758424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
759424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
760424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
761424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
762424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
763424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
764424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
765424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
766424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "lets");
767424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
768424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google2.com"));
769424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
770424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Deliberately no arg url set to make sure it still works when there is no
771424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // arg url.
772424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
773424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
774424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Clean all the URLs.
775424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<GURL> no_url_restrictions;
776424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->RemoveURLs(no_url_restrictions);
777424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
778424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadData(
779424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
780424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky",
781424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      0,
782424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(&FullStreamUIPolicyTest::AllURLsRemoved));
783424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
784424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
785424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
786424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, RemoveSpecificURLs) {
78768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
7884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
789424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
790424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
791424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // when the test is run close to local midnight.
792424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
793424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
794424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                     base::TimeDelta::FromHours(12));
795424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
796424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
797424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Record some actions
798424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url and args url cleared.
799424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action = new Action("punky", mock_clock->Now(),
800424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                            Action::ACTION_DOM_ACCESS, "lets");
801424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
802424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google1.com"));
803424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
804424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google1.com"));
805424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
806424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
807424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url cleared but not args url.
808424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
809424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
810424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
811424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
812424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google1.com"));
813424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
814424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
815424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
816424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
817424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url cleared. The args url is deliberately not set
818424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // to make sure this doesn't cause any issues.
819424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
820424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
821424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
822424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
823424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google2.com"));
824424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
825424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
826424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
827424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the args url cleared but not the page url or page title.
828424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
829424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
830424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
831424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
832424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
833424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
834424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google1.com"));
835424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
836424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
837424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have neither cleared.
838424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
839424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
840424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
841424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
842424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
843424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
844424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.args-url.com"));
845424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
846424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
847424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Clean some URLs.
848424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<GURL> urls;
849424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.google1.com"));
850424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.google2.com"));
851424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.url_not_in_db.com"));
852424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->RemoveURLs(urls);
853424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
854424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadData(
855424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
856424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky",
857424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      0,
858424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(&FullStreamUIPolicyTest::SomeURLsRemoved));
859424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
860424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
861424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
862d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(FullStreamUIPolicyTest, RemoveExtensionData) {
863d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FullStreamUIPolicy* policy = new FullStreamUIPolicy(profile_.get());
8644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
865d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
866d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
867d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // when the test is run close to local midnight.
868d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
869d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
870d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                     base::TimeDelta::FromHours(12));
871d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
872d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
873d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Record some actions
874d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_refptr<Action> action = new Action("deleteextensiondata",
875d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            mock_clock->Now(),
876d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            Action::ACTION_DOM_ACCESS,
877d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            "lets");
878d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
879d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_page_title("Google");
880d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
881d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
882d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
883d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
884d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
885d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_refptr<Action> action2 = new Action("dontdelete",
886d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             mock_clock->Now(),
887d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             Action::ACTION_DOM_ACCESS,
888d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             "lets");
889d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
890d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_page_title("Google");
891d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
892d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action2);
893d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
894d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->Flush();
895d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->RemoveExtensionData("deleteextensiondata");
896d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
897d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CheckReadFilteredData(
898d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      policy,
899d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "deleteextensiondata",
900d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      Action::ACTION_ANY,
901d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
902d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
903d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
904d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      -1,
905d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      base::Bind(
906d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions0));
907d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
908d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CheckReadFilteredData(
909d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      policy,
910d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "dontdelete",
911d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      Action::ACTION_ANY,
912d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
913d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
914d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
915d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      -1,
916d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      base::Bind(
917d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions1));
918d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->Close();
919d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
920d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
92158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(FullStreamUIPolicyTest, CapReturns) {
92258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  FullStreamUIPolicy* policy = new FullStreamUIPolicy(profile_.get());
9234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
92458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
92558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (int i = 0; i < 305; i++) {
92658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    scoped_refptr<Action> action =
92758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        new Action("punky",
92858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   base::Time::Now(),
92958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   Action::ACTION_API_CALL,
93058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   base::StringPrintf("apicall_%d", i));
93158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    policy->ProcessAction(action);
93258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
93358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
93458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Flush();
93558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  BrowserThread::PostTaskAndReply(
93658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      BrowserThread::DB,
93758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      FROM_HERE,
93858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(&base::DoNothing),
93958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::MessageLoop::current()->QuitClosure());
94058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::MessageLoop::current()->Run();
94158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
94258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadFilteredData(
94358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
94458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky",
94558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      Action::ACTION_ANY,
94658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
94758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
94858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
94958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
95058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(
95158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions300));
95258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Close();
95358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
95458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
9555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, DeleteDatabase) {
95668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
9574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
95858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<const Extension> extension =
95958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      ExtensionBuilder()
96058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          .SetManifest(DictionaryBuilder()
96158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                       .Set("name", "Test extension")
96258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                       .Set("version", "1.0.0")
96358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                       .Set("manifest_version", 2))
96458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          .Build();
96558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  extension_service_->AddExtension(extension.get());
96658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  GURL gurl("http://www.google.com");
96758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
96858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Write some API calls.
96958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<Action> action_api = new Action(extension->id(),
97058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                base::Time::Now(),
97158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                Action::ACTION_API_CALL,
97258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                "tabs.testMethod");
97358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action_api->set_args(make_scoped_ptr(new base::ListValue()));
97458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action_api);
97558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
97658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<Action> action_dom = new Action(extension->id(),
97758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                base::Time::Now(),
97858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                Action::ACTION_DOM_ACCESS,
97958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                "document.write");
98058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action_dom->set_args(make_scoped_ptr(new base::ListValue()));
98158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action_dom->set_page_url(gurl);
98258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action_dom);
98358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
98458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadData(
98558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
98658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      extension->id(),
98758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      0,
98858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(&FullStreamUIPolicyTest::RetrieveActions_LogAndFetchActions));
98958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
99058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Now delete them.
99158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->DeleteDatabase();
99258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
99358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadFilteredData(
99458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
99558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
99658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      Action::ACTION_ANY,
99758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
99858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
99958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
100058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
100158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(
100258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &FullStreamUIPolicyTest::RetrieveActions_FetchFilteredActions0));
100358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
100458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Close();
100558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
100658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
10075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(FullStreamUIPolicyTest, RemoveActions) {
10085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new FullStreamUIPolicy(profile_.get());
10095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->Init();
10105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<int64> action_ids;
10125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(policy,
10145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     action_ids,
10155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     base::Bind(&FullStreamUIPolicyTest::NoActionsDeleted));
10165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(-1);
10185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(-10);
10195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(0);
10205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(5);
10215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(10);
10225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(policy,
10235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     action_ids,
10245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     base::Bind(&FullStreamUIPolicyTest::NoActionsDeleted));
10255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
10265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (int i = 0; i < 50; i++) {
10285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action_ids.push_back(i + 5);
10295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
10305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(policy,
10315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     action_ids,
10325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     base::Bind(&FullStreamUIPolicyTest::NoActionsDeleted));
10335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
10345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // CheckRemoveActions pushes four actions to the Activity Log database with
10365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // IDs 1, 2, 3, and 4.
10375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(1);
10385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(2);
10395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(3);
10405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(4);
10415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(policy,
10425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     action_ids,
10435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     base::Bind(&FullStreamUIPolicyTest::AllActionsDeleted));
10445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
10455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(1);
10475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(2);
10485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
10495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&FullStreamUIPolicyTest::Action1Deleted));
10505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
10515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(3);
10535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(4);
10545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
10555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&FullStreamUIPolicyTest::Action2Deleted));
10565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
10575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->Close();
10595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
10605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace extensions
1062