Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_write_watcher_unittest.cc
blob978a274ba4b35b25adf4c8bc62ed907185b1b733
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"
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/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace drive {
17 namespace internal {
19 namespace {
21 class TestObserver {
22 public:
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())
36 quit_closure_.Run();
39 private:
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,
46 bool watch_success) {
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 {
54 protected:
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 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
68 base::FilePath GetTempPath(const std::string& name) {
69 return temp_dir_.path().Append(name);
72 private:
73 content::TestBrowserThreadBundle thread_bundle_;
74 base::ScopedTempDir temp_dir_;
77 } // namespace
79 TEST_F(FileWriteWatcherTest, WatchThreeFiles) {
80 std::set<std::string> expected;
81 expected.insert("1");
82 expected.insert("2");
83 expected.insert("3");
85 base::RunLoop loop;
86 TestObserver observer(expected, loop.QuitClosure());
88 // Set up the watcher.
89 FileWriteWatcher watcher;
90 watcher.DisableDelayForTesting();
92 // Start watching and running.
93 base::FilePath path1 = GetTempPath("foo.txt");
94 base::FilePath path2 = GetTempPath("bar.png");
95 base::FilePath path3 = GetTempPath("buz.doc");
96 base::FilePath path4 = GetTempPath("mya.mp3");
97 watcher.StartWatch(
98 path1,
99 base::Bind(&WriteSomethingAfterStartWatch, path1),
100 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "1"));
101 watcher.StartWatch(
102 path2,
103 base::Bind(&WriteSomethingAfterStartWatch, path2),
104 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "2"));
105 watcher.StartWatch(
106 path3,
107 base::Bind(&WriteSomethingAfterStartWatch, path3),
108 base::Bind(&TestObserver::OnWrite, base::Unretained(&observer), "3"));
110 // Unwatched write. It shouldn't be notified.
111 WriteSomethingAfterStartWatch(path4, true);
113 // The loop should quit if all the three paths are notified to be written.
114 loop.Run();
117 } // namespace internal
118 } // namespace drive