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 "chrome/browser/chromeos/drive/file_write_watcher.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/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
23 // After all the resource_ids in |expected_upload| are notified for the
24 // need of uploading, runs |quit_closure|. Also checks that each id is
25 // notified only once.
26 TestObserver(const std::set<std::string>& expected_upload,
27 const base::Closure& quit_closure)
28 : expected_upload_(expected_upload),
29 quit_closure_(quit_closure) {
32 void OnWrite(const std::string& id) {
33 EXPECT_EQ(1U, expected_upload_.count(id)) << id;
34 expected_upload_.erase(id);
35 if (expected_upload_.empty())
40 std::set<std::string> expected_upload_;
41 base::Closure quit_closure_;
44 // Writes something on the file at |path|.
45 void WriteSomethingAfterStartWatch(const base::FilePath& path,
47 EXPECT_TRUE(watch_success) << path.value();
49 const char kDummy[] = "hello";
50 ASSERT_TRUE(base::WriteFile(path, kDummy, arraysize(kDummy)));
53 class FileWriteWatcherTest : public testing::Test {
55 // The test requires UI thread (FileWriteWatcher DCHECKs that its public
56 // interface is running on UI thread) and FILE thread (Linux version of
57 // base::FilePathWatcher needs to live on an IOAllowed thread with TYPE_IO,
58 // which is FILE thread in the production environment).
60 // By using the IO_MAINLOOP test thread bundle, the main thread is used
61 // both as UI and FILE thread, with TYPE_IO message loop.
62 FileWriteWatcherTest()
63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
66 virtual void SetUp() override {
67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
70 base::FilePath GetTempPath(const std::string& name) {
71 return temp_dir_.path().Append(name);
75 content::TestBrowserThreadBundle thread_bundle_;
76 base::ScopedTempDir temp_dir_;
81 TEST_F(FileWriteWatcherTest, WatchThreeFiles) {
82 std::set<std::string> expected;
88 TestObserver observer(expected, loop.QuitClosure());
90 // Set up the watcher.
91 FileWriteWatcher watcher;
92 watcher.DisableDelayForTesting();
94 // Start watching and running.
95 base::FilePath path1 = GetTempPath("foo.txt");
96 base::FilePath path2 = GetTempPath("bar.png");
97 base::FilePath path3 = GetTempPath("buz.doc");
98 base::FilePath path4 = GetTempPath("mya.mp3");
101 base::Bind(&WriteSomethingAfterStartWatch, path1),
102 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "1"));
105 base::Bind(&WriteSomethingAfterStartWatch, path2),
106 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "2"));
109 base::Bind(&WriteSomethingAfterStartWatch, path3),
110 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "3"));
112 // Unwatched write. It shouldn't be notified.
113 WriteSomethingAfterStartWatch(path4, true);
115 // The loop should quit if all the three paths are notified to be written.
119 } // namespace internal