1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/upgrade_detector/directory_monitor.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
class DirectoryMonitorTest : public ::testing::Test {
protected:
void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
// Returns the test directory to monitor.
base::FilePath GetDirectoryToMonitor() { return temp_dir_.GetPath(); }
base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment task_environment_;
};
// Tests that the callback is not invoked when nothing happens.
TEST_F(DirectoryMonitorTest, NoNoise) {
DirectoryMonitor monitor(GetDirectoryToMonitor());
::testing::StrictMock<base::MockRepeatingCallback<void(bool)>> callback;
monitor.Start(callback.Get());
task_environment_.RunUntilIdle();
}
// Tests that the callback is invoked when the directory is modified.
TEST_F(DirectoryMonitorTest, OneChange) {
DirectoryMonitor monitor(GetDirectoryToMonitor());
::testing::StrictMock<base::MockRepeatingCallback<void(bool)>> callback;
monitor.Start(callback.Get());
task_environment_.RunUntilIdle();
base::RunLoop run_loop;
EXPECT_CALL(callback, Run(false)).WillOnce([&run_loop]() {
run_loop.Quit();
});
ASSERT_TRUE(base::WriteFile(
GetDirectoryToMonitor().Append(FILE_PATH_LITERAL("some file")),
"hi, mom"));
run_loop.Run();
}
// Tests that the callback is invoked multiple times for multiple
// modifications.
TEST_F(DirectoryMonitorTest, MultipleChanges) {
DirectoryMonitor monitor(GetDirectoryToMonitor());
::testing::StrictMock<base::MockRepeatingCallback<void(bool)>> callback;
monitor.Start(callback.Get());
task_environment_.RunUntilIdle();
{
base::RunLoop run_loop;
EXPECT_CALL(callback, Run(false)).WillOnce([&run_loop]() {
run_loop.Quit();
});
ASSERT_TRUE(base::WriteFile(
GetDirectoryToMonitor().Append(FILE_PATH_LITERAL("some file")),
"hi, mom"));
run_loop.Run();
ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&callback));
}
{
base::RunLoop run_loop;
EXPECT_CALL(callback, Run(false)).WillOnce([&run_loop]() {
run_loop.Quit();
});
ASSERT_TRUE(base::WriteFile(
GetDirectoryToMonitor().Append(FILE_PATH_LITERAL("other file")),
"hi, dad"));
run_loop.Run();
ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&callback));
}
}
// Tests that the callback is invoked when the directory's mtime is modified.
TEST_F(DirectoryMonitorTest, MtimeChange) {
DirectoryMonitor monitor(GetDirectoryToMonitor());
::testing::StrictMock<base::MockRepeatingCallback<void(bool)>> callback;
monitor.Start(callback.Get());
task_environment_.RunUntilIdle();
base::RunLoop run_loop;
EXPECT_CALL(callback, Run(false)).WillOnce([&run_loop]() {
run_loop.Quit();
});
const base::Time now = base::Time::Now();
ASSERT_TRUE(base::TouchFile(GetDirectoryToMonitor(), now, now));
run_loop.Run();
}
|