Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1424)

Side by Side Diff: base/files/important_file_writer_unittest.cc

Issue 2299523003: Add synchronous callback support to important_file_writer.cc (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Struct -> Enum Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/important_file_writer.cc ('k') | chrome/browser/prefs/profile_pref_store_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 successful_write_observed_ = true; 64 successful_write_observed_ = true;
65 } 65 }
66 66
67 bool successful_write_observed_; 67 bool successful_write_observed_;
68 68
69 DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteObserver); 69 DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteObserver);
70 }; 70 };
71 71
72 void SuccessfulWriteObserver::ObserveNextSuccessfulWrite( 72 void SuccessfulWriteObserver::ObserveNextSuccessfulWrite(
73 ImportantFileWriter* writer) { 73 ImportantFileWriter* writer) {
74 writer->RegisterOnNextSuccessfulWriteCallback(base::Bind( 74 writer->RegisterOnNextSuccessfulWriteReply(base::Bind(
75 &SuccessfulWriteObserver::on_successful_write, base::Unretained(this))); 75 &SuccessfulWriteObserver::on_successful_write, base::Unretained(this)));
76 } 76 }
77 77
78 bool SuccessfulWriteObserver::GetAndResetObservationState() { 78 bool SuccessfulWriteObserver::GetAndResetObservationState() {
79 bool was_successful_write_observed = successful_write_observed_; 79 bool was_successful_write_observed = successful_write_observed_;
80 successful_write_observed_ = false; 80 successful_write_observed_ = false;
81 return was_successful_write_observed; 81 return was_successful_write_observed;
82 } 82 }
83 83
84 enum WriteCallbackObservationState {
85 NOT_CALLED,
86 CALLED_WITH_ERROR,
87 CALLED_WITH_SUCCESS,
88 };
89
90 class SynchronousWriteCallbackObserver {
91 public:
92 SynchronousWriteCallbackObserver()
93 : write_callback_observed_(false), write_callback_success_value_(false) {}
94
95 // Register on_write() to be called on the next write of |writer|.
96 void ObserveNextWriteCallback(ImportantFileWriter* writer);
97
98 // Returns true if a write was observed via on_write()
99 // and resets the observation state to false regardless.
100 WriteCallbackObservationState GetAndResetObservationState();
101
102 private:
103 void on_write(bool success) {
104 EXPECT_FALSE(write_callback_observed_);
105 write_callback_observed_ = true;
106 write_callback_success_value_ = success;
107 }
108
109 bool write_callback_observed_;
110 bool write_callback_success_value_;
gab 2016/09/01 19:59:53 Use WriteCallbackObservationState here instead of
proberge 2016/09/07 19:07:15 Done.
111
112 DISALLOW_COPY_AND_ASSIGN(SynchronousWriteCallbackObserver);
113 };
114
115 void SynchronousWriteCallbackObserver::ObserveNextWriteCallback(
116 ImportantFileWriter* writer) {
117 writer->RegisterOnNextWriteSynchronousCallback(base::Bind(
118 &SynchronousWriteCallbackObserver::on_write, base::Unretained(this)));
119 }
120
121 WriteCallbackObservationState
122 SynchronousWriteCallbackObserver::GetAndResetObservationState() {
123 if (write_callback_observed_) {
124 write_callback_observed_ = false;
125 return write_callback_success_value_ ? CALLED_WITH_SUCCESS
126 : CALLED_WITH_ERROR;
127 } else {
128 return NOT_CALLED;
129 }
130 }
131
84 } // namespace 132 } // namespace
85 133
86 class ImportantFileWriterTest : public testing::Test { 134 class ImportantFileWriterTest : public testing::Test {
87 public: 135 public:
88 ImportantFileWriterTest() { } 136 ImportantFileWriterTest() { }
89 void SetUp() override { 137 void SetUp() override {
90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 138 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
91 file_ = temp_dir_.path().AppendASCII("test-file"); 139 file_ = temp_dir_.path().AppendASCII("test-file");
92 } 140 }
93 141
94 protected: 142 protected:
95 SuccessfulWriteObserver successful_write_observer_; 143 SuccessfulWriteObserver successful_write_observer_;
144 SynchronousWriteCallbackObserver write_callback_observer_;
96 FilePath file_; 145 FilePath file_;
97 MessageLoop loop_; 146 MessageLoop loop_;
98 147
99 private: 148 private:
100 ScopedTempDir temp_dir_; 149 ScopedTempDir temp_dir_;
101 }; 150 };
102 151
103 TEST_F(ImportantFileWriterTest, Basic) { 152 TEST_F(ImportantFileWriterTest, Basic) {
104 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); 153 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get());
105 EXPECT_FALSE(PathExists(writer.path())); 154 EXPECT_FALSE(PathExists(writer.path()));
106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 155 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState());
156 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
107 writer.WriteNow(WrapUnique(new std::string("foo"))); 157 writer.WriteNow(WrapUnique(new std::string("foo")));
108 RunLoop().RunUntilIdle(); 158 RunLoop().RunUntilIdle();
109 159
110 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 160 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState());
161 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
111 ASSERT_TRUE(PathExists(writer.path())); 162 ASSERT_TRUE(PathExists(writer.path()));
112 EXPECT_EQ("foo", GetFileContent(writer.path())); 163 EXPECT_EQ("foo", GetFileContent(writer.path()));
113 } 164 }
114 165
115 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { 166 TEST_F(ImportantFileWriterTest, BasicWithWriteObservers) {
116 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); 167 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get());
117 EXPECT_FALSE(PathExists(writer.path())); 168 EXPECT_FALSE(PathExists(writer.path()));
118 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 169 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState());
170 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
119 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); 171 successful_write_observer_.ObserveNextSuccessfulWrite(&writer);
172 write_callback_observer_.ObserveNextWriteCallback(&writer);
120 writer.WriteNow(WrapUnique(new std::string("foo"))); 173 writer.WriteNow(WrapUnique(new std::string("foo")));
121 RunLoop().RunUntilIdle(); 174 RunLoop().RunUntilIdle();
122 175
123 // Confirm that the observer is invoked. 176 // Confirm that the observers are invoked.
124 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); 177 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState());
178 EXPECT_EQ(CALLED_WITH_SUCCESS,
179 write_callback_observer_.GetAndResetObservationState());
125 ASSERT_TRUE(PathExists(writer.path())); 180 ASSERT_TRUE(PathExists(writer.path()));
126 EXPECT_EQ("foo", GetFileContent(writer.path())); 181 EXPECT_EQ("foo", GetFileContent(writer.path()));
127 182
128 // Confirm that re-installing the observer works for another write. 183 // Confirm that re-installing the observers works for another write.
129 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 184 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState());
185 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
130 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); 186 successful_write_observer_.ObserveNextSuccessfulWrite(&writer);
187 write_callback_observer_.ObserveNextWriteCallback(&writer);
131 writer.WriteNow(WrapUnique(new std::string("bar"))); 188 writer.WriteNow(WrapUnique(new std::string("bar")));
132 RunLoop().RunUntilIdle(); 189 RunLoop().RunUntilIdle();
133 190
134 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); 191 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState());
192 EXPECT_EQ(CALLED_WITH_SUCCESS,
193 write_callback_observer_.GetAndResetObservationState());
135 ASSERT_TRUE(PathExists(writer.path())); 194 ASSERT_TRUE(PathExists(writer.path()));
136 EXPECT_EQ("bar", GetFileContent(writer.path())); 195 EXPECT_EQ("bar", GetFileContent(writer.path()));
137 196
138 // Confirm that writing again without re-installing the observer doesn't 197 // Confirm that writing again without re-installing the observers doesn't
139 // result in a notification. 198 // result in a notification.
140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 199 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState());
200 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
141 writer.WriteNow(WrapUnique(new std::string("baz"))); 201 writer.WriteNow(WrapUnique(new std::string("baz")));
142 RunLoop().RunUntilIdle(); 202 RunLoop().RunUntilIdle();
143 203
144 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 204 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState());
205 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
145 ASSERT_TRUE(PathExists(writer.path())); 206 ASSERT_TRUE(PathExists(writer.path()));
146 EXPECT_EQ("baz", GetFileContent(writer.path())); 207 EXPECT_EQ("baz", GetFileContent(writer.path()));
147 } 208 }
148 209
210 TEST_F(ImportantFileWriterTest, FailedWriteWithWriteObservers) {
211 // Use an invalid file path to get a FILE_ERROR_ACCESS_DENIED error when
212 // trying to write the file.
213 ImportantFileWriter writer(FilePath().AppendASCII("bad/../path"),
214 ThreadTaskRunnerHandle::Get());
215 EXPECT_FALSE(PathExists(writer.path()));
216 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState());
217 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
218 successful_write_observer_.ObserveNextSuccessfulWrite(&writer);
219 write_callback_observer_.ObserveNextWriteCallback(&writer);
220 writer.WriteNow(WrapUnique(new std::string("foo")));
221 RunLoop().RunUntilIdle();
222
223 // Confirm that the successful write observer was not invoked, and that the
224 // write observer was invoked with its boolean parameter set to false.
225 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState());
226 EXPECT_EQ(CALLED_WITH_ERROR,
227 write_callback_observer_.GetAndResetObservationState());
228 EXPECT_FALSE(PathExists(writer.path()));
229 }
230
149 TEST_F(ImportantFileWriterTest, ScheduleWrite) { 231 TEST_F(ImportantFileWriterTest, ScheduleWrite) {
150 ImportantFileWriter writer(file_, 232 ImportantFileWriter writer(file_,
151 ThreadTaskRunnerHandle::Get(), 233 ThreadTaskRunnerHandle::Get(),
152 TimeDelta::FromMilliseconds(25)); 234 TimeDelta::FromMilliseconds(25));
153 EXPECT_FALSE(writer.HasPendingWrite()); 235 EXPECT_FALSE(writer.HasPendingWrite());
154 DataSerializer serializer("foo"); 236 DataSerializer serializer("foo");
155 writer.ScheduleWrite(&serializer); 237 writer.ScheduleWrite(&serializer);
156 EXPECT_TRUE(writer.HasPendingWrite()); 238 EXPECT_TRUE(writer.HasPendingWrite());
157 ThreadTaskRunnerHandle::Get()->PostDelayedTask( 239 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
158 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), 240 FROM_HERE, MessageLoop::QuitWhenIdleClosure(),
(...skipping 30 matching lines...) Expand all
189 writer.ScheduleWrite(&baz); 271 writer.ScheduleWrite(&baz);
190 ThreadTaskRunnerHandle::Get()->PostDelayedTask( 272 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
191 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), 273 FROM_HERE, MessageLoop::QuitWhenIdleClosure(),
192 TimeDelta::FromMilliseconds(100)); 274 TimeDelta::FromMilliseconds(100));
193 RunLoop().Run(); 275 RunLoop().Run();
194 ASSERT_TRUE(PathExists(writer.path())); 276 ASSERT_TRUE(PathExists(writer.path()));
195 EXPECT_EQ("baz", GetFileContent(writer.path())); 277 EXPECT_EQ("baz", GetFileContent(writer.path()));
196 } 278 }
197 279
198 } // namespace base 280 } // namespace base
OLDNEW
« no previous file with comments | « base/files/important_file_writer.cc ('k') | chrome/browser/prefs/profile_pref_store_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698