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

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

Issue 19234003: Extension activity log database refactoring (step 2) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address some reviewer comments 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_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
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
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("{\"reason\":1}", statement.ColumnString(7));
230 } 239 }
231 240
232 // Check that we can read back recent actions in the db. 241 // Check that we can read back recent actions in the db.
233 TEST_F(ActivityDatabaseTest, GetTodaysActions) { 242 TEST_F(ActivityDatabaseTest, GetTodaysActions) {
234 base::ScopedTempDir temp_dir; 243 base::ScopedTempDir temp_dir;
235 base::FilePath db_file; 244 base::FilePath db_file;
236 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 245 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
237 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 246 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
238 base::DeleteFile(db_file, false); 247 base::DeleteFile(db_file, false);
239 248
240 // Use a mock clock to ensure that events are not recorded on the wrong day 249 // 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. 250 // when the test is run close to local midnight.
242 base::SimpleTestClock mock_clock; 251 base::SimpleTestClock mock_clock;
243 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 252 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
244 base::TimeDelta::FromHours(12)); 253 base::TimeDelta::FromHours(12));
245 254
246 // Record some actions 255 // Record some actions
247 ActivityDatabase* activity_db = OpenDatabase(db_file); 256 ActivityDatabase* activity_db = OpenDatabase(db_file);
257 base::ListValue args_list;
258 args_list.AppendString("woof");
248 scoped_refptr<APIAction> api_action = new APIAction( 259 scoped_refptr<APIAction> api_action = new APIAction(
249 "punky", 260 "punky",
250 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 261 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
251 APIAction::CALL, 262 APIAction::CALL,
252 "brewster", 263 "brewster",
253 "woof", 264 "woof",
265 args_list,
254 "extra"); 266 "extra");
255 scoped_refptr<DOMAction> dom_action = new DOMAction( 267 scoped_refptr<DOMAction> dom_action = new DOMAction(
256 "punky", 268 "punky",
257 mock_clock.Now(), 269 mock_clock.Now(),
258 DomActionType::MODIFIED, 270 DomActionType::MODIFIED,
259 GURL("http://www.google.com"), 271 GURL("http://www.google.com"),
260 string16(), 272 string16(),
261 "lets", 273 "lets",
262 "vamoose", 274 "vamoose",
263 "extra"); 275 "extra");
264 scoped_refptr<DOMAction> extra_dom_action = new DOMAction( 276 scoped_refptr<DOMAction> extra_dom_action = new DOMAction(
265 "scoobydoo", 277 "scoobydoo",
266 mock_clock.Now(), 278 mock_clock.Now(),
267 DomActionType::MODIFIED, 279 DomActionType::MODIFIED,
268 GURL("http://www.google.com"), 280 GURL("http://www.google.com"),
269 string16(), 281 string16(),
270 "lets", 282 "lets",
271 "vamoose", 283 "vamoose",
272 "extra"); 284 "extra");
273 activity_db->RecordAction(api_action); 285 activity_db->RecordAction(api_action);
274 activity_db->RecordAction(dom_action); 286 activity_db->RecordAction(dom_action);
275 activity_db->RecordAction(extra_dom_action); 287 activity_db->RecordAction(extra_dom_action);
276 288
277 // Read them back 289 // Read them back
278 std::string api_print = "ID: punky, CATEGORY: call, " 290 std::string api_print =
279 "API: brewster, ARGS: woof"; 291 "ID=punky CATEGORY=api_call API=brewster ARGS=[\"woof\"] OTHER={}";
280 std::string dom_print = "DOM API CALL: lets, ARGS: vamoose, VERB: modified"; 292 std::string dom_print =
293 "ID=punky CATEGORY=dom_access API=lets ARGS=[\"vamoose\"] "
294 "PAGE_URL=http://www.google.com/ "
295 "OTHER={\"dom_verb\":6,\"extra\":\"extra\",\"page_title\":\"\"}";
281 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = 296 scoped_ptr<std::vector<scoped_refptr<Action> > > actions =
282 activity_db->GetActions("punky", 0); 297 activity_db->GetActions("punky", 0);
283 ASSERT_EQ(2, static_cast<int>(actions->size())); 298 ASSERT_EQ(2, static_cast<int>(actions->size()));
284 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); 299 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug());
285 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); 300 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug());
286 301
287 activity_db->Close(); 302 activity_db->Close();
288 } 303 }
289 304
290 // Check that we can read back recent actions in the db. 305 // Check that we can read back recent actions in the db.
291 TEST_F(ActivityDatabaseTest, GetOlderActions) { 306 TEST_F(ActivityDatabaseTest, GetOlderActions) {
292 base::ScopedTempDir temp_dir; 307 base::ScopedTempDir temp_dir;
293 base::FilePath db_file; 308 base::FilePath db_file;
294 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 309 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
295 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 310 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
296 base::DeleteFile(db_file, false); 311 base::DeleteFile(db_file, false);
297 312
298 // Use a mock clock to ensure that events are not recorded on the wrong day 313 // 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. 314 // when the test is run close to local midnight.
300 base::SimpleTestClock mock_clock; 315 base::SimpleTestClock mock_clock;
301 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 316 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
302 base::TimeDelta::FromHours(12)); 317 base::TimeDelta::FromHours(12));
303 318
304 // Record some actions 319 // Record some actions
305 ActivityDatabase* activity_db = OpenDatabase(db_file); 320 ActivityDatabase* activity_db = OpenDatabase(db_file);
321 base::ListValue args_list;
322 args_list.AppendString("woof");
306 scoped_refptr<APIAction> api_action = new APIAction( 323 scoped_refptr<APIAction> api_action = new APIAction(
307 "punky", 324 "punky",
308 mock_clock.Now() - base::TimeDelta::FromDays(3) 325 mock_clock.Now() - base::TimeDelta::FromDays(3)
309 - base::TimeDelta::FromMinutes(40), 326 - base::TimeDelta::FromMinutes(40),
310 APIAction::CALL, 327 APIAction::CALL,
311 "brewster", 328 "brewster",
312 "woof", 329 "woof",
330 args_list,
313 "extra"); 331 "extra");
314 scoped_refptr<DOMAction> dom_action = new DOMAction( 332 scoped_refptr<DOMAction> dom_action = new DOMAction(
315 "punky", 333 "punky",
316 mock_clock.Now() - base::TimeDelta::FromDays(3), 334 mock_clock.Now() - base::TimeDelta::FromDays(3),
317 DomActionType::MODIFIED, 335 DomActionType::MODIFIED,
318 GURL("http://www.google.com"), 336 GURL("http://www.google.com"),
319 string16(), 337 string16(),
320 "lets", 338 "lets",
321 "vamoose", 339 "vamoose",
322 "extra"); 340 "extra");
(...skipping 14 matching lines...) Expand all
337 string16(), 355 string16(),
338 "too old", 356 "too old",
339 "vamoose", 357 "vamoose",
340 "extra"); 358 "extra");
341 activity_db->RecordAction(api_action); 359 activity_db->RecordAction(api_action);
342 activity_db->RecordAction(dom_action); 360 activity_db->RecordAction(dom_action);
343 activity_db->RecordAction(toonew_dom_action); 361 activity_db->RecordAction(toonew_dom_action);
344 activity_db->RecordAction(tooold_dom_action); 362 activity_db->RecordAction(tooold_dom_action);
345 363
346 // Read them back 364 // Read them back
347 std::string api_print = "ID: punky, CATEGORY: call, " 365 std::string api_print =
348 "API: brewster, ARGS: woof"; 366 "ID=punky CATEGORY=api_call API=brewster ARGS=[\"woof\"] OTHER={}";
349 std::string dom_print = "DOM API CALL: lets, ARGS: vamoose, VERB: modified"; 367 std::string dom_print =
368 "ID=punky CATEGORY=dom_access API=lets ARGS=[\"vamoose\"] "
369 "PAGE_URL=http://www.google.com/ "
370 "OTHER={\"dom_verb\":6,\"extra\":\"extra\",\"page_title\":\"\"}";
350 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = 371 scoped_ptr<std::vector<scoped_refptr<Action> > > actions =
351 activity_db->GetActions("punky", 3); 372 activity_db->GetActions("punky", 3);
352 ASSERT_EQ(2, static_cast<int>(actions->size())); 373 ASSERT_EQ(2, static_cast<int>(actions->size()));
353 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); 374 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug());
354 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); 375 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug());
355 376
356 activity_db->Close(); 377 activity_db->Close();
357 } 378 }
358 379
359 TEST_F(ActivityDatabaseTest, BatchModeOff) { 380 TEST_F(ActivityDatabaseTest, BatchModeOff) {
360 base::ScopedTempDir temp_dir; 381 base::ScopedTempDir temp_dir;
361 base::FilePath db_file; 382 base::FilePath db_file;
362 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 383 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
363 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 384 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
364 base::DeleteFile(db_file, false); 385 base::DeleteFile(db_file, false);
365 386
366 // Use a mock clock to ensure that events are not recorded on the wrong day 387 // 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. 388 // when the test is run close to local midnight.
368 base::SimpleTestClock mock_clock; 389 base::SimpleTestClock mock_clock;
369 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 390 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
370 base::TimeDelta::FromHours(12)); 391 base::TimeDelta::FromHours(12));
371 392
372 // Record some actions 393 // Record some actions
373 ActivityDatabase* activity_db = OpenDatabase(db_file); 394 ActivityDatabase* activity_db = OpenDatabase(db_file);
374 activity_db->SetBatchModeForTesting(false); 395 activity_db->SetBatchModeForTesting(false);
375 activity_db->SetClockForTesting(&mock_clock); 396 activity_db->SetClockForTesting(&mock_clock);
397 base::ListValue args_list;
398 args_list.AppendString("woof");
376 scoped_refptr<APIAction> api_action = new APIAction( 399 scoped_refptr<APIAction> api_action = new APIAction(
377 "punky", 400 "punky",
378 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 401 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
379 APIAction::CALL, 402 APIAction::CALL,
380 "brewster", 403 "brewster",
381 "woof", 404 "woof",
405 args_list,
382 "extra"); 406 "extra");
383 activity_db->RecordAction(api_action); 407 activity_db->RecordAction(api_action);
384 408
385 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = 409 scoped_ptr<std::vector<scoped_refptr<Action> > > actions =
386 activity_db->GetActions("punky", 0); 410 activity_db->GetActions("punky", 0);
387 ASSERT_EQ(1, static_cast<int>(actions->size())); 411 ASSERT_EQ(1, static_cast<int>(actions->size()));
388 activity_db->Close(); 412 activity_db->Close();
389 } 413 }
390 414
391 TEST_F(ActivityDatabaseTest, BatchModeOn) { 415 TEST_F(ActivityDatabaseTest, BatchModeOn) {
392 base::ScopedTempDir temp_dir; 416 base::ScopedTempDir temp_dir;
393 base::FilePath db_file; 417 base::FilePath db_file;
394 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 418 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
395 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 419 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
396 base::DeleteFile(db_file, false); 420 base::DeleteFile(db_file, false);
397 421
398 // Use a mock clock to set the time, and a special timer to control the 422 // Use a mock clock to set the time, and a special timer to control the
399 // timing and skip ahead in time. 423 // timing and skip ahead in time.
400 base::SimpleTestClock mock_clock; 424 base::SimpleTestClock mock_clock;
401 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 425 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
402 base::TimeDelta::FromHours(11)); 426 base::TimeDelta::FromHours(11));
403 427
404 // Record some actions 428 // Record some actions
405 ActivityDatabase* activity_db = OpenDatabase(db_file); 429 ActivityDatabase* activity_db = OpenDatabase(db_file);
406 activity_db->SetBatchModeForTesting(true); 430 activity_db->SetBatchModeForTesting(true);
407 activity_db->SetClockForTesting(&mock_clock); 431 activity_db->SetClockForTesting(&mock_clock);
432 base::ListValue args_list;
433 args_list.AppendString("woof");
408 scoped_refptr<APIAction> api_action = new APIAction( 434 scoped_refptr<APIAction> api_action = new APIAction(
409 "punky", 435 "punky",
410 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 436 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
411 APIAction::CALL, 437 APIAction::CALL,
412 "brewster", 438 "brewster",
413 "woof", 439 "woof",
440 args_list,
414 "extra"); 441 "extra");
415 activity_db->RecordAction(api_action); 442 activity_db->RecordAction(api_action);
416 443
417 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_before = 444 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_before =
418 activity_db->GetActions("punky", 0); 445 activity_db->GetActions("punky", 0);
419 ASSERT_EQ(0, static_cast<int>(actions_before->size())); 446 ASSERT_EQ(0, static_cast<int>(actions_before->size()));
420 447
421 // Artificially trigger and then stop the timer. 448 // Artificially trigger and then stop the timer.
422 activity_db->SetTimerForTesting(0); 449 activity_db->SetTimerForTesting(0);
423 base::MessageLoop::current()->RunUntilIdle(); 450 base::MessageLoop::current()->RunUntilIdle();
424 451
425 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_after = 452 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_after =
426 activity_db->GetActions("punky", 0); 453 activity_db->GetActions("punky", 0);
427 ASSERT_EQ(1, static_cast<int>(actions_after->size())); 454 ASSERT_EQ(1, static_cast<int>(actions_after->size()));
428 455
429 activity_db->Close(); 456 activity_db->Close();
430 } 457 }
431 458
432 // Check that nothing explodes if the DB isn't initialized. 459 // Check that nothing explodes if the DB isn't initialized.
433 TEST_F(ActivityDatabaseTest, InitFailure) { 460 TEST_F(ActivityDatabaseTest, InitFailure) {
434 base::ScopedTempDir temp_dir; 461 base::ScopedTempDir temp_dir;
435 base::FilePath db_file; 462 base::FilePath db_file;
436 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 463 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
437 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 464 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
438 base::DeleteFile(db_file, false); 465 base::DeleteFile(db_file, false);
439 466
440 ActivityDatabase* activity_db = 467 ActivityDatabase* activity_db =
441 new ActivityDatabase(new ActivityDatabaseTestPolicy()); 468 new ActivityDatabase(new ActivityDatabaseTestPolicy());
469 base::ListValue args_list;
470 args_list.AppendString("woof");
442 scoped_refptr<APIAction> action = new APIAction( 471 scoped_refptr<APIAction> action = new APIAction(
443 "punky", 472 "punky",
444 base::Time::Now(), 473 base::Time::Now(),
445 APIAction::CALL, 474 APIAction::CALL,
446 "brewster", 475 "brewster",
447 "woooof", 476 "woooof",
477 args_list,
448 "extra"); 478 "extra");
449 activity_db->RecordAction(action); 479 activity_db->RecordAction(action);
450 activity_db->Close(); 480 activity_db->Close();
451 } 481 }
452 482
453 } // namespace extensions 483 } // 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