1ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
2ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// found in the LICENSE file.
4ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
5ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/cancelable_callback.h"
6ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/command_line.h"
7ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/memory/scoped_ptr.h"
8ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/run_loop.h"
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/strings/string_split.h"
10ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/strings/stringprintf.h"
11ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/synchronization/waitable_event.h"
12ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "base/test/simple_test_clock.h"
1368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "base/test/test_timeouts.h"
14ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/extensions/activity_log/activity_log.h"
15ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/extensions/activity_log/counting_policy.h"
16ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/extensions/extension_service.h"
17ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/extensions/test_extension_system.h"
18ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/common/chrome_constants.h"
19ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/common/chrome_switches.h"
20ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/test/base/chrome_render_view_host_test_harness.h"
21ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/test/base/testing_profile.h"
22ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "content/public/test/test_browser_thread_bundle.h"
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension_builder.h"
24ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "sql/statement.h"
25ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "testing/gtest/include/gtest/gtest.h"
26ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
27ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#if defined(OS_CHROMEOS)
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
29ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/chromeos/settings/cros_settings.h"
30ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/chromeos/settings/device_settings_service.h"
31ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif
32ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using content::BrowserThread;
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
35ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochnamespace extensions {
36ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
37ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochclass CountingPolicyTest : public testing::Test {
38ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch public:
39ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CountingPolicyTest()
40ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
41ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        saved_cmdline_(CommandLine::NO_PROGRAM) {
42ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#if defined OS_CHROMEOS
43ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    test_user_manager_.reset(new chromeos::ScopedTestUserManager());
44ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif
45ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    CommandLine command_line(CommandLine::NO_PROGRAM);
46ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    saved_cmdline_ = *CommandLine::ForCurrentProcess();
47ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    profile_.reset(new TestingProfile());
48ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    CommandLine::ForCurrentProcess()->AppendSwitch(
49ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        switches::kEnableExtensionActivityLogging);
50ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    extension_service_ = static_cast<TestExtensionSystem*>(
51ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        ExtensionSystem::Get(profile_.get()))->CreateExtensionService
52ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch            (&command_line, base::FilePath(), false);
53ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
54ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
55ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  virtual ~CountingPolicyTest() {
56ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#if defined OS_CHROMEOS
57ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    test_user_manager_.reset();
58ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif
59ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    base::RunLoop().RunUntilIdle();
60ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    profile_.reset(NULL);
61ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    base::RunLoop().RunUntilIdle();
62ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    // Restore the original command line and undo the affects of SetUp().
63ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    *CommandLine::ForCurrentProcess() = saved_cmdline_;
64ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
65ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Wait for the task queue for the specified thread to empty.
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void WaitOnThread(const BrowserThread::ID& thread) {
683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    BrowserThread::PostTaskAndReply(
693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        thread,
703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        FROM_HERE,
713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        base::Bind(&base::DoNothing),
723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        base::MessageLoop::current()->QuitClosure());
733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    base::MessageLoop::current()->Run();
743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // A wrapper function for CheckReadFilteredData, so that we don't need to
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // enter empty string values for parameters we don't care about.
78ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  void CheckReadData(
7968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      ActivityLogDatabasePolicy* policy,
80ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      const std::string& extension_id,
813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      int day,
82ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
8358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CheckReadFilteredData(
8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        policy, extension_id, Action::ACTION_ANY, "", "", "", day, checker);
85ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
86ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // A helper function to call ReadFilteredData on a policy object and wait for
88424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // the results to be processed.
89424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void CheckReadFilteredData(
9068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      ActivityLogDatabasePolicy* policy,
91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const std::string& extension_id,
92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const Action::ActionType type,
93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const std::string& api_name,
94424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const std::string& page_url,
95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const std::string& arg_url,
9658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      int day,
97424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
98424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Submit a request to the policy to read back some data, and call the
99424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // checker function when results are available.  This will happen on the
100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // database thread.
101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    policy->ReadFilteredData(
102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        extension_id,
103424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        type,
104424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        api_name,
105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        page_url,
106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        arg_url,
10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        day,
108424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        base::Bind(&CountingPolicyTest::CheckWrapper,
109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   checker,
110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   base::MessageLoop::current()->QuitClosure()));
111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
11268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // Set up a timeout for receiving results; if we haven't received anything
11368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // when the timeout triggers then assume that the test is broken.
114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    base::CancelableClosure timeout(
115424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        base::Bind(&CountingPolicyTest::TimeoutCallback));
116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    base::MessageLoop::current()->PostDelayedTask(
11768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        FROM_HERE, timeout.callback(), TestTimeouts::action_timeout());
118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
119424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Wait for results; either the checker or the timeout callbacks should
120424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // cause the main loop to exit.
121424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    base::MessageLoop::current()->Run();
122424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
123424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    timeout.Cancel();
124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
125424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // A helper function which verifies that the string_ids and url_ids tables in
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // the database have the specified sizes.
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  static void CheckStringTableSizes(CountingPolicy* policy,
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                    int string_size,
1303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                    int url_size) {
1313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    sql::Connection* db = policy->GetDatabaseConnection();
1323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    sql::Statement statement1(db->GetCachedStatement(
1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        sql::StatementID(SQL_FROM_HERE), "SELECT COUNT(*) FROM string_ids"));
1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ASSERT_TRUE(statement1.Step());
1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ASSERT_EQ(string_size, statement1.ColumnInt(0));
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    sql::Statement statement2(db->GetCachedStatement(
1383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        sql::StatementID(SQL_FROM_HERE), "SELECT COUNT(*) FROM url_ids"));
1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ASSERT_TRUE(statement2.Step());
1403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    ASSERT_EQ(url_size, statement2.ColumnInt(0));
1413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
143424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Checks that the number of queued actions to be written out does not exceed
144424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // kSizeThresholdForFlush.  Runs on the database thread.
145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void CheckQueueSize(CountingPolicy* policy) {
146424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // This should be updated if kSizeThresholdForFlush in activity_database.cc
147424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // changes.
148424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_LE(policy->queued_actions_.size(), 200U);
149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
151ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void CheckWrapper(
152ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker,
153ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      const base::Closure& done,
154ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<Action::ActionVector> results) {
155ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    checker.Run(results.Pass());
156ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    done.Run();
157ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
158ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
159ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void TimeoutCallback() {
160ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    base::MessageLoop::current()->QuitWhenIdle();
161ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    FAIL() << "Policy test timed out waiting for results";
162ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
163ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
16458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  static void RetrieveActions_FetchFilteredActions0(
16558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
16658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(0, static_cast<int>(i->size()));
16758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
16858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
169424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void RetrieveActions_FetchFilteredActions1(
170424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
171424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(1, static_cast<int>(i->size()));
172424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
173424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
174424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void RetrieveActions_FetchFilteredActions2(
175424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
176424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(i->size()));
177424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
178424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
17958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  static void RetrieveActions_FetchFilteredActions300(
18058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
18158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(300, static_cast<int>(i->size()));
18258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
184ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void Arguments_Stripped(scoped_ptr<Action::ActionVector> i) {
185ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    scoped_refptr<Action> last = i->front();
1861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*last.get(),
1871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "odlameecjipmbmbejkplpemijjgpljce",
1881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_API_CALL,
1891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "extension.connect",
1901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"hello\",\"world\"]",
1911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
1921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
1931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
1941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
195ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
196ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void Arguments_GetSinglesAction(
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<Action::ActionVector> actions) {
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, static_cast<int>(actions->size()));
2001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
2011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
2061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
211ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void Arguments_GetTodaysActions(
212ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<Action::ActionVector> actions) {
213ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ASSERT_EQ(3, static_cast<int>(actions->size()));
2141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
2151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_API_CALL,
2171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "brewster",
2181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                2);
2231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
2241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
2291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
2321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(2).get(),
2331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_API_CALL,
2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "extension.sendMessage",
2361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "[\"not\",\"stripped\"]",
2371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
24058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                1);
241ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
242ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
243ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void Arguments_GetOlderActions(
244ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<Action::ActionVector> actions) {
245ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ASSERT_EQ(2, static_cast<int>(actions->size()));
2461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
2471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
2491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
2501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
2551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
2561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
2571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_API_CALL,
2581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "brewster",
2591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
2631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
264ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
265ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
266ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void Arguments_CheckMergeCount(
267ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      int count,
268ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<Action::ActionVector> actions) {
269ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if (count > 0) {
270ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ASSERT_EQ(1u, actions->size());
2711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      CheckAction(*actions->at(0).get(),
2721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "punky",
2731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  Action::ACTION_API_CALL,
2741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "brewster",
2751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "",
2761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "",
2771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "",
2781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "",
2791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  count);
280ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    } else {
281ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ASSERT_EQ(0u, actions->size());
282ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    }
283ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
284ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  static void Arguments_CheckMergeCountAndTime(
2863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      int count,
2873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      const base::Time& time,
2883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      scoped_ptr<Action::ActionVector> actions) {
2893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (count > 0) {
2903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      ASSERT_EQ(1u, actions->size());
2911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      CheckAction(*actions->at(0).get(),
2921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "punky",
2931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  Action::ACTION_API_CALL,
2941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "brewster",
2951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "",
2961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "",
2971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "",
2981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "",
2991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  count);
3003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      ASSERT_EQ(time, actions->at(0)->time());
3013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    } else {
3023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      ASSERT_EQ(0u, actions->size());
3033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
3043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
3053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
306424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void AllURLsRemoved(scoped_ptr<Action::ActionVector> actions) {
307424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
3081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
3091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
3101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
3111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
3121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
3171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
3181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
3191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
3201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
3211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
326424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
327424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
328424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void SomeURLsRemoved(scoped_ptr<Action::ActionVector> actions) {
329424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // These will be in the vector in reverse time order.
330424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(5, static_cast<int>(actions->size()));
3311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
3321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
3331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
3341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
3351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
3371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "Google",
3381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.args-url.com/",
3391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
3401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
3411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
3421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
3431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
3441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
3461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "Google",
3471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
3491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(2).get(),
3501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
3511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
3521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
3531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
3581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(3).get(),
3591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
3601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
3611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
3621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "http://www.google.com/",
3661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
3671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(4).get(),
3681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "punky",
3691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                Action::ACTION_DOM_ACCESS,
3701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "lets",
3711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                "",
3751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                1);
37658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
37758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
37858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  static void CheckDuplicates(scoped_ptr<Action::ActionVector> actions) {
37958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(2u, actions->size());
38058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    int total_count = 0;
38158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    for (size_t i = 0; i < actions->size(); i++) {
38258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      total_count += actions->at(i)->count();
38358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
38458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(3, total_count);
385424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
386424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
387424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void CheckAction(const Action& action,
388424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_id,
389424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const Action::ActionType& expected_type,
390424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_api_name,
391424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_args_str,
392424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_page_url,
393424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_page_title,
39458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          const std::string& expected_arg_url,
39558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          int expected_count) {
396424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_id, action.extension_id());
397424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_type, action.action_type());
398424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_api_name, action.api_name());
399424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_args_str,
400424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              ActivityLogPolicy::Util::Serialize(action.args()));
401424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_page_url, action.SerializePageUrl());
402424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_page_title, action.page_title());
403424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_arg_url, action.SerializeArgUrl());
40458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(expected_count, action.count());
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_NE(-1, action.action_id());
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A helper function initializes the policy with a number of actions, calls
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // RemoveActions on a policy object and then checks the result of the
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // deletion.
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void CheckRemoveActions(
4125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ActivityLogDatabasePolicy* policy,
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<int64>& action_ids,
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Use a mock clock to ensure that events are not recorded on the wrong day
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // when the test is run close to local midnight.
4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mock_clock->SetNow(base::Time::Now().LocalMidnight() +
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       base::TimeDelta::FromHours(12));
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record some actions
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<Action> action =
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new Action("punky1",
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   mock_clock->Now() - base::TimeDelta::FromMinutes(40),
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   Action::ACTION_DOM_ACCESS,
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   "lets1");
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->mutable_args()->AppendString("vamoose1");
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_url(GURL("http://www.google1.com"));
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_title("Google1");
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_arg_url(GURL("http://www.args-url1.com"));
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record the same action twice, so there are multiple entries in the
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // database.
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action = new Action("punky2",
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        mock_clock->Now() - base::TimeDelta::FromMinutes(30),
4405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        Action::ACTION_API_CALL,
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        "lets2");
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->mutable_args()->AppendString("vamoose2");
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_url(GURL("http://www.google2.com"));
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_title("Google2");
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_arg_url(GURL("http://www.args-url2.com"));
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record the same action twice, so there are multiple entries in the
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // database.
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Submit a request to delete actions.
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->RemoveActions(action_ids);
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Check the result of the deletion. The checker function gets all
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // activities in the database.
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckReadData(policy, "", -1, checker);
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Clean database.
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->DeleteDatabase();
4605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void AllActionsDeleted(scoped_ptr<Action::ActionVector> actions) {
4635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(0, static_cast<int>(actions->size()));
4645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void NoActionsDeleted(scoped_ptr<Action::ActionVector> actions) {
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
4691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky2",
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_API_CALL,
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets2",
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "",
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google2.com/",
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google2",
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url2.com/",
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                2);
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, actions->at(0)->action_id());
4791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(1).get(),
4805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky1",
4815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_DOM_ACCESS,
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets1",
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "",
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google1.com/",
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google1",
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url1.com/",
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                2);
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, actions->at(1)->action_id());
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void Action1Deleted(scoped_ptr<Action::ActionVector> actions) {
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, static_cast<int>(actions->size()));
4941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky2",
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_API_CALL,
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets2",
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "",
4995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google2.com/",
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google2",
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url2.com/",
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                2);
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, actions->at(0)->action_id());
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
5055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void Action2Deleted(scoped_ptr<Action::ActionVector> actions) {
5075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
5085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, static_cast<int>(actions->size()));
5091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckAction(*actions->at(0).get(),
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky1",
5115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_DOM_ACCESS,
5125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets1",
5135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "",
5145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google1.com/",
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google1",
5165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url1.com/",
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                2);
5185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, actions->at(0)->action_id());
519424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
520424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
521ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch protected:
522ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  ExtensionService* extension_service_;
523ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<TestingProfile> profile_;
524ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  content::TestBrowserThreadBundle thread_bundle_;
525ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Used to preserve a copy of the original command line.
526ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // The test framework will do this itself as well. However, by then,
527ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in
528ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // TearDown().
529ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CommandLine saved_cmdline_;
530ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
531ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#if defined OS_CHROMEOS
532ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
533ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  chromeos::ScopedTestCrosSettings test_cros_settings_;
534ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
535ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif
536ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch};
537ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
538ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, Construct) {
53968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
5404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
541ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<const Extension> extension =
542ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ExtensionBuilder()
543ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          .SetManifest(DictionaryBuilder()
544ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("name", "Test extension")
545ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("version", "1.0.0")
546ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("manifest_version", 2))
547ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          .Build();
548ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  extension_service_->AddExtension(extension.get());
549ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<base::ListValue> args(new base::ListValue());
550ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action = new Action(extension->id(),
551ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            base::Time::Now(),
552ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            Action::ACTION_API_CALL,
553ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            "tabs.testMethod");
554ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_args(args.Pass());
555ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
556ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
557ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
558ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
559ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, LogWithStrippedArguments) {
56068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
5614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
562ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<const Extension> extension =
563ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ExtensionBuilder()
564ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          .SetManifest(DictionaryBuilder()
565ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("name", "Test extension")
566ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("version", "1.0.0")
567ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("manifest_version", 2))
568ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          .Build();
569ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  extension_service_->AddExtension(extension.get());
570ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
571ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<base::ListValue> args(new base::ListValue());
572ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  args->Set(0, new base::StringValue("hello"));
573ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  args->Set(1, new base::StringValue("world"));
574ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action = new Action(extension->id(),
575ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            base::Time::Now(),
576ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            Action::ACTION_API_CALL,
577ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            "extension.connect");
578ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_args(args.Pass());
579ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
580ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
581ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
582ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                extension->id(),
583ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                0,
584ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_Stripped));
585ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
586ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
587ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
588ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, GetTodaysActions) {
589ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CountingPolicy* policy = new CountingPolicy(profile_.get());
5904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
591ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Disable row expiration for this test by setting a time before any actions
592ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // we generate.
593ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->set_retention_time(base::TimeDelta::FromDays(14));
594ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
595ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Use a mock clock to ensure that events are not recorded on the wrong day
596ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // when the test is run close to local midnight.  Note: Ownership is passed
597ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // to the policy, but we still keep a pointer locally.  The policy will take
598ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // care of destruction; this is safe since the policy outlives all our
599ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // accesses to the mock clock.
600ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
601ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
602ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromHours(12));
603ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
604ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
605ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Record some actions
606ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action =
607ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky",
608ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 mock_clock->Now() - base::TimeDelta::FromMinutes(40),
609ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 Action::ACTION_API_CALL,
610ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 "brewster");
611ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("woof");
612ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
613ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
614ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
615ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromMinutes(30),
616ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
617ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "brewster");
618ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("meow");
619ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
620ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
621ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
622ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromMinutes(20),
623ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
624ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "extension.sendMessage");
625ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("not");
626ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("stripped");
627ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
628ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
629ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action =
630ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
631ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("vamoose");
632ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
633ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
634ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
635ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action(
636ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      "scoobydoo", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
637ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("vamoose");
638ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
639ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
640ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
641ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(
642ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      policy,
643ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      "punky",
644ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      0,
645ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      base::Bind(&CountingPolicyTest::Arguments_GetTodaysActions));
646ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
647ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
648ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
649ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Check that we can read back less recent actions in the db.
650ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, GetOlderActions) {
651ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CountingPolicy* policy = new CountingPolicy(profile_.get());
6524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
653ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->set_retention_time(base::TimeDelta::FromDays(14));
654ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
655ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Use a mock clock to ensure that events are not recorded on the wrong day
656ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // when the test is run close to local midnight.
657ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
658ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
659ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromHours(12));
660ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
661ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
662ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Record some actions
663ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action =
664ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky",
665ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 mock_clock->Now() - base::TimeDelta::FromDays(3) -
666ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromMinutes(40),
667ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 Action::ACTION_API_CALL,
668ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 "brewster");
669ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("woof");
670ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
671ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
672ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
673ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(3),
674ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_DOM_ACCESS,
675ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "lets");
676ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("vamoose");
677ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
678ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
679ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
680ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
681ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now(),
682ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_DOM_ACCESS,
683ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "lets");
684ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("too new");
685ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
686ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
687ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
688ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
689ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(7),
690ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_DOM_ACCESS,
691ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "lets");
692ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("too old");
693ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
694ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
695ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
696ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(
697ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      policy,
698ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      "punky",
699ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      3,
700ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      base::Bind(&CountingPolicyTest::Arguments_GetOlderActions));
701ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
702ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
703ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
704ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
705424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(CountingPolicyTest, LogAndFetchFilteredActions) {
70668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
7074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
708424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<const Extension> extension =
709424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      ExtensionBuilder()
710424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          .SetManifest(DictionaryBuilder()
711424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("name", "Test extension")
712424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("version", "1.0.0")
713424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("manifest_version", 2))
714424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          .Build();
715424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  extension_service_->AddExtension(extension.get());
716424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GURL gurl("http://www.google.com");
717424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
718424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Write some API calls
719424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action_api = new Action(extension->id(),
720424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                base::Time::Now(),
721424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                Action::ACTION_API_CALL,
722424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                "tabs.testMethod");
723424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_api->set_args(make_scoped_ptr(new base::ListValue()));
724424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action_api);
725424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
726424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action_dom = new Action(extension->id(),
727424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                base::Time::Now(),
728424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                Action::ACTION_DOM_ACCESS,
729424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                "document.write");
730424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_dom->set_args(make_scoped_ptr(new base::ListValue()));
731424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_dom->set_page_url(gurl);
732424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action_dom);
733424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
734424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
735424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
736424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      extension->id(),
737424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_API_CALL,
738424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "tabs.testMethod",
739424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
740424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
74158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
742424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
743424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
744424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
745424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
746424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
747424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
748424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
749424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
750424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
751424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
75258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
753424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
754424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
755424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
756424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
757424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
758424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
759424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
760424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
761424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.google.com/",
762424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
76358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
764424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
765424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
766424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
767424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
768424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
769424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
770424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
771424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
772424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.google.com",
773424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
77458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
775424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
776424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
777424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
778424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
779424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
780424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
781424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
782424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
783424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.goo",
784424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
78558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
786424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
787424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
788424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
789424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
790424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
791424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      extension->id(),
792424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_ANY,
793424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
794424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
795424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
79658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
797424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
798424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions2));
799424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
800424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
801424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
802424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
803ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Check that merging of actions only occurs within the same day, not across
804ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// days, and that old data can be expired from the database.
805ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, MergingAndExpiring) {
806ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CountingPolicy* policy = new CountingPolicy(profile_.get());
8074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
808ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Initially disable expiration by setting a retention time before any
809ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // actions we generate.
810ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->set_retention_time(base::TimeDelta::FromDays(14));
811ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
812ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Use a mock clock to ensure that events are not recorded on the wrong day
813ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // when the test is run close to local midnight.
814ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
815ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
816ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                    base::TimeDelta::FromHours(12));
817ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
818ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
819ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // The first two actions should be merged; the last one is on a separate day
820ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // and should not be.
821ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action =
822ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky",
823ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 mock_clock->Now() - base::TimeDelta::FromDays(3) -
824ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromMinutes(40),
825ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 Action::ACTION_API_CALL,
826ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 "brewster");
827ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
828ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
829ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
830ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(3) -
831ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                          base::TimeDelta::FromMinutes(20),
832ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
833ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "brewster");
834ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
835ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
836ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
837ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(2) -
838ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                          base::TimeDelta::FromMinutes(20),
839ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
840ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "brewster");
841ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
842ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
843ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
844ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                "punky",
845ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                3,
846ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_CheckMergeCount, 2));
847ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
848ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                "punky",
849ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                2,
850ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_CheckMergeCount, 1));
851ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
852ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Clean actions before midnight two days ago.  Force expiration to run by
853ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // clearing last_database_cleaning_time_ and submitting a new action.
854ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->set_retention_time(base::TimeDelta::FromDays(2));
855ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->last_database_cleaning_time_ = base::Time();
856ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
857ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now(),
858ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
859ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "brewster");
860ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
861ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
862ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
863ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                "punky",
864ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                3,
865ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_CheckMergeCount, 0));
866ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
867ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                "punky",
868ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                2,
869ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_CheckMergeCount, 1));
870ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
871ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
872ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
873ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
8743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Test cleaning of old data in the string and URL tables.
8753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)TEST_F(CountingPolicyTest, StringTableCleaning) {
8763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
8774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
8783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Initially disable expiration by setting a retention time before any
8793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // actions we generate.
8803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->set_retention_time(base::TimeDelta::FromDays(14));
8813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
8833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now());
8843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
8853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Insert an action; this should create entries in both the string table (for
8873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // the extension and API name) and the URL table (for page_url).
8883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_refptr<Action> action =
8893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      new Action("punky",
8903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 mock_clock->Now() - base::TimeDelta::FromDays(7),
8913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 Action::ACTION_API_CALL,
8923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 "brewster");
8933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com/"));
8943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
8953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Add an action which will not be expired, so that some strings will remain
8973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // in use.
8983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action(
8993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "tabs.create");
9003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
9013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // There should now be three strings ("punky", "brewster", "tabs.create") and
9033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // one URL in the tables.
9043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->Flush();
9053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ScheduleAndForget(policy,
9063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            &CountingPolicyTest::CheckStringTableSizes,
9073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            3,
9083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            1);
9093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  WaitOnThread(BrowserThread::DB);
9103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Trigger a cleaning.  The oldest action is expired when we submit a
9123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // duplicate of the newer action.  After this, there should be two strings
9133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // and no URLs.
9143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->set_retention_time(base::TimeDelta::FromDays(2));
9153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->last_database_cleaning_time_ = base::Time();
9163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
9173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->Flush();
9183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ScheduleAndForget(policy,
9193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            &CountingPolicyTest::CheckStringTableSizes,
9203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            2,
9213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            0);
9223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  WaitOnThread(BrowserThread::DB);
9233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->Close();
9253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
9263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// A stress test for memory- and database-based merging of actions.  Submit
9283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// multiple items, not in chronological order, spanning a few days.  Check that
9293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// items are merged properly and final timestamps are correct.
9303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)TEST_F(CountingPolicyTest, MoreMerging) {
9313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
9324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
9333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->set_retention_time(base::TimeDelta::FromDays(14));
9343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
9363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // when the test is run close to local midnight.
9373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
9383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
9393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                    base::TimeDelta::FromHours(12));
9403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
9413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Create an action 2 days ago, then 1 day ago, then 2 days ago.  Make sure
9433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // that we end up with two merged records (one for each day), and each has
9443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // the appropriate timestamp.  These merges should happen in the database
9453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // since the date keeps changing.
9463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time1 =
9473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      mock_clock->Now() - base::TimeDelta::FromDays(2) -
9483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::TimeDelta::FromMinutes(40);
9493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time2 =
9503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      mock_clock->Now() - base::TimeDelta::FromDays(1) -
9513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::TimeDelta::FromMinutes(40);
9523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time3 =
9533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      mock_clock->Now() - base::TimeDelta::FromDays(2) -
9543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::TimeDelta::FromMinutes(20);
9553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_refptr<Action> action =
9573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      new Action("punky", time1, Action::ACTION_API_CALL, "brewster");
9583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
9593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time2, Action::ACTION_API_CALL, "brewster");
9613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
9623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time3, Action::ACTION_API_CALL, "brewster");
9643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
9653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CheckReadData(
9673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      policy,
9683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      "punky",
9693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      2,
9703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(
9713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          &CountingPolicyTest::Arguments_CheckMergeCountAndTime, 2, time3));
9723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CheckReadData(
9733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      policy,
9743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      "punky",
9753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      1,
9763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(
9773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          &CountingPolicyTest::Arguments_CheckMergeCountAndTime, 1, time2));
9783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Create three actions today, where the merges should happen in memory.
9803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Again these are not chronological; timestamp time5 should win out since it
9813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // is the latest.
9823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time4 = mock_clock->Now() - base::TimeDelta::FromMinutes(60);
9833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time5 = mock_clock->Now() - base::TimeDelta::FromMinutes(20);
9843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time6 = mock_clock->Now() - base::TimeDelta::FromMinutes(40);
9853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time4, Action::ACTION_API_CALL, "brewster");
9873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
9883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time5, Action::ACTION_API_CALL, "brewster");
9903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
9913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time6, Action::ACTION_API_CALL, "brewster");
9933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
9943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
9953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CheckReadData(
9963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      policy,
9973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      "punky",
9983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      0,
9993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(
10003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          &CountingPolicyTest::Arguments_CheckMergeCountAndTime, 3, time5));
10013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->Close();
10023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
10033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1004424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Check that actions are flushed to disk before letting too many accumulate in
1005424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// memory.
1006424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(CountingPolicyTest, EarlyFlush) {
1007424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
10084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
1009424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1010424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  for (int i = 0; i < 500; i++) {
1011424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    scoped_refptr<Action> action =
1012424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        new Action("punky",
1013424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   base::Time::Now(),
1014424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   Action::ACTION_API_CALL,
1015424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   base::StringPrintf("apicall_%d", i));
1016424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    policy->ProcessAction(action);
1017424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
1018424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1019424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ScheduleAndForget(policy, &CountingPolicyTest::CheckQueueSize);
1020424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  WaitOnThread(BrowserThread::DB);
1021424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1022424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
1023424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
1024424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
102558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CountingPolicyTest, CapReturns) {
102658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
10274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
102858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
102958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (int i = 0; i < 305; i++) {
103058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    scoped_refptr<Action> action =
103158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        new Action("punky",
103258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   base::Time::Now(),
103358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   Action::ACTION_API_CALL,
103458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   base::StringPrintf("apicall_%d", i));
103558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    policy->ProcessAction(action);
103658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
103758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
103858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Flush();
103958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  WaitOnThread(BrowserThread::DB);
104058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
104158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadFilteredData(
104258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
104358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky",
104458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      Action::ACTION_ANY,
104558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
104658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
104758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
104858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
104958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(
105058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions300));
105158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Close();
105258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
105358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1054424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(CountingPolicyTest, RemoveAllURLs) {
105568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
10564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
1057424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1058424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
1059424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // when the test is run close to local midnight.
1060424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
1061424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
1062424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                     base::TimeDelta::FromHours(12));
1063424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
1064424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1065424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Record some actions
1066424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action =
1067424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      new Action("punky", mock_clock->Now(),
1068424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                 Action::ACTION_DOM_ACCESS, "lets");
1069424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1070424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
1071424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1072424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.args-url.com"));
1073424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1074424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1075424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1076424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1077424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1078424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1079424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google2.com"));
1080424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1081424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Deliberately no arg url set to make sure it stills works if there is no arg
1082424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // url.
1083424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1084424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1085424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Clean all the URLs.
1086424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<GURL> no_url_restrictions;
1087424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->RemoveURLs(no_url_restrictions);
1088424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1089424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadData(
1090424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
1091424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky",
1092424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      0,
1093424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(&CountingPolicyTest::AllURLsRemoved));
1094424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
1095424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
1096424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1097424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(CountingPolicyTest, RemoveSpecificURLs) {
109868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
10994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
1100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
1102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // when the test is run close to local midnight.
1103424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
1104424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
1105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                     base::TimeDelta::FromHours(12));
1106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
1107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1108424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Record some actions
1109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url and args url cleared.
1110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action = new Action("punky", mock_clock->Now(),
1111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                            Action::ACTION_DOM_ACCESS, "lets");
1112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google1.com"));
1114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1115424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google1.com"));
1116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1117424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url cleared but not args url.
1119424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1120424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1121424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1122424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1123424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google1.com"));
1124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1125424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
1126424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1127424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url cleared. The args url is deliberately not
1129424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // set to make sure this doesn't cause any issues.
1130424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1131424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1132424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1133424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1134424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google2.com"));
1135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1138424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the args url cleared but not the page url or page title.
1139424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1141424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1143424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
1144424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google1.com"));
1146424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1147424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1148424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have neither cleared.
1149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1151424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1152424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1153424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
1154424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1155424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.args-url.com"));
115658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_count(5);
1157424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1158424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1159424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Clean some URLs.
1160424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<GURL> urls;
1161424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.google1.com"));
1162424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.google2.com"));
1163424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.url_not_in_db.com"));
1164424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->RemoveURLs(urls);
1165424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1166424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadData(
1167424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
1168424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky",
1169424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      0,
1170424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(&CountingPolicyTest::SomeURLsRemoved));
1171424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
1172424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
1173424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1174d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(CountingPolicyTest, RemoveExtensionData) {
1175d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
11764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
1177d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1178d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
1179d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // when the test is run close to local midnight.
1180d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
1181d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
1182d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                     base::TimeDelta::FromHours(12));
1183d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
1184d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1185d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Record some actions
1186d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_refptr<Action> action = new Action("deleteextensiondata",
1187d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            mock_clock->Now(),
1188d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            Action::ACTION_DOM_ACCESS,
1189d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            "lets");
1190d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1191d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_page_title("Google");
1192d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
1193d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
1194d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
1195d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
1196d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1197d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_refptr<Action> action2 = new Action("dontdelete",
1198d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             mock_clock->Now(),
1199d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             Action::ACTION_DOM_ACCESS,
1200d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             "lets");
1201d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1202d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_page_title("Google");
1203d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
1204d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action2);
1205d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1206d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->Flush();
1207d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->RemoveExtensionData("deleteextensiondata");
1208d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1209d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CheckReadFilteredData(
1210d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      policy,
1211d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "deleteextensiondata",
1212d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      Action::ACTION_ANY,
1213d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1214d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1215d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1216d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      -1,
1217d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      base::Bind(
1218d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions0));
1219d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1220d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CheckReadFilteredData(
1221d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      policy,
1222d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "dontdelete",
1223d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      Action::ACTION_ANY,
1224d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1225d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1226d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1227d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      -1,
1228d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      base::Bind(
1229d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
1230d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->Close();
1231d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
1232d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
12335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(CountingPolicyTest, DeleteDatabase) {
123458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
12354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
123658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Disable row expiration for this test by setting a time before any actions
123758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // we generate.
123858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->set_retention_time(base::TimeDelta::FromDays(14));
123958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
124058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
124158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // when the test is run close to local midnight.  Note: Ownership is passed
124258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // to the policy, but we still keep a pointer locally.  The policy will take
124358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // care of destruction; this is safe since the policy outlives all our
124458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // accesses to the mock clock.
124558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
124658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
124758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                     base::TimeDelta::FromHours(12));
124858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
124958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
125058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Record some actions
125158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<Action> action =
125258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      new Action("punky",
125358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                 mock_clock->Now() - base::TimeDelta::FromMinutes(40),
125458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                 Action::ACTION_API_CALL,
125558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                 "brewster");
125658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("woof");
125758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
125858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
125958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action("punky",
126058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      mock_clock->Now() - base::TimeDelta::FromMinutes(30),
126158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      Action::ACTION_API_CALL,
126258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      "brewster");
126358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("meow");
126458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
126558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
126658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action("punky",
126758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      mock_clock->Now() - base::TimeDelta::FromMinutes(20),
126858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      Action::ACTION_API_CALL,
126958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      "extension.sendMessage");
127058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("not");
127158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("stripped");
127258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
127358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
127458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action =
127558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
127658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
127758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
127858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
127958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
128058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action(
128158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "scoobydoo", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
128258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
128358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
128458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
128558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
128658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadData(
128758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
128858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky",
128958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      0,
129058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(&CountingPolicyTest::Arguments_GetTodaysActions));
129158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
129258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->DeleteDatabase();
129358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
129458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadFilteredData(
129558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
129658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
129758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      Action::ACTION_ANY,
129858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
129958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
130058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
130158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
130258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(
130358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions0));
130458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The following code tests that the caches of url and string tables were
13065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // cleared by the deletion above.
13075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // https://code.google.com/p/chromium/issues/detail?id=341674.
13085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action =
13095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
13105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
13115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
13125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->ProcessAction(action);
13135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckReadData(
13155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy,
13165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
13175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      -1,
13185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(&CountingPolicyTest::Arguments_GetSinglesAction));
13195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->DeleteDatabase();
13215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckReadFilteredData(
13235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy,
13245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
13255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Action::ACTION_ANY,
13265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
13275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
13285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
13295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      -1,
13305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(
13315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions0));
13325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
133358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Close();
133458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
133558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
133658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Tests that duplicate rows in the activity log database are handled properly
133758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// when updating counts.
133858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CountingPolicyTest, DuplicateRows) {
133958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
13404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
134158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
134258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
134358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                     base::TimeDelta::FromHours(12));
134458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
134558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
134658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Record two actions with distinct URLs.
134758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<Action> action;
134858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action(
134958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "brewster");
135058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
135158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
135258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
135358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action(
135458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "brewster");
135558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_page_url(GURL("http://www.google.co.uk"));
135658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
135758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
135858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Manipulate the database to clear the URLs, so that we end up with
135958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // duplicate rows.
136058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<GURL> no_url_restrictions;
136158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->RemoveURLs(no_url_restrictions);
136258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
136358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Record one more action, with no URL.  This should increment the count on
136458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // one, and exactly one, of the existing rows.
136558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action(
136658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "brewster");
136758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
136858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
136958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadData(
137058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
137158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky",
137258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      0,
137358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(&CountingPolicyTest::CheckDuplicates));
137458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Close();
137558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
137658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(CountingPolicyTest, RemoveActions) {
13785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
13795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->Init();
13805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<int64> action_ids;
13825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
13845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
13855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(-1);
13875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(-10);
13885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(0);
13895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(5);
13905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(10);
13915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
13925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
13935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
13945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (int i = 0; i < 50; i++) {
13965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action_ids.push_back(i + 3);
13975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
13985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
13995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
14005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
14015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // CheckRemoveActions pushes two actions to the Activity Log database with IDs
14035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // 1 and 2.
14045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(1);
14055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(2);
14065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
14075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::AllActionsDeleted));
14085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
14095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(1);
14115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
14125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::Action1Deleted));
14135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
14145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(2);
14165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
14175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted));
14185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
14195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->Close();
14215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
14225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1423ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}  // namespace extensions
1424