OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/path_service.h" | 9 #include "base/memory/scoped_temp_dir.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/history/text_database.h" | 12 #include "chrome/browser/history/text_database.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
15 | 15 |
16 using base::Time; | 16 using base::Time; |
17 | 17 |
18 namespace history { | 18 namespace history { |
19 | 19 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 | 85 |
86 } // namespace | 86 } // namespace |
87 | 87 |
88 class TextDatabaseTest : public PlatformTest { | 88 class TextDatabaseTest : public PlatformTest { |
89 public: | 89 public: |
90 TextDatabaseTest() {} | 90 TextDatabaseTest() {} |
91 | 91 |
92 protected: | 92 protected: |
93 void SetUp() { | 93 void SetUp() { |
94 PlatformTest::SetUp(); | 94 PlatformTest::SetUp(); |
95 PathService::Get(base::DIR_TEMP, &temp_path_); | 95 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
96 } | |
97 | |
98 void TearDown() { | |
99 for (size_t i = 0; i < opened_files_.size(); i++) | |
100 file_util::Delete(opened_files_[i], false); | |
101 file_util::Delete(file_name_, false); | |
102 PlatformTest::TearDown(); | |
103 } | 96 } |
104 | 97 |
105 // Create databases with this function, which will ensure that the files are | 98 // Create databases with this function, which will ensure that the files are |
106 // deleted on shutdown. Only open one database for each file. Returns NULL on | 99 // deleted on shutdown. Only open one database for each file. Returns NULL on |
107 // failure. | 100 // failure. |
108 // | 101 // |
109 // Set |delete_file| to delete any existing file. If we are trying to create | 102 // Set |delete_file| to delete any existing file. If we are trying to create |
110 // the file for the first time, we don't want a previous test left in a | 103 // the file for the first time, we don't want a previous test left in a |
111 // weird state to have left a file that would affect us. | 104 // weird state to have left a file that would affect us. |
112 TextDatabase* CreateDB(TextDatabase::DBIdent id, | 105 TextDatabase* CreateDB(TextDatabase::DBIdent id, |
113 bool allow_create, | 106 bool allow_create, |
114 bool delete_file) { | 107 bool delete_file) { |
115 TextDatabase* db = new TextDatabase(temp_path_, id, allow_create); | 108 TextDatabase* db = new TextDatabase(temp_dir_.path(), id, allow_create); |
116 | 109 |
117 if (delete_file) | 110 if (delete_file) |
118 file_util::Delete(db->file_name(), false); | 111 file_util::Delete(db->file_name(), false); |
119 | 112 |
120 if (!db->Init()) { | 113 if (!db->Init()) { |
121 delete db; | 114 delete db; |
122 return NULL; | 115 return NULL; |
123 } | 116 } |
124 opened_files_.push_back(db->file_name()); | |
125 return db; | 117 return db; |
126 } | 118 } |
127 | 119 |
128 // Directory containing the databases. | 120 // Directory containing the databases. |
129 FilePath temp_path_; | 121 ScopedTempDir temp_dir_; |
130 | 122 |
131 // Name of the main database file. | 123 // Name of the main database file. |
132 FilePath file_name_; | 124 FilePath file_name_; |
133 | |
134 std::vector<FilePath> opened_files_; | |
135 }; | 125 }; |
136 | 126 |
137 TEST_F(TextDatabaseTest, AttachDetach) { | 127 TEST_F(TextDatabaseTest, AttachDetach) { |
138 // First database with one page. | 128 // First database with one page. |
139 const int kIdee1 = 200801; | 129 const int kIdee1 = 200801; |
140 scoped_ptr<TextDatabase> db1(CreateDB(kIdee1, true, true)); | 130 scoped_ptr<TextDatabase> db1(CreateDB(kIdee1, true, true)); |
141 ASSERT_TRUE(!!db1.get()); | 131 ASSERT_TRUE(!!db1.get()); |
142 EXPECT_TRUE(db1->AddPageData( | 132 EXPECT_TRUE(db1->AddPageData( |
143 Time::FromInternalValue(kTime1), kURL1, kTitle1, kBody1)); | 133 Time::FromInternalValue(kTime1), kURL1, kTitle1, kBody1)); |
144 | 134 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 URLID id3 = db->AddPageData( | 166 URLID id3 = db->AddPageData( |
177 Time::FromInternalValue(kTime3), kURL3, kTitle3, kBody3); | 167 Time::FromInternalValue(kTime3), kURL3, kTitle3, kBody3); |
178 EXPECT_NE(0, id3); | 168 EXPECT_NE(0, id3); |
179 EXPECT_EQ(3, RowCount(db.get())); | 169 EXPECT_EQ(3, RowCount(db.get())); |
180 | 170 |
181 // Make sure we can delete some of the data. | 171 // Make sure we can delete some of the data. |
182 db->DeletePageData(Time::FromInternalValue(kTime1), kURL1); | 172 db->DeletePageData(Time::FromInternalValue(kTime1), kURL1); |
183 EXPECT_EQ(2, RowCount(db.get())); | 173 EXPECT_EQ(2, RowCount(db.get())); |
184 | 174 |
185 // Close and reopen. | 175 // Close and reopen. |
186 db.reset(new TextDatabase(temp_path_, kIdee1, false)); | 176 db.reset(new TextDatabase(temp_dir_.path(), kIdee1, false)); |
187 EXPECT_TRUE(db->Init()); | 177 EXPECT_TRUE(db->Init()); |
188 | 178 |
189 // Verify that the deleted ID is gone and try to delete another one. | 179 // Verify that the deleted ID is gone and try to delete another one. |
190 EXPECT_EQ(2, RowCount(db.get())); | 180 EXPECT_EQ(2, RowCount(db.get())); |
191 db->DeletePageData(Time::FromInternalValue(kTime2), kURL2); | 181 db->DeletePageData(Time::FromInternalValue(kTime2), kURL2); |
192 EXPECT_EQ(1, RowCount(db.get())); | 182 EXPECT_EQ(1, RowCount(db.get())); |
193 } | 183 } |
194 | 184 |
195 TEST_F(TextDatabaseTest, Query) { | 185 TEST_F(TextDatabaseTest, Query) { |
196 // Make a database with some pages. | 186 // Make a database with some pages. |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 311 |
322 // There should be one result, the most recent one. | 312 // There should be one result, the most recent one. |
323 EXPECT_EQ(1U, results.size()); | 313 EXPECT_EQ(1U, results.size()); |
324 EXPECT_TRUE(ResultsHaveURL(results, kURL2)); | 314 EXPECT_TRUE(ResultsHaveURL(results, kURL2)); |
325 | 315 |
326 // The max time considered should be the date of the returned item. | 316 // The max time considered should be the date of the returned item. |
327 EXPECT_EQ(kTime2, first_time_searched.ToInternalValue()); | 317 EXPECT_EQ(kTime2, first_time_searched.ToInternalValue()); |
328 } | 318 } |
329 | 319 |
330 } // namespace history | 320 } // namespace history |
OLD | NEW |