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/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/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 virtual ~ConfigFileWatcherTest();
44 // testing::Test overrides
45 virtual void SetUp() OVERRIDE
;
46 virtual void TearDown() OVERRIDE
;
48 // Stops the config file watcher.
52 base::MessageLoop 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()
65 : message_loop_(base::MessageLoop::TYPE_UI
) {
68 ConfigFileWatcherTest::~ConfigFileWatcherTest() {
71 void ConfigFileWatcherTest::StopWatcher() {
75 void ConfigFileWatcherTest::SetUp() {
76 // Arrange to run |message_loop_| until no components depend on it.
77 scoped_refptr
<AutoThreadTaskRunner
> task_runner
= new AutoThreadTaskRunner(
78 message_loop_
.message_loop_proxy(), run_loop_
.QuitClosure());
80 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
=
81 AutoThread::CreateWithType(
82 "IPC thread", task_runner
, base::MessageLoop::TYPE_IO
);
84 // Create an instance of the config watcher.
86 new ConfigFileWatcher(task_runner
, io_task_runner
, &delegate_
));
89 void ConfigFileWatcherTest::TearDown() {
90 // Delete the test file.
91 if (!config_file_
.empty())
92 file_util::Delete(config_file_
, false);
95 // Verifies that the initial notification is delivered.
96 TEST_F(ConfigFileWatcherTest
, Basic
) {
97 EXPECT_TRUE(file_util::CreateTemporaryFile(&config_file_
));
99 std::string
data("test");
100 EXPECT_NE(file_util::WriteFile(config_file_
, data
.c_str(),
101 static_cast<int>(data
.size())), -1);
103 EXPECT_CALL(delegate_
, OnConfigUpdated(_
))
105 .WillOnce(InvokeWithoutArgs(this, &ConfigFileWatcherTest::StopWatcher
));
106 EXPECT_CALL(delegate_
, OnConfigWatcherError())
109 watcher_
->Watch(config_file_
);
113 MATCHER_P(EqualsString
, s
, "") {
117 // Verifies that an update notification is sent when the file is changed.
118 TEST_F(ConfigFileWatcherTest
, Update
) {
119 EXPECT_TRUE(file_util::CreateTemporaryFile(&config_file_
));
121 EXPECT_CALL(delegate_
, OnConfigUpdated(EqualsString("test")))
123 .WillOnce(InvokeWithoutArgs(this, &ConfigFileWatcherTest::StopWatcher
));
124 EXPECT_CALL(delegate_
, OnConfigWatcherError())
127 watcher_
->Watch(config_file_
);
129 // Modify the watched file.
130 std::string
data("test");
131 EXPECT_NE(file_util::WriteFile(config_file_
, data
.c_str(),
132 static_cast<int>(data
.size())), -1);
137 } // namespace remoting