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..60fe9d8548e49beb0eb916240f2371f3b9f63ce7 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,42 @@ 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 +89,7 @@ class ImportantFileWriterTest : public testing::Test { |
| } |
| protected: |
| + SuccessfulWriteObserver successful_write_observer_; |
| FilePath file_; |
| MessageLoop loop_; |
| @@ -62,11 +100,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) { |
| + 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 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.
|
| + // 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) { |