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

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: 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), 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_OTHER), statement.ColumnInt(2));
188 ASSERT_EQ("http://www.google.com", statement.ColumnString(3)); 191 //ASSERT_EQ(DomActionType::MODIFIED, statement.ColumnInt(2));
felt 2013/07/16 06:44:03 miss a line?
mvrable 2013/07/16 18:12:35 Fixed. verb wasn't getting saved/checked before,
189 if (CommandLine::ForCurrentProcess()->HasSwitch( 192 if (CommandLine::ForCurrentProcess()->HasSwitch(
190 switches::kEnableExtensionActivityLogTesting)) 193 switches::kEnableExtensionActivityLogTesting))
felt 2013/07/16 06:44:03 If you want to test both, we should have a separat
mvrable 2013/07/16 18:12:35 Is there an easy way to remove a command-line flag
felt 2013/07/16 18:30:17 I must have missed this in a previous review. No,
mvrable 2013/07/16 18:38:39 Done.
191 ASSERT_EQ("/foo?bar", statement.ColumnString(4)); 194 ASSERT_EQ("http://www.google.com/foo?bar", statement.ColumnString(5));
192 else 195 else
193 ASSERT_EQ("/foo", statement.ColumnString(4)); 196 ASSERT_EQ("http://www.google.com/foo", statement.ColumnString(5));
194 ASSERT_EQ("lets", statement.ColumnString(6)); 197 ASSERT_EQ("lets", statement.ColumnString(3));
195 ASSERT_EQ("vamoose", statement.ColumnString(7)); 198 ASSERT_EQ("vamoose", statement.ColumnString(4));
196 ASSERT_EQ("extra", statement.ColumnString(8)); 199 ASSERT_EQ("{\"extra\":\"extra\",\"page_title\":\"\"}",
200 statement.ColumnString(7));
197 } 201 }
198 202
199 // Check that blocked actions are recorded in the db. 203 // Check that blocked actions are recorded in the db.
200 TEST_F(ActivityDatabaseTest, RecordBlockedAction) { 204 TEST_F(ActivityDatabaseTest, RecordBlockedAction) {
201 base::ScopedTempDir temp_dir; 205 base::ScopedTempDir temp_dir;
202 base::FilePath db_file; 206 base::FilePath db_file;
203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 207 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
204 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 208 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
205 base::Delete(db_file, false); 209 base::Delete(db_file, false);
206 210
207 ActivityDatabase* activity_db = OpenDatabase(db_file); 211 ActivityDatabase* activity_db = OpenDatabase(db_file);
208 scoped_refptr<BlockedAction> action = new BlockedAction( 212 scoped_refptr<BlockedAction> action = new BlockedAction(
209 "punky", 213 "punky",
210 base::Time::Now(), 214 base::Time::Now(),
211 "do.evilThings", 215 "do.evilThings",
212 "1, 2", 216 "1, 2",
213 BlockedAction::ACCESS_DENIED, 217 BlockedAction::ACCESS_DENIED,
214 "extra"); 218 "extra");
215 activity_db->RecordAction(action); 219 activity_db->RecordAction(action);
216 activity_db->Close(); 220 activity_db->Close();
217 221
218 sql::Connection db; 222 sql::Connection db;
219 ASSERT_TRUE(db.Open(db_file)); 223 ASSERT_TRUE(db.Open(db_file));
220 224
221 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); 225 ASSERT_TRUE(db.DoesTableExist(FullStreamUIPolicy::kTableName));
222 std::string sql_str = "SELECT * FROM " + 226 std::string sql_str = "SELECT * FROM " +
223 std::string(BlockedAction::kTableName); 227 std::string(FullStreamUIPolicy::kTableName);
224 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); 228 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str()));
225 ASSERT_TRUE(statement.Step()); 229 ASSERT_TRUE(statement.Step());
226 ASSERT_EQ("punky", statement.ColumnString(0)); 230 ASSERT_EQ("punky", statement.ColumnString(0));
227 ASSERT_EQ("do.evilThings", statement.ColumnString(2)); 231 ASSERT_EQ(static_cast<int>(Action::ACTION_API_BLOCKED),
228 ASSERT_EQ("1, 2", statement.ColumnString(3)); 232 statement.ColumnInt(2));
229 ASSERT_EQ(1, statement.ColumnInt(4)); 233 ASSERT_EQ("do.evilThings", statement.ColumnString(3));
230 ASSERT_EQ("extra", statement.ColumnString(5)); 234 ASSERT_EQ("1, 2", statement.ColumnString(4));
235 //ASSERT_EQ(1, statement.ColumnInt(4));
236 ASSERT_EQ("{\"reason\":1}", statement.ColumnString(7));
231 } 237 }
232 238
233 // Check that we can read back recent actions in the db. 239 // Check that we can read back recent actions in the db.
234 TEST_F(ActivityDatabaseTest, GetTodaysActions) { 240 TEST_F(ActivityDatabaseTest, GetTodaysActions) {
235 base::ScopedTempDir temp_dir; 241 base::ScopedTempDir temp_dir;
236 base::FilePath db_file; 242 base::FilePath db_file;
237 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 243 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
238 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 244 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
239 base::Delete(db_file, false); 245 base::Delete(db_file, false);
240 246
241 // Use a mock clock to ensure that events are not recorded on the wrong day 247 // 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. 248 // when the test is run close to local midnight.
243 base::SimpleTestClock mock_clock; 249 base::SimpleTestClock mock_clock;
244 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 250 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
245 base::TimeDelta::FromHours(12)); 251 base::TimeDelta::FromHours(12));
246 252
247 // Record some actions 253 // Record some actions
248 ActivityDatabase* activity_db = OpenDatabase(db_file); 254 ActivityDatabase* activity_db = OpenDatabase(db_file);
255 base::ListValue args_list;
256 args_list.AppendString("woof");
249 scoped_refptr<APIAction> api_action = new APIAction( 257 scoped_refptr<APIAction> api_action = new APIAction(
250 "punky", 258 "punky",
251 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 259 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
252 APIAction::CALL, 260 APIAction::CALL,
253 "brewster", 261 "brewster",
254 "woof", 262 "woof",
263 args_list,
255 "extra"); 264 "extra");
256 scoped_refptr<DOMAction> dom_action = new DOMAction( 265 scoped_refptr<DOMAction> dom_action = new DOMAction(
257 "punky", 266 "punky",
258 mock_clock.Now(), 267 mock_clock.Now(),
259 DomActionType::MODIFIED, 268 DomActionType::MODIFIED,
260 GURL("http://www.google.com"), 269 GURL("http://www.google.com"),
261 string16(), 270 string16(),
262 "lets", 271 "lets",
263 "vamoose", 272 "vamoose",
264 "extra"); 273 "extra");
265 scoped_refptr<DOMAction> extra_dom_action = new DOMAction( 274 scoped_refptr<DOMAction> extra_dom_action = new DOMAction(
266 "scoobydoo", 275 "scoobydoo",
267 mock_clock.Now(), 276 mock_clock.Now(),
268 DomActionType::MODIFIED, 277 DomActionType::MODIFIED,
269 GURL("http://www.google.com"), 278 GURL("http://www.google.com"),
270 string16(), 279 string16(),
271 "lets", 280 "lets",
272 "vamoose", 281 "vamoose",
273 "extra"); 282 "extra");
274 activity_db->RecordAction(api_action); 283 activity_db->RecordAction(api_action);
275 activity_db->RecordAction(dom_action); 284 activity_db->RecordAction(dom_action);
276 activity_db->RecordAction(extra_dom_action); 285 activity_db->RecordAction(extra_dom_action);
277 286
278 // Read them back 287 // Read them back
279 std::string api_print = "ID: punky, CATEGORY: call, " 288 std::string api_print =
280 "API: brewster, ARGS: woof"; 289 "ID=punky CATEGORY=api_call API=brewster ARGS=[\"woof\"] OTHER={}";
281 std::string dom_print = "DOM API CALL: lets, ARGS: vamoose, VERB: modified"; 290 std::string dom_print = "ID=punky CATEGORY=dom_other API=lets ARGS=[] "
291 "PAGE_URL=http://www.google.com/ OTHER={}";
282 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = 292 scoped_ptr<std::vector<scoped_refptr<Action> > > actions =
283 activity_db->GetActions("punky", 0); 293 activity_db->GetActions("punky", 0);
284 ASSERT_EQ(2, static_cast<int>(actions->size())); 294 ASSERT_EQ(2, static_cast<int>(actions->size()));
285 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); 295 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug());
286 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); 296 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug());
287 297
288 activity_db->Close(); 298 activity_db->Close();
289 } 299 }
290 300
291 // Check that we can read back recent actions in the db. 301 // Check that we can read back recent actions in the db.
292 TEST_F(ActivityDatabaseTest, GetOlderActions) { 302 TEST_F(ActivityDatabaseTest, GetOlderActions) {
293 base::ScopedTempDir temp_dir; 303 base::ScopedTempDir temp_dir;
294 base::FilePath db_file; 304 base::FilePath db_file;
295 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 305 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
296 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 306 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
297 base::Delete(db_file, false); 307 base::Delete(db_file, false);
298 308
299 // Use a mock clock to ensure that events are not recorded on the wrong day 309 // 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. 310 // when the test is run close to local midnight.
301 base::SimpleTestClock mock_clock; 311 base::SimpleTestClock mock_clock;
302 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 312 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
303 base::TimeDelta::FromHours(12)); 313 base::TimeDelta::FromHours(12));
304 314
305 // Record some actions 315 // Record some actions
306 ActivityDatabase* activity_db = OpenDatabase(db_file); 316 ActivityDatabase* activity_db = OpenDatabase(db_file);
317 base::ListValue args_list;
318 args_list.AppendString("woof");
307 scoped_refptr<APIAction> api_action = new APIAction( 319 scoped_refptr<APIAction> api_action = new APIAction(
308 "punky", 320 "punky",
309 mock_clock.Now() - base::TimeDelta::FromDays(3) 321 mock_clock.Now() - base::TimeDelta::FromDays(3)
310 - base::TimeDelta::FromMinutes(40), 322 - base::TimeDelta::FromMinutes(40),
311 APIAction::CALL, 323 APIAction::CALL,
312 "brewster", 324 "brewster",
313 "woof", 325 "woof",
326 args_list,
314 "extra"); 327 "extra");
315 scoped_refptr<DOMAction> dom_action = new DOMAction( 328 scoped_refptr<DOMAction> dom_action = new DOMAction(
316 "punky", 329 "punky",
317 mock_clock.Now() - base::TimeDelta::FromDays(3), 330 mock_clock.Now() - base::TimeDelta::FromDays(3),
318 DomActionType::MODIFIED, 331 DomActionType::MODIFIED,
319 GURL("http://www.google.com"), 332 GURL("http://www.google.com"),
320 string16(), 333 string16(),
321 "lets", 334 "lets",
322 "vamoose", 335 "vamoose",
323 "extra"); 336 "extra");
(...skipping 14 matching lines...) Expand all
338 string16(), 351 string16(),
339 "too old", 352 "too old",
340 "vamoose", 353 "vamoose",
341 "extra"); 354 "extra");
342 activity_db->RecordAction(api_action); 355 activity_db->RecordAction(api_action);
343 activity_db->RecordAction(dom_action); 356 activity_db->RecordAction(dom_action);
344 activity_db->RecordAction(toonew_dom_action); 357 activity_db->RecordAction(toonew_dom_action);
345 activity_db->RecordAction(tooold_dom_action); 358 activity_db->RecordAction(tooold_dom_action);
346 359
347 // Read them back 360 // Read them back
348 std::string api_print = "ID: punky, CATEGORY: call, " 361 std::string api_print =
349 "API: brewster, ARGS: woof"; 362 "ID=punky CATEGORY=api_call API=brewster ARGS=[\"woof\"] OTHER={}";
350 std::string dom_print = "DOM API CALL: lets, ARGS: vamoose, VERB: modified"; 363 std::string dom_print = "ID=punky CATEGORY=dom_other API=lets ARGS=[] "
364 "PAGE_URL=http://www.google.com/ OTHER={}";
351 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = 365 scoped_ptr<std::vector<scoped_refptr<Action> > > actions =
352 activity_db->GetActions("punky", 3); 366 activity_db->GetActions("punky", 3);
353 ASSERT_EQ(2, static_cast<int>(actions->size())); 367 ASSERT_EQ(2, static_cast<int>(actions->size()));
354 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug()); 368 ASSERT_EQ(dom_print, actions->at(0)->PrintForDebug());
355 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); 369 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug());
356 370
357 activity_db->Close(); 371 activity_db->Close();
358 } 372 }
359 373
360 TEST_F(ActivityDatabaseTest, BatchModeOff) { 374 TEST_F(ActivityDatabaseTest, BatchModeOff) {
361 base::ScopedTempDir temp_dir; 375 base::ScopedTempDir temp_dir;
362 base::FilePath db_file; 376 base::FilePath db_file;
363 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 377 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
364 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 378 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
365 base::Delete(db_file, false); 379 base::Delete(db_file, false);
366 380
367 // Use a mock clock to ensure that events are not recorded on the wrong day 381 // 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. 382 // when the test is run close to local midnight.
369 base::SimpleTestClock mock_clock; 383 base::SimpleTestClock mock_clock;
370 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 384 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
371 base::TimeDelta::FromHours(12)); 385 base::TimeDelta::FromHours(12));
372 386
373 // Record some actions 387 // Record some actions
374 ActivityDatabase* activity_db = OpenDatabase(db_file); 388 ActivityDatabase* activity_db = OpenDatabase(db_file);
375 activity_db->SetBatchModeForTesting(false); 389 activity_db->SetBatchModeForTesting(false);
376 activity_db->SetClockForTesting(&mock_clock); 390 activity_db->SetClockForTesting(&mock_clock);
391 base::ListValue args_list;
392 args_list.AppendString("woof");
377 scoped_refptr<APIAction> api_action = new APIAction( 393 scoped_refptr<APIAction> api_action = new APIAction(
378 "punky", 394 "punky",
379 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 395 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
380 APIAction::CALL, 396 APIAction::CALL,
381 "brewster", 397 "brewster",
382 "woof", 398 "woof",
399 args_list,
383 "extra"); 400 "extra");
384 activity_db->RecordAction(api_action); 401 activity_db->RecordAction(api_action);
385 402
386 scoped_ptr<std::vector<scoped_refptr<Action> > > actions = 403 scoped_ptr<std::vector<scoped_refptr<Action> > > actions =
387 activity_db->GetActions("punky", 0); 404 activity_db->GetActions("punky", 0);
388 ASSERT_EQ(1, static_cast<int>(actions->size())); 405 ASSERT_EQ(1, static_cast<int>(actions->size()));
389 activity_db->Close(); 406 activity_db->Close();
390 } 407 }
391 408
392 TEST_F(ActivityDatabaseTest, BatchModeOn) { 409 TEST_F(ActivityDatabaseTest, BatchModeOn) {
393 base::ScopedTempDir temp_dir; 410 base::ScopedTempDir temp_dir;
394 base::FilePath db_file; 411 base::FilePath db_file;
395 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 412 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
396 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 413 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
397 base::Delete(db_file, false); 414 base::Delete(db_file, false);
398 415
399 // Use a mock clock to set the time, and a special timer to control the 416 // Use a mock clock to set the time, and a special timer to control the
400 // timing and skip ahead in time. 417 // timing and skip ahead in time.
401 base::SimpleTestClock mock_clock; 418 base::SimpleTestClock mock_clock;
402 mock_clock.SetNow(base::Time::Now().LocalMidnight() + 419 mock_clock.SetNow(base::Time::Now().LocalMidnight() +
403 base::TimeDelta::FromHours(11)); 420 base::TimeDelta::FromHours(11));
404 421
405 // Record some actions 422 // Record some actions
406 ActivityDatabase* activity_db = OpenDatabase(db_file); 423 ActivityDatabase* activity_db = OpenDatabase(db_file);
407 activity_db->SetBatchModeForTesting(true); 424 activity_db->SetBatchModeForTesting(true);
408 activity_db->SetClockForTesting(&mock_clock); 425 activity_db->SetClockForTesting(&mock_clock);
426 base::ListValue args_list;
427 args_list.AppendString("woof");
409 scoped_refptr<APIAction> api_action = new APIAction( 428 scoped_refptr<APIAction> api_action = new APIAction(
410 "punky", 429 "punky",
411 mock_clock.Now() - base::TimeDelta::FromMinutes(40), 430 mock_clock.Now() - base::TimeDelta::FromMinutes(40),
412 APIAction::CALL, 431 APIAction::CALL,
413 "brewster", 432 "brewster",
414 "woof", 433 "woof",
434 args_list,
415 "extra"); 435 "extra");
416 activity_db->RecordAction(api_action); 436 activity_db->RecordAction(api_action);
417 437
418 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_before = 438 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_before =
419 activity_db->GetActions("punky", 0); 439 activity_db->GetActions("punky", 0);
420 ASSERT_EQ(0, static_cast<int>(actions_before->size())); 440 ASSERT_EQ(0, static_cast<int>(actions_before->size()));
421 441
422 // Artificially trigger and then stop the timer. 442 // Artificially trigger and then stop the timer.
423 activity_db->SetTimerForTesting(0); 443 activity_db->SetTimerForTesting(0);
424 base::MessageLoop::current()->RunUntilIdle(); 444 base::MessageLoop::current()->RunUntilIdle();
425 445
426 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_after = 446 scoped_ptr<std::vector<scoped_refptr<Action> > > actions_after =
427 activity_db->GetActions("punky", 0); 447 activity_db->GetActions("punky", 0);
428 ASSERT_EQ(1, static_cast<int>(actions_after->size())); 448 ASSERT_EQ(1, static_cast<int>(actions_after->size()));
429 449
430 activity_db->Close(); 450 activity_db->Close();
431 } 451 }
432 452
433 // Check that nothing explodes if the DB isn't initialized. 453 // Check that nothing explodes if the DB isn't initialized.
434 TEST_F(ActivityDatabaseTest, InitFailure) { 454 TEST_F(ActivityDatabaseTest, InitFailure) {
435 base::ScopedTempDir temp_dir; 455 base::ScopedTempDir temp_dir;
436 base::FilePath db_file; 456 base::FilePath db_file;
437 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 457 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
438 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); 458 db_file = temp_dir.path().AppendASCII("ActivityRecord.db");
439 base::Delete(db_file, false); 459 base::Delete(db_file, false);
440 460
441 ActivityDatabase* activity_db = 461 ActivityDatabase* activity_db =
442 new ActivityDatabase(new ActivityDatabaseTestPolicy()); 462 new ActivityDatabase(new ActivityDatabaseTestPolicy());
463 base::ListValue args_list;
464 args_list.AppendString("woof");
443 scoped_refptr<APIAction> action = new APIAction( 465 scoped_refptr<APIAction> action = new APIAction(
444 "punky", 466 "punky",
445 base::Time::Now(), 467 base::Time::Now(),
446 APIAction::CALL, 468 APIAction::CALL,
447 "brewster", 469 "brewster",
448 "woooof", 470 "woooof",
471 args_list,
449 "extra"); 472 "extra");
450 activity_db->RecordAction(action); 473 activity_db->RecordAction(action);
451 activity_db->Close(); 474 activity_db->Close();
452 } 475 }
453 476
454 } // namespace extensions 477 } // namespace extensions
455 478
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698