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

Side by Side Diff: chromeos/disks/disk_mount_manager_unittest.cc

Issue 2292473002: Add a mock class for DiskMountManagerObserver. disk_mount_manager_observer_unittests will be rewrit… (Closed)
Patch Set: Add TODO comment. 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 | « no previous file | no next file » | 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <sstream>
satorux1 2016/08/30 07:27:29 Streams are not recommended. let's use StringPrint
yamaguchi 2016/08/30 09:49:18 Done.
9
8 #include "base/bind.h" 10 #include "base/bind.h"
9 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
10 #include "chromeos/dbus/dbus_thread_manager.h" 12 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/fake_cros_disks_client.h" 13 #include "chromeos/dbus/fake_cros_disks_client.h"
12 #include "chromeos/disks/disk_mount_manager.h" 14 #include "chromeos/disks/disk_mount_manager.h"
13 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 using chromeos::disks::DiskMountManager; 18 using chromeos::disks::DiskMountManager;
17 using chromeos::CrosDisksClient; 19 using chromeos::CrosDisksClient;
18 using chromeos::DBusThreadManager; 20 using chromeos::DBusThreadManager;
19 using chromeos::FakeCrosDisksClient; 21 using chromeos::FakeCrosDisksClient;
22 using chromeos::MountType;
23 using chromeos::disks::MountCondition;
20 using testing::_; 24 using testing::_;
21 using testing::Field; 25 using testing::Field;
22 using testing::InSequence; 26 using testing::InSequence;
23 using testing::InvokeWithoutArgs; 27 using testing::InvokeWithoutArgs;
24 28
25 namespace { 29 namespace {
26 30
27 const char kReadOnlyMountpath[] = "/device/read_only_mount_path"; 31 const char kReadOnlyMountpath[] = "/device/read_only_mount_path";
28 const char kReadOnlyDeviceSource[] = "/device/read_only_source_path"; 32 const char kReadOnlyDeviceSource[] = "/device/read_only_source_path";
29 33
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 chromeos::disks::MOUNT_CONDITION_NONE 148 chromeos::disks::MOUNT_CONDITION_NONE
145 }, 149 },
146 { 150 {
147 kReadOnlyDeviceSource, 151 kReadOnlyDeviceSource,
148 kReadOnlyMountpath, 152 kReadOnlyMountpath,
149 chromeos::MOUNT_TYPE_DEVICE, 153 chromeos::MOUNT_TYPE_DEVICE,
150 chromeos::disks::MOUNT_CONDITION_NONE 154 chromeos::disks::MOUNT_CONDITION_NONE
151 }, 155 },
152 }; 156 };
153 157
154 // Mocks DiskMountManager observer. 158 // Mocks DiskMountManager observer using Google Mock.
155 class MockDiskMountManagerObserver : public DiskMountManager::Observer { 159 // TODO(yamaguchi): remove this class when all tests migrated to the new
160 // MockDiskMountManagerObserver.
satorux1 2016/08/30 07:27:29 Can you update the tests in the same patch? That'd
yamaguchi 2016/08/30 09:49:18 Updated the tests.
161 class GmockDiskMountManagerObserver : public DiskMountManager::Observer {
156 public: 162 public:
157 virtual ~MockDiskMountManagerObserver() {} 163 virtual ~GmockDiskMountManagerObserver() {}
158 164
159 MOCK_METHOD2(OnDiskEvent, void(DiskMountManager::DiskEvent event, 165 MOCK_METHOD2(OnDiskEvent, void(DiskMountManager::DiskEvent event,
160 const DiskMountManager::Disk* disk)); 166 const DiskMountManager::Disk* disk));
161 MOCK_METHOD2(OnDeviceEvent, void(DiskMountManager::DeviceEvent event, 167 MOCK_METHOD2(OnDeviceEvent, void(DiskMountManager::DeviceEvent event,
162 const std::string& device_path)); 168 const std::string& device_path));
163 MOCK_METHOD3(OnMountEvent, 169 MOCK_METHOD3(OnMountEvent,
164 void(DiskMountManager::MountEvent event, 170 void(DiskMountManager::MountEvent event,
165 chromeos::MountError error_code, 171 chromeos::MountError error_code,
166 const DiskMountManager::MountPointInfo& mount_point)); 172 const DiskMountManager::MountPointInfo& mount_point));
167 MOCK_METHOD3(OnFormatEvent, 173 MOCK_METHOD3(OnFormatEvent,
168 void(DiskMountManager::FormatEvent event, 174 void(DiskMountManager::FormatEvent event,
169 chromeos::FormatError error_code, 175 chromeos::FormatError error_code,
170 const std::string& device_path)); 176 const std::string& device_path));
171 }; 177 };
172 178
179 // Event classes which represents each invocation of functions in |Observer|.
satorux1 2016/08/30 07:27:29 represents -> represent
yamaguchi 2016/08/30 09:49:18 Done.
180 // OnDeviceEvent()
181 class DeviceEvent {
satorux1 2016/08/30 07:27:29 can this be just a struct? ditto with other simila
yamaguchi 2016/08/30 09:49:18 Done.
182 public:
183 DeviceEvent() {}
184 ~DeviceEvent() {}
185
186 DeviceEvent(DiskMountManager::DeviceEvent event,
187 const std::string& device_path)
188 : event_(event), device_path_(device_path) {}
189
190 DeviceEvent(const DeviceEvent& other)
191 : event_(other.event_), device_path_(other.device_path_) {}
192
193 bool operator==(const DeviceEvent& other) const {
194 return event_ == other.event_ && device_path_ == other.device_path_;
195 }
196
197 DiskMountManager::DeviceEvent GetEvent() const { return event_; }
198
199 const std::string& GetDevicePath() const { return device_path_; }
200
201 std::string DebugString() const {
202 std::stringstream ss;
203 ss << "OnDeviceEvent(" << event_ << ", " << device_path_ << ")";
204 return ss.str();
205 }
206
207 private:
208 DiskMountManager::DeviceEvent event_;
209 std::string device_path_;
210 };
211
212 // OnDiskEvent()
213 class DiskEvent {
214 public:
215 DiskEvent() {}
216
217 DiskEvent(DiskMountManager::DiskEvent event,
218 const DiskMountManager::Disk* disk)
219 : event_(event), disk_(disk) {}
220
221 DiskEvent(const DiskEvent& other)
222 : event_(other.event_), disk_(other.disk_) {}
223
224 DiskEvent& operator=(const DiskEvent& other) {
225 event_ = other.event_;
226 disk_ = other.disk_;
227 return *this;
228 }
229
230 bool operator==(const DiskEvent& other) const {
231 return event_ == other.event_ && disk_ == other.disk_;
232 }
233
234 std::string DebugString() const {
235 std::stringstream ss;
236 ss << "OnDiskEvent(event=" << event_
237 << ", device_path=" << disk_->device_path()
238 << ", mount_path=" << disk_->mount_path() << ")";
239 return ss.str();
240 }
241
242 private:
243 DiskMountManager::DiskEvent event_;
244 const DiskMountManager::Disk* disk_;
245 };
246
247 // OnFormatEvent()
248 class FormatEvent {
249 public:
250 FormatEvent() {}
251 FormatEvent(DiskMountManager::FormatEvent event,
252 chromeos::FormatError error_code,
253 const std::string& device_path)
254 : event_(event), error_code_(error_code), device_path_(device_path) {}
255
256 bool operator==(const FormatEvent& other) const {
257 return event_ == other.event_ && error_code_ == other.error_code_ &&
258 device_path_ == other.device_path_;
259 }
260
261 DiskMountManager::FormatEvent GetEvent() const { return event_; }
262
263 chromeos::FormatError GetErrorCode() const { return error_code_; }
264
265 const std::string& GetDevicePath() const { return device_path_; }
266
267 std::string DebugString() const {
268 std::stringstream ss;
269 ss << "OnFormatEvent(" << event_ << ", " << error_code_ << ", "
270 << device_path_ << ")";
271 return ss.str();
272 }
273
274 private:
275 DiskMountManager::FormatEvent event_;
276 chromeos::FormatError error_code_;
277 std::string device_path_;
278 };
279
280 // OnMountEvent()
281 class MountEvent {
282 public:
283 const DiskMountManager::MountEvent& GetEvent() const { return event_; }
284 chromeos::MountError GetErrorCode() const { return error_code_; }
285 const DiskMountManager::MountPointInfo& GetMountPoint() const {
286 return mount_point_;
287 }
288
289 MountEvent()
290 : mount_point_("",
291 "",
292 MountType::MOUNT_TYPE_INVALID,
293 MountCondition::MOUNT_CONDITION_NONE) {}
294 ~MountEvent() {}
295
296 MountEvent(const MountEvent& other)
297 : event_(other.event_),
298 error_code_(other.error_code_),
299 mount_point_(other.mount_point_),
300 disk_(other.disk_) {}
301 MountEvent(DiskMountManager::MountEvent event,
302 chromeos::MountError error_code,
303 const DiskMountManager::MountPointInfo& mount_point,
304 std::shared_ptr<DiskMountManager::Disk> disk)
305 : event_(event),
306 error_code_(error_code),
307 mount_point_(mount_point),
308 disk_(disk) {}
309
310 bool operator==(const MountEvent& other) const;
311
312 std::string DebugString() const {
313 std::stringstream ss;
314 ss << "OnMountEvent(" << event_ << ", " << error_code_ << ", "
315 << mount_point_.source_path << ", " << mount_point_.mount_path << ", "
316 << mount_point_.mount_type << ", " << mount_point_.mount_condition
317 << ")";
318 return ss.str();
319 }
320
321 private:
322 DiskMountManager::MountEvent event_;
323 chromeos::MountError error_code_;
324 DiskMountManager::MountPointInfo mount_point_;
325
326 // Not passed to callback, but read by handlers. So it's captured upon
327 // callback.
328 std::shared_ptr<DiskMountManager::Disk> disk_;
329 };
330
331 // Represents every event notified to |Observer|.
332 class ObserverEvent {
333 private:
334 struct Event {
335 DeviceEvent device;
336 DiskEvent disk;
337 FormatEvent format;
338 MountEvent mount;
339 Event() {}
340 Event(const Event& other)
341 : device(other.device),
342 disk(other.disk),
343 format(other.format),
344 mount(other.mount) {}
345 ~Event() {}
346 } event_;
347 ObserverEvent() {}
348
349 public:
350 enum ObserverEventType {
351 DEVICE_EVENT,
352 DISK_EVENT,
353 FORMAT_EVENT,
354 MOUNT_EVENT
355 } type_;
356
357 static ObserverEvent FromDeviceEvent(DeviceEvent event) {
358 ObserverEvent result;
359 result.type_ = DEVICE_EVENT;
360 result.event_.device = event;
361 return result;
362 }
363
364 static ObserverEvent FromDiskEvent(DiskEvent event) {
365 ObserverEvent result;
366 result.type_ = DISK_EVENT;
367 result.event_.disk = event;
368 return result;
369 }
370
371 static ObserverEvent FromFormatEvent(FormatEvent event) {
372 ObserverEvent result;
373 result.type_ = FORMAT_EVENT;
374 result.event_.format = event;
375 return result;
376 }
377
378 static ObserverEvent FromMountEvent(MountEvent event) {
379 ObserverEvent result;
380 result.type_ = MOUNT_EVENT;
381 result.event_.mount = event;
382 return result;
383 }
384
385 ObserverEvent(const ObserverEvent& other) : type_(other.type_) {
386 switch (other.type_) {
387 case DEVICE_EVENT:
388 event_.device = other.event_.device;
389 break;
390 case DISK_EVENT:
391 event_.disk = other.event_.disk;
392 break;
393 case FORMAT_EVENT:
394 event_.format = other.event_.format;
395 break;
396 case MOUNT_EVENT:
397 event_.mount = other.event_.mount;
398 break;
399 }
400 }
401
402 Event GetEvent() { return event_; }
403
404 ObserverEventType GetType() { return type_; }
405
406 std::string DebugString() const {
407 switch (type_) {
408 case DEVICE_EVENT:
409 return event_.device.DebugString();
410 case DISK_EVENT:
411 return event_.disk.DebugString();
412 case FORMAT_EVENT:
413 return event_.format.DebugString();
414 case MOUNT_EVENT:
415 return event_.mount.DebugString();
416 }
417 return "(Unknown event type)";
418 }
419 };
420
421 // A mock |Observer| class which records all invocation of the methods invoked
422 // from DiskMountManager and all the arguments passed to them.
423 class MockDiskMountManagerObserver : public DiskMountManager::Observer {
424 public:
425 MockDiskMountManagerObserver(const DiskMountManager* manager)
426 : manager_(manager) {}
427 ~MockDiskMountManagerObserver() override {}
428
429 // Mock notify methods.
430 void OnDeviceEvent(DiskMountManager::DeviceEvent event,
431 const std::string& device_path) override {
432 events_.push_back(
433 ObserverEvent::FromDeviceEvent(DeviceEvent(event, device_path)));
434 }
435
436 void OnDiskEvent(DiskMountManager::DiskEvent event,
437 const DiskMountManager::Disk* disk) override {
438 // Take a snapshot (copy) of the Disk object at the time of invocation for
439 // later verification.
440 events_.push_back(ObserverEvent::FromDiskEvent(
441 DiskEvent(event, new DiskMountManager::Disk(*disk))));
442 }
443
444 void OnFormatEvent(DiskMountManager::FormatEvent event,
445 chromeos::FormatError error_code,
446 const std::string& device_path) override {
447 events_.push_back(ObserverEvent::FromFormatEvent(
448 FormatEvent(event, error_code, device_path)));
449 }
450
451 void OnMountEvent(
452 DiskMountManager::MountEvent event,
453 chromeos::MountError error_code,
454 const DiskMountManager::MountPointInfo& mount_point) override {
455 // Take a snapshot (copy) of a Disk object at the time of invocation.
456 // It can be verified later besides the arguments.
457 events_.push_back(ObserverEvent::FromMountEvent(MountEvent(
458 event, error_code, mount_point,
459 std::shared_ptr<DiskMountManager::Disk>(new DiskMountManager::Disk(
460 *manager_->disks().find(mount_point.source_path)->second)))));
461 }
462
463 // Get invocation history to be verified by testcases.
464 DeviceEvent GetDeviceEvent(size_t index) {
465 assert(events_.size() >= index + 1);
466 assert(ObserverEvent::DEVICE_EVENT == events_.at(index).GetType());
467 return events_[index].GetEvent().device;
468 }
469
470 DiskEvent GetDiskEvent(size_t index) {
471 assert(events_.size() >= index + 1);
satorux1 2016/08/30 07:27:29 assert -> DCHECK
satorux1 2016/08/30 07:28:06 for this casek DCHECK_GE
yamaguchi 2016/08/30 09:49:18 Done.
yamaguchi 2016/08/30 09:49:18 Done.
472 assert(ObserverEvent::DISK_EVENT == events_.at(index).GetType());
473 return events_[index].GetEvent().disk;
474 }
475
476 FormatEvent GetFormatEvent(size_t index) {
477 assert(events_.size() >= index + 1);
478 assert(ObserverEvent::FORMAT_EVENT == events_.at(index).GetType());
479 return events_[index].GetEvent().format;
480 }
481
482 MountEvent GetMountEvent(size_t index) {
483 assert(events_.size() >= index + 1);
484 assert(ObserverEvent::MOUNT_EVENT == events_.at(index).GetType());
485 return events_[index].GetEvent().mount;
486 }
487
488 size_t GetEventCount() { return events_.size(); }
489
490 ObserverEvent GetEvent(size_t index) { return events_.at(index); }
491
492 private:
493 // Pointer to the manager object to which this |Observer| is registered.
494 const DiskMountManager* manager_;
495
496 // Records all invocations.
497 std::vector<ObserverEvent> events_;
498 };
499
500 // Shift operators of ostream.
501 // Needed to print values in case of EXPECT_* failure in gtest.
502 std::ostream& operator<<(std::ostream& stream,
503 const DeviceEvent& device_event) {
504 return stream << device_event.DebugString();
505 }
506
507 std::ostream& operator<<(std::ostream& stream, const DiskEvent& disk_event) {
508 return stream << disk_event.DebugString();
509 }
510
511 std::ostream& operator<<(std::ostream& stream,
512 const FormatEvent& format_event) {
513 return stream << format_event.DebugString();
514 }
515
516 std::ostream& operator<<(std::ostream& stream, const MountEvent& mount_event) {
517 return stream << mount_event.DebugString();
518 }
519
520 std::ostream& operator<<(std::ostream& stream, const ObserverEvent& event) {
521 return stream << event.DebugString();
522 }
523
173 // Expect |is_read_only| value of a disk object keyed by |source_path|. 524 // Expect |is_read_only| value of a disk object keyed by |source_path|.
174 void ExpectDiskReadOnly(const DiskMountManager* manager, 525 void ExpectDiskReadOnly(const DiskMountManager* manager,
175 const std::string& source_path, 526 const std::string& source_path,
176 bool expected) { 527 bool expected) {
177 EXPECT_EQ(expected, 528 EXPECT_EQ(expected,
178 manager->disks().find(source_path)->second->is_read_only()); 529 manager->disks().find(source_path)->second->is_read_only());
179 } 530 }
180 531
181 class DiskMountManagerTest : public testing::Test { 532 class DiskMountManagerTest : public testing::Test {
182 public: 533 public:
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 // expected that the corresponding disk is already added). 608 // expected that the corresponding disk is already added).
258 for (size_t i = 0; i < arraysize(kTestDisks); i++) 609 for (size_t i = 0; i < arraysize(kTestDisks); i++)
259 AddTestDisk(kTestDisks[i]); 610 AddTestDisk(kTestDisks[i]);
260 611
261 for (size_t i = 0; i < arraysize(kTestMountPoints); i++) 612 for (size_t i = 0; i < arraysize(kTestMountPoints); i++)
262 AddTestMountPoint(kTestMountPoints[i]); 613 AddTestMountPoint(kTestMountPoints[i]);
263 } 614 }
264 615
265 protected: 616 protected:
266 chromeos::FakeCrosDisksClient* fake_cros_disks_client_; 617 chromeos::FakeCrosDisksClient* fake_cros_disks_client_;
267 MockDiskMountManagerObserver observer_; 618 GmockDiskMountManagerObserver observer_;
268 base::MessageLoopForUI message_loop_; 619 base::MessageLoopForUI message_loop_;
269 }; 620 };
270 621
271 // Tests that the observer gets notified on attempt to format non existent mount 622 // Tests that the observer gets notified on attempt to format non existent mount
272 // point. 623 // point.
273 TEST_F(DiskMountManagerTest, Format_NotMounted) { 624 TEST_F(DiskMountManagerTest, Format_NotMounted) {
274 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED, 625 EXPECT_CALL(observer_, OnFormatEvent(DiskMountManager::FORMAT_COMPLETED,
275 chromeos::FORMAT_ERROR_UNKNOWN, 626 chromeos::FORMAT_ERROR_UNKNOWN,
276 "/mount/non_existent")) 627 "/mount/non_existent"))
277 .Times(1); 628 .Times(1);
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 kMountPath2); 1048 kMountPath2);
698 1049
699 const DiskMountManager::DiskMap& disks = manager->disks(); 1050 const DiskMountManager::DiskMap& disks = manager->disks();
700 ASSERT_GT(disks.count(kSourcePath1), 0U); 1051 ASSERT_GT(disks.count(kSourcePath1), 0U);
701 EXPECT_FALSE(disks.find(kSourcePath1)->second->is_read_only()); 1052 EXPECT_FALSE(disks.find(kSourcePath1)->second->is_read_only());
702 ASSERT_GT(disks.count(kSourcePath2), 0U); 1053 ASSERT_GT(disks.count(kSourcePath2), 0U);
703 EXPECT_TRUE(disks.find(kSourcePath2)->second->is_read_only()); 1054 EXPECT_TRUE(disks.find(kSourcePath2)->second->is_read_only());
704 } 1055 }
705 1056
706 } // namespace 1057 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698