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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 MessageLoop loop_; | 97 MessageLoop loop_; |
98 | 98 |
99 private: | 99 private: |
100 ScopedTempDir temp_dir_; | 100 ScopedTempDir temp_dir_; |
101 }; | 101 }; |
102 | 102 |
103 TEST_F(ImportantFileWriterTest, Basic) { | 103 TEST_F(ImportantFileWriterTest, Basic) { |
104 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 104 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
105 EXPECT_FALSE(PathExists(writer.path())); | 105 EXPECT_FALSE(PathExists(writer.path())); |
106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
107 writer.WriteNow(WrapUnique(new std::string("foo"))); | 107 writer.WriteNow(MakeUnique<std::string>("foo")); |
108 RunLoop().RunUntilIdle(); | 108 RunLoop().RunUntilIdle(); |
109 | 109 |
110 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 110 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
111 ASSERT_TRUE(PathExists(writer.path())); | 111 ASSERT_TRUE(PathExists(writer.path())); |
112 EXPECT_EQ("foo", GetFileContent(writer.path())); | 112 EXPECT_EQ("foo", GetFileContent(writer.path())); |
113 } | 113 } |
114 | 114 |
115 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { | 115 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { |
116 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | 116 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
117 EXPECT_FALSE(PathExists(writer.path())); | 117 EXPECT_FALSE(PathExists(writer.path())); |
118 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 118 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
119 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | 119 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
120 writer.WriteNow(WrapUnique(new std::string("foo"))); | 120 writer.WriteNow(MakeUnique<std::string>("foo")); |
121 RunLoop().RunUntilIdle(); | 121 RunLoop().RunUntilIdle(); |
122 | 122 |
123 // Confirm that the observer is invoked. | 123 // Confirm that the observer is invoked. |
124 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | 124 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
125 ASSERT_TRUE(PathExists(writer.path())); | 125 ASSERT_TRUE(PathExists(writer.path())); |
126 EXPECT_EQ("foo", GetFileContent(writer.path())); | 126 EXPECT_EQ("foo", GetFileContent(writer.path())); |
127 | 127 |
128 // Confirm that re-installing the observer works for another write. | 128 // Confirm that re-installing the observer works for another write. |
129 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 129 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
130 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | 130 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
131 writer.WriteNow(WrapUnique(new std::string("bar"))); | 131 writer.WriteNow(MakeUnique<std::string>("bar")); |
132 RunLoop().RunUntilIdle(); | 132 RunLoop().RunUntilIdle(); |
133 | 133 |
134 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | 134 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
135 ASSERT_TRUE(PathExists(writer.path())); | 135 ASSERT_TRUE(PathExists(writer.path())); |
136 EXPECT_EQ("bar", GetFileContent(writer.path())); | 136 EXPECT_EQ("bar", GetFileContent(writer.path())); |
137 | 137 |
138 // Confirm that writing again without re-installing the observer doesn't | 138 // Confirm that writing again without re-installing the observer doesn't |
139 // result in a notification. | 139 // result in a notification. |
140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
141 writer.WriteNow(WrapUnique(new std::string("baz"))); | 141 writer.WriteNow(MakeUnique<std::string>("baz")); |
142 RunLoop().RunUntilIdle(); | 142 RunLoop().RunUntilIdle(); |
143 | 143 |
144 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 144 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
145 ASSERT_TRUE(PathExists(writer.path())); | 145 ASSERT_TRUE(PathExists(writer.path())); |
146 EXPECT_EQ("baz", GetFileContent(writer.path())); | 146 EXPECT_EQ("baz", GetFileContent(writer.path())); |
147 } | 147 } |
148 | 148 |
149 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 149 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
150 ImportantFileWriter writer(file_, | 150 ImportantFileWriter writer(file_, |
151 ThreadTaskRunnerHandle::Get(), | 151 ThreadTaskRunnerHandle::Get(), |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 writer.ScheduleWrite(&baz); | 189 writer.ScheduleWrite(&baz); |
190 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 190 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
191 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | 191 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), |
192 TimeDelta::FromMilliseconds(100)); | 192 TimeDelta::FromMilliseconds(100)); |
193 RunLoop().Run(); | 193 RunLoop().Run(); |
194 ASSERT_TRUE(PathExists(writer.path())); | 194 ASSERT_TRUE(PathExists(writer.path())); |
195 EXPECT_EQ("baz", GetFileContent(writer.path())); | 195 EXPECT_EQ("baz", GetFileContent(writer.path())); |
196 } | 196 } |
197 | 197 |
198 } // namespace base | 198 } // namespace base |
OLD | NEW |