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

Unified Diff: chromeos/disks/mock_disk_mount_manager_observer.h

Issue 2292473002: Add a mock class for DiskMountManagerObserver. disk_mount_manager_observer_unittests will be rewrit… (Closed)
Patch Set: Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/chromeos.gyp ('k') | chromeos/disks/mock_disk_mount_manager_observer.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/disks/mock_disk_mount_manager_observer.h
diff --git a/chromeos/disks/mock_disk_mount_manager_observer.h b/chromeos/disks/mock_disk_mount_manager_observer.h
new file mode 100644
index 0000000000000000000000000000000000000000..0f5ae47ea09aaab8c47a28658ee5976621636ed6
--- /dev/null
+++ b/chromeos/disks/mock_disk_mount_manager_observer.h
@@ -0,0 +1,231 @@
+// Copyright (c) 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_DISKS_MOCK_DISK_MOUNT_MANAGER_OBSERVER_H_
+#define CHROMEOS_DISKS_MOCK_DISK_MOUNT_MANAGER_OBSERVER_H_
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "chromeos/disks/disk_mount_manager.h"
+using chromeos::disks::DiskMountManager;
+
+namespace chromeos {
+namespace disks {
+
+// Event classes which represents each invocation of functions in |Observer|.
+// OnDeviceEvent()
+class DeviceEvent {
+ public:
+ DeviceEvent() {}
+ ~DeviceEvent() {}
+
+ DeviceEvent(DiskMountManager::DeviceEvent event,
+ const std::string& device_path)
+ : event_(event), device_path_(device_path) {}
+
+ DeviceEvent(const DeviceEvent& other)
+ : event_(other.event_), device_path_(other.device_path_) {}
+
+ bool operator==(const DeviceEvent& other) const {
+ return event_ == other.event_ && device_path_ == other.device_path_;
+ }
+
+ DiskMountManager::DeviceEvent GetEvent() const { return event_; }
+
+ const std::string& GetDevicePath() const { return device_path_; }
+
+ std::string DebugString() const;
+
+ private:
+ DiskMountManager::DeviceEvent event_;
+ std::string device_path_;
+};
+
+// OnDiskEvent()
+class DiskEvent {
+ public:
+ DiskEvent() {}
+
+ DiskEvent(DiskMountManager::DiskEvent event,
+ const DiskMountManager::Disk* disk)
+ : event_(event), disk_(disk) {}
+
+ DiskEvent(const DiskEvent& other)
+ : event_(other.event_), disk_(other.disk_) {}
+
+ DiskEvent& operator=(const DiskEvent& other) {
+ event_ = other.event_;
+ disk_ = other.disk_;
+ return *this;
+ }
+
+ bool operator==(const DiskEvent& other) const {
+ return event_ == other.event_ && disk_ == other.disk_;
+ }
+
+ std::string DebugString() const;
+
+ private:
+ DiskMountManager::DiskEvent event_;
+ const DiskMountManager::Disk* disk_;
+};
+
+// OnFormatEvent()
+class FormatEvent {
+ public:
+ FormatEvent() {}
+ FormatEvent(DiskMountManager::FormatEvent event,
+ chromeos::FormatError error_code,
+ const std::string& device_path)
+ : event_(event), error_code_(error_code), device_path_(device_path) {}
+
+ bool operator==(const FormatEvent& other) const {
+ return event_ == other.event_ && error_code_ == other.error_code_ &&
+ device_path_ == other.device_path_;
+ }
+
+ DiskMountManager::FormatEvent GetEvent() const { return event_; }
+
+ chromeos::FormatError GetErrorCode() const { return error_code_; }
+
+ const std::string& GetDevicePath() const { return device_path_; }
+
+ std::string DebugString() const;
+
+ private:
+ DiskMountManager::FormatEvent event_;
+ chromeos::FormatError error_code_;
+ std::string device_path_;
+};
+
+// OnMountEvent()
+class MountEvent {
+ public:
+ DiskMountManager::MountEvent GetEvent() { return event_; }
+ chromeos::MountError GetErrorCode() { return error_code_; }
+ const DiskMountManager::MountPointInfo& GetMountPoint() {
+ return mount_point_;
+ }
+ MountEvent();
+ MountEvent(const MountEvent& other);
+ MountEvent(DiskMountManager::MountEvent event,
+ chromeos::MountError error_code,
+ const DiskMountManager::MountPointInfo& mount_point,
+ std::shared_ptr<DiskMountManager::Disk> disk);
+ ~MountEvent();
+ bool operator==(const MountEvent& other) const;
+ std::string DebugString() const;
+
+ private:
+ DiskMountManager::MountEvent event_;
+ chromeos::MountError error_code_;
+ DiskMountManager::MountPointInfo mount_point_;
+
+ // Not passed to callback, but read by handlers. So it's captured upon
+ // callback.
+ std::shared_ptr<DiskMountManager::Disk> disk_;
+};
+
+// Represents every event notified to |Observer|.
+class ObserverEvent {
+ private:
+ struct Event {
+ DeviceEvent device;
+ DiskEvent disk;
+ FormatEvent format;
+ MountEvent mount;
+ Event();
+ Event(const Event& other);
+ ~Event() {}
+ } event_;
+ ObserverEvent() {}
+
+ public:
+ enum ObserverEventType {
+ DEVICE_EVENT,
+ DISK_EVENT,
+ FORMAT_EVENT,
+ MOUNT_EVENT
+ } type_;
+
+ static ObserverEvent FromDeviceEvent(DeviceEvent event);
+ static ObserverEvent FromDiskEvent(DiskEvent event);
+ static ObserverEvent FromFormatEvent(FormatEvent event);
+ static ObserverEvent FromMountEvent(MountEvent event);
+
+ ObserverEvent(const ObserverEvent& other) : type_(other.type_) {
+ switch (other.type_) {
+ case DEVICE_EVENT:
+ event_.device = other.event_.device;
+ break;
+ case DISK_EVENT:
+ event_.disk = other.event_.disk;
+ break;
+ case FORMAT_EVENT:
+ event_.format = other.event_.format;
+ break;
+ case MOUNT_EVENT:
+ event_.mount = other.event_.mount;
+ break;
+ }
+ }
+
+ Event GetEvent() { return event_; }
+
+ ObserverEventType GetType() { return type_; }
+};
+
+// A mock |Observer| class which records all invocation of the methods invoked
+// from DiskMountManager and all the arguments passed to them.
+class MockDiskMountManagerObserver : public DiskMountManager::Observer {
+ public:
+ MockDiskMountManagerObserver(const DiskMountManager* manager);
+ ~MockDiskMountManagerObserver() override;
+
+ // Mock notify methods.
+ void OnDeviceEvent(DiskMountManager::DeviceEvent event,
+ const std::string& device_path) override;
+
+ void OnDiskEvent(DiskMountManager::DiskEvent event,
+ const DiskMountManager::Disk* disk) override;
+
+ void OnMountEvent(
+ DiskMountManager::MountEvent event,
+ chromeos::MountError error_code,
+ const DiskMountManager::MountPointInfo& mount_point) override;
+
+ void OnFormatEvent(DiskMountManager::FormatEvent event,
+ chromeos::FormatError error_code,
+ const std::string& device_path) override;
+
+ // Get invocation history to be verified by testcases.
+ DeviceEvent GetDeviceEvent(size_t index);
yamaguchi 2016/08/30 02:20:10 It'd be easier to have tests get and verify Observ
yamaguchi 2016/08/30 04:39:50 In the current unit test, MountEvent is not examin
+ FormatEvent GetFormatEvent(size_t index);
+ DiskEvent GetDiskEvent(size_t index);
+ MountEvent GetMountEvent(size_t index);
+
+ size_t GetEventCount() { return events_.size(); }
+
+ ObserverEvent GetEvent(size_t index) { return events_.at(index); }
+
+ private:
+ // Pointer to the manager object to which this |Observer| is registered.
+ const DiskMountManager* manager_;
+
+ // Records all invocations.
+ std::vector<ObserverEvent> events_;
+};
+
+// Needed to print values in case of EXPECT_* failure in gtest.
+std::ostream& operator<<(std::ostream& stream, const DeviceEvent& device_event);
+std::ostream& operator<<(std::ostream& stream, const DiskEvent& disk_event);
+std::ostream& operator<<(std::ostream& stream, const FormatEvent& format_event);
+std::ostream& operator<<(std::ostream& stream, const MountEvent& mount_event);
+
+} // namespace disks
+} // namespace chromeos
+
+#endif // CHROMEOS_DISKS_MOCK_DISK_MOUNT_MANAGER_OBSERVER_H_
« no previous file with comments | « chromeos/chromeos.gyp ('k') | chromeos/disks/mock_disk_mount_manager_observer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698