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 void SetUp() override
{ ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir()); }
68 base::FilePath
GetTempPath(const std::string
& name
) {
69 return temp_dir_
.path().Append(name
);
73 content::TestBrowserThreadBundle thread_bundle_
;
74 base::ScopedTempDir temp_dir_
;
79 TEST_F(FileWriteWatcherTest
, WatchThreeFiles
) {
80 std::set
<std::string
> expected
;
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");
99 base::Bind(&WriteSomethingAfterStartWatch
, path1
),
100 base::Bind(&TestObserver::OnWrite
, base::Unretained(&observer
), "1"));
103 base::Bind(&WriteSomethingAfterStartWatch
, path2
),
104 base::Bind(&TestObserver::OnWrite
, base::Unretained(&observer
), "2"));
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.
117 } // namespace internal