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

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 comments in #1 and add a simple test 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/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h"
13 #include "base/run_loop.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace chromeos {
20
21 // Provide basic sanity test of the FileFlusher. Note it only tests that
22 // flush is called for the expected files but not testing the underlying
23 // file system for actually persisting the data.
24 class FileFlusherTest : public testing::Test {
25 public:
26 FileFlusherTest() {}
27 ~FileFlusherTest() override {}
28
29 // testing::Test
30 void TearDown() override {
31 content::BrowserThread::GetBlockingPool()->FlushForTesting();
32 base::RunLoop().RunUntilIdle();
33 }
34
35 scoped_ptr<FileFlusher> CreateFileFlusher() {
36 scoped_ptr<FileFlusher> flusher(new FileFlusher);
37 flusher->set_on_flush_callback_for_test(
38 base::Bind(&FileFlusherTest::OnFlush, base::Unretained(this)));
39 return flusher;
40 }
41
42 base::FilePath GetTestFilePath(const std::string& path_string) {
43 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.
44 if (path.IsAbsolute())
45 return path;
46
47 base::FilePath test_file_path;
48 PathService::Get(chrome::DIR_TEST_DATA, &test_file_path);
49 return test_file_path.Append(path);
50 }
51
52 void OnFlush(const base::FilePath& path) { ++flush_counts_[path]; }
53
54 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.
55 return flush_counts_[GetTestFilePath(path_string)];
56 }
57
58 private:
59 content::TestBrowserThreadBundle thread_bundle_;
60 std::map<base::FilePath, int> flush_counts_;
61
62 DISALLOW_COPY_AND_ASSIGN(FileFlusherTest);
63 };
64
65 TEST_F(FileFlusherTest, Flush) {
66 scoped_ptr<FileFlusher> flusher(CreateFileFlusher());
67 base::RunLoop run_loop;
68 flusher->RequestFlush(GetTestFilePath("profiles/instant"),
69 std::vector<base::FilePath>(), base::Closure());
70 flusher->RequestFlush(GetTestFilePath("profiles/sample"),
71 std::vector<base::FilePath>(), run_loop.QuitClosure());
72 run_loop.Run();
73
74 EXPECT_EQ(1, GetFlushCount("profiles/instant/Web Data"));
75
76 EXPECT_EQ(1, GetFlushCount("profiles/sample/Full Text Index"));
77 EXPECT_EQ(1, GetFlushCount("profiles/sample/History"));
78 EXPECT_EQ(1, GetFlushCount("profiles/sample/Preferences"));
79 EXPECT_EQ(1, GetFlushCount("profiles/sample/Thumbnails"));
80 EXPECT_EQ(1, GetFlushCount("profiles/sample/Visited Links"));
81 EXPECT_EQ(1, GetFlushCount("profiles/sample/Web Data"));
82 }
83
84 TEST_F(FileFlusherTest, Exclude) {
85 scoped_ptr<FileFlusher> flusher(CreateFileFlusher());
86
87 std::vector<base::FilePath> excludes;
88 // Relative exclude
89 excludes.push_back(base::FilePath::FromUTF8Unsafe("Full Text Index"));
90 // Absolute exclude
91 excludes.push_back(GetTestFilePath("profiles/sample/Web Data"));
92 // Invalid exclude will be ignore.
achuithb 2016/03/21 22:32:56 s/ignore/ignored
xiyuan 2016/03/22 18:41:33 Done.
93 excludes.push_back(base::FilePath::FromUTF8Unsafe("Bad file"));
94
95 base::RunLoop run_loop;
96 flusher->RequestFlush(GetTestFilePath("profiles/sample"), excludes,
97 run_loop.QuitClosure());
98 run_loop.Run();
99
100 EXPECT_EQ(0, GetFlushCount("profiles/sample/Full Text Index"));
101 EXPECT_EQ(1, GetFlushCount("profiles/sample/History"));
102 EXPECT_EQ(1, GetFlushCount("profiles/sample/Preferences"));
103 EXPECT_EQ(1, GetFlushCount("profiles/sample/Thumbnails"));
104 EXPECT_EQ(1, GetFlushCount("profiles/sample/Visited Links"));
105 EXPECT_EQ(0, GetFlushCount("profiles/sample/Web Data"));
106
107 EXPECT_EQ(0, GetFlushCount("profiles/sample/Bad file"));
108 }
109
110 TEST_F(FileFlusherTest, DuplicateRequests) {
111 scoped_ptr<FileFlusher> flusher(CreateFileFlusher());
112 base::RunLoop run_loop;
113 flusher->RequestFlush(GetTestFilePath("profiles/sample"),
114 std::vector<base::FilePath>(), base::Closure());
115 flusher->RequestFlush(GetTestFilePath("profiles/sample"),
116 std::vector<base::FilePath>(), run_loop.QuitClosure());
117 run_loop.Run();
118
119 EXPECT_EQ(1, GetFlushCount("profiles/sample/Full Text Index"));
120 EXPECT_EQ(1, GetFlushCount("profiles/sample/History"));
121 EXPECT_EQ(1, GetFlushCount("profiles/sample/Preferences"));
122 EXPECT_EQ(1, GetFlushCount("profiles/sample/Thumbnails"));
123 EXPECT_EQ(1, GetFlushCount("profiles/sample/Visited Links"));
124 EXPECT_EQ(1, GetFlushCount("profiles/sample/Web Data"));
125 }
126
127 } // 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