history_data_store_unittest.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file. 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/basictypes.h" 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/bind.h" 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/file_util.h" 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/files/scoped_temp_dir.h" 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/memory/ref_counted.h" 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/message_loop.h" 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/run_loop.h" 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/ui/app_list/search/history_data.h" 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/ui/app_list/search/history_data_store.h" 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/test/test_browser_thread.h" 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace app_list { 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace test { 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace { 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string GetDataContent(const HistoryData::Data& data) { 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::string str = std::string("p:") + data.primary + ";s:"; 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool first = true; 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (HistoryData::SecondaryDeque::const_iterator it = data.secondary.begin(); 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) it != data.secondary.end(); ++it) { 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (first) 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) first = false; 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) else 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) str += ','; 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) str += *it; 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return str; 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class HistoryDataStoreTest : public testing::Test { 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public: 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) HistoryDataStoreTest() 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) : ui_thread_(content::BrowserThread::UI, &message_loop_) {} 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual ~HistoryDataStoreTest() {} 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // testing::Test overrides: 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual void SetUp() OVERRIDE { 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual void TearDown() OVERRIDE { 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Release |store_| while ui loop is still running. 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store_ = NULL; 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void OpenStore(const std::string& file_name) { 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) data_file_ = temp_dir_.path().AppendASCII(file_name); 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store_ = new HistoryDataStore(data_file_); 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Load(); 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void Flush() { 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store_->Flush(HistoryDataStore::OnFlushedCallback()); 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void Load() { 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store_->Load(base::Bind(&HistoryDataStoreTest::OnRead, 6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::Unretained(this))); 6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) run_loop_.reset(new base::RunLoop); 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) run_loop_->Run(); 7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) run_loop_.reset(); 7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void WriteDataFile(const std::string& file_name, 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const std::string& data) { 7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) file_util::WriteFile( 7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) temp_dir_.path().AppendASCII(file_name), data.c_str(), data.size()); 7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) HistoryDataStore* store() { return store_.get(); } 8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const HistoryData::Associations& associations() const { 8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return associations_; 8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private: 8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void OnRead(scoped_ptr<HistoryData::Associations> associations) { 8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) associations_.clear(); 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (associations) 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) associations->swap(associations_); 8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (run_loop_) 9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) run_loop_->Quit(); 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) MessageLoopForUI message_loop_; 9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) content::TestBrowserThread ui_thread_; 9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::ScopedTempDir temp_dir_; 9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath data_file_; 9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) scoped_ptr<base::RunLoop> run_loop_; 9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) scoped_refptr<HistoryDataStore> store_; 10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) HistoryData::Associations associations_; 10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(HistoryDataStoreTest); 10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(HistoryDataStoreTest, NewFile) { 10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) OpenStore("new_data_file.json"); 10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(associations().empty()); 10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(HistoryDataStoreTest, BadFile) { 11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char kDataFile[] = "invalid_data_file"; 11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) WriteDataFile(kDataFile, "invalid json"); 11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) OpenStore(kDataFile); 11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(associations().empty()); 11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(HistoryDataStoreTest, GoodFile) { 12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char kDataFile[] = "good_data_file.json"; 12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char kGoodJson[] = "{" 12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "\"version\": \"1\"," 12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "\"associations\": {" 12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "\"query\": {" 12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "\"p\": \"primary\"," 12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "\"s\": [\"secondary1\",\"secondary2\"]," 12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "\"t\": \"123\"" 12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "}" 12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "}" 13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "}"; 13190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) WriteDataFile(kDataFile, kGoodJson); 13290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) OpenStore(kDataFile); 13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_FALSE(associations().empty()); 13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(1u, associations().size()); 13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 13790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) HistoryData::Associations::const_iterator it = associations().find("query"); 13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(it != associations().end()); 13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ("p:primary;s:secondary1,secondary2", GetDataContent(it->second)); 14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(HistoryDataStoreTest, Change) { 14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char kDataFile[] = "change_test.json"; 14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) OpenStore(kDataFile); 14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(associations().empty()); 14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char kQuery[] = "query"; 14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::Time now = base::Time::Now(); 15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store()->SetPrimary(kQuery, "primary"); 15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store()->SetUpdateTime(kQuery, now); 15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Flush(); 15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Load(); 15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(1u, associations().size()); 15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) HistoryData::Associations::const_iterator it = associations().find(kQuery); 15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(it != associations().end()); 15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ("primary", it->second.primary); 15890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(0u, it->second.secondary.size()); 15990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(now, it->second.update_time); 16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) HistoryData::SecondaryDeque secondary; 16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) secondary.push_back("s1"); 16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) secondary.push_back("s2"); 16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store()->SetSecondary(kQuery, secondary); 16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Flush(); 16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Load(); 16790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(1u, associations().size()); 16890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) it = associations().find(kQuery); 16990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(it != associations().end()); 17090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ("p:primary;s:s1,s2", GetDataContent(it->second)); 17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(now, it->second.update_time); 17290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store()->Delete(kQuery); 17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Flush(); 17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Load(); 17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(associations().empty()); 17790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 17890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 17990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace test 18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace app_list 181