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(base::Bind( |
| 71 &SuccessfulWriteObserver::on_successful_write, base::Unretained(this))); |
| 72 } |
| 73 |
| 74 bool SuccessfulWriteObserver::GetAndResetObservationState() { |
| 75 bool was_successful_write_observed = successful_write_observed_; |
| 76 successful_write_observed_ = false; |
| 77 return was_successful_write_observed; |
| 78 } |
| 79 |
44 } // namespace | 80 } // namespace |
45 | 81 |
46 class ImportantFileWriterTest : public testing::Test { | 82 class ImportantFileWriterTest : public testing::Test { |
47 public: | 83 public: |
48 ImportantFileWriterTest() { } | 84 ImportantFileWriterTest() { } |
49 virtual void SetUp() { | 85 virtual void SetUp() { |
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 86 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
51 file_ = temp_dir_.path().AppendASCII("test-file"); | 87 file_ = temp_dir_.path().AppendASCII("test-file"); |
52 } | 88 } |
53 | 89 |
54 protected: | 90 protected: |
| 91 SuccessfulWriteObserver successful_write_observer_; |
55 FilePath file_; | 92 FilePath file_; |
56 MessageLoop loop_; | 93 MessageLoop loop_; |
57 | 94 |
58 private: | 95 private: |
59 ScopedTempDir temp_dir_; | 96 ScopedTempDir temp_dir_; |
60 }; | 97 }; |
61 | 98 |
62 TEST_F(ImportantFileWriterTest, Basic) { | 99 TEST_F(ImportantFileWriterTest, Basic) { |
63 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 100 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
64 EXPECT_FALSE(PathExists(writer.path())); | 101 EXPECT_FALSE(PathExists(writer.path())); |
| 102 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
65 writer.WriteNow("foo"); | 103 writer.WriteNow("foo"); |
66 RunLoop().RunUntilIdle(); | 104 RunLoop().RunUntilIdle(); |
67 | 105 |
| 106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
68 ASSERT_TRUE(PathExists(writer.path())); | 107 ASSERT_TRUE(PathExists(writer.path())); |
69 EXPECT_EQ("foo", GetFileContent(writer.path())); | 108 EXPECT_EQ("foo", GetFileContent(writer.path())); |
70 } | 109 } |
71 | 110 |
| 111 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { |
| 112 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
| 113 EXPECT_FALSE(PathExists(writer.path())); |
| 114 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 115 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
| 116 writer.WriteNow("foo"); |
| 117 RunLoop().RunUntilIdle(); |
| 118 |
| 119 // Confirm that the observer is invoked. |
| 120 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
| 121 ASSERT_TRUE(PathExists(writer.path())); |
| 122 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 123 |
| 124 // Confirm that re-installing the observer works for another write. |
| 125 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 126 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
| 127 writer.WriteNow("bar"); |
| 128 RunLoop().RunUntilIdle(); |
| 129 |
| 130 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
| 131 ASSERT_TRUE(PathExists(writer.path())); |
| 132 EXPECT_EQ("bar", GetFileContent(writer.path())); |
| 133 |
| 134 // Confirm that writing again without re-installing the observer doesn't |
| 135 // result in a notification. |
| 136 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 137 writer.WriteNow("baz"); |
| 138 RunLoop().RunUntilIdle(); |
| 139 |
| 140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 141 ASSERT_TRUE(PathExists(writer.path())); |
| 142 EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 143 } |
| 144 |
72 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 145 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
73 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 146 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
74 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 147 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
75 EXPECT_FALSE(writer.HasPendingWrite()); | 148 EXPECT_FALSE(writer.HasPendingWrite()); |
76 DataSerializer serializer("foo"); | 149 DataSerializer serializer("foo"); |
77 writer.ScheduleWrite(&serializer); | 150 writer.ScheduleWrite(&serializer); |
78 EXPECT_TRUE(writer.HasPendingWrite()); | 151 EXPECT_TRUE(writer.HasPendingWrite()); |
79 MessageLoop::current()->PostDelayedTask( | 152 MessageLoop::current()->PostDelayedTask( |
80 FROM_HERE, | 153 FROM_HERE, |
81 MessageLoop::QuitWhenIdleClosure(), | 154 MessageLoop::QuitWhenIdleClosure(), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 MessageLoop::current()->PostDelayedTask( | 186 MessageLoop::current()->PostDelayedTask( |
114 FROM_HERE, | 187 FROM_HERE, |
115 MessageLoop::QuitWhenIdleClosure(), | 188 MessageLoop::QuitWhenIdleClosure(), |
116 TimeDelta::FromMilliseconds(100)); | 189 TimeDelta::FromMilliseconds(100)); |
117 MessageLoop::current()->Run(); | 190 MessageLoop::current()->Run(); |
118 ASSERT_TRUE(PathExists(writer.path())); | 191 ASSERT_TRUE(PathExists(writer.path())); |
119 EXPECT_EQ("baz", GetFileContent(writer.path())); | 192 EXPECT_EQ("baz", GetFileContent(writer.path())); |
120 } | 193 } |
121 | 194 |
122 } // namespace base | 195 } // namespace base |
OLD | NEW |