Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/test/simple_test_clock.h" | 11 #include "base/test/simple_test_clock.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/browser/extensions/activity_log/activity_database.h" | 13 #include "chrome/browser/extensions/activity_log/activity_database.h" |
| 14 #include "chrome/browser/extensions/activity_log/api_actions.h" | 14 #include "chrome/browser/extensions/activity_log/api_actions.h" |
| 15 #include "chrome/browser/extensions/activity_log/blocked_actions.h" | 15 #include "chrome/browser/extensions/activity_log/blocked_actions.h" |
| 16 #include "chrome/browser/extensions/activity_log/dom_actions.h" | 16 #include "chrome/browser/extensions/activity_log/dom_actions.h" |
| 17 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | |
| 17 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/extensions/test_extension_system.h" | 19 #include "chrome/browser/extensions/test_extension_system.h" |
| 19 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
| 20 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/extensions/dom_action_types.h" | 22 #include "chrome/common/extensions/dom_action_types.h" |
| 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 23 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 24 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/test/test_browser_thread.h" | 26 #include "content/public/test/test_browser_thread.h" |
| 26 #include "sql/statement.h" | 27 #include "sql/statement.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 40 namespace extensions { | 41 namespace extensions { |
| 41 | 42 |
| 42 // A dummy implementation of ActivityDatabase::Delegate, sufficient for | 43 // A dummy implementation of ActivityDatabase::Delegate, sufficient for |
| 43 // the unit tests. | 44 // the unit tests. |
| 44 class ActivityDatabaseTestPolicy : public ActivityDatabase::Delegate { | 45 class ActivityDatabaseTestPolicy : public ActivityDatabase::Delegate { |
| 45 public: | 46 public: |
| 46 ActivityDatabaseTestPolicy() {}; | 47 ActivityDatabaseTestPolicy() {}; |
| 47 | 48 |
| 48 protected: | 49 protected: |
| 49 virtual bool OnDatabaseInit(sql::Connection* db) OVERRIDE { | 50 virtual bool OnDatabaseInit(sql::Connection* db) OVERRIDE { |
| 50 if (!DOMAction::InitializeTable(db)) return false; | 51 return ActivityDatabase::InitializeTable( |
| 51 if (!APIAction::InitializeTable(db)) return false; | 52 db, |
| 52 if (!BlockedAction::InitializeTable(db)) return false; | 53 FullStreamUIPolicy::kTableName, |
| 53 return true; | 54 FullStreamUIPolicy::kTableContentFields, |
| 55 FullStreamUIPolicy::kTableFieldTypes, | |
| 56 FullStreamUIPolicy::kTableFieldCount); | |
| 54 } | 57 } |
| 55 | 58 |
| 56 // Called by ActivityDatabase just before the ActivityDatabase object is | 59 // Called by ActivityDatabase just before the ActivityDatabase object is |
| 57 // deleted. The database will make no further callbacks after invoking this | 60 // deleted. The database will make no further callbacks after invoking this |
| 58 // method, so it is an appropriate time for the policy to delete itself. | 61 // method, so it is an appropriate time for the policy to delete itself. |
| 59 virtual void OnDatabaseClose() OVERRIDE { | 62 virtual void OnDatabaseClose() OVERRIDE { |
| 60 delete this; | 63 delete this; |
| 61 } | 64 } |
| 62 }; | 65 }; |
| 63 | 66 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 base::FilePath db_file; | 108 base::FilePath db_file; |
| 106 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 109 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 107 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); | 110 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); |
| 108 base::DeleteFile(db_file, false); | 111 base::DeleteFile(db_file, false); |
| 109 | 112 |
| 110 ActivityDatabase* activity_db = OpenDatabase(db_file); | 113 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 111 activity_db->Close(); | 114 activity_db->Close(); |
| 112 | 115 |
| 113 sql::Connection db; | 116 sql::Connection db; |
| 114 ASSERT_TRUE(db.Open(db_file)); | 117 ASSERT_TRUE(db.Open(db_file)); |
| 115 ASSERT_TRUE(db.DoesTableExist(DOMAction::kTableName)); | 118 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName)); |
| 116 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); | |
| 117 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); | |
| 118 db.Close(); | 119 db.Close(); |
| 119 } | 120 } |
| 120 | 121 |
| 121 // Check that API actions are recorded in the db. | 122 // Check that API actions are recorded in the db. |
| 122 TEST_F(ActivityDatabaseTest, RecordAPIAction) { | 123 TEST_F(ActivityDatabaseTest, RecordAPIAction) { |
| 123 base::ScopedTempDir temp_dir; | 124 base::ScopedTempDir temp_dir; |
| 124 base::FilePath db_file; | 125 base::FilePath db_file; |
| 125 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 126 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 126 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 127 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 127 base::DeleteFile(db_file, false); | 128 base::DeleteFile(db_file, false); |
| 128 | 129 |
| 129 ActivityDatabase* activity_db = OpenDatabase(db_file); | 130 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 130 activity_db->SetBatchModeForTesting(false); | 131 activity_db->SetBatchModeForTesting(false); |
| 132 base::ListValue args_list; | |
| 133 args_list.AppendString("woof"); | |
| 131 scoped_refptr<APIAction> action = new APIAction( | 134 scoped_refptr<APIAction> action = new APIAction( |
| 132 "punky", | 135 "punky", |
| 133 base::Time::Now(), | 136 base::Time::Now(), |
| 134 APIAction::CALL, | 137 APIAction::CALL, |
| 135 "brewster", | 138 "brewster", |
| 136 "woof", | 139 "woof", |
| 140 args_list, | |
| 137 "extra"); | 141 "extra"); |
| 138 activity_db->RecordAction(action); | 142 activity_db->RecordAction(action); |
| 139 activity_db->Close(); | 143 activity_db->Close(); |
| 140 | 144 |
| 141 sql::Connection db; | 145 sql::Connection db; |
| 142 ASSERT_TRUE(db.Open(db_file)); | 146 ASSERT_TRUE(db.Open(db_file)); |
| 143 | 147 |
| 144 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); | 148 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName)); |
| 145 std::string sql_str = "SELECT * FROM " + | 149 std::string sql_str = "SELECT * FROM " + |
| 146 std::string(APIAction::kTableName); | 150 std::string(FullStreamUIPolicy::kTableName); |
| 147 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); | 151 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); |
| 148 ASSERT_TRUE(statement.Step()); | 152 ASSERT_TRUE(statement.Step()); |
| 149 ASSERT_EQ("punky", statement.ColumnString(0)); | 153 ASSERT_EQ("punky", statement.ColumnString(0)); |
| 150 ASSERT_EQ(0, statement.ColumnInt(2)); | 154 ASSERT_EQ(static_cast<int>(Action::ACTION_API_CALL), statement.ColumnInt(2)); |
| 151 ASSERT_EQ("brewster", statement.ColumnString(3)); | 155 ASSERT_EQ("brewster", statement.ColumnString(3)); |
| 152 ASSERT_EQ("woof", statement.ColumnString(4)); | 156 ASSERT_EQ("[\"woof\"]", statement.ColumnString(4)); |
| 153 } | 157 } |
| 154 | 158 |
| 155 // Check that DOM actions are recorded in the db. | 159 // Check that DOM actions are recorded in the db. |
| 156 TEST_F(ActivityDatabaseTest, RecordDOMAction) { | 160 TEST_F(ActivityDatabaseTest, RecordDOMAction) { |
| 157 base::ScopedTempDir temp_dir; | 161 base::ScopedTempDir temp_dir; |
| 158 base::FilePath db_file; | 162 base::FilePath db_file; |
| 159 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 163 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 160 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 164 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 161 base::DeleteFile(db_file, false); | 165 base::DeleteFile(db_file, false); |
| 162 | 166 |
| 163 ActivityDatabase* activity_db = OpenDatabase(db_file); | 167 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 164 activity_db->SetBatchModeForTesting(false); | 168 activity_db->SetBatchModeForTesting(false); |
| 165 scoped_refptr<DOMAction> action = new DOMAction( | 169 scoped_refptr<DOMAction> action = new DOMAction( |
| 166 "punky", | 170 "punky", |
| 167 base::Time::Now(), | 171 base::Time::Now(), |
| 168 DomActionType::MODIFIED, | 172 DomActionType::MODIFIED, |
| 169 GURL("http://www.google.com/foo?bar"), | 173 GURL("http://www.google.com/foo?bar"), |
| 170 string16(), | 174 string16(), |
| 171 "lets", | 175 "lets", |
| 172 "vamoose", | 176 "vamoose", |
| 173 "extra"); | 177 "extra"); |
| 174 activity_db->RecordAction(action); | 178 activity_db->RecordAction(action); |
| 175 activity_db->Close(); | 179 activity_db->Close(); |
| 176 | 180 |
| 177 sql::Connection db; | 181 sql::Connection db; |
| 178 ASSERT_TRUE(db.Open(db_file)); | 182 ASSERT_TRUE(db.Open(db_file)); |
| 179 | 183 |
| 180 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); | 184 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName)); |
| 181 std::string sql_str = "SELECT * FROM " + | 185 std::string sql_str = "SELECT * FROM " + |
| 182 std::string(DOMAction::kTableName); | 186 std::string(FullStreamUIPolicy::kTableName); |
| 183 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); | 187 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); |
| 184 ASSERT_TRUE(statement.Step()); | 188 ASSERT_TRUE(statement.Step()); |
| 185 ASSERT_EQ("punky", statement.ColumnString(0)); | 189 ASSERT_EQ("punky", statement.ColumnString(0)); |
| 186 ASSERT_EQ(DomActionType::MODIFIED, statement.ColumnInt(2)); | 190 ASSERT_EQ(static_cast<int>(Action::ACTION_DOM_ACCESS), |
| 187 ASSERT_EQ("http://www.google.com", statement.ColumnString(3)); | 191 statement.ColumnInt(2)); |
| 192 // TODO(mvrable): This test doesn't work properly, due to crbug.com/260784 | |
| 193 // This will be fixed when URL sanitization is moved into the activity log | |
| 194 // policies in some upcoming code refactoring. | |
| 188 if (CommandLine::ForCurrentProcess()->HasSwitch( | 195 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 189 switches::kEnableExtensionActivityLogTesting)) | 196 switches::kEnableExtensionActivityLogTesting)) |
| 190 ASSERT_EQ("/foo?bar", statement.ColumnString(4)); | 197 ASSERT_EQ("http://www.google.com/foo?bar", statement.ColumnString(5)); |
| 191 else | 198 else |
| 192 ASSERT_EQ("/foo", statement.ColumnString(4)); | 199 ASSERT_EQ("http://www.google.com/foo", statement.ColumnString(5)); |
| 193 ASSERT_EQ("lets", statement.ColumnString(6)); | 200 ASSERT_EQ("lets", statement.ColumnString(3)); |
| 194 ASSERT_EQ("vamoose", statement.ColumnString(7)); | 201 ASSERT_EQ("[\"vamoose\"]", statement.ColumnString(4)); |
| 195 ASSERT_EQ("extra", statement.ColumnString(8)); | 202 ASSERT_EQ("{\"dom_verb\":6,\"extra\":\"extra\",\"page_title\":\"\"}", |
| 203 statement.ColumnString(7)); | |
| 196 } | 204 } |
| 197 | 205 |
| 198 // Check that blocked actions are recorded in the db. | 206 // Check that blocked actions are recorded in the db. |
| 199 TEST_F(ActivityDatabaseTest, RecordBlockedAction) { | 207 TEST_F(ActivityDatabaseTest, RecordBlockedAction) { |
| 200 base::ScopedTempDir temp_dir; | 208 base::ScopedTempDir temp_dir; |
| 201 base::FilePath db_file; | 209 base::FilePath db_file; |
| 202 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 210 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 203 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 211 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 204 base::DeleteFile(db_file, false); | 212 base::DeleteFile(db_file, false); |
| 205 | 213 |
| 206 ActivityDatabase* activity_db = OpenDatabase(db_file); | 214 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 207 scoped_refptr<BlockedAction> action = new BlockedAction( | 215 scoped_refptr<BlockedAction> action = new BlockedAction( |
| 208 "punky", | 216 "punky", |
| 209 base::Time::Now(), | 217 base::Time::Now(), |
| 210 "do.evilThings", | 218 "do.evilThings", |
| 211 "1, 2", | 219 "1, 2", |
| 212 BlockedAction::ACCESS_DENIED, | 220 BlockedAction::ACCESS_DENIED, |
| 213 "extra"); | 221 "extra"); |
| 214 activity_db->RecordAction(action); | 222 activity_db->RecordAction(action); |
| 215 activity_db->Close(); | 223 activity_db->Close(); |
| 216 | 224 |
| 217 sql::Connection db; | 225 sql::Connection db; |
| 218 ASSERT_TRUE(db.Open(db_file)); | 226 ASSERT_TRUE(db.Open(db_file)); |
| 219 | 227 |
| 220 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); | 228 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName)); |
| 221 std::string sql_str = "SELECT * FROM " + | 229 std::string sql_str = "SELECT * FROM " + |
| 222 std::string(BlockedAction::kTableName); | 230 std::string(FullStreamUIPolicy::kTableName); |
| 223 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); | 231 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); |
| 224 ASSERT_TRUE(statement.Step()); | 232 ASSERT_TRUE(statement.Step()); |
| 225 ASSERT_EQ("punky", statement.ColumnString(0)); | 233 ASSERT_EQ("punky", statement.ColumnString(0)); |
| 226 ASSERT_EQ("do.evilThings", statement.ColumnString(2)); | 234 ASSERT_EQ(static_cast<int>(Action::ACTION_API_BLOCKED), |
| 227 ASSERT_EQ("1, 2", statement.ColumnString(3)); | 235 statement.ColumnInt(2)); |
| 228 ASSERT_EQ(1, statement.ColumnInt(4)); | 236 ASSERT_EQ("do.evilThings", statement.ColumnString(3)); |
| 229 ASSERT_EQ("extra", statement.ColumnString(5)); | 237 ASSERT_EQ("1, 2", statement.ColumnString(4)); |
| 238 //ASSERT_EQ(1, statement.ColumnInt(4)); | |
|
felt
2013/07/18 17:09:59
I think you missed this commented out line
mvrable
2013/07/18 18:20:34
Done.
| |
| 239 ASSERT_EQ("{\"reason\":1}", statement.ColumnString(7)); | |
| 230 } | 240 } |
| 231 | 241 |
| 232 // Check that we can read back recent actions in the db. | 242 // Check that we can read back recent actions in the db. |
| 233 TEST_F(ActivityDatabaseTest, GetTodaysActions) { | 243 TEST_F(ActivityDatabaseTest, GetTodaysActions) { |
| 234 base::ScopedTempDir temp_dir; | 244 base::ScopedTempDir temp_dir; |
| 235 base::FilePath db_file; | 245 base::FilePath db_file; |
| 236 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 246 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 237 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 247 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 238 base::DeleteFile(db_file, false); | 248 base::DeleteFile(db_file, false); |
| 239 | 249 |
| 240 // Use a mock clock to ensure that events are not recorded on the wrong day | 250 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 241 // when the test is run close to local midnight. | 251 // when the test is run close to local midnight. |
| 242 base::SimpleTestClock mock_clock; | 252 base::SimpleTestClock mock_clock; |
| 243 mock_clock.SetNow(base::Time::Now().LocalMidnight() + | 253 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 244 base::TimeDelta::FromHours(12)); | 254 base::TimeDelta::FromHours(12)); |
| 245 | 255 |
| 246 // Record some actions | 256 // Record some actions |
| 247 ActivityDatabase* activity_db = OpenDatabase(db_file); | 257 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 258 base::ListValue args_list; | |
| 259 args_list.AppendString("woof"); | |
| 248 scoped_refptr<APIAction> api_action = new APIAction( | 260 scoped_refptr<APIAction> api_action = new APIAction( |
| 249 "punky", | 261 "punky", |
| 250 mock_clock.Now() - base::TimeDelta::FromMinutes(40), | 262 mock_clock.Now() - base::TimeDelta::FromMinutes(40), |
| 251 APIAction::CALL, | 263 APIAction::CALL, |
| 252 "brewster", | 264 "brewster", |
| 253 "woof", | 265 "woof", |
| 266 args_list, | |
| 254 "extra"); | 267 "extra"); |
| 255 scoped_refptr<DOMAction> dom_action = new DOMAction( | 268 scoped_refptr<DOMAction> dom_action = new DOMAction( |
| 256 "punky", | 269 "punky", |
| 257 mock_clock.Now(), | 270 mock_clock.Now(), |
| 258 DomActionType::MODIFIED, | 271 DomActionType::MODIFIED, |
| 259 GURL("http://www.google.com"), | 272 GURL("http://www.google.com"), |
| 260 string16(), | 273 string16(), |
| 261 "lets", | 274 "lets", |
| 262 "vamoose", | 275 "vamoose", |
| 263 "extra"); | 276 "extra"); |
| 264 scoped_refptr<DOMAction> extra_dom_action = new DOMAction( | 277 scoped_refptr<DOMAction> extra_dom_action = new DOMAction( |
| 265 "scoobydoo", | 278 "scoobydoo", |
| 266 mock_clock.Now(), | 279 mock_clock.Now(), |
| 267 DomActionType::MODIFIED, | 280 DomActionType::MODIFIED, |
| 268 GURL("http://www.google.com"), | 281 GURL("http://www.google.com"), |
| 269 string16(), | 282 string16(), |
| 270 "lets", | 283 "lets", |
| 271 "vamoose", | 284 "vamoose", |
| 272 "extra"); | 285 "extra"); |
| 273 activity_db->RecordAction(api_action); | 286 activity_db->RecordAction(api_action); |
| 274 activity_db->RecordAction(dom_action); | 287 activity_db->RecordAction(dom_action); |
| 275 activity_db->RecordAction(extra_dom_action); | 288 activity_db->RecordAction(extra_dom_action); |
| 276 | 289 |
| 277 // Read them back | 290 // Read them back |
| 278 std::string api_print = "ID: punky, CATEGORY: call, " | 291 std::string api_print = |
| 279 "API: brewster, ARGS: woof"; | 292 "ID=punky CATEGORY=api_call API=brewster ARGS=[\"woof\"] OTHER={}"; |
| 280 std::string dom_print = "DOM API CALL: lets, ARGS: vamoose, VERB: modified"; | 293 std::string dom_print = |
| 294 "ID=punky CATEGORY=dom_access API=lets ARGS=[\"vamoose\"] " | |
| 295 "PAGE_URL=http://www.google.com/ " | |
| 296 "OTHER={\"dom_verb\":6,\"extra\":\"extra\",\"page_title\":\"\"}"; | |
| 281 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = | 297 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = |
| 282 activity_db->GetActions("punky", 0); | 298 activity_db->GetActions("punky", 0); |
| 283 ASSERT_EQ(2, static_cast<int>(actions->size())); | 299 ASSERT_EQ(2, static_cast<int>(actions->size())); |
| 284 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); | 300 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); |
| 285 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); | 301 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); |
| 286 | 302 |
| 287 activity_db->Close(); | 303 activity_db->Close(); |
| 288 } | 304 } |
| 289 | 305 |
| 290 // Check that we can read back recent actions in the db. | 306 // Check that we can read back recent actions in the db. |
| 291 TEST_F(ActivityDatabaseTest, GetOlderActions) { | 307 TEST_F(ActivityDatabaseTest, GetOlderActions) { |
| 292 base::ScopedTempDir temp_dir; | 308 base::ScopedTempDir temp_dir; |
| 293 base::FilePath db_file; | 309 base::FilePath db_file; |
| 294 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 310 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 295 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 311 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 296 base::DeleteFile(db_file, false); | 312 base::DeleteFile(db_file, false); |
| 297 | 313 |
| 298 // Use a mock clock to ensure that events are not recorded on the wrong day | 314 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 299 // when the test is run close to local midnight. | 315 // when the test is run close to local midnight. |
| 300 base::SimpleTestClock mock_clock; | 316 base::SimpleTestClock mock_clock; |
| 301 mock_clock.SetNow(base::Time::Now().LocalMidnight() + | 317 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 302 base::TimeDelta::FromHours(12)); | 318 base::TimeDelta::FromHours(12)); |
| 303 | 319 |
| 304 // Record some actions | 320 // Record some actions |
| 305 ActivityDatabase* activity_db = OpenDatabase(db_file); | 321 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 322 base::ListValue args_list; | |
| 323 args_list.AppendString("woof"); | |
| 306 scoped_refptr<APIAction> api_action = new APIAction( | 324 scoped_refptr<APIAction> api_action = new APIAction( |
| 307 "punky", | 325 "punky", |
| 308 mock_clock.Now() - base::TimeDelta::FromDays(3) | 326 mock_clock.Now() - base::TimeDelta::FromDays(3) |
| 309 - base::TimeDelta::FromMinutes(40), | 327 - base::TimeDelta::FromMinutes(40), |
| 310 APIAction::CALL, | 328 APIAction::CALL, |
| 311 "brewster", | 329 "brewster", |
| 312 "woof", | 330 "woof", |
| 331 args_list, | |
| 313 "extra"); | 332 "extra"); |
| 314 scoped_refptr<DOMAction> dom_action = new DOMAction( | 333 scoped_refptr<DOMAction> dom_action = new DOMAction( |
| 315 "punky", | 334 "punky", |
| 316 mock_clock.Now() - base::TimeDelta::FromDays(3), | 335 mock_clock.Now() - base::TimeDelta::FromDays(3), |
| 317 DomActionType::MODIFIED, | 336 DomActionType::MODIFIED, |
| 318 GURL("http://www.google.com"), | 337 GURL("http://www.google.com"), |
| 319 string16(), | 338 string16(), |
| 320 "lets", | 339 "lets", |
| 321 "vamoose", | 340 "vamoose", |
| 322 "extra"); | 341 "extra"); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 337 string16(), | 356 string16(), |
| 338 "too old", | 357 "too old", |
| 339 "vamoose", | 358 "vamoose", |
| 340 "extra"); | 359 "extra"); |
| 341 activity_db->RecordAction(api_action); | 360 activity_db->RecordAction(api_action); |
| 342 activity_db->RecordAction(dom_action); | 361 activity_db->RecordAction(dom_action); |
| 343 activity_db->RecordAction(toonew_dom_action); | 362 activity_db->RecordAction(toonew_dom_action); |
| 344 activity_db->RecordAction(tooold_dom_action); | 363 activity_db->RecordAction(tooold_dom_action); |
| 345 | 364 |
| 346 // Read them back | 365 // Read them back |
| 347 std::string api_print = "ID: punky, CATEGORY: call, " | 366 std::string api_print = |
| 348 "API: brewster, ARGS: woof"; | 367 "ID=punky CATEGORY=api_call API=brewster ARGS=[\"woof\"] OTHER={}"; |
| 349 std::string dom_print = "DOM API CALL: lets, ARGS: vamoose, VERB: modified"; | 368 std::string dom_print = |
| 369 "ID=punky CATEGORY=dom_access API=lets ARGS=[\"vamoose\"] " | |
| 370 "PAGE_URL=http://www.google.com/ " | |
| 371 "OTHER={\"dom_verb\":6,\"extra\":\"extra\",\"page_title\":\"\"}"; | |
| 350 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = | 372 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = |
| 351 activity_db->GetActions("punky", 3); | 373 activity_db->GetActions("punky", 3); |
| 352 ASSERT_EQ(2, static_cast<int>(actions->size())); | 374 ASSERT_EQ(2, static_cast<int>(actions->size())); |
| 353 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); | 375 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); |
| 354 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); | 376 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); |
| 355 | 377 |
| 356 activity_db->Close(); | 378 activity_db->Close(); |
| 357 } | 379 } |
| 358 | 380 |
| 359 TEST_F(ActivityDatabaseTest, BatchModeOff) { | 381 TEST_F(ActivityDatabaseTest, BatchModeOff) { |
| 360 base::ScopedTempDir temp_dir; | 382 base::ScopedTempDir temp_dir; |
| 361 base::FilePath db_file; | 383 base::FilePath db_file; |
| 362 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 384 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 363 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 385 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 364 base::DeleteFile(db_file, false); | 386 base::DeleteFile(db_file, false); |
| 365 | 387 |
| 366 // Use a mock clock to ensure that events are not recorded on the wrong day | 388 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 367 // when the test is run close to local midnight. | 389 // when the test is run close to local midnight. |
| 368 base::SimpleTestClock mock_clock; | 390 base::SimpleTestClock mock_clock; |
| 369 mock_clock.SetNow(base::Time::Now().LocalMidnight() + | 391 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 370 base::TimeDelta::FromHours(12)); | 392 base::TimeDelta::FromHours(12)); |
| 371 | 393 |
| 372 // Record some actions | 394 // Record some actions |
| 373 ActivityDatabase* activity_db = OpenDatabase(db_file); | 395 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 374 activity_db->SetBatchModeForTesting(false); | 396 activity_db->SetBatchModeForTesting(false); |
| 375 activity_db->SetClockForTesting(&mock_clock); | 397 activity_db->SetClockForTesting(&mock_clock); |
| 398 base::ListValue args_list; | |
| 399 args_list.AppendString("woof"); | |
| 376 scoped_refptr<APIAction> api_action = new APIAction( | 400 scoped_refptr<APIAction> api_action = new APIAction( |
| 377 "punky", | 401 "punky", |
| 378 mock_clock.Now() - base::TimeDelta::FromMinutes(40), | 402 mock_clock.Now() - base::TimeDelta::FromMinutes(40), |
| 379 APIAction::CALL, | 403 APIAction::CALL, |
| 380 "brewster", | 404 "brewster", |
| 381 "woof", | 405 "woof", |
| 406 args_list, | |
| 382 "extra"); | 407 "extra"); |
| 383 activity_db->RecordAction(api_action); | 408 activity_db->RecordAction(api_action); |
| 384 | 409 |
| 385 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = | 410 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = |
| 386 activity_db->GetActions("punky", 0); | 411 activity_db->GetActions("punky", 0); |
| 387 ASSERT_EQ(1, static_cast<int>(actions->size())); | 412 ASSERT_EQ(1, static_cast<int>(actions->size())); |
| 388 activity_db->Close(); | 413 activity_db->Close(); |
| 389 } | 414 } |
| 390 | 415 |
| 391 TEST_F(ActivityDatabaseTest, BatchModeOn) { | 416 TEST_F(ActivityDatabaseTest, BatchModeOn) { |
| 392 base::ScopedTempDir temp_dir; | 417 base::ScopedTempDir temp_dir; |
| 393 base::FilePath db_file; | 418 base::FilePath db_file; |
| 394 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 419 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 395 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 420 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 396 base::DeleteFile(db_file, false); | 421 base::DeleteFile(db_file, false); |
| 397 | 422 |
| 398 // Use a mock clock to set the time, and a special timer to control the | 423 // Use a mock clock to set the time, and a special timer to control the |
| 399 // timing and skip ahead in time. | 424 // timing and skip ahead in time. |
| 400 base::SimpleTestClock mock_clock; | 425 base::SimpleTestClock mock_clock; |
| 401 mock_clock.SetNow(base::Time::Now().LocalMidnight() + | 426 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 402 base::TimeDelta::FromHours(11)); | 427 base::TimeDelta::FromHours(11)); |
| 403 | 428 |
| 404 // Record some actions | 429 // Record some actions |
| 405 ActivityDatabase* activity_db = OpenDatabase(db_file); | 430 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 406 activity_db->SetBatchModeForTesting(true); | 431 activity_db->SetBatchModeForTesting(true); |
| 407 activity_db->SetClockForTesting(&mock_clock); | 432 activity_db->SetClockForTesting(&mock_clock); |
| 433 base::ListValue args_list; | |
| 434 args_list.AppendString("woof"); | |
| 408 scoped_refptr<APIAction> api_action = new APIAction( | 435 scoped_refptr<APIAction> api_action = new APIAction( |
| 409 "punky", | 436 "punky", |
| 410 mock_clock.Now() - base::TimeDelta::FromMinutes(40), | 437 mock_clock.Now() - base::TimeDelta::FromMinutes(40), |
| 411 APIAction::CALL, | 438 APIAction::CALL, |
| 412 "brewster", | 439 "brewster", |
| 413 "woof", | 440 "woof", |
| 441 args_list, | |
| 414 "extra"); | 442 "extra"); |
| 415 activity_db->RecordAction(api_action); | 443 activity_db->RecordAction(api_action); |
| 416 | 444 |
| 417 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_before = | 445 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_before = |
| 418 activity_db->GetActions("punky", 0); | 446 activity_db->GetActions("punky", 0); |
| 419 ASSERT_EQ(0, static_cast<int>(actions_before->size())); | 447 ASSERT_EQ(0, static_cast<int>(actions_before->size())); |
| 420 | 448 |
| 421 // Artificially trigger and then stop the timer. | 449 // Artificially trigger and then stop the timer. |
| 422 activity_db->SetTimerForTesting(0); | 450 activity_db->SetTimerForTesting(0); |
| 423 base::MessageLoop::current()->RunUntilIdle(); | 451 base::MessageLoop::current()->RunUntilIdle(); |
| 424 | 452 |
| 425 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_after = | 453 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_after = |
| 426 activity_db->GetActions("punky", 0); | 454 activity_db->GetActions("punky", 0); |
| 427 ASSERT_EQ(1, static_cast<int>(actions_after->size())); | 455 ASSERT_EQ(1, static_cast<int>(actions_after->size())); |
| 428 | 456 |
| 429 activity_db->Close(); | 457 activity_db->Close(); |
| 430 } | 458 } |
| 431 | 459 |
| 432 // Check that nothing explodes if the DB isn't initialized. | 460 // Check that nothing explodes if the DB isn't initialized. |
| 433 TEST_F(ActivityDatabaseTest, InitFailure) { | 461 TEST_F(ActivityDatabaseTest, InitFailure) { |
| 434 base::ScopedTempDir temp_dir; | 462 base::ScopedTempDir temp_dir; |
| 435 base::FilePath db_file; | 463 base::FilePath db_file; |
| 436 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 464 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 437 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 465 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 438 base::DeleteFile(db_file, false); | 466 base::DeleteFile(db_file, false); |
| 439 | 467 |
| 440 ActivityDatabase* activity_db = | 468 ActivityDatabase* activity_db = |
| 441 new ActivityDatabase(new ActivityDatabaseTestPolicy()); | 469 new ActivityDatabase(new ActivityDatabaseTestPolicy()); |
| 470 base::ListValue args_list; | |
| 471 args_list.AppendString("woof"); | |
| 442 scoped_refptr<APIAction> action = new APIAction( | 472 scoped_refptr<APIAction> action = new APIAction( |
| 443 "punky", | 473 "punky", |
| 444 base::Time::Now(), | 474 base::Time::Now(), |
| 445 APIAction::CALL, | 475 APIAction::CALL, |
| 446 "brewster", | 476 "brewster", |
| 447 "woooof", | 477 "woooof", |
| 478 args_list, | |
| 448 "extra"); | 479 "extra"); |
| 449 activity_db->RecordAction(action); | 480 activity_db->RecordAction(action); |
| 450 activity_db->Close(); | 481 activity_db->Close(); |
| 451 } | 482 } |
| 452 | 483 |
| 453 } // namespace extensions | 484 } // namespace extensions |
| OLD | NEW |