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/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 MessageLoop loop_; | 93 MessageLoop loop_; |
94 | 94 |
95 private: | 95 private: |
96 ScopedTempDir temp_dir_; | 96 ScopedTempDir temp_dir_; |
97 }; | 97 }; |
98 | 98 |
99 TEST_F(ImportantFileWriterTest, Basic) { | 99 TEST_F(ImportantFileWriterTest, Basic) { |
100 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 100 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
101 EXPECT_FALSE(PathExists(writer.path())); | 101 EXPECT_FALSE(PathExists(writer.path())); |
102 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 102 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
103 writer.WriteNow("foo"); | 103 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); |
104 RunLoop().RunUntilIdle(); | 104 RunLoop().RunUntilIdle(); |
105 | 105 |
106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
107 ASSERT_TRUE(PathExists(writer.path())); | 107 ASSERT_TRUE(PathExists(writer.path())); |
108 EXPECT_EQ("foo", GetFileContent(writer.path())); | 108 EXPECT_EQ("foo", GetFileContent(writer.path())); |
109 } | 109 } |
110 | 110 |
111 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { | 111 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { |
112 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 112 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
113 EXPECT_FALSE(PathExists(writer.path())); | 113 EXPECT_FALSE(PathExists(writer.path())); |
114 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 114 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
115 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | 115 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
116 writer.WriteNow("foo"); | 116 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); |
117 RunLoop().RunUntilIdle(); | 117 RunLoop().RunUntilIdle(); |
118 | 118 |
119 // Confirm that the observer is invoked. | 119 // Confirm that the observer is invoked. |
120 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | 120 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
121 ASSERT_TRUE(PathExists(writer.path())); | 121 ASSERT_TRUE(PathExists(writer.path())); |
122 EXPECT_EQ("foo", GetFileContent(writer.path())); | 122 EXPECT_EQ("foo", GetFileContent(writer.path())); |
123 | 123 |
124 // Confirm that re-installing the observer works for another write. | 124 // Confirm that re-installing the observer works for another write. |
125 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 125 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
126 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | 126 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
127 writer.WriteNow("bar"); | 127 writer.WriteNow(make_scoped_ptr(new std::string("bar"))); |
128 RunLoop().RunUntilIdle(); | 128 RunLoop().RunUntilIdle(); |
129 | 129 |
130 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | 130 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
131 ASSERT_TRUE(PathExists(writer.path())); | 131 ASSERT_TRUE(PathExists(writer.path())); |
132 EXPECT_EQ("bar", GetFileContent(writer.path())); | 132 EXPECT_EQ("bar", GetFileContent(writer.path())); |
133 | 133 |
134 // Confirm that writing again without re-installing the observer doesn't | 134 // Confirm that writing again without re-installing the observer doesn't |
135 // result in a notification. | 135 // result in a notification. |
136 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 136 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
137 writer.WriteNow("baz"); | 137 writer.WriteNow(make_scoped_ptr(new std::string("baz"))); |
138 RunLoop().RunUntilIdle(); | 138 RunLoop().RunUntilIdle(); |
139 | 139 |
140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
141 ASSERT_TRUE(PathExists(writer.path())); | 141 ASSERT_TRUE(PathExists(writer.path())); |
142 EXPECT_EQ("baz", GetFileContent(writer.path())); | 142 EXPECT_EQ("baz", GetFileContent(writer.path())); |
143 } | 143 } |
144 | 144 |
145 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 145 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
146 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 146 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
147 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 147 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 MessageLoop::current()->PostDelayedTask( | 186 MessageLoop::current()->PostDelayedTask( |
187 FROM_HERE, | 187 FROM_HERE, |
188 MessageLoop::QuitWhenIdleClosure(), | 188 MessageLoop::QuitWhenIdleClosure(), |
189 TimeDelta::FromMilliseconds(100)); | 189 TimeDelta::FromMilliseconds(100)); |
190 MessageLoop::current()->Run(); | 190 MessageLoop::current()->Run(); |
191 ASSERT_TRUE(PathExists(writer.path())); | 191 ASSERT_TRUE(PathExists(writer.path())); |
192 EXPECT_EQ("baz", GetFileContent(writer.path())); | 192 EXPECT_EQ("baz", GetFileContent(writer.path())); |
193 } | 193 } |
194 | 194 |
195 } // namespace base | 195 } // namespace base |
OLD | NEW |