Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: chrome/browser/chromeos/base/file_flusher_unittest.cc

Issue 1815853002: cros: Flush profile files at critical moments (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix nits and create test files to fix trybots Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "chrome/browser/chromeos/base/file_flusher.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace chromeos {
21
22 namespace {
23
24 void WriteStringToFile(const base::FilePath path, const std::string& data) {
25 ASSERT_TRUE(base::CreateDirectory(path.DirName()))
26 << "Failed to create directory " << path.DirName().value();
27
28 int size = data.size();
29 ASSERT_TRUE(base::WriteFile(path, data.c_str(), size) == size)
30 << "Failed to write " << path.value();
31 }
32
33 } // namespace
34
35 // Provide basic sanity test of the FileFlusher. Note it only tests that
36 // flush is called for the expected files but not testing the underlying
37 // file system for actually persisting the data.
38 class FileFlusherTest : public testing::Test {
39 public:
40 FileFlusherTest() {}
41 ~FileFlusherTest() override {}
42
43 // testing::Test
44 void SetUp() override {
45 // Create test files under a temp dir.
46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
47
48 const size_t kNumDirs = 2;
49 const size_t kNumFiles = 3;
50 for (size_t i = 1; i <= kNumDirs; ++i) {
51 for (size_t j = 1; j <= kNumFiles; ++j) {
52 const std::string path = base::StringPrintf("dir%zu/file%zu", i, j);
53 const std::string content = base::StringPrintf("content %zu %zu", i, j);
54 WriteStringToFile(temp_dir_.path().AppendASCII(path), content);
55 }
56 }
57 }
58
59 void TearDown() override {
60 content::BrowserThread::GetBlockingPool()->FlushForTesting();
61 base::RunLoop().RunUntilIdle();
62 }
63
64 scoped_ptr<FileFlusher> CreateFileFlusher() {
65 scoped_ptr<FileFlusher> flusher(new FileFlusher);
66 flusher->set_on_flush_callback_for_test(
67 base::Bind(&FileFlusherTest::OnFlush, base::Unretained(this)));
68 return flusher;
69 }
70
71 base::FilePath GetTestFilePath(const std::string& path_string) const {
72 const base::FilePath path = base::FilePath::FromUTF8Unsafe(path_string);
73 if (path.IsAbsolute())
74 return path;
75
76 return temp_dir_.path().Append(path);
77 }
78
79 void OnFlush(const base::FilePath& path) { ++flush_counts_[path]; }
80
81 int GetFlushCount(const std::string& path_string) const {
82 const base::FilePath path(GetTestFilePath(path_string));
83 const auto& it = flush_counts_.find(path);
84 return it == flush_counts_.end() ? 0 : it->second;
85 }
86
87 private:
88 content::TestBrowserThreadBundle thread_bundle_;
89 base::ScopedTempDir temp_dir_;
90 std::map<base::FilePath, int> flush_counts_;
91
92 DISALLOW_COPY_AND_ASSIGN(FileFlusherTest);
93 };
94
95 TEST_F(FileFlusherTest, Flush) {
96 scoped_ptr<FileFlusher> flusher(CreateFileFlusher());
97 base::RunLoop run_loop;
98 flusher->RequestFlush(GetTestFilePath("dir1"), std::vector<base::FilePath>(),
99 base::Closure());
100 flusher->RequestFlush(GetTestFilePath("dir2"), std::vector<base::FilePath>(),
101 run_loop.QuitClosure());
102 run_loop.Run();
103
104 EXPECT_EQ(1, GetFlushCount("dir1/file1"));
105 EXPECT_EQ(1, GetFlushCount("dir1/file2"));
106 EXPECT_EQ(1, GetFlushCount("dir1/file3"));
107
108 EXPECT_EQ(1, GetFlushCount("dir2/file1"));
109 EXPECT_EQ(1, GetFlushCount("dir2/file2"));
110 EXPECT_EQ(1, GetFlushCount("dir2/file3"));
111 }
112
113 TEST_F(FileFlusherTest, Exclude) {
114 scoped_ptr<FileFlusher> flusher(CreateFileFlusher());
115
116 std::vector<base::FilePath> excludes;
117 // Relative exclude
118 excludes.push_back(base::FilePath::FromUTF8Unsafe("file1"));
119 // Absolute exclude
120 excludes.push_back(GetTestFilePath("dir1/file2"));
121 // Invalid exclude will be ignored.
122 excludes.push_back(base::FilePath::FromUTF8Unsafe("Bad file"));
123
124 base::RunLoop run_loop;
125 flusher->RequestFlush(GetTestFilePath("dir1"), excludes,
126 run_loop.QuitClosure());
127 run_loop.Run();
128
129 EXPECT_EQ(0, GetFlushCount("dir1/file1"));
130 EXPECT_EQ(0, GetFlushCount("dir1/file2"));
131 EXPECT_EQ(1, GetFlushCount("dir1/file3"));
132
133 EXPECT_EQ(0, GetFlushCount("dir1/Bad file"));
134 }
135
136 TEST_F(FileFlusherTest, DuplicateRequests) {
137 scoped_ptr<FileFlusher> flusher(CreateFileFlusher());
138 base::RunLoop run_loop;
139 flusher->RequestFlush(GetTestFilePath("dir1"), std::vector<base::FilePath>(),
140 base::Closure());
141 flusher->RequestFlush(GetTestFilePath("dir1"), std::vector<base::FilePath>(),
142 run_loop.QuitClosure());
143 run_loop.Run();
144
145 EXPECT_EQ(1, GetFlushCount("dir1/file1"));
146 EXPECT_EQ(1, GetFlushCount("dir1/file2"));
147 EXPECT_EQ(1, GetFlushCount("dir1/file3"));
148 }
149
150 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/base/file_flusher.cc ('k') | chrome/browser/chromeos/chrome_browser_main_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698