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

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

Powered by Google App Engine
This is Rietveld 408576698