Chromium Code Reviews| Index: base/files/important_file_writer_unittest.cc |
| diff --git a/base/files/important_file_writer_unittest.cc b/base/files/important_file_writer_unittest.cc |
| index 02a5f76b2b6e7a9d775d4b0f4551d205a2aa4c8d..b5290bc6b8bb01bcb4b9a58cdd38f3715a84caaa 100644 |
| --- a/base/files/important_file_writer_unittest.cc |
| +++ b/base/files/important_file_writer_unittest.cc |
| @@ -4,6 +4,7 @@ |
| #include "base/files/important_file_writer.h" |
| +#include "base/bind.h" |
| #include "base/compiler_specific.h" |
| #include "base/file_util.h" |
| #include "base/files/file_path.h" |
| @@ -41,6 +42,41 @@ class DataSerializer : public ImportantFileWriter::DataSerializer { |
| const std::string data_; |
| }; |
| +class SuccessfulWriteObserver { |
| + public: |
| + SuccessfulWriteObserver() : successful_write_observed_(false) {} |
| + |
| + // Register on_successful_write() to be called on the next successful write |
| + // of |writer|. |
| + void ObserveNextSuccessfulWrite(ImportantFileWriter* writer); |
| + |
| + // Returns true if a successful write was observed via on_successful_write() |
| + // and resets the observation state to false regardless. |
| + bool GetAndResetObservationState(); |
| + |
| + private: |
| + void on_successful_write() { |
| + EXPECT_FALSE(successful_write_observed_); |
| + successful_write_observed_ = true; |
| + } |
| + |
| + bool successful_write_observed_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteObserver); |
| +}; |
| + |
| +void SuccessfulWriteObserver::ObserveNextSuccessfulWrite( |
| + ImportantFileWriter* writer) { |
| + writer->RegisterOnNextSuccessfulWriteCallback(base::Bind( |
| + &SuccessfulWriteObserver::on_successful_write, base::Unretained(this))); |
| +} |
| + |
| +bool SuccessfulWriteObserver::GetAndResetObservationState() { |
| + bool was_successful_write_observed = successful_write_observed_; |
| + successful_write_observed_ = false; |
| + return was_successful_write_observed; |
| +} |
| + |
| } // namespace |
| class ImportantFileWriterTest : public testing::Test { |
| @@ -52,6 +88,7 @@ class ImportantFileWriterTest : public testing::Test { |
| } |
| protected: |
| + SuccessfulWriteObserver successful_write_observer_; |
| FilePath file_; |
| MessageLoop loop_; |
| @@ -62,11 +99,47 @@ class ImportantFileWriterTest : public testing::Test { |
| TEST_F(ImportantFileWriterTest, Basic) { |
| ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
| EXPECT_FALSE(PathExists(writer.path())); |
| + EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| + writer.WriteNow("foo"); |
| + RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| + ASSERT_TRUE(PathExists(writer.path())); |
| + EXPECT_EQ("foo", GetFileContent(writer.path())); |
| +} |
| + |
| +TEST_F(ImportantFileWriterTest, BasicWithSucessfulWriteObserver) { |
|
gab
2014/05/05 13:53:46
s/Sucess/Success
gab
2014/05/05 17:30:36
Done.
|
| + ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
| + EXPECT_FALSE(PathExists(writer.path())); |
| + EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| + successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
| writer.WriteNow("foo"); |
| RunLoop().RunUntilIdle(); |
| + // Confirm that the observer is invoked. |
| + EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
| ASSERT_TRUE(PathExists(writer.path())); |
| EXPECT_EQ("foo", GetFileContent(writer.path())); |
| + |
| + // Confirm that re-installing the observer works for another write. |
| + EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| + successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
| + writer.WriteNow("bar"); |
| + RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
| + ASSERT_TRUE(PathExists(writer.path())); |
| + EXPECT_EQ("bar", GetFileContent(writer.path())); |
| + |
| + // Confirm that writing again without re-installing the observer doesn't |
| + // result in a notification. |
| + EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| + writer.WriteNow("baz"); |
| + RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| + ASSERT_TRUE(PathExists(writer.path())); |
| + EXPECT_EQ("baz", GetFileContent(writer.path())); |
| } |
| TEST_F(ImportantFileWriterTest, ScheduleWrite) { |