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 #include "base/file_path.h" | 6 #include "base/file_path.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "chrome/browser/extensions/activity_database.h" | 9 #include "chrome/browser/extensions/activity_database.h" |
10 #include "chrome/browser/extensions/api_actions.h" | 10 #include "chrome/browser/extensions/api_actions.h" |
11 #include "chrome/browser/extensions/blocked_actions.h" | 11 #include "chrome/browser/extensions/blocked_actions.h" |
12 #include "chrome/browser/extensions/url_actions.h" | 12 #include "chrome/browser/extensions/dom_actions.h" |
13 #include "sql/statement.h" | 13 #include "sql/statement.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace extensions { | 16 namespace extensions { |
17 | 17 |
18 // Check that the database is initialized properly. | 18 // Check that the database is initialized properly. |
19 TEST(ActivityDatabaseTest, Init) { | 19 TEST(ActivityDatabaseTest, Init) { |
20 base::ScopedTempDir temp_dir; | 20 base::ScopedTempDir temp_dir; |
21 base::FilePath db_file; | 21 base::FilePath db_file; |
22 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 22 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
23 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); | 23 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); |
24 file_util::Delete(db_file, false); | 24 file_util::Delete(db_file, false); |
25 | 25 |
26 ActivityDatabase* activity_db = new ActivityDatabase(); | 26 ActivityDatabase* activity_db = new ActivityDatabase(); |
27 activity_db->AddRef(); | 27 activity_db->AddRef(); |
28 activity_db->Init(db_file); | 28 activity_db->Init(db_file); |
29 ASSERT_TRUE(activity_db->initialized()); | 29 ASSERT_TRUE(activity_db->initialized()); |
30 activity_db->Release(); | 30 activity_db->Release(); |
31 | 31 |
32 sql::Connection db; | 32 sql::Connection db; |
33 ASSERT_TRUE(db.Open(db_file)); | 33 ASSERT_TRUE(db.Open(db_file)); |
34 ASSERT_TRUE(db.DoesTableExist(UrlAction::kTableName)); | 34 ASSERT_TRUE(db.DoesTableExist(DOMAction::kTableName)); |
35 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); | 35 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); |
36 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); | 36 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); |
37 db.Close(); | 37 db.Close(); |
38 } | 38 } |
39 | 39 |
40 // Check that actions are recorded in the db. | 40 // Check that API actions are recorded in the db. |
41 TEST(ActivityDatabaseTest, RecordAction) { | 41 TEST(ActivityDatabaseTest, RecordAPIAction) { |
42 base::ScopedTempDir temp_dir; | 42 base::ScopedTempDir temp_dir; |
43 base::FilePath db_file; | 43 base::FilePath db_file; |
44 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 44 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
45 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 45 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
46 file_util::Delete(db_file, false); | 46 file_util::Delete(db_file, false); |
47 | 47 |
48 ActivityDatabase* activity_db = new ActivityDatabase(); | 48 ActivityDatabase* activity_db = new ActivityDatabase(); |
49 activity_db->AddRef(); | 49 activity_db->AddRef(); |
50 activity_db->Init(db_file); | 50 activity_db->Init(db_file); |
51 ASSERT_TRUE(activity_db->initialized()); | 51 ASSERT_TRUE(activity_db->initialized()); |
52 scoped_refptr<APIAction> action = new APIAction( | 52 scoped_refptr<APIAction> action = new APIAction( |
53 "punky", | 53 "punky", |
54 base::Time::Now(), | 54 base::Time::Now(), |
55 APIAction::CALL, | 55 APIAction::CALL, |
56 APIAction::READ, | 56 APIAction::READ, |
57 APIAction::BOOKMARK, | 57 APIAction::BOOKMARK, |
58 "brewster", | 58 "brewster", |
59 ""); | 59 "woof", |
| 60 "extra"); |
60 activity_db->RecordAction(action); | 61 activity_db->RecordAction(action); |
61 activity_db->Close(); | 62 activity_db->Close(); |
62 activity_db->Release(); | 63 activity_db->Release(); |
63 | 64 |
64 sql::Connection db; | 65 sql::Connection db; |
65 ASSERT_TRUE(db.Open(db_file)); | 66 ASSERT_TRUE(db.Open(db_file)); |
66 | 67 |
67 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); | 68 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); |
68 std::string sql_str = "SELECT * FROM " + | 69 std::string sql_str = "SELECT * FROM " + |
69 std::string(APIAction::kTableName); | 70 std::string(APIAction::kTableName); |
70 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); | 71 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); |
71 ASSERT_TRUE(statement.Step()); | 72 ASSERT_TRUE(statement.Step()); |
72 ASSERT_EQ("punky", statement.ColumnString(0)); | 73 ASSERT_EQ("punky", statement.ColumnString(0)); |
73 ASSERT_EQ("CALL", statement.ColumnString(2)); | 74 ASSERT_EQ("CALL", statement.ColumnString(2)); |
74 ASSERT_EQ("READ", statement.ColumnString(3)); | 75 ASSERT_EQ("READ", statement.ColumnString(3)); |
75 ASSERT_EQ("BOOKMARK", statement.ColumnString(4)); | 76 ASSERT_EQ("BOOKMARK", statement.ColumnString(4)); |
76 ASSERT_EQ("brewster", statement.ColumnString(5)); | 77 ASSERT_EQ("brewster", statement.ColumnString(5)); |
| 78 ASSERT_EQ("woof", statement.ColumnString(6)); |
| 79 } |
| 80 |
| 81 // Check that blocked actions are recorded in the db. |
| 82 TEST(ActivityDatabaseTest, RecordBlockedAction) { |
| 83 base::ScopedTempDir temp_dir; |
| 84 FilePath db_file; |
| 85 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 86 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 87 file_util::Delete(db_file, false); |
| 88 |
| 89 ActivityDatabase* activity_db = new ActivityDatabase(); |
| 90 activity_db->AddRef(); |
| 91 activity_db->Init(db_file); |
| 92 ASSERT_TRUE(activity_db->initialized()); |
| 93 scoped_refptr<BlockedAction> action = new BlockedAction( |
| 94 "punky", |
| 95 base::Time::Now(), |
| 96 "do.evilThings", |
| 97 "1, 2", |
| 98 "because i said so", |
| 99 "extra"); |
| 100 activity_db->RecordAction(action); |
| 101 activity_db->Close(); |
| 102 activity_db->Release(); |
| 103 |
| 104 sql::Connection db; |
| 105 ASSERT_TRUE(db.Open(db_file)); |
| 106 |
| 107 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); |
| 108 std::string sql_str = "SELECT * FROM " + |
| 109 std::string(BlockedAction::kTableName); |
| 110 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); |
| 111 ASSERT_TRUE(statement.Step()); |
| 112 ASSERT_EQ("punky", statement.ColumnString(0)); |
| 113 ASSERT_EQ("do.evilThings", statement.ColumnString(2)); |
| 114 ASSERT_EQ("1, 2", statement.ColumnString(3)); |
| 115 ASSERT_EQ("because i said so", statement.ColumnString(4)); |
| 116 ASSERT_EQ("extra", statement.ColumnString(5)); |
77 } | 117 } |
78 | 118 |
79 // Check that nothing explodes if the DB isn't initialized. | 119 // Check that nothing explodes if the DB isn't initialized. |
80 TEST(ActivityDatabaseTest, InitFailure) { | 120 TEST(ActivityDatabaseTest, InitFailure) { |
81 base::ScopedTempDir temp_dir; | 121 base::ScopedTempDir temp_dir; |
82 base::FilePath db_file; | 122 base::FilePath db_file; |
83 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
84 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 124 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
85 file_util::Delete(db_file, false); | 125 file_util::Delete(db_file, false); |
86 | 126 |
87 ActivityDatabase* activity_db = new ActivityDatabase(); | 127 ActivityDatabase* activity_db = new ActivityDatabase(); |
88 activity_db->AddRef(); | 128 activity_db->AddRef(); |
89 scoped_refptr<APIAction> action = new APIAction( | 129 scoped_refptr<APIAction> action = new APIAction( |
90 "punky", | 130 "punky", |
91 base::Time::Now(), | 131 base::Time::Now(), |
92 APIAction::CALL, | 132 APIAction::CALL, |
93 APIAction::READ, | 133 APIAction::READ, |
94 APIAction::BOOKMARK, | 134 APIAction::BOOKMARK, |
95 "brewster", | 135 "brewster", |
96 ""); | 136 "woooof", |
| 137 "extra"); |
97 activity_db->RecordAction(action); | 138 activity_db->RecordAction(action); |
98 activity_db->Close(); | 139 activity_db->Close(); |
99 activity_db->Release(); | 140 activity_db->Release(); |
100 } | 141 } |
101 | 142 |
102 } // namespace | 143 } // namespace |
103 | 144 |
OLD | NEW |