Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "base/bind.h" | |
| 6 #include "base/files/file_util.h" | |
| 7 #include "base/files/scoped_temp_dir.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 | |
|
Sergey Ulanov
2016/03/30 21:02:46
nit: remove this empty line
Yuwei
2016/03/31 17:40:06
Done.
| |
| 11 #include "remoting/base/auto_thread.h" | |
| 12 #include "remoting/host/linux/certificate_watcher.h" | |
|
Sergey Ulanov
2016/03/30 21:02:45
this should be first include: https://google.githu
Yuwei
2016/03/31 17:40:05
Done.
| |
| 13 | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace remoting { | |
| 17 const std::string WATCH_FILENAME = "testfile.txt"; | |
|
Sergey Ulanov
2016/03/30 21:02:45
kWatchFilename
https://google.github.io/styleguide
Sergey Ulanov
2016/03/30 21:02:46
static constant of std::string are not allowed bec
Yuwei
2016/03/31 17:40:05
Done.
| |
| 18 const std::string TEST_WRITE_STRING = "TEST STRING"; | |
| 19 | |
| 20 class TestSetup { | |
| 21 public: | |
|
Sergey Ulanov
2016/03/30 21:02:46
None of these fields and methods need to be public
Yuwei
2016/03/31 17:40:06
Done.
| |
| 22 TestSetup() | |
| 23 : watcher_loop_(), | |
| 24 watcher_runner_( | |
|
Sergey Ulanov
2016/03/30 21:02:46
You don't need this. MessageLoop already has a tas
Yuwei
2016/03/31 17:40:05
Done.
| |
| 25 new AutoThreadTaskRunner(watcher_loop_.task_runner(), | |
| 26 base::MessageLoop::QuitWhenIdleClosure())), | |
| 27 temp_dir(), | |
|
Sergey Ulanov
2016/03/30 21:02:46
Don't need this initializer - the compiler will ca
Yuwei
2016/03/31 17:40:05
Done.
| |
| 28 watch_path(CreateAndGetUniqueTempDir().AppendASCII(WATCH_FILENAME)), | |
| 29 watcher(0, | |
| 30 base::Bind(&TestSetup::OnRestart, base::Unretained(this)), | |
| 31 base::Bind(&TestSetup::OnDeferred, base::Unretained(this)), | |
| 32 watch_path) {} | |
| 33 | |
| 34 void Start() { | |
| 35 watcher.StartOn(watcher_runner_, base::WeakPtr<HostStatusMonitor>()); | |
| 36 } | |
| 37 | |
| 38 void Stop() { watcher.Stop(); } | |
| 39 | |
| 40 void RunLoop() { watcher_loop_.Run(); } | |
| 41 | |
| 42 void TouchFile() { | |
| 43 watcher_runner_->PostTask(FROM_HERE, base::Bind(&TestSetup::TouchFileOnIO, | |
| 44 base::Unretained(this))); | |
| 45 } | |
| 46 | |
| 47 base::MessageLoopForIO watcher_loop_; | |
|
Sergey Ulanov
2016/03/30 21:02:46
The convention is to call MessageLoop instances in
Yuwei
2016/03/31 17:40:05
Done.
| |
| 48 const scoped_refptr<AutoThreadTaskRunner> watcher_runner_; | |
| 49 base::ScopedTempDir temp_dir; | |
| 50 base::FilePath watch_path; | |
| 51 CertificateWatcher watcher; | |
|
Sergey Ulanov
2016/03/30 21:02:45
watcher_ with '_' suffix. Same for other members
Yuwei
2016/03/31 17:40:05
Done.
| |
| 52 int restart_count = 0; | |
| 53 | |
| 54 private: | |
| 55 const base::FilePath& CreateAndGetUniqueTempDir() { | |
| 56 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 57 return temp_dir.path(); | |
| 58 } | |
| 59 | |
| 60 void TouchFileOnIO() { | |
| 61 EXPECT_TRUE(watcher_runner_->BelongsToCurrentThread()); | |
| 62 base::WriteFile(watch_path, TEST_WRITE_STRING.c_str(), | |
| 63 TEST_WRITE_STRING.length()); | |
| 64 } | |
| 65 | |
| 66 void OnRestart() { | |
| 67 EXPECT_TRUE(watcher_runner_->BelongsToCurrentThread()); | |
| 68 restart_count++; | |
| 69 // watcher_loop_.QuitNow(); | |
|
Sergey Ulanov
2016/03/30 21:02:45
Please remove commented code.
Yuwei
2016/03/31 17:40:05
Done.
| |
| 70 watcher_loop_.QuitNow(); | |
| 71 } | |
| 72 | |
| 73 void OnDeferred() { | |
| 74 EXPECT_TRUE(watcher_runner_->BelongsToCurrentThread()); | |
| 75 watcher_loop_.QuitNow(); | |
| 76 } | |
| 77 }; | |
| 78 | |
| 79 TEST(CertificateWatcherTest, StartAndStop) { | |
| 80 TestSetup setup; | |
| 81 | |
| 82 EXPECT_EQ(0, setup.restart_count); | |
| 83 setup.Start(); | |
| 84 setup.TouchFile(); | |
| 85 setup.RunLoop(); | |
| 86 EXPECT_EQ(1, setup.restart_count); | |
| 87 setup.Stop(); | |
| 88 EXPECT_EQ(1, setup.restart_count); | |
| 89 } | |
| 90 | |
| 91 TEST(CertificateWatcherTest, InhibitDeferRestart) { | |
| 92 TestSetup setup; | |
| 93 | |
| 94 setup.Start(); | |
| 95 EXPECT_EQ(0, setup.restart_count); | |
| 96 setup.watcher.Inhibit(); | |
| 97 EXPECT_EQ(0, setup.restart_count); | |
| 98 setup.TouchFile(); | |
| 99 setup.RunLoop(); | |
| 100 EXPECT_EQ(0, setup.restart_count); | |
| 101 setup.watcher.Uninhibit(); | |
| 102 setup.RunLoop(); | |
| 103 EXPECT_EQ(1, setup.restart_count); | |
| 104 } | |
| 105 | |
| 106 TEST(CertificateWatcherTest, UninhibitAndRestart) { | |
| 107 TestSetup setup; | |
| 108 | |
| 109 setup.Start(); | |
| 110 EXPECT_EQ(0, setup.restart_count); | |
| 111 setup.watcher.Inhibit(); | |
| 112 EXPECT_EQ(0, setup.restart_count); | |
| 113 setup.watcher.Uninhibit(); | |
| 114 EXPECT_EQ(0, setup.restart_count); | |
| 115 setup.TouchFile(); | |
| 116 setup.RunLoop(); | |
| 117 EXPECT_EQ(1, setup.restart_count); | |
| 118 } | |
| 119 | |
| 120 } // namespace remoting | |
| OLD | NEW |