1 // Copyright (c) 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 "remoting/host/config_file_watcher.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "remoting/base/auto_thread.h"
12 #include "remoting/base/auto_thread_task_runner.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gmock_mutant.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using testing::AnyNumber
;
19 using testing::Return
;
25 class ConfigFileWatcherDelegate
: public ConfigFileWatcher::Delegate
{
27 ConfigFileWatcherDelegate() {}
28 virtual ~ConfigFileWatcherDelegate() {}
30 MOCK_METHOD1(OnConfigUpdated
, void(const std::string
&));
31 MOCK_METHOD0(OnConfigWatcherError
, void());
34 DISALLOW_COPY_AND_ASSIGN(ConfigFileWatcherDelegate
);
39 class ConfigFileWatcherTest
: public testing::Test
{
41 ConfigFileWatcherTest();
42 ~ConfigFileWatcherTest() override
;
44 // testing::Test overrides
45 void SetUp() override
;
46 void TearDown() override
;
48 // Stops the config file watcher.
52 base::MessageLoopForUI message_loop_
;
53 base::RunLoop run_loop_
;
55 ConfigFileWatcherDelegate delegate_
;
57 // Path to the configuration file used.
58 base::FilePath config_file_
;
60 // The configuration file watcher that is being tested.
61 scoped_ptr
<ConfigFileWatcher
> watcher_
;
64 ConfigFileWatcherTest::ConfigFileWatcherTest() {
67 ConfigFileWatcherTest::~ConfigFileWatcherTest() {
70 void ConfigFileWatcherTest::StopWatcher() {
74 void ConfigFileWatcherTest::SetUp() {
75 EXPECT_TRUE(base::CreateTemporaryFile(&config_file_
));
77 // Arrange to run |message_loop_| until no components depend on it.
78 scoped_refptr
<AutoThreadTaskRunner
> task_runner
= new AutoThreadTaskRunner(
79 message_loop_
.message_loop_proxy(), run_loop_
.QuitClosure());
81 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
=
82 AutoThread::CreateWithType(
83 "IPC thread", task_runner
, base::MessageLoop::TYPE_IO
);
85 // Create an instance of the config watcher.
87 new ConfigFileWatcher(task_runner
, io_task_runner
, config_file_
));
90 void ConfigFileWatcherTest::TearDown() {
91 // Delete the test file.
92 if (!config_file_
.empty())
93 base::DeleteFile(config_file_
, false);
96 // Verifies that the initial notification is delivered.
97 TEST_F(ConfigFileWatcherTest
, Basic
) {
98 std::string
data("test");
99 EXPECT_NE(base::WriteFile(config_file_
, data
.c_str(),
100 static_cast<int>(data
.size())), -1);
102 EXPECT_CALL(delegate_
, OnConfigUpdated(_
))
104 .WillOnce(InvokeWithoutArgs(this, &ConfigFileWatcherTest::StopWatcher
));
105 EXPECT_CALL(delegate_
, OnConfigWatcherError())
108 watcher_
->Watch(&delegate_
);
112 MATCHER_P(EqualsString
, s
, "") {
116 // Verifies that an update notification is sent when the file is changed.
117 TEST_F(ConfigFileWatcherTest
, Update
) {
118 EXPECT_CALL(delegate_
, OnConfigUpdated(EqualsString("test")))
120 .WillOnce(InvokeWithoutArgs(this, &ConfigFileWatcherTest::StopWatcher
));
121 EXPECT_CALL(delegate_
, OnConfigWatcherError())
124 watcher_
->Watch(&delegate_
);
126 // Modify the watched file.
127 std::string
data("test");
128 EXPECT_NE(base::WriteFile(config_file_
, data
.c_str(),
129 static_cast<int>(data
.size())), -1);
134 } // namespace remoting