Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / drive / file_write_watcher_unittest.cc
blobabd9d511281e46341c0cb13afa1a379ec2f745a0
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.
5 #include "components/drive/file_write_watcher.h"
7 #include <set>
9 #include "base/bind.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/run_loop.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace drive {
18 namespace internal {
20 namespace {
22 class TestObserver {
23 public:
24 // After all the resource_ids in |expected_upload| are notified for the
25 // need of uploading, runs |quit_closure|. Also checks that each id is
26 // notified only once.
27 TestObserver(const std::set<std::string>& expected_upload,
28 const base::Closure& quit_closure)
29 : expected_upload_(expected_upload),
30 quit_closure_(quit_closure) {
33 void OnWrite(const std::string& id) {
34 EXPECT_EQ(1U, expected_upload_.count(id)) << id;
35 expected_upload_.erase(id);
36 if (expected_upload_.empty())
37 quit_closure_.Run();
40 private:
41 std::set<std::string> expected_upload_;
42 base::Closure quit_closure_;
45 // Writes something on the file at |path|.
46 void WriteSomethingAfterStartWatch(const base::FilePath& path,
47 bool watch_success) {
48 EXPECT_TRUE(watch_success) << path.value();
50 const char kDummy[] = "hello";
51 ASSERT_TRUE(base::WriteFile(path, kDummy, arraysize(kDummy)));
54 class FileWriteWatcherTest : public testing::Test {
55 protected:
56 // The test requires UI thread (FileWriteWatcher DCHECKs that its public
57 // interface is running on UI thread) and FILE thread (Linux version of
58 // base::FilePathWatcher needs to live on an IOAllowed thread with TYPE_IO,
59 // which is FILE thread in the production environment).
61 // By using the IO_MAINLOOP test thread bundle, the main thread is used
62 // both as UI and FILE thread, with TYPE_IO message loop.
63 FileWriteWatcherTest()
64 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
67 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
69 base::FilePath GetTempPath(const std::string& name) {
70 return temp_dir_.path().Append(name);
73 private:
74 content::TestBrowserThreadBundle thread_bundle_;
75 base::ScopedTempDir temp_dir_;
78 } // namespace
80 TEST_F(FileWriteWatcherTest, WatchThreeFiles) {
81 std::set<std::string> expected;
82 expected.insert("1");
83 expected.insert("2");
84 expected.insert("3");
86 base::RunLoop loop;
87 TestObserver observer(expected, loop.QuitClosure());
89 // Set up the watcher.
90 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner =
91 content::BrowserThread::GetMessageLoopProxyForThread(
92 content::BrowserThread::FILE);
93 FileWriteWatcher watcher(file_task_runner.get());
94 watcher.DisableDelayForTesting();
96 // Start watching and running.
97 base::FilePath path1 = GetTempPath("foo.txt");
98 base::FilePath path2 = GetTempPath("bar.png");
99 base::FilePath path3 = GetTempPath("buz.doc");
100 base::FilePath path4 = GetTempPath("mya.mp3");
101 watcher.StartWatch(
102 path1,
103 base::Bind(&WriteSomethingAfterStartWatch, path1),
104 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "1"));
105 watcher.StartWatch(
106 path2,
107 base::Bind(&WriteSomethingAfterStartWatch, path2),
108 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "2"));
109 watcher.StartWatch(
110 path3,
111 base::Bind(&WriteSomethingAfterStartWatch, path3),
112 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "3"));
114 // Unwatched write. It shouldn't be notified.
115 WriteSomethingAfterStartWatch(path4, true);
117 // The loop should quit if all the three paths are notified to be written.
118 loop.Run();
121 } // namespace internal
122 } // namespace drive