Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Side by Side Diff: chrome/browser/extensions/activity_log/activity_database_unittest.cc

Issue 18660004: Extension activity log database refactoring (step 1) (Closed) Base URL: http://git.chromium.org/chromium/src.git@incognito-tests
Patch Set: Delegate renaming and comment cleanup Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 21 matching lines...) Expand all
32 #include "chrome/browser/chromeos/login/user_manager.h" 32 #include "chrome/browser/chromeos/login/user_manager.h"
33 #include "chrome/browser/chromeos/settings/cros_settings.h" 33 #include "chrome/browser/chromeos/settings/cros_settings.h"
34 #include "chrome/browser/chromeos/settings/device_settings_service.h" 34 #include "chrome/browser/chromeos/settings/device_settings_service.h"
35 #include "chromeos/chromeos_switches.h" 35 #include "chromeos/chromeos_switches.h"
36 #endif 36 #endif
37 37
38 using content::BrowserThread; 38 using content::BrowserThread;
39 39
40 namespace extensions { 40 namespace extensions {
41 41
42 // A dummy implementation of ActivityDatabase::Delegate, sufficient for
43 // the unit tests.
44 class ActivityDatabaseTestPolicy : public ActivityDatabase::Delegate {
45 public:
46 ActivityDatabaseTestPolicy() {};
47
48 protected:
49 virtual bool OnDatabaseInit(sql::Connection* db) OVERRIDE {
50 if (!DOMAction::InitializeTable(db)) return false;
51 if (!APIAction::InitializeTable(db)) return false;
52 if (!BlockedAction::InitializeTable(db)) return false;
53 return true;
54 }
55
56 // Called by ActivityDatabase just before the ActivityDatabase object is
57 // 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.
59 virtual void OnDatabaseClose() OVERRIDE {
60 delete this;
61 }
62 };
63
42 class ActivityDatabaseTest : public ChromeRenderViewHostTestHarness { 64 class ActivityDatabaseTest : public ChromeRenderViewHostTestHarness {
43 protected: 65 protected:
44 virtual void SetUp() OVERRIDE { 66 virtual void SetUp() OVERRIDE {
45 ChromeRenderViewHostTestHarness::SetUp(); 67 ChromeRenderViewHostTestHarness::SetUp();
46 #if defined OS_CHROMEOS 68 #if defined OS_CHROMEOS
47 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); 69 test_user_manager_.reset(new chromeos::ScopedTestUserManager());
48 #endif 70 #endif
49 CommandLine command_line(CommandLine::NO_PROGRAM); 71 CommandLine command_line(CommandLine::NO_PROGRAM);
50 CommandLine::ForCurrentProcess()->AppendSwitch( 72 CommandLine::ForCurrentProcess()->AppendSwitch(
51 switches::kEnableExtensionActivityLogTesting); 73 switches::kEnableExtensionActivityLogTesting);
52 } 74 }
53 75
54 virtual void TearDown() OVERRIDE { 76 virtual void TearDown() OVERRIDE {
55 #if defined OS_CHROMEOS 77 #if defined OS_CHROMEOS
56 test_user_manager_.reset(); 78 test_user_manager_.reset();
57 #endif 79 #endif
58 ChromeRenderViewHostTestHarness::TearDown(); 80 ChromeRenderViewHostTestHarness::TearDown();
59 } 81 }
60 82
83 // Creates a test database and initializes the table schema.
84 ActivityDatabase* OpenDatabase(const base::FilePath& db_file) const {
85 ActivityDatabase* activity_db =
86 new ActivityDatabase(new ActivityDatabaseTestPolicy());
87 activity_db->Init(db_file);
88 CHECK(activity_db->is_db_valid());
89 return activity_db;
90 }
91
61 private: 92 private:
62 #if defined OS_CHROMEOS 93 #if defined OS_CHROMEOS
63 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; 94 chromeos::ScopedStubCrosEnabler stub_cros_enabler_;
64 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 95 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
65 chromeos::ScopedTestCrosSettings test_cros_settings_; 96 chromeos::ScopedTestCrosSettings test_cros_settings_;
66 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; 97 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
67 #endif 98 #endif
68 99
69 }; 100 };
70 101
71 102
72 // Check that the database is initialized properly. 103 // Check that the database is initialized properly.
73 TEST_F(ActivityDatabaseTest, Init) { 104 TEST_F(ActivityDatabaseTest, Init) {
74 base::ScopedTempDir temp_dir; 105 base::ScopedTempDir temp_dir;
75 base::FilePath db_file; 106 base::FilePath db_file;
76 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 107 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
77 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); 108 db_file = temp_dir.path().AppendASCII("ActivityInit.db");
78 base::Delete(db_file, false); 109 base::Delete(db_file, false);
79 110
80 ActivityDatabase* activity_db = new ActivityDatabase(); 111 ActivityDatabase* activity_db = OpenDatabase(db_file);
81 activity_db->Init(db_file);
82 ASSERT_TRUE(activity_db->is_db_valid());
83 activity_db->Close(); 112 activity_db->Close();
84 113
85 sql::Connection db; 114 sql::Connection db;
86 ASSERT_TRUE(db.Open(db_file)); 115 ASSERT_TRUE(db.Open(db_file));
87 ASSERT_TRUE(db.DoesTableExist(DOMAction::kTableName)); 116 ASSERT_TRUE(db.DoesTableExist(DOMAction::kTableName));
88 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); 117 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName));
89 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); 118 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName));
90 db.Close(); 119 db.Close();
91 } 120 }
92 121
93 // Check that API actions are recorded in the db. 122 // Check that API actions are recorded in the db.
94 TEST_F(ActivityDatabaseTest, RecordAPIAction) { 123 TEST_F(ActivityDatabaseTest, RecordAPIAction) {
95 base::ScopedTempDir temp_dir; 124 base::ScopedTempDir temp_dir;
96 base::FilePath db_file; 125 base::FilePath db_file;
97 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 126 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
98 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 127 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
99 base::Delete(db_file, false); 128 base::Delete(db_file, false);
100 129
101 ActivityDatabase* activity_db = new ActivityDatabase(); 130 ActivityDatabase* activity_db = OpenDatabase(db_file);
102 activity_db->Init(db_file);
103 activity_db->SetBatchModeForTesting(false); 131 activity_db->SetBatchModeForTesting(false);
104 ASSERT_TRUE(activity_db->is_db_valid());
105 scoped_refptr<APIAction> action = new APIAction( 132 scoped_refptr<APIAction> action = new APIAction(
106 "punky", 133 "punky",
107 base::Time::Now(), 134 base::Time::Now(),
108 APIAction::CALL, 135 APIAction::CALL,
109 "brewster", 136 "brewster",
110 "woof", 137 "woof",
111 "extra"); 138 "extra");
112 activity_db->RecordAction(action); 139 activity_db->RecordAction(action);
113 activity_db->Close(); 140 activity_db->Close();
114 141
(...skipping 12 matching lines...) Expand all
127 } 154 }
128 155
129 // Check that DOM actions are recorded in the db. 156 // Check that DOM actions are recorded in the db.
130 TEST_F(ActivityDatabaseTest, RecordDOMAction) { 157 TEST_F(ActivityDatabaseTest, RecordDOMAction) {
131 base::ScopedTempDir temp_dir; 158 base::ScopedTempDir temp_dir;
132 base::FilePath db_file; 159 base::FilePath db_file;
133 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 160 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
134 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 161 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
135 base::Delete(db_file, false); 162 base::Delete(db_file, false);
136 163
137 ActivityDatabase* activity_db = new ActivityDatabase(); 164 ActivityDatabase* activity_db = OpenDatabase(db_file);
138 activity_db->Init(db_file);
139 activity_db->SetBatchModeForTesting(false); 165 activity_db->SetBatchModeForTesting(false);
140 ASSERT_TRUE(activity_db->is_db_valid());
141 scoped_refptr<DOMAction> action = new DOMAction( 166 scoped_refptr<DOMAction> action = new DOMAction(
142 "punky", 167 "punky",
143 base::Time::Now(), 168 base::Time::Now(),
144 DomActionType::MODIFIED, 169 DomActionType::MODIFIED,
145 GURL("http://www.google.com/foo?bar"), 170 GURL("http://www.google.com/foo?bar"),
146 string16(), 171 string16(),
147 "lets", 172 "lets",
148 "vamoose", 173 "vamoose",
149 "extra"); 174 "extra");
150 activity_db->RecordAction(action); 175 activity_db->RecordAction(action);
(...skipping 21 matching lines...) Expand all
172 } 197 }
173 198
174 // Check that blocked actions are recorded in the db. 199 // Check that blocked actions are recorded in the db.
175 TEST_F(ActivityDatabaseTest, RecordBlockedAction) { 200 TEST_F(ActivityDatabaseTest, RecordBlockedAction) {
176 base::ScopedTempDir temp_dir; 201 base::ScopedTempDir temp_dir;
177 base::FilePath db_file; 202 base::FilePath db_file;
178 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
179 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 204 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
180 base::Delete(db_file, false); 205 base::Delete(db_file, false);
181 206
182 ActivityDatabase* activity_db = new ActivityDatabase(); 207 ActivityDatabase* activity_db = OpenDatabase(db_file);
183 activity_db->Init(db_file);
184 ASSERT_TRUE(activity_db->is_db_valid());
185 scoped_refptr<BlockedAction> action = new BlockedAction( 208 scoped_refptr<BlockedAction> action = new BlockedAction(
186 "punky", 209 "punky",
187 base::Time::Now(), 210 base::Time::Now(),
188 "do.evilThings", 211 "do.evilThings",
189 "1, 2", 212 "1, 2",
190 BlockedAction::ACCESS_DENIED, 213 BlockedAction::ACCESS_DENIED,
191 "extra"); 214 "extra");
192 activity_db->RecordAction(action); 215 activity_db->RecordAction(action);
193 activity_db->Close(); 216 activity_db->Close();
194 217
(...skipping 20 matching lines...) Expand all
215 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 238 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
216 base::Delete(db_file, false); 239 base::Delete(db_file, false);
217 240
218 // Use a mock clock to ensure that events are not recorded on the wrong day 241 // Use a mock clock to ensure that events are not recorded on the wrong day
219 // when the test is run close to local midnight. 242 // when the test is run close to local midnight.
220 base::SimpleTestClock mock_clock; 243 base::SimpleTestClock mock_clock;
221 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 244 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
222 base::TimeDelta::FromHours(12)); 245 base::TimeDelta::FromHours(12));
223 246
224 // Record some actions 247 // Record some actions
225 ActivityDatabase* activity_db = new ActivityDatabase(); 248 ActivityDatabase* activity_db = OpenDatabase(db_file);
226 activity_db->Init(db_file);
227 ASSERT_TRUE(activity_db->is_db_valid());
228 scoped_refptr<APIAction> api_action = new APIAction( 249 scoped_refptr<APIAction> api_action = new APIAction(
229 "punky", 250 "punky",
230 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 251 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
231 APIAction::CALL, 252 APIAction::CALL,
232 "brewster", 253 "brewster",
233 "woof", 254 "woof",
234 "extra"); 255 "extra");
235 scoped_refptr<DOMAction> dom_action = new DOMAction( 256 scoped_refptr<DOMAction> dom_action = new DOMAction(
236 "punky", 257 "punky",
237 mock_clock.Now(), 258 mock_clock.Now(),
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 296 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
276 base::Delete(db_file, false); 297 base::Delete(db_file, false);
277 298
278 // Use a mock clock to ensure that events are not recorded on the wrong day 299 // Use a mock clock to ensure that events are not recorded on the wrong day
279 // when the test is run close to local midnight. 300 // when the test is run close to local midnight.
280 base::SimpleTestClock mock_clock; 301 base::SimpleTestClock mock_clock;
281 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 302 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
282 base::TimeDelta::FromHours(12)); 303 base::TimeDelta::FromHours(12));
283 304
284 // Record some actions 305 // Record some actions
285 ActivityDatabase* activity_db = new ActivityDatabase(); 306 ActivityDatabase* activity_db = OpenDatabase(db_file);
286 activity_db->Init(db_file);
287 ASSERT_TRUE(activity_db->is_db_valid());
288 scoped_refptr<APIAction> api_action = new APIAction( 307 scoped_refptr<APIAction> api_action = new APIAction(
289 "punky", 308 "punky",
290 mock_clock.Now() - base::TimeDelta::FromDays(3) 309 mock_clock.Now() - base::TimeDelta::FromDays(3)
291 - base::TimeDelta::FromMinutes(40), 310 - base::TimeDelta::FromMinutes(40),
292 APIAction::CALL, 311 APIAction::CALL,
293 "brewster", 312 "brewster",
294 "woof", 313 "woof",
295 "extra"); 314 "extra");
296 scoped_refptr<DOMAction> dom_action = new DOMAction( 315 scoped_refptr<DOMAction> dom_action = new DOMAction(
297 "punky", 316 "punky",
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 364 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
346 base::Delete(db_file, false); 365 base::Delete(db_file, false);
347 366
348 // Use a mock clock to ensure that events are not recorded on the wrong day 367 // Use a mock clock to ensure that events are not recorded on the wrong day
349 // when the test is run close to local midnight. 368 // when the test is run close to local midnight.
350 base::SimpleTestClock mock_clock; 369 base::SimpleTestClock mock_clock;
351 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 370 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
352 base::TimeDelta::FromHours(12)); 371 base::TimeDelta::FromHours(12));
353 372
354 // Record some actions 373 // Record some actions
355 ActivityDatabase* activity_db = new ActivityDatabase(); 374 ActivityDatabase* activity_db = OpenDatabase(db_file);
356 activity_db->Init(db_file);
357 activity_db->SetBatchModeForTesting(false); 375 activity_db->SetBatchModeForTesting(false);
358 activity_db->SetClockForTesting(&mock_clock); 376 activity_db->SetClockForTesting(&mock_clock);
359 ASSERT_TRUE(activity_db->is_db_valid());
360 scoped_refptr<APIAction> api_action = new APIAction( 377 scoped_refptr<APIAction> api_action = new APIAction(
361 "punky", 378 "punky",
362 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 379 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
363 APIAction::CALL, 380 APIAction::CALL,
364 "brewster", 381 "brewster",
365 "woof", 382 "woof",
366 "extra"); 383 "extra");
367 activity_db->RecordAction(api_action); 384 activity_db->RecordAction(api_action);
368 385
369 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = 386 scoped_ptr<std::vector<scoped_refptr<Action> > > actions =
370 activity_db->GetActions("punky", 0); 387 activity_db->GetActions("punky", 0);
371 ASSERT_EQ(1, static_cast<int>(actions->size())); 388 ASSERT_EQ(1, static_cast<int>(actions->size()));
372 activity_db->Close(); 389 activity_db->Close();
373 } 390 }
374 391
375 TEST_F(ActivityDatabaseTest, BatchModeOn) { 392 TEST_F(ActivityDatabaseTest, BatchModeOn) {
376 base::ScopedTempDir temp_dir; 393 base::ScopedTempDir temp_dir;
377 base::FilePath db_file; 394 base::FilePath db_file;
378 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 395 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
379 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 396 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
380 base::Delete(db_file, false); 397 base::Delete(db_file, false);
381 398
382 // Use a mock clock to set the time, and a special timer to control the 399 // Use a mock clock to set the time, and a special timer to control the
383 // timing and skip ahead in time. 400 // timing and skip ahead in time.
384 base::SimpleTestClock mock_clock; 401 base::SimpleTestClock mock_clock;
385 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 402 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
386 base::TimeDelta::FromHours(11)); 403 base::TimeDelta::FromHours(11));
387 404
388 // Record some actions 405 // Record some actions
389 ActivityDatabase* activity_db = new ActivityDatabase(); 406 ActivityDatabase* activity_db = OpenDatabase(db_file);
390 activity_db->Init(db_file);
391 activity_db->SetBatchModeForTesting(true); 407 activity_db->SetBatchModeForTesting(true);
392 activity_db->SetClockForTesting(&mock_clock); 408 activity_db->SetClockForTesting(&mock_clock);
393 ASSERT_TRUE(activity_db->is_db_valid());
394 scoped_refptr<APIAction> api_action = new APIAction( 409 scoped_refptr<APIAction> api_action = new APIAction(
395 "punky", 410 "punky",
396 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 411 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
397 APIAction::CALL, 412 APIAction::CALL,
398 "brewster", 413 "brewster",
399 "woof", 414 "woof",
400 "extra"); 415 "extra");
401 activity_db->RecordAction(api_action); 416 activity_db->RecordAction(api_action);
402 417
403 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_before = 418 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_before =
(...skipping 12 matching lines...) Expand all
416 } 431 }
417 432
418 // Check that nothing explodes if the DB isn't initialized. 433 // Check that nothing explodes if the DB isn't initialized.
419 TEST_F(ActivityDatabaseTest, InitFailure) { 434 TEST_F(ActivityDatabaseTest, InitFailure) {
420 base::ScopedTempDir temp_dir; 435 base::ScopedTempDir temp_dir;
421 base::FilePath db_file; 436 base::FilePath db_file;
422 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 437 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
423 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 438 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
424 base::Delete(db_file, false); 439 base::Delete(db_file, false);
425 440
426 ActivityDatabase* activity_db = new ActivityDatabase(); 441 ActivityDatabase* activity_db =
442 new ActivityDatabase(new ActivityDatabaseTestPolicy());
427 scoped_refptr<APIAction> action = new APIAction( 443 scoped_refptr<APIAction> action = new APIAction(
428 "punky", 444 "punky",
429 base::Time::Now(), 445 base::Time::Now(),
430 APIAction::CALL, 446 APIAction::CALL,
431 "brewster", 447 "brewster",
432 "woooof", 448 "woooof",
433 "extra"); 449 "extra");
434 activity_db->RecordAction(action); 450 activity_db->RecordAction(action);
435 activity_db->Close(); 451 activity_db->Close();
436 } 452 }
437 453
438 } // namespace extensions 454 } // namespace extensions
439 455
OLDNEW
« no previous file with comments | « chrome/browser/extensions/activity_log/activity_database.cc ('k') | chrome/browser/extensions/activity_log/activity_log.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698