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/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
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())
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
,
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
{
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
);
74 content::TestBrowserThreadBundle thread_bundle_
;
75 base::ScopedTempDir temp_dir_
;
80 TEST_F(FileWriteWatcherTest
, WatchThreeFiles
) {
81 std::set
<std::string
> expected
;
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");
103 base::Bind(&WriteSomethingAfterStartWatch
, path1
),
104 base::Bind(&TestObserver::OnWrite
, base::Unretained(&observer
), "1"));
107 base::Bind(&WriteSomethingAfterStartWatch
, path2
),
108 base::Bind(&TestObserver::OnWrite
, base::Unretained(&observer
), "2"));
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.
121 } // namespace internal