Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / search / history_data_store_unittest.cc
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.
4
5 #include "base/basictypes.h"
6 #include "base/bind.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"
17
18 namespace app_list {
19 namespace test {
20
21 namespace {
22
23 std::string GetDataContent(const HistoryData::Data& data) {
24   std::string str = std::string("p:") + data.primary + ";s:";
25   bool first = true;
26   for (HistoryData::SecondaryDeque::const_iterator it = data.secondary.begin();
27        it != data.secondary.end(); ++it) {
28     if (first)
29       first = false;
30     else
31       str += ',';
32
33     str += *it;
34   }
35
36   return str;
37 }
38
39 }  // namespace
40
41 class HistoryDataStoreTest : public testing::Test {
42  public:
43   HistoryDataStoreTest()
44       : ui_thread_(content::BrowserThread::UI, &message_loop_) {}
45   virtual ~HistoryDataStoreTest() {}
46
47   // testing::Test overrides:
48   virtual void SetUp() OVERRIDE {
49     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
50   }
51   virtual void TearDown() OVERRIDE {
52     // Release |store_| while ui loop is still running.
53     store_ = NULL;
54   }
55
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_)));
60     Load();
61   }
62
63   void Flush() {
64     store_->Flush(DictionaryDataStore::OnFlushedCallback());
65   }
66
67   void Load() {
68     store_->Load(base::Bind(&HistoryDataStoreTest::OnRead,
69                             base::Unretained(this)));
70     run_loop_.reset(new base::RunLoop);
71     run_loop_->Run();
72     run_loop_.reset();
73   }
74
75   void WriteDataFile(const std::string& file_name,
76                      const std::string& data) {
77     base::WriteFile(
78         temp_dir_.path().AppendASCII(file_name), data.c_str(), data.size());
79   }
80
81   HistoryDataStore* store() { return store_.get(); }
82   const HistoryData::Associations& associations() const {
83     return associations_;
84   }
85
86  private:
87   void OnRead(scoped_ptr<HistoryData::Associations> associations) {
88     associations_.clear();
89     if (associations)
90       associations->swap(associations_);
91
92     if (run_loop_)
93       run_loop_->Quit();
94   }
95
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_;
101
102   scoped_refptr<HistoryDataStore> store_;
103   HistoryData::Associations associations_;
104
105   DISALLOW_COPY_AND_ASSIGN(HistoryDataStoreTest);
106 };
107
108 TEST_F(HistoryDataStoreTest, NewFile) {
109   OpenStore("new_data_file.json");
110   EXPECT_TRUE(associations().empty());
111 }
112
113 TEST_F(HistoryDataStoreTest, BadFile) {
114   const char kDataFile[] = "invalid_data_file";
115   WriteDataFile(kDataFile, "invalid json");
116
117   OpenStore(kDataFile);
118   EXPECT_TRUE(associations().empty());
119 }
120
121 TEST_F(HistoryDataStoreTest, GoodFile) {
122   const char kDataFile[] = "good_data_file.json";
123   const char kGoodJson[] = "{"
124       "\"version\": \"1\","
125       "\"associations\": {"
126           "\"query\": {"
127              "\"p\": \"primary\","
128              "\"s\": [\"secondary1\",\"secondary2\"],"
129              "\"t\": \"123\""
130           "}"
131         "}"
132       "}";
133   WriteDataFile(kDataFile, kGoodJson);
134
135   OpenStore(kDataFile);
136   EXPECT_FALSE(associations().empty());
137   EXPECT_EQ(1u, associations().size());
138
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));
142 }
143
144 TEST_F(HistoryDataStoreTest, Change) {
145   const char kDataFile[] = "change_test.json";
146
147   OpenStore(kDataFile);
148   EXPECT_TRUE(associations().empty());
149
150   const char kQuery[] = "query";
151   const base::Time now = base::Time::Now();
152   store()->SetPrimary(kQuery, "primary");
153   store()->SetUpdateTime(kQuery, now);
154   Flush();
155   Load();
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);
162
163   HistoryData::SecondaryDeque secondary;
164   secondary.push_back("s1");
165   secondary.push_back("s2");
166   store()->SetSecondary(kQuery, secondary);
167   Flush();
168   Load();
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);
174
175   store()->Delete(kQuery);
176   Flush();
177   Load();
178   EXPECT_TRUE(associations().empty());
179 }
180
181 }  // namespace test
182 }  // namespace app_list