Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / counting_policy_unittest.cc
index 354d0e6..4a1652d 100644 (file)
@@ -30,6 +30,8 @@
 #include "chrome/browser/chromeos/settings/device_settings_service.h"
 #endif
 
+using content::BrowserThread;
+
 namespace extensions {
 
 class CountingPolicyTest : public testing::Test {
@@ -62,7 +64,7 @@ class CountingPolicyTest : public testing::Test {
   }
 
   // Wait for the task queue for the specified thread to empty.
-  void WaitOnThread(const content::BrowserThread::ID& thread) {
+  void WaitOnThread(const BrowserThread::ID& thread) {
     BrowserThread::PostTaskAndReply(
         thread,
         FROM_HERE,
@@ -186,6 +188,13 @@ class CountingPolicyTest : public testing::Test {
                 "[\"hello\",\"world\"]", "", "", "", 1);
   }
 
+  static void Arguments_GetSinglesAction(
+      scoped_ptr<Action::ActionVector> actions) {
+    ASSERT_EQ(1, static_cast<int>(actions->size()));
+    CheckAction(*actions->at(0), "punky", Action::ACTION_DOM_ACCESS, "lets",
+                "", "http://www.google.com/", "", "", 1);
+  }
+
   static void Arguments_GetTodaysActions(
       scoped_ptr<Action::ActionVector> actions) {
     ASSERT_EQ(3, static_cast<int>(actions->size()));
@@ -284,6 +293,120 @@ class CountingPolicyTest : public testing::Test {
     ASSERT_EQ(expected_page_title, action.page_title());
     ASSERT_EQ(expected_arg_url, action.SerializeArgUrl());
     ASSERT_EQ(expected_count, action.count());
+    ASSERT_NE(-1, action.action_id());
+  }
+
+  // A helper function initializes the policy with a number of actions, calls
+  // RemoveActions on a policy object and then checks the result of the
+  // deletion.
+  void CheckRemoveActions(
+      ActivityLogDatabasePolicy* policy,
+      const std::vector<int64>& action_ids,
+      const base::Callback<void(scoped_ptr<Action::ActionVector>)>& checker) {
+
+    // Use a mock clock to ensure that events are not recorded on the wrong day
+    // when the test is run close to local midnight.
+    base::SimpleTestClock* mock_clock = new base::SimpleTestClock();
+    mock_clock->SetNow(base::Time::Now().LocalMidnight() +
+                       base::TimeDelta::FromHours(12));
+    policy->SetClockForTesting(scoped_ptr<base::Clock>(mock_clock));
+
+    // Record some actions
+    scoped_refptr<Action> action =
+        new Action("punky1",
+                   mock_clock->Now() - base::TimeDelta::FromMinutes(40),
+                   Action::ACTION_DOM_ACCESS,
+                   "lets1");
+    action->mutable_args()->AppendString("vamoose1");
+    action->set_page_url(GURL("http://www.google1.com"));
+    action->set_page_title("Google1");
+    action->set_arg_url(GURL("http://www.args-url1.com"));
+    policy->ProcessAction(action);
+    // Record the same action twice, so there are multiple entries in the
+    // database.
+    policy->ProcessAction(action);
+
+    action = new Action("punky2",
+                        mock_clock->Now() - base::TimeDelta::FromMinutes(30),
+                        Action::ACTION_API_CALL,
+                        "lets2");
+    action->mutable_args()->AppendString("vamoose2");
+    action->set_page_url(GURL("http://www.google2.com"));
+    action->set_page_title("Google2");
+    action->set_arg_url(GURL("http://www.args-url2.com"));
+    policy->ProcessAction(action);
+    // Record the same action twice, so there are multiple entries in the
+    // database.
+    policy->ProcessAction(action);
+
+    // Submit a request to delete actions.
+    policy->RemoveActions(action_ids);
+
+    // Check the result of the deletion. The checker function gets all
+    // activities in the database.
+    CheckReadData(policy, "", -1, checker);
+
+    // Clean database.
+    policy->DeleteDatabase();
+  }
+
+  static void AllActionsDeleted(scoped_ptr<Action::ActionVector> actions) {
+    ASSERT_EQ(0, static_cast<int>(actions->size()));
+  }
+
+  static void NoActionsDeleted(scoped_ptr<Action::ActionVector> actions) {
+    // These will be in the vector in reverse time order.
+    ASSERT_EQ(2, static_cast<int>(actions->size()));
+    CheckAction(*actions->at(0),
+                "punky2",
+                Action::ACTION_API_CALL,
+                "lets2",
+                "",
+                "http://www.google2.com/",
+                "Google2",
+                "http://www.args-url2.com/",
+                2);
+    ASSERT_EQ(2, actions->at(0)->action_id());
+    CheckAction(*actions->at(1),
+                "punky1",
+                Action::ACTION_DOM_ACCESS,
+                "lets1",
+                "",
+                "http://www.google1.com/",
+                "Google1",
+                "http://www.args-url1.com/",
+                2);
+    ASSERT_EQ(1, actions->at(1)->action_id());
+  }
+
+  static void Action1Deleted(scoped_ptr<Action::ActionVector> actions) {
+    // These will be in the vector in reverse time order.
+    ASSERT_EQ(1, static_cast<int>(actions->size()));
+    CheckAction(*actions->at(0),
+                "punky2",
+                Action::ACTION_API_CALL,
+                "lets2",
+                "",
+                "http://www.google2.com/",
+                "Google2",
+                "http://www.args-url2.com/",
+                2);
+    ASSERT_EQ(2, actions->at(0)->action_id());
+  }
+
+  static void Action2Deleted(scoped_ptr<Action::ActionVector> actions) {
+    // These will be in the vector in reverse time order.
+    ASSERT_EQ(1, static_cast<int>(actions->size()));
+    CheckAction(*actions->at(0),
+                "punky1",
+                Action::ACTION_DOM_ACCESS,
+                "lets1",
+                "",
+                "http://www.google1.com/",
+                "Google1",
+                "http://www.args-url1.com/",
+                2);
+    ASSERT_EQ(1, actions->at(0)->action_id());
   }
 
  protected:
@@ -998,7 +1121,7 @@ TEST_F(CountingPolicyTest, RemoveExtensionData) {
   policy->Close();
 }
 
-TEST_F(CountingPolicyTest, DeleteActions) {
+TEST_F(CountingPolicyTest, DeleteDatabase) {
   CountingPolicy* policy = new CountingPolicy(profile_.get());
   policy->Init();
   // Disable row expiration for this test by setting a time before any actions
@@ -1070,6 +1193,34 @@ TEST_F(CountingPolicyTest, DeleteActions) {
       base::Bind(
           &CountingPolicyTest::RetrieveActions_FetchFilteredActions0));
 
+  // The following code tests that the caches of url and string tables were
+  // cleared by the deletion above.
+  // https://code.google.com/p/chromium/issues/detail?id=341674.
+  action =
+    new Action("punky", mock_clock->Now(), Action::ACTION_DOM_ACCESS, "lets");
+  action->mutable_args()->AppendString("vamoose");
+  action->set_page_url(GURL("http://www.google.com"));
+  policy->ProcessAction(action);
+
+  CheckReadData(
+      policy,
+      "",
+      -1,
+      base::Bind(&CountingPolicyTest::Arguments_GetSinglesAction));
+
+  policy->DeleteDatabase();
+
+  CheckReadFilteredData(
+      policy,
+      "",
+      Action::ACTION_ANY,
+      "",
+      "",
+      "",
+      -1,
+      base::Bind(
+          &CountingPolicyTest::RetrieveActions_FetchFilteredActions0));
+
   policy->Close();
 }
 
@@ -1114,4 +1265,50 @@ TEST_F(CountingPolicyTest, DuplicateRows) {
   policy->Close();
 }
 
+TEST_F(CountingPolicyTest, RemoveActions) {
+  ActivityLogDatabasePolicy* policy = new CountingPolicy(profile_.get());
+  policy->Init();
+
+  std::vector<int64> action_ids;
+
+  CheckRemoveActions(
+      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
+
+  action_ids.push_back(-1);
+  action_ids.push_back(-10);
+  action_ids.push_back(0);
+  action_ids.push_back(5);
+  action_ids.push_back(10);
+  CheckRemoveActions(
+      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
+  action_ids.clear();
+
+  for (int i = 0; i < 50; i++) {
+    action_ids.push_back(i + 3);
+  }
+  CheckRemoveActions(
+      policy, action_ids, base::Bind(&CountingPolicyTest::NoActionsDeleted));
+  action_ids.clear();
+
+  // CheckRemoveActions pushes two actions to the Activity Log database with IDs
+  // 1 and 2.
+  action_ids.push_back(1);
+  action_ids.push_back(2);
+  CheckRemoveActions(
+      policy, action_ids, base::Bind(&CountingPolicyTest::AllActionsDeleted));
+  action_ids.clear();
+
+  action_ids.push_back(1);
+  CheckRemoveActions(
+      policy, action_ids, base::Bind(&CountingPolicyTest::Action1Deleted));
+  action_ids.clear();
+
+  action_ids.push_back(2);
+  CheckRemoveActions(
+      policy, action_ids, base::Bind(&CountingPolicyTest::Action2Deleted));
+  action_ids.clear();
+
+  policy->Close();
+}
+
 }  // namespace extensions