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)
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chromeos/login/users/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();
18658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CheckAction(*last, "odlameecjipmbmbejkplpemijjgpljce",
18758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                Action::ACTION_API_CALL, "extension.connect",
18858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "[\"hello\",\"world\"]", "", "", "", 1);
189ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
190ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void Arguments_GetSinglesAction(
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<Action::ActionVector> actions) {
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, static_cast<int>(actions->size()));
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets",
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "", "http://www.google.com/", "", "", 1);
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
198ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void Arguments_GetTodaysActions(
199ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<Action::ActionVector> actions) {
200ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ASSERT_EQ(3, static_cast<int>(actions->size()));
20158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CheckAction(*actions->at(0), "punky", Action::ACTION_API_CALL, "brewster",
20258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "", "", "", 2);
20358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CheckAction(*actions->at(1), "punky", Action::ACTION_DOM_ACCESS, "lets",
20458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "http://www.google.com/", "", "", 1);
20558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CheckAction(*actions->at(2), "punky", Action::ACTION_API_CALL,
20658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "extension.sendMessage", "[\"not\",\"stripped\"]", "", "", "",
20758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                1);
208ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
209ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
210ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void Arguments_GetOlderActions(
211ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<Action::ActionVector> actions) {
212ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ASSERT_EQ(2, static_cast<int>(actions->size()));
21358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets",
21458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "http://www.google.com/", "", "", 1);
21558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CheckAction(*actions->at(1), "punky", Action::ACTION_API_CALL, "brewster",
21658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "", "", "", 1);
217ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
218ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
219ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  static void Arguments_CheckMergeCount(
220ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      int count,
221ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<Action::ActionVector> actions) {
222ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if (count > 0) {
223ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ASSERT_EQ(1u, actions->size());
22458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      CheckAction(*actions->at(0), "punky", Action::ACTION_API_CALL, "brewster",
22558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                  "", "", "", "", count);
226ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    } else {
227ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ASSERT_EQ(0u, actions->size());
228ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    }
229ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
230ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  static void Arguments_CheckMergeCountAndTime(
2323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      int count,
2333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      const base::Time& time,
2343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      scoped_ptr<Action::ActionVector> actions) {
2353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (count > 0) {
2363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      ASSERT_EQ(1u, actions->size());
23758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      CheckAction(*actions->at(0), "punky", Action::ACTION_API_CALL, "brewster",
23858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                  "", "", "", "", count);
2393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      ASSERT_EQ(time, actions->at(0)->time());
2403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    } else {
2413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      ASSERT_EQ(0u, actions->size());
2423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
2433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
2443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
245424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void AllURLsRemoved(scoped_ptr<Action::ActionVector> actions) {
246424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
247424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets",
24858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "", "", "", 1);
249424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    CheckAction(*actions->at(1), "punky", Action::ACTION_DOM_ACCESS, "lets",
25058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "", "", "", 1);
251424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
252424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
253424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void SomeURLsRemoved(scoped_ptr<Action::ActionVector> actions) {
254424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // These will be in the vector in reverse time order.
255424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(5, static_cast<int>(actions->size()));
256424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets",
25758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "http://www.google.com/", "Google",
25858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "http://www.args-url.com/", 1);
259424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    CheckAction(*actions->at(1), "punky", Action::ACTION_DOM_ACCESS, "lets",
26058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "http://www.google.com/", "Google", "", 1);
261424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    CheckAction(*actions->at(2), "punky", Action::ACTION_DOM_ACCESS, "lets",
26258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "", "", "", 1);
263424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    CheckAction(*actions->at(3), "punky", Action::ACTION_DOM_ACCESS, "lets",
26458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "", "", "http://www.google.com/", 1);
265424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    CheckAction(*actions->at(4), "punky", Action::ACTION_DOM_ACCESS, "lets",
26658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                "", "", "", "", 1);
26758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
26858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
26958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  static void CheckDuplicates(scoped_ptr<Action::ActionVector> actions) {
27058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(2u, actions->size());
27158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    int total_count = 0;
27258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    for (size_t i = 0; i < actions->size(); i++) {
27358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      total_count += actions->at(i)->count();
27458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
27558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(3, total_count);
276424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
277424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
278424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  static void CheckAction(const Action& action,
279424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_id,
280424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const Action::ActionType& expected_type,
281424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_api_name,
282424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_args_str,
283424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_page_url,
284424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                          const std::string& expected_page_title,
28558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          const std::string& expected_arg_url,
28658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                          int expected_count) {
287424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_id, action.extension_id());
288424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_type, action.action_type());
289424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_api_name, action.api_name());
290424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_args_str,
291424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              ActivityLogPolicy::Util::Serialize(action.args()));
292424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_page_url, action.SerializePageUrl());
293424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_page_title, action.page_title());
294424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ASSERT_EQ(expected_arg_url, action.SerializeArgUrl());
29558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ASSERT_EQ(expected_count, action.count());
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_NE(-1, action.action_id());
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A helper function initializes the policy with a number of actions, calls
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // RemoveActions on a policy object and then checks the result of the
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // deletion.
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void CheckRemoveActions(
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ActivityLogDatabasePolicy* policy,
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<int64>& action_ids,
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Use a mock clock to ensure that events are not recorded on the wrong day
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // when the test is run close to local midnight.
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mock_clock->SetNow(base::Time::Now().LocalMidnight() +
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       base::TimeDelta::FromHours(12));
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record some actions
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<Action> action =
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new Action("punky1",
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   mock_clock->Now() - base::TimeDelta::FromMinutes(40),
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   Action::ACTION_DOM_ACCESS,
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   "lets1");
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->mutable_args()->AppendString("vamoose1");
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_url(GURL("http://www.google1.com"));
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_title("Google1");
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_arg_url(GURL("http://www.args-url1.com"));
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record the same action twice, so there are multiple entries in the
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // database.
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action = new Action("punky2",
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        mock_clock->Now() - base::TimeDelta::FromMinutes(30),
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        Action::ACTION_API_CALL,
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        "lets2");
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->mutable_args()->AppendString("vamoose2");
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_url(GURL("http://www.google2.com"));
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_page_title("Google2");
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action->set_arg_url(GURL("http://www.args-url2.com"));
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Record the same action twice, so there are multiple entries in the
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // database.
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->ProcessAction(action);
3415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Submit a request to delete actions.
3435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->RemoveActions(action_ids);
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Check the result of the deletion. The checker function gets all
3465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // activities in the database.
3475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckReadData(policy, "", -1, checker);
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Clean database.
3505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    policy->DeleteDatabase();
3515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void AllActionsDeleted(scoped_ptr<Action::ActionVector> actions) {
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(0, static_cast<int>(actions->size()));
3555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void NoActionsDeleted(scoped_ptr<Action::ActionVector> actions) {
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
3595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, static_cast<int>(actions->size()));
3605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckAction(*actions->at(0),
3615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky2",
3625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_API_CALL,
3635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets2",
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "",
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google2.com/",
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google2",
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url2.com/",
3685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                2);
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, actions->at(0)->action_id());
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckAction(*actions->at(1),
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky1",
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_DOM_ACCESS,
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets1",
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "",
3755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google1.com/",
3765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google1",
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url1.com/",
3785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                2);
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, actions->at(1)->action_id());
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void Action1Deleted(scoped_ptr<Action::ActionVector> actions) {
3835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, static_cast<int>(actions->size()));
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckAction(*actions->at(0),
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky2",
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_API_CALL,
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets2",
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "",
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google2.com/",
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google2",
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url2.com/",
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                2);
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(2, actions->at(0)->action_id());
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static void Action2Deleted(scoped_ptr<Action::ActionVector> actions) {
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // These will be in the vector in reverse time order.
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, static_cast<int>(actions->size()));
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckAction(*actions->at(0),
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "punky1",
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Action::ACTION_DOM_ACCESS,
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "lets1",
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "",
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.google1.com/",
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "Google1",
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "http://www.args-url1.com/",
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                2);
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(1, actions->at(0)->action_id());
410424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
411424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
412ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch protected:
413ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  ExtensionService* extension_service_;
414ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<TestingProfile> profile_;
415ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  content::TestBrowserThreadBundle thread_bundle_;
416ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Used to preserve a copy of the original command line.
417ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // The test framework will do this itself as well. However, by then,
418ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in
419ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // TearDown().
420ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CommandLine saved_cmdline_;
421ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
422ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#if defined OS_CHROMEOS
423ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
424ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  chromeos::ScopedTestCrosSettings test_cros_settings_;
425ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
426ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif
427ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch};
428ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
429ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, Construct) {
43068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
4314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
432ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<const Extension> extension =
433ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ExtensionBuilder()
434ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          .SetManifest(DictionaryBuilder()
435ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("name", "Test extension")
436ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("version", "1.0.0")
437ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("manifest_version", 2))
438ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          .Build();
439ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  extension_service_->AddExtension(extension.get());
440ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<base::ListValue> args(new base::ListValue());
441ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action = new Action(extension->id(),
442ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            base::Time::Now(),
443ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            Action::ACTION_API_CALL,
444ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            "tabs.testMethod");
445ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_args(args.Pass());
446ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
447ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
448ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
449ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
450ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, LogWithStrippedArguments) {
45168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
4524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
453ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<const Extension> extension =
454ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ExtensionBuilder()
455ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          .SetManifest(DictionaryBuilder()
456ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("name", "Test extension")
457ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("version", "1.0.0")
458ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                       .Set("manifest_version", 2))
459ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch          .Build();
460ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  extension_service_->AddExtension(extension.get());
461ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
462ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<base::ListValue> args(new base::ListValue());
463ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  args->Set(0, new base::StringValue("hello"));
464ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  args->Set(1, new base::StringValue("world"));
465ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action = new Action(extension->id(),
466ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            base::Time::Now(),
467ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            Action::ACTION_API_CALL,
468ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                            "extension.connect");
469ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_args(args.Pass());
470ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
471ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
472ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
473ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                extension->id(),
474ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                0,
475ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_Stripped));
476ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
477ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
478ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
479ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, GetTodaysActions) {
480ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CountingPolicy* policy = new CountingPolicy(profile_.get());
4814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
482ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Disable row expiration for this test by setting a time before any actions
483ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // we generate.
484ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->set_retention_time(base::TimeDelta::FromDays(14));
485ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
486ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Use a mock clock to ensure that events are not recorded on the wrong day
487ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // when the test is run close to local midnight.  Note: Ownership is passed
488ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // to the policy, but we still keep a pointer locally.  The policy will take
489ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // care of destruction; this is safe since the policy outlives all our
490ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // accesses to the mock clock.
491ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
492ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
493ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromHours(12));
494ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
495ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
496ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Record some actions
497ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action =
498ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky",
499ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 mock_clock->Now() - base::TimeDelta::FromMinutes(40),
500ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 Action::ACTION_API_CALL,
501ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 "brewster");
502ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("woof");
503ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
504ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
505ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
506ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromMinutes(30),
507ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
508ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "brewster");
509ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("meow");
510ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
511ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
512ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
513ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromMinutes(20),
514ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
515ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "extension.sendMessage");
516ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("not");
517ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("stripped");
518ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
519ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
520ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action =
521ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
522ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("vamoose");
523ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
524ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
525ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
526ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action(
527ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      "scoobydoo", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
528ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("vamoose");
529ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
530ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
531ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
532ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(
533ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      policy,
534ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      "punky",
535ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      0,
536ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      base::Bind(&CountingPolicyTest::Arguments_GetTodaysActions));
537ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
538ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
539ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
540ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Check that we can read back less recent actions in the db.
541ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, GetOlderActions) {
542ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CountingPolicy* policy = new CountingPolicy(profile_.get());
5434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
544ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->set_retention_time(base::TimeDelta::FromDays(14));
545ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
546ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Use a mock clock to ensure that events are not recorded on the wrong day
547ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // when the test is run close to local midnight.
548ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
549ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
550ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromHours(12));
551ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
552ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
553ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Record some actions
554ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action =
555ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky",
556ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 mock_clock->Now() - base::TimeDelta::FromDays(3) -
557ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromMinutes(40),
558ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 Action::ACTION_API_CALL,
559ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 "brewster");
560ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("woof");
561ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
562ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
563ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
564ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(3),
565ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_DOM_ACCESS,
566ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "lets");
567ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("vamoose");
568ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
569ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
570ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
571ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
572ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now(),
573ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_DOM_ACCESS,
574ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "lets");
575ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("too new");
576ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
577ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
578ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
579ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
580ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(7),
581ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_DOM_ACCESS,
582ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "lets");
583ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->mutable_args()->AppendString("too old");
584ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action->set_page_url(GURL("http://www.google.com"));
585ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
586ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
587ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(
588ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      policy,
589ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      "punky",
590ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      3,
591ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      base::Bind(&CountingPolicyTest::Arguments_GetOlderActions));
592ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
593ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
594ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
595ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
596424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(CountingPolicyTest, LogAndFetchFilteredActions) {
59768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
5984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
599424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<const Extension> extension =
600424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      ExtensionBuilder()
601424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          .SetManifest(DictionaryBuilder()
602424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("name", "Test extension")
603424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("version", "1.0.0")
604424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                       .Set("manifest_version", 2))
605424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          .Build();
606424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  extension_service_->AddExtension(extension.get());
607424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GURL gurl("http://www.google.com");
608424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
609424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Write some API calls
610424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action_api = new Action(extension->id(),
611424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                base::Time::Now(),
612424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                Action::ACTION_API_CALL,
613424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                "tabs.testMethod");
614424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_api->set_args(make_scoped_ptr(new base::ListValue()));
615424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action_api);
616424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
617424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action_dom = new Action(extension->id(),
618424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                base::Time::Now(),
619424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                Action::ACTION_DOM_ACCESS,
620424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                "document.write");
621424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_dom->set_args(make_scoped_ptr(new base::ListValue()));
622424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action_dom->set_page_url(gurl);
623424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action_dom);
624424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
625424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
626424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
627424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      extension->id(),
628424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_API_CALL,
629424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "tabs.testMethod",
630424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
631424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
63258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
633424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
634424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
635424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
636424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
637424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
638424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
639424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
640424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
641424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
642424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
64358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
644424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
645424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
646424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
647424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
648424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
649424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
650424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
651424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
652424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.google.com/",
653424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
65458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
655424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
656424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
657424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
658424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
659424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
660424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
661424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
662424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
663424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.google.com",
664424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
66558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
666424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
667424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
668424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
669424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
670424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
671424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
672424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_DOM_ACCESS,
673424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
674424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "http://www.goo",
675424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
67658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
677424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
678424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
679424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
680424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadFilteredData(
681424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
682424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      extension->id(),
683424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      Action::ACTION_ANY,
684424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
685424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
686424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "",
68758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
688424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(
689424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions2));
690424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
691424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
692424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
693424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
694ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Check that merging of actions only occurs within the same day, not across
695ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// days, and that old data can be expired from the database.
696ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben MurdochTEST_F(CountingPolicyTest, MergingAndExpiring) {
697ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CountingPolicy* policy = new CountingPolicy(profile_.get());
6984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
699ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Initially disable expiration by setting a retention time before any
700ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // actions we generate.
701ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->set_retention_time(base::TimeDelta::FromDays(14));
702ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
703ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Use a mock clock to ensure that events are not recorded on the wrong day
704ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // when the test is run close to local midnight.
705ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
706ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
707ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                    base::TimeDelta::FromHours(12));
708ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
709ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
710ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // The first two actions should be merged; the last one is on a separate day
711ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // and should not be.
712ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_refptr<Action> action =
713ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      new Action("punky",
714ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 mock_clock->Now() - base::TimeDelta::FromDays(3) -
715ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                     base::TimeDelta::FromMinutes(40),
716ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 Action::ACTION_API_CALL,
717ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                 "brewster");
718ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
719ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
720ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
721ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(3) -
722ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                          base::TimeDelta::FromMinutes(20),
723ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
724ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "brewster");
725ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
726ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
727ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
728ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now() - base::TimeDelta::FromDays(2) -
729ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                          base::TimeDelta::FromMinutes(20),
730ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
731ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "brewster");
732ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
733ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
734ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
735ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                "punky",
736ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                3,
737ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_CheckMergeCount, 2));
738ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
739ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                "punky",
740ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                2,
741ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_CheckMergeCount, 1));
742ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
743ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Clean actions before midnight two days ago.  Force expiration to run by
744ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // clearing last_database_cleaning_time_ and submitting a new action.
745ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->set_retention_time(base::TimeDelta::FromDays(2));
746ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->last_database_cleaning_time_ = base::Time();
747ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  action = new Action("punky",
748ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      mock_clock->Now(),
749ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      Action::ACTION_API_CALL,
750ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                      "brewster");
751ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->ProcessAction(action);
752ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
753ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
754ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                "punky",
755ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                3,
756ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_CheckMergeCount, 0));
757ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  CheckReadData(policy,
758ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                "punky",
759ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                2,
760ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                base::Bind(&CountingPolicyTest::Arguments_CheckMergeCount, 1));
761ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
762ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  policy->Close();
763ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
764ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
7653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Test cleaning of old data in the string and URL tables.
7663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)TEST_F(CountingPolicyTest, StringTableCleaning) {
7673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
7684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
7693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Initially disable expiration by setting a retention time before any
7703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // actions we generate.
7713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->set_retention_time(base::TimeDelta::FromDays(14));
7723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
7733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
7743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now());
7753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
7763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
7773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Insert an action; this should create entries in both the string table (for
7783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // the extension and API name) and the URL table (for page_url).
7793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_refptr<Action> action =
7803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      new Action("punky",
7813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 mock_clock->Now() - base::TimeDelta::FromDays(7),
7823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 Action::ACTION_API_CALL,
7833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                 "brewster");
7843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com/"));
7853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
7863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
7873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Add an action which will not be expired, so that some strings will remain
7883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // in use.
7893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action(
7903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "tabs.create");
7913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
7923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
7933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // There should now be three strings ("punky", "brewster", "tabs.create") and
7943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // one URL in the tables.
7953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->Flush();
7963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ScheduleAndForget(policy,
7973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            &CountingPolicyTest::CheckStringTableSizes,
7983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            3,
7993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            1);
8003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  WaitOnThread(BrowserThread::DB);
8013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Trigger a cleaning.  The oldest action is expired when we submit a
8033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // duplicate of the newer action.  After this, there should be two strings
8043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // and no URLs.
8053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->set_retention_time(base::TimeDelta::FromDays(2));
8063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->last_database_cleaning_time_ = base::Time();
8073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
8083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->Flush();
8093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ScheduleAndForget(policy,
8103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            &CountingPolicyTest::CheckStringTableSizes,
8113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            2,
8123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                            0);
8133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  WaitOnThread(BrowserThread::DB);
8143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->Close();
8163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
8173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// A stress test for memory- and database-based merging of actions.  Submit
8193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// multiple items, not in chronological order, spanning a few days.  Check that
8203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// items are merged properly and final timestamps are correct.
8213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)TEST_F(CountingPolicyTest, MoreMerging) {
8223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
8234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
8243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->set_retention_time(base::TimeDelta::FromDays(14));
8253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
8273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // when the test is run close to local midnight.
8283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
8293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
8303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                    base::TimeDelta::FromHours(12));
8313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
8323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Create an action 2 days ago, then 1 day ago, then 2 days ago.  Make sure
8343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // that we end up with two merged records (one for each day), and each has
8353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // the appropriate timestamp.  These merges should happen in the database
8363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // since the date keeps changing.
8373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time1 =
8383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      mock_clock->Now() - base::TimeDelta::FromDays(2) -
8393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::TimeDelta::FromMinutes(40);
8403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time2 =
8413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      mock_clock->Now() - base::TimeDelta::FromDays(1) -
8423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::TimeDelta::FromMinutes(40);
8433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time3 =
8443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      mock_clock->Now() - base::TimeDelta::FromDays(2) -
8453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::TimeDelta::FromMinutes(20);
8463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_refptr<Action> action =
8483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      new Action("punky", time1, Action::ACTION_API_CALL, "brewster");
8493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
8503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time2, Action::ACTION_API_CALL, "brewster");
8523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
8533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time3, Action::ACTION_API_CALL, "brewster");
8553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
8563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CheckReadData(
8583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      policy,
8593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      "punky",
8603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      2,
8613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(
8623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          &CountingPolicyTest::Arguments_CheckMergeCountAndTime, 2, time3));
8633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CheckReadData(
8643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      policy,
8653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      "punky",
8663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      1,
8673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(
8683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          &CountingPolicyTest::Arguments_CheckMergeCountAndTime, 1, time2));
8693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Create three actions today, where the merges should happen in memory.
8713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Again these are not chronological; timestamp time5 should win out since it
8723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // is the latest.
8733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time4 = mock_clock->Now() - base::TimeDelta::FromMinutes(60);
8743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time5 = mock_clock->Now() - base::TimeDelta::FromMinutes(20);
8753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::Time time6 = mock_clock->Now() - base::TimeDelta::FromMinutes(40);
8763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time4, Action::ACTION_API_CALL, "brewster");
8783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
8793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time5, Action::ACTION_API_CALL, "brewster");
8813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
8823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  action = new Action("punky", time6, Action::ACTION_API_CALL, "brewster");
8843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->ProcessAction(action);
8853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
8863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  CheckReadData(
8873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      policy,
8883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      "punky",
8893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      0,
8903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      base::Bind(
8913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          &CountingPolicyTest::Arguments_CheckMergeCountAndTime, 3, time5));
8923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  policy->Close();
8933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
8943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
895424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Check that actions are flushed to disk before letting too many accumulate in
896424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// memory.
897424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(CountingPolicyTest, EarlyFlush) {
898424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
8994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
900424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
901424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  for (int i = 0; i < 500; i++) {
902424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    scoped_refptr<Action> action =
903424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        new Action("punky",
904424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   base::Time::Now(),
905424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   Action::ACTION_API_CALL,
906424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   base::StringPrintf("apicall_%d", i));
907424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    policy->ProcessAction(action);
908424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
909424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
910424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ScheduleAndForget(policy, &CountingPolicyTest::CheckQueueSize);
911424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  WaitOnThread(BrowserThread::DB);
912424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
913424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
914424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
915424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
91658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CountingPolicyTest, CapReturns) {
91758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
9184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
91958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
92058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (int i = 0; i < 305; i++) {
92158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    scoped_refptr<Action> action =
92258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        new Action("punky",
92358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   base::Time::Now(),
92458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   Action::ACTION_API_CALL,
92558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   base::StringPrintf("apicall_%d", i));
92658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    policy->ProcessAction(action);
92758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
92858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
92958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Flush();
93058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  WaitOnThread(BrowserThread::DB);
93158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
93258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadFilteredData(
93358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
93458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky",
93558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      Action::ACTION_ANY,
93658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
93758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
93858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
93958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
94058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(
94158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions300));
94258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Close();
94358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
94458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
945424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(CountingPolicyTest, RemoveAllURLs) {
94668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
9474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
948424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
949424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
950424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // when the test is run close to local midnight.
951424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
952424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
953424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                     base::TimeDelta::FromHours(12));
954424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
955424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
956424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Record some actions
957424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action =
958424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      new Action("punky", mock_clock->Now(),
959424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                 Action::ACTION_DOM_ACCESS, "lets");
960424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
961424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
962424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
963424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.args-url.com"));
964424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
965424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
966424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
967424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
968424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
969424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
970424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google2.com"));
971424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
972424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Deliberately no arg url set to make sure it stills works if there is no arg
973424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // url.
974424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
975424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
976424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Clean all the URLs.
977424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<GURL> no_url_restrictions;
978424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->RemoveURLs(no_url_restrictions);
979424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
980424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadData(
981424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
982424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky",
983424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      0,
984424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(&CountingPolicyTest::AllURLsRemoved));
985424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
986424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
987424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
988424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(CountingPolicyTest, RemoveSpecificURLs) {
98968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
9904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
991424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
992424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
993424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // when the test is run close to local midnight.
994424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
995424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
996424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                     base::TimeDelta::FromHours(12));
997424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
998424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
999424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Record some actions
1000424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url and args url cleared.
1001424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<Action> action = new Action("punky", mock_clock->Now(),
1002424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                            Action::ACTION_DOM_ACCESS, "lets");
1003424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1004424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google1.com"));
1005424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1006424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google1.com"));
1007424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1008424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1009424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url cleared but not args url.
1010424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1011424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1012424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1013424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1014424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google1.com"));
1015424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1016424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
1017424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1018424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1019424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the page url cleared. The args url is deliberately not
1020424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // set to make sure this doesn't cause any issues.
1021424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1022424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1023424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1024424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1025424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google2.com"));
1026424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1027424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1028424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1029424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have the args url cleared but not the page url or page title.
1030424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1031424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1032424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1033424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1034424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
1035424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1036424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.google1.com"));
1037424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1038424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1039424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This should have neither cleared.
1040424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  mock_clock->Advance(base::TimeDelta::FromSeconds(1));
1041424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action = new Action(
1042424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
1043424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1044424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
1045424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_page_title("Google");
1046424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  action->set_arg_url(GURL("http://www.args-url.com"));
104758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_count(5);
1048424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->ProcessAction(action);
1049424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1050424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Clean some URLs.
1051424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<GURL> urls;
1052424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.google1.com"));
1053424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.google2.com"));
1054424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  urls.push_back(GURL("http://www.url_not_in_db.com"));
1055424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->RemoveURLs(urls);
1056424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1057424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CheckReadData(
1058424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      policy,
1059424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      "punky",
1060424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      0,
1061424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::Bind(&CountingPolicyTest::SomeURLsRemoved));
1062424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  policy->Close();
1063424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
1064424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1065d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(CountingPolicyTest, RemoveExtensionData) {
1066d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
10674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
1068d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1069d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
1070d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // when the test is run close to local midnight.
1071d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
1072d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
1073d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                     base::TimeDelta::FromHours(12));
1074d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
1075d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1076d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Record some actions
1077d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_refptr<Action> action = new Action("deleteextensiondata",
1078d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            mock_clock->Now(),
1079d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            Action::ACTION_DOM_ACCESS,
1080d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            "lets");
1081d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1082d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_page_title("Google");
1083d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
1084d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
1085d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
1086d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action);
1087d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1088d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_refptr<Action> action2 = new Action("dontdelete",
1089d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             mock_clock->Now(),
1090d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             Action::ACTION_DOM_ACCESS,
1091d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                             "lets");
1092d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
1093d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_page_title("Google");
1094d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  action->set_arg_url(GURL("http://www.google.com"));
1095d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->ProcessAction(action2);
1096d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1097d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->Flush();
1098d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->RemoveExtensionData("deleteextensiondata");
1099d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1100d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CheckReadFilteredData(
1101d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      policy,
1102d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "deleteextensiondata",
1103d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      Action::ACTION_ANY,
1104d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1105d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      -1,
1108d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      base::Bind(
1109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions0));
1110d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1111d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CheckReadFilteredData(
1112d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      policy,
1113d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "dontdelete",
1114d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      Action::ACTION_ANY,
1115d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1116d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1117d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      "",
1118d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      -1,
1119d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      base::Bind(
1120d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions1));
1121d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  policy->Close();
1122d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
1123d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
11245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(CountingPolicyTest, DeleteDatabase) {
112558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
11264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
112758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Disable row expiration for this test by setting a time before any actions
112858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // we generate.
112958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->set_retention_time(base::TimeDelta::FromDays(14));
113058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
113158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Use a mock clock to ensure that events are not recorded on the wrong day
113258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // when the test is run close to local midnight.  Note: Ownership is passed
113358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // to the policy, but we still keep a pointer locally.  The policy will take
113458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // care of destruction; this is safe since the policy outlives all our
113558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // accesses to the mock clock.
113658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
113758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
113858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                     base::TimeDelta::FromHours(12));
113958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
114058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
114158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Record some actions
114258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<Action> action =
114358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      new Action("punky",
114458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                 mock_clock->Now() - base::TimeDelta::FromMinutes(40),
114558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                 Action::ACTION_API_CALL,
114658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                 "brewster");
114758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("woof");
114858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
114958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
115058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action("punky",
115158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      mock_clock->Now() - base::TimeDelta::FromMinutes(30),
115258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      Action::ACTION_API_CALL,
115358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      "brewster");
115458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("meow");
115558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
115658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
115758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action("punky",
115858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      mock_clock->Now() - base::TimeDelta::FromMinutes(20),
115958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      Action::ACTION_API_CALL,
116058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      "extension.sendMessage");
116158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("not");
116258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("stripped");
116358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
116458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
116558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action =
116658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
116758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
116858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
116958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
117058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
117158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action(
117258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "scoobydoo", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
117358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
117458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
117558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
117658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
117758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadData(
117858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
117958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky",
118058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      0,
118158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(&CountingPolicyTest::Arguments_GetTodaysActions));
118258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
118358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->DeleteDatabase();
118458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
118558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadFilteredData(
118658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
118758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
118858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      Action::ACTION_ANY,
118958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
119058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
119158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "",
119258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      -1,
119358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(
119458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions0));
119558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
11965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The following code tests that the caches of url and string tables were
11975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // cleared by the deletion above.
11985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // https://code.google.com/p/chromium/issues/detail?id=341674.
11995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action =
12005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
12015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action->mutable_args()->AppendString("vamoose");
12025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
12035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->ProcessAction(action);
12045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckReadData(
12065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy,
12075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
12085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      -1,
12095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(&CountingPolicyTest::Arguments_GetSinglesAction));
12105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->DeleteDatabase();
12125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckReadFilteredData(
12145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy,
12155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
12165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Action::ACTION_ANY,
12175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
12185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
12195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
12205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      -1,
12215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(
12225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          &CountingPolicyTest::RetrieveActions_FetchFilteredActions0));
12235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
122458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Close();
122558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
122658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
122758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Tests that duplicate rows in the activity log database are handled properly
122858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// when updating counts.
122958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(CountingPolicyTest, DuplicateRows) {
123058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CountingPolicy* policy = new CountingPolicy(profile_.get());
12314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  policy->Init();
123258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
123358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  mock_clock->SetNow(base::Time::Now().LocalMidnight() +
123458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                     base::TimeDelta::FromHours(12));
123558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
123658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
123758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Record two actions with distinct URLs.
123858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_refptr<Action> action;
123958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action(
124058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "brewster");
124158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_page_url(GURL("http://www.google.com"));
124258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
124358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
124458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action(
124558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "brewster");
124658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action->set_page_url(GURL("http://www.google.co.uk"));
124758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
124858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
124958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Manipulate the database to clear the URLs, so that we end up with
125058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // duplicate rows.
125158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<GURL> no_url_restrictions;
125258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->RemoveURLs(no_url_restrictions);
125358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
125458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Record one more action, with no URL.  This should increment the count on
125558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // one, and exactly one, of the existing rows.
125658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  action = new Action(
125758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky", mock_clock->Now(), Action::ACTION_API_CALL, "brewster");
125858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->ProcessAction(action);
125958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
126058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CheckReadData(
126158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      policy,
126258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "punky",
126358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      0,
126458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::Bind(&CountingPolicyTest::CheckDuplicates));
126558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  policy->Close();
126658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
126758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
12685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(CountingPolicyTest, RemoveActions) {
12695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
12705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->Init();
12715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<int64> action_ids;
12735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
12755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
12765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(-1);
12785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(-10);
12795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(0);
12805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(5);
12815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(10);
12825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
12835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
12845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
12855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (int i = 0; i < 50; i++) {
12875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    action_ids.push_back(i + 3);
12885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
12895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
12905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
12915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
12925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // CheckRemoveActions pushes two actions to the Activity Log database with IDs
12945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // 1 and 2.
12955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(1);
12965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(2);
12975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
12985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::AllActionsDeleted));
12995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
13005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(1);
13025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
13035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::Action1Deleted));
13045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
13055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.push_back(2);
13075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CheckRemoveActions(
13085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted));
13095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  action_ids.clear();
13105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy->Close();
13125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
13135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1314ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}  // namespace extensions
1315