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

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

Issue 2980503002: [Extensions][TaskScheduler] Update ActivityLog for scheduling migration (Closed)
Patch Set: Experiments Created 3 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/test/simple_test_clock.h" 15 #include "base/test/simple_test_clock.h"
16 #include "base/threading/thread_task_runner_handle.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
17 #include "build/build_config.h" 18 #include "build/build_config.h"
18 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" 19 #include "chrome/browser/extensions/activity_log/activity_action_constants.h"
19 #include "chrome/browser/extensions/activity_log/activity_database.h" 20 #include "chrome/browser/extensions/activity_log/activity_database.h"
21 #include "chrome/browser/extensions/activity_log/activity_log_task_runner.h"
20 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" 22 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h"
21 #include "chrome/browser/extensions/extension_service.h" 23 #include "chrome/browser/extensions/extension_service.h"
22 #include "chrome/browser/extensions/test_extension_system.h" 24 #include "chrome/browser/extensions/test_extension_system.h"
23 #include "chrome/common/chrome_constants.h" 25 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
25 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 27 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
26 #include "chrome/test/base/testing_profile.h" 28 #include "chrome/test/base/testing_profile.h"
27 #include "content/public/browser/web_contents.h" 29 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/test_browser_thread.h" 30 #include "content/public/test/test_browser_thread.h"
31 #include "content/public/test/test_utils.h"
29 #include "extensions/common/dom_action_types.h" 32 #include "extensions/common/dom_action_types.h"
30 #include "sql/statement.h" 33 #include "sql/statement.h"
31 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
32 35
33 using content::BrowserThread; 36 #define TEST_ON_ACTIVITY_LOG_RUNNER(test_class, test_name) \
Devlin 2017/07/12 19:30:36 gab@, I'm curious what you think of a pattern like
37 class test_class##_##test_name##_Runner : public test_class { \
38 public: \
39 void RunTest(); \
40 }; \
41 TEST_F(test_class##_##test_name##_Runner, test_name) { \
42 base::RunLoop run_loop; \
43 GetActivityLogTaskRunner()->PostTask( \
44 FROM_HERE, base::Bind(&test_class##_##test_name##_Runner::RunTest, \
45 base::Unretained(this))); \
46 content::RunAllBlockingPoolTasksUntilIdle(); \
47 } \
48 void test_class##_##test_name##_Runner::RunTest()
34 49
35 namespace constants = activity_log_constants; 50 namespace constants = activity_log_constants;
36 51
37 namespace extensions { 52 namespace extensions {
38 53
39 // A dummy implementation of ActivityDatabase::Delegate, sufficient for 54 // A dummy implementation of ActivityDatabase::Delegate, sufficient for
40 // the unit tests. 55 // the unit tests.
41 class ActivityDatabaseTestPolicy : public ActivityDatabase::Delegate { 56 class ActivityDatabaseTestPolicy : public ActivityDatabase::Delegate {
42 public: 57 public:
43 ActivityDatabaseTestPolicy() {} 58 ActivityDatabaseTestPolicy() {}
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 const std::string& api_name) const { 142 const std::string& api_name) const {
128 scoped_refptr<Action> action( 143 scoped_refptr<Action> action(
129 new Action("punky", time, Action::ACTION_API_CALL, api_name)); 144 new Action("punky", time, Action::ACTION_API_CALL, api_name));
130 return action; 145 return action;
131 } 146 }
132 147
133 void Record(ActivityDatabase* db, scoped_refptr<Action> action) { 148 void Record(ActivityDatabase* db, scoped_refptr<Action> action) {
134 db_delegate_->Record(db, action); 149 db_delegate_->Record(db, action);
135 } 150 }
136 151
152 int CountActions(ActivityDatabase* db, const std::string& api_name_pattern) {
153 return CountActions(&db->db_, api_name_pattern);
154 }
155
137 int CountActions(sql::Connection* db, const std::string& api_name_pattern) { 156 int CountActions(sql::Connection* db, const std::string& api_name_pattern) {
138 if (!db->DoesTableExist(ActivityDatabaseTestPolicy::kTableName)) 157 if (!db->DoesTableExist(ActivityDatabaseTestPolicy::kTableName))
139 return -1; 158 return -1;
140 std::string sql_str = "SELECT COUNT(*) FROM " + 159 std::string sql_str = "SELECT COUNT(*) FROM " +
141 std::string(ActivityDatabaseTestPolicy::kTableName) + 160 std::string(ActivityDatabaseTestPolicy::kTableName) +
142 " WHERE api_name LIKE ?"; 161 " WHERE api_name LIKE ?";
143 sql::Statement statement(db->GetCachedStatement( 162 sql::Statement statement(db->GetCachedStatement(
144 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); 163 sql::StatementID(SQL_FROM_HERE), sql_str.c_str()));
145 statement.BindString(0, api_name_pattern); 164 statement.BindString(0, api_name_pattern);
146 if (!statement.Step()) 165 if (!statement.Step())
147 return -1; 166 return -1;
148 return statement.ColumnInt(0); 167 return statement.ColumnInt(0);
149 } 168 }
150 169
170 void RecordBatchedActions(ActivityDatabase* db) {
171 db->RecordBatchedActionsWhileTesting();
172 }
173
151 private: 174 private:
152 ActivityDatabaseTestPolicy* db_delegate_; 175 ActivityDatabaseTestPolicy* db_delegate_;
153 }; 176 };
154 177
155 // Check that the database is initialized properly. 178 // Check that the database is initialized properly.
156 TEST_F(ActivityDatabaseTest, Init) { 179 TEST_ON_ACTIVITY_LOG_RUNNER(ActivityDatabaseTest, Init) {
157 base::ScopedTempDir temp_dir; 180 base::ScopedTempDir temp_dir;
158 base::FilePath db_file; 181 base::FilePath db_file;
159 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 182 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
160 db_file = temp_dir.GetPath().AppendASCII("ActivityInit.db"); 183 db_file = temp_dir.GetPath().AppendASCII("ActivityInit.db");
161 sql::Connection::Delete(db_file); 184 sql::Connection::Delete(db_file);
162 185
163 ActivityDatabase* activity_db = OpenDatabase(db_file); 186 ActivityDatabase* activity_db = OpenDatabase(db_file);
164 activity_db->Close(); 187 activity_db->Close();
165 188
166 sql::Connection db; 189 sql::Connection db;
167 ASSERT_TRUE(db.Open(db_file)); 190 ASSERT_TRUE(db.Open(db_file));
168 ASSERT_TRUE(db.DoesTableExist(ActivityDatabaseTestPolicy::kTableName)); 191 ASSERT_TRUE(db.DoesTableExist(ActivityDatabaseTestPolicy::kTableName));
169 db.Close(); 192 db.Close();
170 } 193 }
171 194
172 // Check that actions are recorded in the db. 195 // Check that actions are recorded in the db.
173 TEST_F(ActivityDatabaseTest, RecordAction) { 196 TEST_ON_ACTIVITY_LOG_RUNNER(ActivityDatabaseTest, RecordAction) {
174 base::ScopedTempDir temp_dir; 197 base::ScopedTempDir temp_dir;
175 base::FilePath db_file; 198 base::FilePath db_file;
176 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 199 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
177 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db"); 200 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db");
178 sql::Connection::Delete(db_file); 201 sql::Connection::Delete(db_file);
179 202
180 ActivityDatabase* activity_db = OpenDatabase(db_file); 203 ActivityDatabase* activity_db = OpenDatabase(db_file);
181 activity_db->SetBatchModeForTesting(false); 204 activity_db->SetBatchModeForTesting(false);
182 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); 205 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster");
183 Record(activity_db, action); 206 Record(activity_db, action);
184 activity_db->Close(); 207 activity_db->Close();
185 208
186 sql::Connection db; 209 sql::Connection db;
187 ASSERT_TRUE(db.Open(db_file)); 210 ASSERT_TRUE(db.Open(db_file));
188 211
189 ASSERT_EQ(1, CountActions(&db, "brewster")); 212 ASSERT_EQ(1, CountActions(&db, "brewster"));
190 } 213 }
191 214
192 TEST_F(ActivityDatabaseTest, BatchModeOff) { 215 TEST_ON_ACTIVITY_LOG_RUNNER(ActivityDatabaseTest, BatchModeOff) {
193 base::ScopedTempDir temp_dir; 216 base::ScopedTempDir temp_dir;
194 base::FilePath db_file; 217 base::FilePath db_file;
195 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 218 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
196 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db"); 219 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db");
197 sql::Connection::Delete(db_file); 220 sql::Connection::Delete(db_file);
198 221
199 // Record some actions 222 // Record some actions
200 ActivityDatabase* activity_db = OpenDatabase(db_file); 223 ActivityDatabase* activity_db = OpenDatabase(db_file);
201 activity_db->SetBatchModeForTesting(false); 224 activity_db->SetBatchModeForTesting(false);
202 225
203 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); 226 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster");
204 Record(activity_db, action); 227 Record(activity_db, action);
205 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); 228 ASSERT_EQ(1, CountActions(activity_db, "brewster"));
206 229
207 activity_db->Close(); 230 activity_db->Close();
208 } 231 }
209 232
210 TEST_F(ActivityDatabaseTest, BatchModeOn) { 233 TEST_ON_ACTIVITY_LOG_RUNNER(ActivityDatabaseTest, BatchModeOn) {
211 base::ScopedTempDir temp_dir; 234 base::ScopedTempDir temp_dir;
212 base::FilePath db_file; 235 base::FilePath db_file;
213 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 236 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
214 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db"); 237 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db");
215 sql::Connection::Delete(db_file); 238 sql::Connection::Delete(db_file);
216 239
217 // Record some actions 240 // Record some actions
218 ActivityDatabase* activity_db = OpenDatabase(db_file); 241 ActivityDatabase* activity_db = OpenDatabase(db_file);
219 activity_db->SetBatchModeForTesting(true); 242 activity_db->SetBatchModeForTesting(true);
220 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); 243 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster");
221 Record(activity_db, action); 244 Record(activity_db, action);
222 ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); 245 ASSERT_EQ(0, CountActions(activity_db, "brewster"));
223 246
224 // Artificially trigger and then stop the timer. 247 // Artificially trigger and then stop the timer.
225 activity_db->SetTimerForTesting(0); 248 RecordBatchedActions(activity_db);
226 base::RunLoop().RunUntilIdle(); 249 ASSERT_EQ(1, CountActions(activity_db, "brewster"));
Devlin 2017/07/12 19:30:36 We can remove this here because SetTimerForTesting
227 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster"));
228 250
229 activity_db->Close(); 251 activity_db->Close();
230 } 252 }
231 253
232 TEST_F(ActivityDatabaseTest, BatchModeFlush) { 254 TEST_ON_ACTIVITY_LOG_RUNNER(ActivityDatabaseTest, BatchModeFlush) {
233 base::ScopedTempDir temp_dir; 255 base::ScopedTempDir temp_dir;
234 base::FilePath db_file; 256 base::FilePath db_file;
235 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 257 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
236 db_file = temp_dir.GetPath().AppendASCII("ActivityFlush.db"); 258 db_file = temp_dir.GetPath().AppendASCII("ActivityFlush.db");
237 sql::Connection::Delete(db_file); 259 sql::Connection::Delete(db_file);
238 260
239 // Record some actions 261 // Record some actions
240 ActivityDatabase* activity_db = OpenDatabase(db_file); 262 ActivityDatabase* activity_db = OpenDatabase(db_file);
241 activity_db->SetBatchModeForTesting(true); 263 activity_db->SetBatchModeForTesting(true);
242 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); 264 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster");
243 Record(activity_db, action); 265 Record(activity_db, action);
244 ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); 266 ASSERT_EQ(0, CountActions(activity_db, "brewster"));
245 267
246 // Request an immediate database flush. 268 // Request an immediate database flush.
247 activity_db->AdviseFlush(ActivityDatabase::kFlushImmediately); 269 activity_db->AdviseFlush(ActivityDatabase::kFlushImmediately);
248 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); 270 ASSERT_EQ(1, CountActions(activity_db, "brewster"));
249 271
250 activity_db->Close(); 272 activity_db->Close();
251 } 273 }
252 274
253 // Check that nothing explodes if the DB isn't initialized. 275 // Check that nothing explodes if the DB isn't initialized.
254 TEST_F(ActivityDatabaseTest, InitFailure) { 276 TEST_ON_ACTIVITY_LOG_RUNNER(ActivityDatabaseTest, InitFailure) {
255 base::ScopedTempDir temp_dir; 277 base::ScopedTempDir temp_dir;
256 base::FilePath db_file; 278 base::FilePath db_file;
257 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 279 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
258 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db"); 280 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db");
259 sql::Connection::Delete(db_file); 281 sql::Connection::Delete(db_file);
260 282
261 ActivityDatabaseTestPolicy* delegate = new ActivityDatabaseTestPolicy(); 283 ActivityDatabaseTestPolicy* delegate = new ActivityDatabaseTestPolicy();
262 ActivityDatabase* activity_db = new ActivityDatabase(delegate); 284 ActivityDatabase* activity_db = new ActivityDatabase(delegate);
263 scoped_refptr<Action> action = new Action( 285 scoped_refptr<Action> action = new Action(
264 "punky", base::Time::Now(), Action::ACTION_API_CALL, "brewster"); 286 "punky", base::Time::Now(), Action::ACTION_API_CALL, "brewster");
265 action->mutable_args()->AppendString("woof"); 287 action->mutable_args()->AppendString("woof");
266 delegate->Record(activity_db, action); 288 delegate->Record(activity_db, action);
267 activity_db->Close(); 289 activity_db->Close();
268 } 290 }
269 291
270 } // namespace extensions 292 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698