OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <stdio.h> |
| 6 |
| 7 #include <fstream> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "app/sql/statement.h" |
| 12 #include "base/file_util.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/scoped_ptr.h" |
| 15 #include "base/time.h" |
| 16 #include "base/utf_string_conversions.h" |
5 #include "chrome/browser/history/in_memory_url_index.h" | 17 #include "chrome/browser/history/in_memory_url_index.h" |
| 18 #include "chrome/browser/history/url_database.h" |
| 19 #include "chrome/common/chrome_paths.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
6 | 21 |
7 #include "base/scoped_ptr.h" | 22 // The test version of the history url database table ('url') is contained in |
8 #include "testing/gtest/include/gtest/gtest.h" | 23 // a database file created from a text file('url_history_provider_test.db.txt'). |
| 24 // The only difference between this table and a live 'urls' table from a |
| 25 // profile is that the last_visit_time column in the test table contains a |
| 26 // number specifying the number of days relative to 'today' to which the |
| 27 // absolute time should be set during the test setup stage. |
| 28 // |
| 29 // The format of the test database text file is of a SQLite .dump file. |
| 30 // Note that only lines whose first character is an upper-case letter are |
| 31 // processed when creating the test database. |
| 32 |
| 33 using base::Time; |
| 34 using base::TimeDelta; |
9 | 35 |
10 namespace history { | 36 namespace history { |
11 | 37 |
12 class InMemoryURLIndexTest : public testing::Test { | 38 class InMemoryURLIndexTest : public testing::Test, |
| 39 public URLDatabase { |
| 40 public: |
| 41 InMemoryURLIndexTest() {} |
| 42 |
13 protected: | 43 protected: |
| 44 // Test setup. |
| 45 void SetUp() { |
| 46 // Create and populate a working copy of the URL history database. |
| 47 FilePath history_proto_path; |
| 48 PathService::Get(chrome::DIR_TEST_DATA, &history_proto_path); |
| 49 history_proto_path = history_proto_path.Append( |
| 50 FILE_PATH_LITERAL("History")); |
| 51 history_proto_path = history_proto_path.Append( |
| 52 FILE_PATH_LITERAL("url_history_provider_test.db.txt")); |
| 53 EXPECT_TRUE(file_util::PathExists(history_proto_path)); |
| 54 EXPECT_TRUE(db_.OpenInMemory()); |
| 55 |
| 56 std::ifstream proto_file(history_proto_path.value().c_str()); |
| 57 static const size_t kCommandBufferMaxSize = 2048; |
| 58 char sql_cmd_line[kCommandBufferMaxSize]; |
| 59 while (!proto_file.eof()) { |
| 60 proto_file.getline(sql_cmd_line, kCommandBufferMaxSize); |
| 61 if (!proto_file.eof()) { |
| 62 // We only process lines which begin with a upper-case letter. |
| 63 // TODO(mrossetti): Can iswupper() be used here? |
| 64 if (sql_cmd_line[0] >= 'A' && sql_cmd_line[0] <= 'Z') { |
| 65 std::string sql_cmd(sql_cmd_line); |
| 66 sql::Statement sql_stmt(db_.GetUniqueStatement(sql_cmd_line)); |
| 67 EXPECT_TRUE(sql_stmt.Run()); |
| 68 } |
| 69 } |
| 70 } |
| 71 proto_file.close(); |
| 72 |
| 73 // Update the last_visit_time table column |
| 74 // such that it represents a time relative to 'now'. |
| 75 sql::Statement statement(db_.GetUniqueStatement( |
| 76 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls;")); |
| 77 EXPECT_TRUE(statement); |
| 78 Time time_right_now = Time::NowFromSystemTime(); |
| 79 TimeDelta day_delta = TimeDelta::FromDays(1); |
| 80 while (statement.Step()) { |
| 81 URLRow row; |
| 82 FillURLRow(statement, &row); |
| 83 Time last_visit = time_right_now; |
| 84 for (int64 i = row.last_visit().ToInternalValue(); i > 0; --i) |
| 85 last_visit -= day_delta; |
| 86 row.set_last_visit(last_visit); |
| 87 UpdateURLRow(row.id(), row); |
| 88 } |
| 89 } |
| 90 |
| 91 void TearDown() { |
| 92 db_.Close(); |
| 93 } |
| 94 |
| 95 virtual sql::Connection& GetDB() { return db_; } |
| 96 |
14 scoped_ptr<InMemoryURLIndex> url_index_; | 97 scoped_ptr<InMemoryURLIndex> url_index_; |
| 98 sql::Connection db_; |
15 }; | 99 }; |
16 | 100 |
17 TEST_F(InMemoryURLIndexTest, Construction) { | 101 TEST_F(InMemoryURLIndexTest, Construction) { |
18 url_index_.reset(new InMemoryURLIndex); | 102 url_index_.reset(new InMemoryURLIndex); |
19 EXPECT_TRUE(url_index_.get()); | 103 EXPECT_TRUE(url_index_.get()); |
20 } | 104 } |
21 | 105 |
| 106 TEST_F(InMemoryURLIndexTest, Initialization) { |
| 107 url_index_.reset(new InMemoryURLIndex); |
| 108 url_index_->Init(this, UTF8ToUTF16("en,ja,hi,zh")); |
| 109 // There should have been 25 of the 29 urls accepted during filtering. |
| 110 EXPECT_EQ(25U, url_index_->history_item_count_); |
| 111 // The resulting indexes should account for: |
| 112 // 37 characters |
| 113 // 88 words |
| 114 EXPECT_EQ(37U, url_index_->char_word_map_.size()); |
| 115 EXPECT_EQ(88U, url_index_->word_map_.size()); |
| 116 } |
| 117 |
22 } // namespace history | 118 } // namespace history |
OLD | NEW |