Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/important_file_writer.h" | 5 #include "base/files/important_file_writer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 34 | 35 |
| 35 virtual bool SerializeData(std::string* output) OVERRIDE { | 36 virtual bool SerializeData(std::string* output) OVERRIDE { |
| 36 output->assign(data_); | 37 output->assign(data_); |
| 37 return true; | 38 return true; |
| 38 } | 39 } |
| 39 | 40 |
| 40 private: | 41 private: |
| 41 const std::string data_; | 42 const std::string data_; |
| 42 }; | 43 }; |
| 43 | 44 |
| 45 class SuccessfulWriteObserver { | |
| 46 public: | |
| 47 SuccessfulWriteObserver() : successful_write_observed_(false) {} | |
| 48 | |
| 49 // Register on_successful_write() to be called on the next successful write | |
| 50 // of |writer|. | |
| 51 void ObserveNextSuccessfulWrite(ImportantFileWriter* writer); | |
| 52 | |
| 53 // Returns true if a successful write was observed via on_successful_write() | |
| 54 // and resets the observation state to false regardless. | |
| 55 bool GetAndResetObservationState(); | |
| 56 | |
| 57 private: | |
| 58 void on_successful_write() { | |
| 59 EXPECT_FALSE(successful_write_observed_); | |
| 60 successful_write_observed_ = true; | |
| 61 } | |
| 62 | |
| 63 bool successful_write_observed_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteObserver); | |
| 66 }; | |
| 67 | |
| 68 void SuccessfulWriteObserver::ObserveNextSuccessfulWrite( | |
| 69 ImportantFileWriter* writer) { | |
| 70 writer->RegisterOnNextSuccessfulWriteCallback( | |
| 71 base::Bind(&SuccessfulWriteObserver::on_successful_write, \ | |
| 72 base::Unretained(this))); | |
| 73 } | |
| 74 | |
| 75 bool SuccessfulWriteObserver::GetAndResetObservationState() { | |
| 76 bool was_successful_write_observed = successful_write_observed_; | |
| 77 successful_write_observed_ = false; | |
| 78 return was_successful_write_observed; | |
| 79 } | |
| 80 | |
| 44 } // namespace | 81 } // namespace |
| 45 | 82 |
| 46 class ImportantFileWriterTest : public testing::Test { | 83 class ImportantFileWriterTest : public testing::Test { |
| 47 public: | 84 public: |
| 48 ImportantFileWriterTest() { } | 85 ImportantFileWriterTest() { } |
| 49 virtual void SetUp() { | 86 virtual void SetUp() { |
| 50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 87 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 51 file_ = temp_dir_.path().AppendASCII("test-file"); | 88 file_ = temp_dir_.path().AppendASCII("test-file"); |
| 52 } | 89 } |
| 53 | 90 |
| 54 protected: | 91 protected: |
| 92 SuccessfulWriteObserver successful_write_observer_; | |
| 55 FilePath file_; | 93 FilePath file_; |
| 56 MessageLoop loop_; | 94 MessageLoop loop_; |
| 57 | 95 |
| 58 private: | 96 private: |
| 59 ScopedTempDir temp_dir_; | 97 ScopedTempDir temp_dir_; |
| 60 }; | 98 }; |
| 61 | 99 |
| 62 TEST_F(ImportantFileWriterTest, Basic) { | 100 TEST_F(ImportantFileWriterTest, Basic) { |
| 63 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 101 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
| 64 EXPECT_FALSE(PathExists(writer.path())); | 102 EXPECT_FALSE(PathExists(writer.path())); |
| 103 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 65 writer.WriteNow("foo"); | 104 writer.WriteNow("foo"); |
| 66 RunLoop().RunUntilIdle(); | 105 RunLoop().RunUntilIdle(); |
| 67 | 106 |
| 107 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 68 ASSERT_TRUE(PathExists(writer.path())); | 108 ASSERT_TRUE(PathExists(writer.path())); |
| 69 EXPECT_EQ("foo", GetFileContent(writer.path())); | 109 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 70 } | 110 } |
| 71 | 111 |
| 112 TEST_F(ImportantFileWriterTest, BasicWithSucessfulWriteObserver) { | |
| 113 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | |
| 114 EXPECT_FALSE(PathExists(writer.path())); | |
| 115 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 116 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | |
| 117 writer.WriteNow("foo"); | |
| 118 RunLoop().RunUntilIdle(); | |
| 119 | |
| 120 // Confirm that the observer is invoked. | |
| 121 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | |
| 122 ASSERT_TRUE(PathExists(writer.path())); | |
| 123 EXPECT_EQ("foo", GetFileContent(writer.path())); | |
| 124 | |
| 125 // Confirm that re-installing the observer works for another write. | |
| 126 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 127 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | |
| 128 writer.WriteNow("bar"); | |
| 129 RunLoop().RunUntilIdle(); | |
| 130 | |
| 131 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | |
| 132 ASSERT_TRUE(PathExists(writer.path())); | |
| 133 EXPECT_EQ("bar", GetFileContent(writer.path())); | |
| 134 | |
| 135 // Confirm that writting again without re-installing the observer doesn't | |
|
Bernhard Bauer
2014/05/01 12:37:09
Nit: "writing"
gab
2014/05/01 15:34:54
Done.
| |
| 136 // result in a notification. | |
| 137 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 138 writer.WriteNow("baz"); | |
| 139 RunLoop().RunUntilIdle(); | |
| 140 | |
| 141 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 142 ASSERT_TRUE(PathExists(writer.path())); | |
| 143 EXPECT_EQ("baz", GetFileContent(writer.path())); | |
| 144 } | |
| 145 | |
| 72 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 146 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
| 73 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 147 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
| 74 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 148 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
| 75 EXPECT_FALSE(writer.HasPendingWrite()); | 149 EXPECT_FALSE(writer.HasPendingWrite()); |
| 76 DataSerializer serializer("foo"); | 150 DataSerializer serializer("foo"); |
| 77 writer.ScheduleWrite(&serializer); | 151 writer.ScheduleWrite(&serializer); |
| 78 EXPECT_TRUE(writer.HasPendingWrite()); | 152 EXPECT_TRUE(writer.HasPendingWrite()); |
| 79 MessageLoop::current()->PostDelayedTask( | 153 MessageLoop::current()->PostDelayedTask( |
| 80 FROM_HERE, | 154 FROM_HERE, |
| 81 MessageLoop::QuitWhenIdleClosure(), | 155 MessageLoop::QuitWhenIdleClosure(), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 MessageLoop::current()->PostDelayedTask( | 187 MessageLoop::current()->PostDelayedTask( |
| 114 FROM_HERE, | 188 FROM_HERE, |
| 115 MessageLoop::QuitWhenIdleClosure(), | 189 MessageLoop::QuitWhenIdleClosure(), |
| 116 TimeDelta::FromMilliseconds(100)); | 190 TimeDelta::FromMilliseconds(100)); |
| 117 MessageLoop::current()->Run(); | 191 MessageLoop::current()->Run(); |
| 118 ASSERT_TRUE(PathExists(writer.path())); | 192 ASSERT_TRUE(PathExists(writer.path())); |
| 119 EXPECT_EQ("baz", GetFileContent(writer.path())); | 193 EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 120 } | 194 } |
| 121 | 195 |
| 122 } // namespace base | 196 } // namespace base |
| OLD | NEW |