OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 #include "base/file_path.h" |
| 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "chrome/browser/extensions/activity_database.h" |
| 10 #include "chrome/browser/extensions/api_actions.h" |
| 11 #include "chrome/browser/extensions/blocked_actions.h" |
| 12 #include "chrome/browser/extensions/url_actions.h" |
| 13 #include "sql/statement.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 // Check that the database is initialized properly. |
| 19 TEST(ActivityDatabaseTest, Init) { |
| 20 base::ScopedTempDir temp_dir; |
| 21 FilePath db_file; |
| 22 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 23 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); |
| 24 file_util::Delete(db_file, false); |
| 25 |
| 26 ActivityDatabase* activity_db = new ActivityDatabase(); |
| 27 activity_db->AddRef(); |
| 28 activity_db->Init(db_file); |
| 29 ASSERT_TRUE(activity_db->initialized()); |
| 30 activity_db->Release(); |
| 31 |
| 32 sql::Connection db; |
| 33 ASSERT_TRUE(db.Open(db_file)); |
| 34 ASSERT_TRUE(db.DoesTableExist(UrlAction::kTableName)); |
| 35 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); |
| 36 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); |
| 37 db.Close(); |
| 38 } |
| 39 |
| 40 // Check that actions are recorded in the db. |
| 41 TEST(ActivityDatabaseTest, RecordAction) { |
| 42 base::ScopedTempDir temp_dir; |
| 43 FilePath db_file; |
| 44 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 45 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 46 file_util::Delete(db_file, false); |
| 47 |
| 48 ActivityDatabase* activity_db = new ActivityDatabase(); |
| 49 activity_db->AddRef(); |
| 50 activity_db->Init(db_file); |
| 51 ASSERT_TRUE(activity_db->initialized()); |
| 52 scoped_refptr<APIAction> action = new APIAction( |
| 53 "punky", |
| 54 base::Time::Now(), |
| 55 APIAction::READ, |
| 56 APIAction::BOOKMARK, |
| 57 "brewster", |
| 58 ""); |
| 59 activity_db->RecordAction(action); |
| 60 activity_db->Close(); |
| 61 activity_db->Release(); |
| 62 |
| 63 sql::Connection db; |
| 64 ASSERT_TRUE(db.Open(db_file)); |
| 65 |
| 66 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); |
| 67 std::string sql_str = "SELECT * FROM " + |
| 68 std::string(APIAction::kTableName); |
| 69 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); |
| 70 ASSERT_TRUE(statement.Step()); |
| 71 ASSERT_EQ("punky", statement.ColumnString(0)); |
| 72 ASSERT_EQ("READ", statement.ColumnString(2)); |
| 73 ASSERT_EQ("BOOKMARK", statement.ColumnString(3)); |
| 74 ASSERT_EQ("brewster", statement.ColumnString(4)); |
| 75 } |
| 76 |
| 77 // Check that nothing explodes if the DB isn't initialized. |
| 78 TEST(ActivityDatabaseTest, InitFailure) { |
| 79 base::ScopedTempDir temp_dir; |
| 80 FilePath db_file; |
| 81 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 82 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 83 file_util::Delete(db_file, false); |
| 84 |
| 85 ActivityDatabase* activity_db = new ActivityDatabase(); |
| 86 activity_db->AddRef(); |
| 87 scoped_refptr<APIAction> action = new APIAction( |
| 88 "punky", |
| 89 base::Time::Now(), |
| 90 APIAction::READ, |
| 91 APIAction::BOOKMARK, |
| 92 "brewster", |
| 93 ""); |
| 94 activity_db->RecordAction(action); |
| 95 activity_db->Close(); |
| 96 activity_db->Release(); |
| 97 } |
| 98 |
| 99 } // namespace |
| 100 |
OLD | NEW |