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