Chromium Code Reviews| 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/memory/scoped_temp_dir.h" | |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 10 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/history/text_database.h" | 13 #include "chrome/browser/history/text_database.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 15 | 16 |
| 16 using base::Time; | 17 using base::Time; |
| 17 | 18 |
| 18 namespace history { | 19 namespace history { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 } | 85 } |
| 85 | 86 |
| 86 } // namespace | 87 } // namespace |
| 87 | 88 |
| 88 class TextDatabaseTest : public PlatformTest { | 89 class TextDatabaseTest : public PlatformTest { |
| 89 public: | 90 public: |
| 90 TextDatabaseTest() {} | 91 TextDatabaseTest() {} |
| 91 | 92 |
| 92 protected: | 93 protected: |
| 93 void SetUp() { | 94 void SetUp() { |
| 94 PlatformTest::SetUp(); | 95 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
|
Paweł Hajdan Jr.
2011/04/08 15:58:17
Make sure to call PlatformTest::SetUp(), that call
Mike West
2011/04/11 14:47:04
Yup, I hit `d` too many times. Good catch! :)
| |
| 95 PathService::Get(base::DIR_TEMP, &temp_path_); | |
| 96 } | 96 } |
| 97 | 97 |
| 98 void TearDown() { | 98 void TearDown() { |
| 99 for (size_t i = 0; i < opened_files_.size(); i++) | 99 for (size_t i = 0; i < opened_files_.size(); i++) |
|
Paweł Hajdan Jr.
2011/04/08 15:58:17
Can we get rid of this TearDown then? And maybe op
Mike West
2011/04/11 14:47:04
Done.
| |
| 100 file_util::Delete(opened_files_[i], false); | 100 file_util::Delete(opened_files_[i], false); |
| 101 file_util::Delete(file_name_, false); | 101 file_util::Delete(file_name_, false); |
| 102 PlatformTest::TearDown(); | 102 PlatformTest::TearDown(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 // Create databases with this function, which will ensure that the files are | 105 // 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 | 106 // deleted on shutdown. Only open one database for each file. Returns NULL on |
| 107 // failure. | 107 // failure. |
| 108 // | 108 // |
| 109 // Set |delete_file| to delete any existing file. If we are trying to create | 109 // 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 | 110 // 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. | 111 // weird state to have left a file that would affect us. |
| 112 TextDatabase* CreateDB(TextDatabase::DBIdent id, | 112 TextDatabase* CreateDB(TextDatabase::DBIdent id, |
| 113 bool allow_create, | 113 bool allow_create, |
| 114 bool delete_file) { | 114 bool delete_file) { |
| 115 TextDatabase* db = new TextDatabase(temp_path_, id, allow_create); | 115 TextDatabase* db = new TextDatabase(temp_dir_.path(), id, allow_create); |
| 116 | 116 |
| 117 if (delete_file) | 117 if (delete_file) |
| 118 file_util::Delete(db->file_name(), false); | 118 file_util::Delete(db->file_name(), false); |
| 119 | 119 |
| 120 if (!db->Init()) { | 120 if (!db->Init()) { |
| 121 delete db; | 121 delete db; |
| 122 return NULL; | 122 return NULL; |
| 123 } | 123 } |
| 124 opened_files_.push_back(db->file_name()); | 124 opened_files_.push_back(db->file_name()); |
| 125 return db; | 125 return db; |
| 126 } | 126 } |
| 127 | 127 |
| 128 // Directory containing the databases. | 128 // Directory containing the databases. |
| 129 FilePath temp_path_; | 129 ScopedTempDir temp_dir_; |
| 130 | 130 |
| 131 // Name of the main database file. | 131 // Name of the main database file. |
| 132 FilePath file_name_; | 132 FilePath file_name_; |
| 133 | 133 |
| 134 std::vector<FilePath> opened_files_; | 134 std::vector<FilePath> opened_files_; |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 TEST_F(TextDatabaseTest, AttachDetach) { | 137 TEST_F(TextDatabaseTest, AttachDetach) { |
| 138 // First database with one page. | 138 // First database with one page. |
| 139 const int kIdee1 = 200801; | 139 const int kIdee1 = 200801; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 URLID id3 = db->AddPageData( | 176 URLID id3 = db->AddPageData( |
| 177 Time::FromInternalValue(kTime3), kURL3, kTitle3, kBody3); | 177 Time::FromInternalValue(kTime3), kURL3, kTitle3, kBody3); |
| 178 EXPECT_NE(0, id3); | 178 EXPECT_NE(0, id3); |
| 179 EXPECT_EQ(3, RowCount(db.get())); | 179 EXPECT_EQ(3, RowCount(db.get())); |
| 180 | 180 |
| 181 // Make sure we can delete some of the data. | 181 // Make sure we can delete some of the data. |
| 182 db->DeletePageData(Time::FromInternalValue(kTime1), kURL1); | 182 db->DeletePageData(Time::FromInternalValue(kTime1), kURL1); |
| 183 EXPECT_EQ(2, RowCount(db.get())); | 183 EXPECT_EQ(2, RowCount(db.get())); |
| 184 | 184 |
| 185 // Close and reopen. | 185 // Close and reopen. |
| 186 db.reset(new TextDatabase(temp_path_, kIdee1, false)); | 186 db.reset(new TextDatabase(temp_dir_.path(), kIdee1, false)); |
| 187 EXPECT_TRUE(db->Init()); | 187 EXPECT_TRUE(db->Init()); |
| 188 | 188 |
| 189 // Verify that the deleted ID is gone and try to delete another one. | 189 // Verify that the deleted ID is gone and try to delete another one. |
| 190 EXPECT_EQ(2, RowCount(db.get())); | 190 EXPECT_EQ(2, RowCount(db.get())); |
| 191 db->DeletePageData(Time::FromInternalValue(kTime2), kURL2); | 191 db->DeletePageData(Time::FromInternalValue(kTime2), kURL2); |
| 192 EXPECT_EQ(1, RowCount(db.get())); | 192 EXPECT_EQ(1, RowCount(db.get())); |
| 193 } | 193 } |
| 194 | 194 |
| 195 TEST_F(TextDatabaseTest, Query) { | 195 TEST_F(TextDatabaseTest, Query) { |
| 196 // Make a database with some pages. | 196 // Make a database with some pages. |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 | 321 |
| 322 // There should be one result, the most recent one. | 322 // There should be one result, the most recent one. |
| 323 EXPECT_EQ(1U, results.size()); | 323 EXPECT_EQ(1U, results.size()); |
| 324 EXPECT_TRUE(ResultsHaveURL(results, kURL2)); | 324 EXPECT_TRUE(ResultsHaveURL(results, kURL2)); |
| 325 | 325 |
| 326 // The max time considered should be the date of the returned item. | 326 // The max time considered should be the date of the returned item. |
| 327 EXPECT_EQ(kTime2, first_time_searched.ToInternalValue()); | 327 EXPECT_EQ(kTime2, first_time_searched.ToInternalValue()); |
| 328 } | 328 } |
| 329 | 329 |
| 330 } // namespace history | 330 } // namespace history |
| OLD | NEW |