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

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

Issue 19690003: Extension activity log database refactoring (step 3) (Closed) Base URL: http://git.chromium.org/chromium/src.git@refactor2
Patch Set: Do not set bad BlockedChromeActivityDetail::Reason values 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"
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_action_constants.h"
13 #include "chrome/browser/extensions/activity_log/activity_database.h" 14 #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" 15 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h"
18 #include "chrome/browser/extensions/extension_service.h" 16 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/test_extension_system.h" 17 #include "chrome/browser/extensions/test_extension_system.h"
20 #include "chrome/common/chrome_constants.h" 18 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/extensions/dom_action_types.h" 20 #include "chrome/common/extensions/dom_action_types.h"
23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 21 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
24 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
25 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
26 #include "content/public/test/test_browser_thread.h" 24 #include "content/public/test/test_browser_thread.h"
27 #include "sql/statement.h" 25 #include "sql/statement.h"
28 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
29 27
30 #if defined(OS_CHROMEOS) 28 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/cros/network_library.h" 29 #include "chrome/browser/chromeos/cros/network_library.h"
32 #include "chrome/browser/chromeos/login/mock_user_manager.h" 30 #include "chrome/browser/chromeos/login/mock_user_manager.h"
33 #include "chrome/browser/chromeos/login/user_manager.h" 31 #include "chrome/browser/chromeos/login/user_manager.h"
34 #include "chrome/browser/chromeos/settings/cros_settings.h" 32 #include "chrome/browser/chromeos/settings/cros_settings.h"
35 #include "chrome/browser/chromeos/settings/device_settings_service.h" 33 #include "chrome/browser/chromeos/settings/device_settings_service.h"
36 #include "chromeos/chromeos_switches.h" 34 #include "chromeos/chromeos_switches.h"
37 #endif 35 #endif
38 36
39 using content::BrowserThread; 37 using content::BrowserThread;
40 38
39 namespace constants = activity_log_constants;
40
41 namespace extensions { 41 namespace extensions {
42 42
43 // A dummy implementation of ActivityDatabase::Delegate, sufficient for 43 // A dummy implementation of ActivityDatabase::Delegate, sufficient for
44 // the unit tests. 44 // the unit tests.
45 class ActivityDatabaseTestPolicy : public ActivityDatabase::Delegate { 45 class ActivityDatabaseTestPolicy : public ActivityDatabase::Delegate {
46 public: 46 public:
47 ActivityDatabaseTestPolicy() {}; 47 ActivityDatabaseTestPolicy() {};
48 48
49 protected: 49 protected:
50 virtual bool OnDatabaseInit(sql::Connection* db) OVERRIDE { 50 virtual bool OnDatabaseInit(sql::Connection* db) OVERRIDE {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 112
113 ActivityDatabase* activity_db = OpenDatabase(db_file); 113 ActivityDatabase* activity_db = OpenDatabase(db_file);
114 activity_db->Close(); 114 activity_db->Close();
115 115
116 sql::Connection db; 116 sql::Connection db;
117 ASSERT_TRUE(db.Open(db_file)); 117 ASSERT_TRUE(db.Open(db_file));
118 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName)); 118 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName));
119 db.Close(); 119 db.Close();
120 } 120 }
121 121
122 // Check that API actions are recorded in the db. 122 // Check that actions are recorded in the db.
123 TEST_F(ActivityDatabaseTest, RecordAPIAction) { 123 TEST_F(ActivityDatabaseTest, RecordAction) {
124 base::ScopedTempDir temp_dir; 124 base::ScopedTempDir temp_dir;
125 base::FilePath db_file; 125 base::FilePath db_file;
126 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 126 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
127 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 127 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
128 base::DeleteFile(db_file, false); 128 base::DeleteFile(db_file, false);
129 129
130 ActivityDatabase* activity_db = OpenDatabase(db_file); 130 ActivityDatabase* activity_db = OpenDatabase(db_file);
131 activity_db->SetBatchModeForTesting(false); 131 activity_db->SetBatchModeForTesting(false);
132 base::ListValue args_list; 132 scoped_refptr<Action> action(new Action(
133 args_list.AppendString("woof"); 133 "punky", base::Time::Now(), Action::ACTION_API_CALL, "brewster"));
134 scoped_refptr<APIAction> action = new APIAction( 134 action->mutable_args()->AppendString("woof");
135 "punky", 135 action->set_page_url(GURL("http://www.google.com/"));
136 base::Time::Now(), 136 action->mutable_other()->SetString(constants::kActionExtra, "extra");
137 APIAction::CALL,
138 "brewster",
139 "woof",
140 args_list,
141 "extra");
142 activity_db->RecordAction(action); 137 activity_db->RecordAction(action);
143 activity_db->Close(); 138 activity_db->Close();
144 139
145 sql::Connection db; 140 sql::Connection db;
146 ASSERT_TRUE(db.Open(db_file)); 141 ASSERT_TRUE(db.Open(db_file));
147 142
148 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName)); 143 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName));
149 std::string sql_str = "SELECT * FROM " + 144 std::string sql_str = "SELECT * FROM " +
150 std::string(FullStreamUIPolicy::kTableName); 145 std::string(FullStreamUIPolicy::kTableName);
151 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); 146 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str()));
152 ASSERT_TRUE(statement.Step()); 147 ASSERT_TRUE(statement.Step());
153 ASSERT_EQ("punky", statement.ColumnString(0)); 148 ASSERT_EQ("punky", statement.ColumnString(0));
154 ASSERT_EQ(static_cast<int>(Action::ACTION_API_CALL), statement.ColumnInt(2)); 149 ASSERT_EQ(static_cast<int>(Action::ACTION_API_CALL), statement.ColumnInt(2));
155 ASSERT_EQ("brewster", statement.ColumnString(3)); 150 ASSERT_EQ("brewster", statement.ColumnString(3));
156 ASSERT_EQ("[\"woof\"]", statement.ColumnString(4)); 151 ASSERT_EQ("[\"woof\"]", statement.ColumnString(4));
157 } 152 ASSERT_EQ("http://www.google.com/", statement.ColumnString(5));
158 153 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 } 154 }
240 155
241 // Check that we can read back recent actions in the db. 156 // Check that we can read back recent actions in the db.
242 TEST_F(ActivityDatabaseTest, GetTodaysActions) { 157 TEST_F(ActivityDatabaseTest, GetTodaysActions) {
243 base::ScopedTempDir temp_dir; 158 base::ScopedTempDir temp_dir;
244 base::FilePath db_file; 159 base::FilePath db_file;
245 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 160 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
246 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 161 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
247 base::DeleteFile(db_file, false); 162 base::DeleteFile(db_file, false);
248 163
249 // Use a mock clock to ensure that events are not recorded on the wrong day 164 // 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. 165 // when the test is run close to local midnight.
251 base::SimpleTestClock mock_clock; 166 base::SimpleTestClock mock_clock;
252 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 167 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
253 base::TimeDelta::FromHours(12)); 168 base::TimeDelta::FromHours(12));
254 169
255 // Record some actions 170 // Record some actions
256 ActivityDatabase* activity_db = OpenDatabase(db_file); 171 ActivityDatabase* activity_db = OpenDatabase(db_file);
257 base::ListValue args_list; 172
258 args_list.AppendString("woof"); 173 scoped_refptr<Action> action;
259 scoped_refptr<APIAction> api_action = new APIAction( 174 action = new Action("punky",
260 "punky", 175 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
261 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 176 Action::ACTION_API_CALL,
262 APIAction::CALL, 177 "brewster");
263 "brewster", 178 action->mutable_args()->AppendString("woof");
264 "woof", 179 activity_db->RecordAction(action);
265 args_list, 180
266 "extra"); 181 action =
267 scoped_refptr<DOMAction> dom_action = new DOMAction( 182 new Action("punky", mock_clock.Now(), Action::ACTION_DOM_ACCESS, "lets");
268 "punky", 183 action->mutable_args()->AppendString("vamoose");
269 mock_clock.Now(), 184 action->set_page_url(GURL("http://www.google.com"));
270 DomActionType::MODIFIED, 185 activity_db->RecordAction(action);
271 GURL("http://www.google.com"), 186
272 string16(), 187 action = new Action(
273 "lets", 188 "scoobydoo", mock_clock.Now(), Action::ACTION_DOM_ACCESS, "lets");
274 "vamoose", 189 action->mutable_args()->AppendString("vamoose");
275 "extra"); 190 action->set_page_url(GURL("http://www.google.com"));
276 scoped_refptr<DOMAction> extra_dom_action = new DOMAction( 191 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 192
289 // Read them back 193 // Read them back
290 std::string api_print = 194 std::string api_print =
291 "ID=punky CATEGORY=api_call API=brewster ARGS=[\"woof\"] OTHER={}"; 195 "ID=punky CATEGORY=api_call API=brewster ARGS=[\"woof\"]";
292 std::string dom_print = 196 std::string dom_print =
293 "ID=punky CATEGORY=dom_access API=lets ARGS=[\"vamoose\"] " 197 "ID=punky CATEGORY=dom_access API=lets ARGS=[\"vamoose\"] "
294 "PAGE_URL=http://www.google.com/ " 198 "PAGE_URL=http://www.google.com/";
295 "OTHER={\"dom_verb\":6,\"extra\":\"extra\",\"page_title\":\"\"}";
296 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = 199 scoped_ptr<std::vector<scoped_refptr<Action> > > actions =
297 activity_db->GetActions("punky", 0); 200 activity_db->GetActions("punky", 0);
298 ASSERT_EQ(2, static_cast<int>(actions->size())); 201 ASSERT_EQ(2, static_cast<int>(actions->size()));
299 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); 202 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug());
300 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); 203 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug());
301 204
302 activity_db->Close(); 205 activity_db->Close();
303 } 206 }
304 207
305 // Check that we can read back recent actions in the db. 208 // Check that we can read back less recent actions in the db.
306 TEST_F(ActivityDatabaseTest, GetOlderActions) { 209 TEST_F(ActivityDatabaseTest, GetOlderActions) {
307 base::ScopedTempDir temp_dir; 210 base::ScopedTempDir temp_dir;
308 base::FilePath db_file; 211 base::FilePath db_file;
309 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 212 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
310 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 213 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
311 base::DeleteFile(db_file, false); 214 base::DeleteFile(db_file, false);
312 215
313 // Use a mock clock to ensure that events are not recorded on the wrong day 216 // 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. 217 // when the test is run close to local midnight.
315 base::SimpleTestClock mock_clock; 218 base::SimpleTestClock mock_clock;
316 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 219 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
317 base::TimeDelta::FromHours(12)); 220 base::TimeDelta::FromHours(12));
318 221
319 // Record some actions 222 // Record some actions
320 ActivityDatabase* activity_db = OpenDatabase(db_file); 223 ActivityDatabase* activity_db = OpenDatabase(db_file);
321 base::ListValue args_list; 224 scoped_refptr<Action> action =
322 args_list.AppendString("woof"); 225 new Action("punky",
323 scoped_refptr<APIAction> api_action = new APIAction( 226 mock_clock.Now() - base::TimeDelta::FromDays(3) -
324 "punky", 227 base::TimeDelta::FromMinutes(40),
325 mock_clock.Now() - base::TimeDelta::FromDays(3) 228 Action::ACTION_API_CALL,
326 - base::TimeDelta::FromMinutes(40), 229 "brewster");
327 APIAction::CALL, 230 action->mutable_args()->AppendString("woof");
328 "brewster", 231 activity_db->RecordAction(action);
329 "woof", 232
330 args_list, 233 action = new Action("punky",
331 "extra"); 234 mock_clock.Now() - base::TimeDelta::FromDays(3),
332 scoped_refptr<DOMAction> dom_action = new DOMAction( 235 Action::ACTION_DOM_ACCESS,
333 "punky", 236 "lets");
334 mock_clock.Now() - base::TimeDelta::FromDays(3), 237 action->mutable_args()->AppendString("vamoose");
335 DomActionType::MODIFIED, 238 action->set_page_url(GURL("http://www.google.com"));
336 GURL("http://www.google.com"), 239 activity_db->RecordAction(action);
337 string16(), 240
338 "lets", 241 action =
339 "vamoose", 242 new Action("punky", mock_clock.Now(), Action::ACTION_DOM_ACCESS, "lets");
340 "extra"); 243 action->mutable_args()->AppendString("too new");
341 scoped_refptr<DOMAction> toonew_dom_action = new DOMAction( 244 action->set_page_url(GURL("http://www.google.com"));
342 "punky", 245 activity_db->RecordAction(action);
343 mock_clock.Now(), 246
344 DomActionType::MODIFIED, 247 action = new Action("punky",
345 GURL("http://www.google.com"), 248 mock_clock.Now() - base::TimeDelta::FromDays(7),
346 string16(), 249 Action::ACTION_DOM_ACCESS,
347 "too new", 250 "lets");
348 "vamoose", 251 action->mutable_args()->AppendString("too old");
349 "extra"); 252 action->set_page_url(GURL("http://www.google.com"));
350 scoped_refptr<DOMAction> tooold_dom_action = new DOMAction( 253 activity_db->RecordAction(action);
351 "punky",
352 mock_clock.Now() - base::TimeDelta::FromDays(7),
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 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 "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 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 "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 = new Action(
470 args_list.AppendString("woof"); 352 "punky", base::Time::Now(), Action::ACTION_API_CALL, "brewster");
471 scoped_refptr<APIAction> action = new APIAction( 353 action->mutable_args()->AppendString("woof");
472 "punky",
473 base::Time::Now(),
474 APIAction::CALL,
475 "brewster",
476 "woooof",
477 args_list,
478 "extra");
479 activity_db->RecordAction(action); 354 activity_db->RecordAction(action);
480 activity_db->Close(); 355 activity_db->Close();
481 } 356 }
482 357
483 } // namespace extensions 358 } // namespace extensions
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