1 // Copyright 2013 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.
5 #include "base/basictypes.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/ui/app_list/search/common/dictionary_data_store.h"
13 #include "chrome/browser/ui/app_list/search/history_data.h"
14 #include "chrome/browser/ui/app_list/search/history_data_store.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
23 std::string GetDataContent(const HistoryData::Data& data) {
24 std::string str = std::string("p:") + data.primary + ";s:";
26 for (HistoryData::SecondaryDeque::const_iterator it = data.secondary.begin();
27 it != data.secondary.end(); ++it) {
41 class HistoryDataStoreTest : public testing::Test {
43 HistoryDataStoreTest()
44 : ui_thread_(content::BrowserThread::UI, &message_loop_) {}
45 virtual ~HistoryDataStoreTest() {}
47 // testing::Test overrides:
48 virtual void SetUp() OVERRIDE {
49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
51 virtual void TearDown() OVERRIDE {
52 // Release |store_| while ui loop is still running.
56 void OpenStore(const std::string& file_name) {
57 data_file_ = temp_dir_.path().AppendASCII(file_name);
58 store_ = new HistoryDataStore(scoped_refptr<DictionaryDataStore>(
59 new DictionaryDataStore(data_file_)));
64 store_->Flush(DictionaryDataStore::OnFlushedCallback());
68 store_->Load(base::Bind(&HistoryDataStoreTest::OnRead,
69 base::Unretained(this)));
70 run_loop_.reset(new base::RunLoop);
75 void WriteDataFile(const std::string& file_name,
76 const std::string& data) {
78 temp_dir_.path().AppendASCII(file_name), data.c_str(), data.size());
81 HistoryDataStore* store() { return store_.get(); }
82 const HistoryData::Associations& associations() const {
87 void OnRead(scoped_ptr<HistoryData::Associations> associations) {
88 associations_.clear();
90 associations->swap(associations_);
96 base::MessageLoopForUI message_loop_;
97 content::TestBrowserThread ui_thread_;
98 base::ScopedTempDir temp_dir_;
99 base::FilePath data_file_;
100 scoped_ptr<base::RunLoop> run_loop_;
102 scoped_refptr<HistoryDataStore> store_;
103 HistoryData::Associations associations_;
105 DISALLOW_COPY_AND_ASSIGN(HistoryDataStoreTest);
108 TEST_F(HistoryDataStoreTest, NewFile) {
109 OpenStore("new_data_file.json");
110 EXPECT_TRUE(associations().empty());
113 TEST_F(HistoryDataStoreTest, BadFile) {
114 const char kDataFile[] = "invalid_data_file";
115 WriteDataFile(kDataFile, "invalid json");
117 OpenStore(kDataFile);
118 EXPECT_TRUE(associations().empty());
121 TEST_F(HistoryDataStoreTest, GoodFile) {
122 const char kDataFile[] = "good_data_file.json";
123 const char kGoodJson[] = "{"
124 "\"version\": \"1\","
125 "\"associations\": {"
127 "\"p\": \"primary\","
128 "\"s\": [\"secondary1\",\"secondary2\"],"
133 WriteDataFile(kDataFile, kGoodJson);
135 OpenStore(kDataFile);
136 EXPECT_FALSE(associations().empty());
137 EXPECT_EQ(1u, associations().size());
139 HistoryData::Associations::const_iterator it = associations().find("query");
140 EXPECT_TRUE(it != associations().end());
141 EXPECT_EQ("p:primary;s:secondary1,secondary2", GetDataContent(it->second));
144 TEST_F(HistoryDataStoreTest, Change) {
145 const char kDataFile[] = "change_test.json";
147 OpenStore(kDataFile);
148 EXPECT_TRUE(associations().empty());
150 const char kQuery[] = "query";
151 const base::Time now = base::Time::Now();
152 store()->SetPrimary(kQuery, "primary");
153 store()->SetUpdateTime(kQuery, now);
156 EXPECT_EQ(1u, associations().size());
157 HistoryData::Associations::const_iterator it = associations().find(kQuery);
158 EXPECT_TRUE(it != associations().end());
159 EXPECT_EQ("primary", it->second.primary);
160 EXPECT_EQ(0u, it->second.secondary.size());
161 EXPECT_EQ(now, it->second.update_time);
163 HistoryData::SecondaryDeque secondary;
164 secondary.push_back("s1");
165 secondary.push_back("s2");
166 store()->SetSecondary(kQuery, secondary);
169 EXPECT_EQ(1u, associations().size());
170 it = associations().find(kQuery);
171 EXPECT_TRUE(it != associations().end());
172 EXPECT_EQ("p:primary;s:s1,s2", GetDataContent(it->second));
173 EXPECT_EQ(now, it->second.update_time);
175 store()->Delete(kQuery);
178 EXPECT_TRUE(associations().empty());
182 } // namespace app_list