Chromium Code Reviews| Index: chrome/browser/chromeos/base/file_flusher_unittest.cc |
| diff --git a/chrome/browser/chromeos/base/file_flusher_unittest.cc b/chrome/browser/chromeos/base/file_flusher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cff6f84a743a4c5cafdae0246e34849dc033160a |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/base/file_flusher_unittest.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/base/file_flusher.h" |
| + |
| +#include <map> |
| + |
| +#include "base/bind.h" |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/path_service.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace chromeos { |
| + |
| +// Provide basic sanity test of the FileFlusher. Note it only tests that |
| +// flush is called for the expected files but not testing the underlying |
| +// file system for actually persisting the data. |
| +class FileFlusherTest : public testing::Test { |
| + public: |
| + FileFlusherTest() {} |
| + ~FileFlusherTest() override {} |
| + |
| + // testing::Test |
| + void TearDown() override { |
| + content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + } |
| + |
| + scoped_ptr<FileFlusher> CreateFileFlusher() { |
| + scoped_ptr<FileFlusher> flusher(new FileFlusher); |
| + flusher->set_on_flush_callback_for_test( |
| + base::Bind(&FileFlusherTest::OnFlush, base::Unretained(this))); |
| + return flusher; |
| + } |
| + |
| + base::FilePath GetTestFilePath(const std::string& path_string) { |
| + base::FilePath path = base::FilePath::FromUTF8Unsafe(path_string); |
|
achuithb
2016/03/21 22:32:56
nit: const
xiyuan
2016/03/22 18:41:33
Done.
|
| + if (path.IsAbsolute()) |
| + return path; |
| + |
| + base::FilePath test_file_path; |
| + PathService::Get(chrome::DIR_TEST_DATA, &test_file_path); |
| + return test_file_path.Append(path); |
| + } |
| + |
| + void OnFlush(const base::FilePath& path) { ++flush_counts_[path]; } |
| + |
| + int GetFlushCount(const std::string& path_string) { |
|
achuithb
2016/03/21 22:32:56
nit: const function
xiyuan
2016/03/22 18:41:33
Done.
|
| + return flush_counts_[GetTestFilePath(path_string)]; |
| + } |
| + |
| + private: |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + std::map<base::FilePath, int> flush_counts_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FileFlusherTest); |
| +}; |
| + |
| +TEST_F(FileFlusherTest, Flush) { |
| + scoped_ptr<FileFlusher> flusher(CreateFileFlusher()); |
| + base::RunLoop run_loop; |
| + flusher->RequestFlush(GetTestFilePath("profiles/instant"), |
| + std::vector<base::FilePath>(), base::Closure()); |
| + flusher->RequestFlush(GetTestFilePath("profiles/sample"), |
| + std::vector<base::FilePath>(), run_loop.QuitClosure()); |
| + run_loop.Run(); |
| + |
| + EXPECT_EQ(1, GetFlushCount("profiles/instant/Web Data")); |
| + |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Full Text Index")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/History")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Preferences")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Thumbnails")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Visited Links")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Web Data")); |
| +} |
| + |
| +TEST_F(FileFlusherTest, Exclude) { |
| + scoped_ptr<FileFlusher> flusher(CreateFileFlusher()); |
| + |
| + std::vector<base::FilePath> excludes; |
| + // Relative exclude |
| + excludes.push_back(base::FilePath::FromUTF8Unsafe("Full Text Index")); |
| + // Absolute exclude |
| + excludes.push_back(GetTestFilePath("profiles/sample/Web Data")); |
| + // Invalid exclude will be ignore. |
|
achuithb
2016/03/21 22:32:56
s/ignore/ignored
xiyuan
2016/03/22 18:41:33
Done.
|
| + excludes.push_back(base::FilePath::FromUTF8Unsafe("Bad file")); |
| + |
| + base::RunLoop run_loop; |
| + flusher->RequestFlush(GetTestFilePath("profiles/sample"), excludes, |
| + run_loop.QuitClosure()); |
| + run_loop.Run(); |
| + |
| + EXPECT_EQ(0, GetFlushCount("profiles/sample/Full Text Index")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/History")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Preferences")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Thumbnails")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Visited Links")); |
| + EXPECT_EQ(0, GetFlushCount("profiles/sample/Web Data")); |
| + |
| + EXPECT_EQ(0, GetFlushCount("profiles/sample/Bad file")); |
| +} |
| + |
| +TEST_F(FileFlusherTest, DuplicateRequests) { |
| + scoped_ptr<FileFlusher> flusher(CreateFileFlusher()); |
| + base::RunLoop run_loop; |
| + flusher->RequestFlush(GetTestFilePath("profiles/sample"), |
| + std::vector<base::FilePath>(), base::Closure()); |
| + flusher->RequestFlush(GetTestFilePath("profiles/sample"), |
| + std::vector<base::FilePath>(), run_loop.QuitClosure()); |
| + run_loop.Run(); |
| + |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Full Text Index")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/History")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Preferences")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Thumbnails")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Visited Links")); |
| + EXPECT_EQ(1, GetFlushCount("profiles/sample/Web Data")); |
| +} |
| + |
| +} // namespace chromeos |