- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / host / config_file_watcher_unittest.cc
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.
4
5 #include "remoting/host/config_file_watcher.h"
6
7 #include "base/file_util.h"
8 #include "base/files/file_path.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"
16
17 using testing::_;
18 using testing::AnyNumber;
19 using testing::Return;
20
21 namespace remoting {
22
23 namespace {
24
25 class ConfigFileWatcherDelegate : public ConfigFileWatcher::Delegate {
26  public:
27   ConfigFileWatcherDelegate() {}
28   virtual ~ConfigFileWatcherDelegate() {}
29
30   MOCK_METHOD1(OnConfigUpdated, void(const std::string&));
31   MOCK_METHOD0(OnConfigWatcherError, void());
32
33  private:
34   DISALLOW_COPY_AND_ASSIGN(ConfigFileWatcherDelegate);
35 };
36
37 }  // namespace
38
39 class ConfigFileWatcherTest : public testing::Test {
40  public:
41   ConfigFileWatcherTest();
42   virtual ~ConfigFileWatcherTest();
43
44   // testing::Test overrides
45   virtual void SetUp() OVERRIDE;
46   virtual void TearDown() OVERRIDE;
47
48   // Stops the config file watcher.
49   void StopWatcher();
50
51  protected:
52   base::MessageLoop message_loop_;
53   base::RunLoop run_loop_;
54
55   ConfigFileWatcherDelegate delegate_;
56
57   // Path to the configuration file used.
58   base::FilePath config_file_;
59
60   // The configuration file watcher that is being tested.
61   scoped_ptr<ConfigFileWatcher> watcher_;
62 };
63
64 ConfigFileWatcherTest::ConfigFileWatcherTest()
65     : message_loop_(base::MessageLoop::TYPE_UI) {
66 }
67
68 ConfigFileWatcherTest::~ConfigFileWatcherTest() {
69 }
70
71 void ConfigFileWatcherTest::StopWatcher() {
72   watcher_.reset();
73 }
74
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());
79
80   scoped_refptr<AutoThreadTaskRunner> io_task_runner =
81       AutoThread::CreateWithType(
82           "IPC thread", task_runner, base::MessageLoop::TYPE_IO);
83
84   // Create an instance of the config watcher.
85   watcher_.reset(
86       new ConfigFileWatcher(task_runner, io_task_runner, &delegate_));
87 }
88
89 void ConfigFileWatcherTest::TearDown() {
90   // Delete the test file.
91   if (!config_file_.empty())
92     base::DeleteFile(config_file_, false);
93 }
94
95 // Verifies that the initial notification is delivered.
96 TEST_F(ConfigFileWatcherTest, Basic) {
97   EXPECT_TRUE(file_util::CreateTemporaryFile(&config_file_));
98
99   std::string data("test");
100   EXPECT_NE(file_util::WriteFile(config_file_, data.c_str(),
101                                  static_cast<int>(data.size())), -1);
102
103   EXPECT_CALL(delegate_, OnConfigUpdated(_))
104       .Times(1)
105       .WillOnce(InvokeWithoutArgs(this, &ConfigFileWatcherTest::StopWatcher));
106   EXPECT_CALL(delegate_, OnConfigWatcherError())
107       .Times(0);
108
109   watcher_->Watch(config_file_);
110   run_loop_.Run();
111 }
112
113 MATCHER_P(EqualsString, s, "") {
114   return arg == s;
115 }
116
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_));
120
121   EXPECT_CALL(delegate_, OnConfigUpdated(EqualsString("test")))
122       .Times(1)
123       .WillOnce(InvokeWithoutArgs(this, &ConfigFileWatcherTest::StopWatcher));
124   EXPECT_CALL(delegate_, OnConfigWatcherError())
125       .Times(0);
126
127   watcher_->Watch(config_file_);
128
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);
133
134   run_loop_.Run();
135 }
136
137 }  // namespace remoting