| OLD | NEW | 
|---|
| 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 | 6 | 
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" | 
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" | 
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" | 
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" | 
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 96     } | 96     } | 
| 97   } | 97   } | 
| 98 | 98 | 
| 99   queue_.clear(); | 99   queue_.clear(); | 
| 100   return true; | 100   return true; | 
| 101 } | 101 } | 
| 102 | 102 | 
| 103 void ActivityDatabaseTestPolicy::Record(ActivityDatabase* db, | 103 void ActivityDatabaseTestPolicy::Record(ActivityDatabase* db, | 
| 104                                         scoped_refptr<Action> action) { | 104                                         scoped_refptr<Action> action) { | 
| 105   queue_.push_back(action); | 105   queue_.push_back(action); | 
| 106   db->NotifyAction(); | 106   db->AdviseFlush(queue_.size()); | 
| 107 } | 107 } | 
| 108 | 108 | 
| 109 class ActivityDatabaseTest : public ChromeRenderViewHostTestHarness { | 109 class ActivityDatabaseTest : public ChromeRenderViewHostTestHarness { | 
| 110  protected: | 110  protected: | 
| 111   virtual void SetUp() OVERRIDE { | 111   virtual void SetUp() OVERRIDE { | 
| 112     ChromeRenderViewHostTestHarness::SetUp(); | 112     ChromeRenderViewHostTestHarness::SetUp(); | 
| 113 #if defined OS_CHROMEOS | 113 #if defined OS_CHROMEOS | 
| 114     test_user_manager_.reset(new chromeos::ScopedTestUserManager()); | 114     test_user_manager_.reset(new chromeos::ScopedTestUserManager()); | 
| 115 #endif | 115 #endif | 
| 116     CommandLine command_line(CommandLine::NO_PROGRAM); | 116     CommandLine command_line(CommandLine::NO_PROGRAM); | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
| 144   void Record(ActivityDatabase* db, scoped_refptr<Action> action) { | 144   void Record(ActivityDatabase* db, scoped_refptr<Action> action) { | 
| 145     db_delegate_->Record(db, action); | 145     db_delegate_->Record(db, action); | 
| 146   } | 146   } | 
| 147 | 147 | 
| 148   int CountActions(sql::Connection* db, const std::string& api_name_pattern) { | 148   int CountActions(sql::Connection* db, const std::string& api_name_pattern) { | 
| 149     if (!db->DoesTableExist(ActivityDatabaseTestPolicy::kTableName)) | 149     if (!db->DoesTableExist(ActivityDatabaseTestPolicy::kTableName)) | 
| 150       return -1; | 150       return -1; | 
| 151     std::string sql_str = "SELECT COUNT(*) FROM " + | 151     std::string sql_str = "SELECT COUNT(*) FROM " + | 
| 152                           std::string(ActivityDatabaseTestPolicy::kTableName) + | 152                           std::string(ActivityDatabaseTestPolicy::kTableName) + | 
| 153                           " WHERE api_name LIKE ?"; | 153                           " WHERE api_name LIKE ?"; | 
| 154     sql::Statement statement(db->GetUniqueStatement(sql_str.c_str())); | 154     sql::Statement statement(db->GetCachedStatement( | 
|  | 155         sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | 
| 155     statement.BindString(0, api_name_pattern); | 156     statement.BindString(0, api_name_pattern); | 
| 156     if (!statement.Step()) | 157     if (!statement.Step()) | 
| 157       return -1; | 158       return -1; | 
| 158     return statement.ColumnInt(0); | 159     return statement.ColumnInt(0); | 
| 159   } | 160   } | 
| 160 | 161 | 
| 161  private: | 162  private: | 
| 162 #if defined OS_CHROMEOS | 163 #if defined OS_CHROMEOS | 
| 163   chromeos::ScopedStubNetworkLibraryEnabler stub_network_library_enabler_; | 164   chromeos::ScopedStubNetworkLibraryEnabler stub_network_library_enabler_; | 
| 164   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 165   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 239   ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); | 240   ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); | 
| 240 | 241 | 
| 241   // Artificially trigger and then stop the timer. | 242   // Artificially trigger and then stop the timer. | 
| 242   activity_db->SetTimerForTesting(0); | 243   activity_db->SetTimerForTesting(0); | 
| 243   base::MessageLoop::current()->RunUntilIdle(); | 244   base::MessageLoop::current()->RunUntilIdle(); | 
| 244   ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); | 245   ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); | 
| 245 | 246 | 
| 246   activity_db->Close(); | 247   activity_db->Close(); | 
| 247 } | 248 } | 
| 248 | 249 | 
|  | 250 TEST_F(ActivityDatabaseTest, BatchModeFlush) { | 
|  | 251   base::ScopedTempDir temp_dir; | 
|  | 252   base::FilePath db_file; | 
|  | 253   ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 
|  | 254   db_file = temp_dir.path().AppendASCII("ActivityFlush.db"); | 
|  | 255   base::DeleteFile(db_file, false); | 
|  | 256 | 
|  | 257   // Record some actions | 
|  | 258   ActivityDatabase* activity_db = OpenDatabase(db_file); | 
|  | 259   activity_db->SetBatchModeForTesting(true); | 
|  | 260   scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); | 
|  | 261   Record(activity_db, action); | 
|  | 262   ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); | 
|  | 263 | 
|  | 264   // Request an immediate database flush. | 
|  | 265   activity_db->AdviseFlush(ActivityDatabase::kFlushImmediately); | 
|  | 266   ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); | 
|  | 267 | 
|  | 268   activity_db->Close(); | 
|  | 269 } | 
|  | 270 | 
| 249 // Check that nothing explodes if the DB isn't initialized. | 271 // Check that nothing explodes if the DB isn't initialized. | 
| 250 TEST_F(ActivityDatabaseTest, InitFailure) { | 272 TEST_F(ActivityDatabaseTest, InitFailure) { | 
| 251   base::ScopedTempDir temp_dir; | 273   base::ScopedTempDir temp_dir; | 
| 252   base::FilePath db_file; | 274   base::FilePath db_file; | 
| 253   ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 275   ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 
| 254   db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 276   db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 
| 255   base::DeleteFile(db_file, false); | 277   base::DeleteFile(db_file, false); | 
| 256 | 278 | 
| 257   ActivityDatabaseTestPolicy* delegate = new ActivityDatabaseTestPolicy(); | 279   ActivityDatabaseTestPolicy* delegate = new ActivityDatabaseTestPolicy(); | 
| 258   ActivityDatabase* activity_db = new ActivityDatabase(delegate); | 280   ActivityDatabase* activity_db = new ActivityDatabase(delegate); | 
| 259   scoped_refptr<Action> action = new Action( | 281   scoped_refptr<Action> action = new Action( | 
| 260       "punky", base::Time::Now(), Action::ACTION_API_CALL, "brewster"); | 282       "punky", base::Time::Now(), Action::ACTION_API_CALL, "brewster"); | 
| 261   action->mutable_args()->AppendString("woof"); | 283   action->mutable_args()->AppendString("woof"); | 
| 262   delegate->Record(activity_db, action); | 284   delegate->Record(activity_db, action); | 
| 263   activity_db->Close(); | 285   activity_db->Close(); | 
| 264 } | 286 } | 
| 265 | 287 | 
| 266 }  // namespace extensions | 288 }  // namespace extensions | 
| OLD | NEW | 
|---|