Index: chromeos/disks/disk_mount_manager_unittest.cc |
diff --git a/chromeos/disks/disk_mount_manager_unittest.cc b/chromeos/disks/disk_mount_manager_unittest.cc |
index e104750629944de22db7f010562278d0808636f4..783944772aea734f51231d38253bcad26c4ed812 100644 |
--- a/chromeos/disks/disk_mount_manager_unittest.cc |
+++ b/chromeos/disks/disk_mount_manager_unittest.cc |
@@ -5,6 +5,8 @@ |
#include <stddef.h> |
#include <stdint.h> |
+#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.
|
+ |
#include "base/bind.h" |
#include "base/message_loop/message_loop.h" |
#include "chromeos/dbus/dbus_thread_manager.h" |
@@ -17,6 +19,8 @@ using chromeos::disks::DiskMountManager; |
using chromeos::CrosDisksClient; |
using chromeos::DBusThreadManager; |
using chromeos::FakeCrosDisksClient; |
+using chromeos::MountType; |
+using chromeos::disks::MountCondition; |
using testing::_; |
using testing::Field; |
using testing::InSequence; |
@@ -151,10 +155,12 @@ const TestMountPointInfo kTestMountPoints[] = { |
}, |
}; |
-// Mocks DiskMountManager observer. |
-class MockDiskMountManagerObserver : public DiskMountManager::Observer { |
+// Mocks DiskMountManager observer using Google Mock. |
+// TODO(yamaguchi): remove this class when all tests migrated to the new |
+// 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.
|
+class GmockDiskMountManagerObserver : public DiskMountManager::Observer { |
public: |
- virtual ~MockDiskMountManagerObserver() {} |
+ virtual ~GmockDiskMountManagerObserver() {} |
MOCK_METHOD2(OnDiskEvent, void(DiskMountManager::DiskEvent event, |
const DiskMountManager::Disk* disk)); |
@@ -170,6 +176,351 @@ class MockDiskMountManagerObserver : public DiskMountManager::Observer { |
const std::string& device_path)); |
}; |
+// 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.
|
+// OnDeviceEvent() |
+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.
|
+ 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 { |
+ std::stringstream ss; |
+ ss << "OnDeviceEvent(" << event_ << ", " << device_path_ << ")"; |
+ return ss.str(); |
+ } |
+ |
+ 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 { |
+ std::stringstream ss; |
+ ss << "OnDiskEvent(event=" << event_ |
+ << ", device_path=" << disk_->device_path() |
+ << ", mount_path=" << disk_->mount_path() << ")"; |
+ return ss.str(); |
+ } |
+ |
+ 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 { |
+ std::stringstream ss; |
+ ss << "OnFormatEvent(" << event_ << ", " << error_code_ << ", " |
+ << device_path_ << ")"; |
+ return ss.str(); |
+ } |
+ |
+ private: |
+ DiskMountManager::FormatEvent event_; |
+ chromeos::FormatError error_code_; |
+ std::string device_path_; |
+}; |
+ |
+// OnMountEvent() |
+class MountEvent { |
+ public: |
+ const DiskMountManager::MountEvent& GetEvent() const { return event_; } |
+ chromeos::MountError GetErrorCode() const { return error_code_; } |
+ const DiskMountManager::MountPointInfo& GetMountPoint() const { |
+ return mount_point_; |
+ } |
+ |
+ MountEvent() |
+ : mount_point_("", |
+ "", |
+ MountType::MOUNT_TYPE_INVALID, |
+ MountCondition::MOUNT_CONDITION_NONE) {} |
+ ~MountEvent() {} |
+ |
+ MountEvent(const MountEvent& other) |
+ : event_(other.event_), |
+ error_code_(other.error_code_), |
+ mount_point_(other.mount_point_), |
+ disk_(other.disk_) {} |
+ MountEvent(DiskMountManager::MountEvent event, |
+ chromeos::MountError error_code, |
+ const DiskMountManager::MountPointInfo& mount_point, |
+ std::shared_ptr<DiskMountManager::Disk> disk) |
+ : event_(event), |
+ error_code_(error_code), |
+ mount_point_(mount_point), |
+ disk_(disk) {} |
+ |
+ bool operator==(const MountEvent& other) const; |
+ |
+ std::string DebugString() const { |
+ std::stringstream ss; |
+ ss << "OnMountEvent(" << event_ << ", " << error_code_ << ", " |
+ << mount_point_.source_path << ", " << mount_point_.mount_path << ", " |
+ << mount_point_.mount_type << ", " << mount_point_.mount_condition |
+ << ")"; |
+ return ss.str(); |
+ } |
+ |
+ 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) |
+ : device(other.device), |
+ disk(other.disk), |
+ format(other.format), |
+ mount(other.mount) {} |
+ ~Event() {} |
+ } event_; |
+ ObserverEvent() {} |
+ |
+ public: |
+ enum ObserverEventType { |
+ DEVICE_EVENT, |
+ DISK_EVENT, |
+ FORMAT_EVENT, |
+ MOUNT_EVENT |
+ } type_; |
+ |
+ static ObserverEvent FromDeviceEvent(DeviceEvent event) { |
+ ObserverEvent result; |
+ result.type_ = DEVICE_EVENT; |
+ result.event_.device = event; |
+ return result; |
+ } |
+ |
+ static ObserverEvent FromDiskEvent(DiskEvent event) { |
+ ObserverEvent result; |
+ result.type_ = DISK_EVENT; |
+ result.event_.disk = event; |
+ return result; |
+ } |
+ |
+ static ObserverEvent FromFormatEvent(FormatEvent event) { |
+ ObserverEvent result; |
+ result.type_ = FORMAT_EVENT; |
+ result.event_.format = event; |
+ return result; |
+ } |
+ |
+ static ObserverEvent FromMountEvent(MountEvent event) { |
+ ObserverEvent result; |
+ result.type_ = MOUNT_EVENT; |
+ result.event_.mount = event; |
+ return result; |
+ } |
+ |
+ 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_; } |
+ |
+ std::string DebugString() const { |
+ switch (type_) { |
+ case DEVICE_EVENT: |
+ return event_.device.DebugString(); |
+ case DISK_EVENT: |
+ return event_.disk.DebugString(); |
+ case FORMAT_EVENT: |
+ return event_.format.DebugString(); |
+ case MOUNT_EVENT: |
+ return event_.mount.DebugString(); |
+ } |
+ return "(Unknown event 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) |
+ : manager_(manager) {} |
+ ~MockDiskMountManagerObserver() override {} |
+ |
+ // Mock notify methods. |
+ void OnDeviceEvent(DiskMountManager::DeviceEvent event, |
+ const std::string& device_path) override { |
+ events_.push_back( |
+ ObserverEvent::FromDeviceEvent(DeviceEvent(event, device_path))); |
+ } |
+ |
+ void OnDiskEvent(DiskMountManager::DiskEvent event, |
+ const DiskMountManager::Disk* disk) override { |
+ // Take a snapshot (copy) of the Disk object at the time of invocation for |
+ // later verification. |
+ events_.push_back(ObserverEvent::FromDiskEvent( |
+ DiskEvent(event, new DiskMountManager::Disk(*disk)))); |
+ } |
+ |
+ void OnFormatEvent(DiskMountManager::FormatEvent event, |
+ chromeos::FormatError error_code, |
+ const std::string& device_path) override { |
+ events_.push_back(ObserverEvent::FromFormatEvent( |
+ FormatEvent(event, error_code, device_path))); |
+ } |
+ |
+ void OnMountEvent( |
+ DiskMountManager::MountEvent event, |
+ chromeos::MountError error_code, |
+ const DiskMountManager::MountPointInfo& mount_point) override { |
+ // Take a snapshot (copy) of a Disk object at the time of invocation. |
+ // It can be verified later besides the arguments. |
+ events_.push_back(ObserverEvent::FromMountEvent(MountEvent( |
+ event, error_code, mount_point, |
+ std::shared_ptr<DiskMountManager::Disk>(new DiskMountManager::Disk( |
+ *manager_->disks().find(mount_point.source_path)->second))))); |
+ } |
+ |
+ // Get invocation history to be verified by testcases. |
+ DeviceEvent GetDeviceEvent(size_t index) { |
+ assert(events_.size() >= index + 1); |
+ assert(ObserverEvent::DEVICE_EVENT == events_.at(index).GetType()); |
+ return events_[index].GetEvent().device; |
+ } |
+ |
+ DiskEvent GetDiskEvent(size_t index) { |
+ 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.
|
+ assert(ObserverEvent::DISK_EVENT == events_.at(index).GetType()); |
+ return events_[index].GetEvent().disk; |
+ } |
+ |
+ FormatEvent GetFormatEvent(size_t index) { |
+ assert(events_.size() >= index + 1); |
+ assert(ObserverEvent::FORMAT_EVENT == events_.at(index).GetType()); |
+ return events_[index].GetEvent().format; |
+ } |
+ |
+ MountEvent GetMountEvent(size_t index) { |
+ assert(events_.size() >= index + 1); |
+ assert(ObserverEvent::MOUNT_EVENT == events_.at(index).GetType()); |
+ return events_[index].GetEvent().mount; |
+ } |
+ |
+ 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_; |
+}; |
+ |
+// Shift operators of ostream. |
+// Needed to print values in case of EXPECT_* failure in gtest. |
+std::ostream& operator<<(std::ostream& stream, |
+ const DeviceEvent& device_event) { |
+ return stream << device_event.DebugString(); |
+} |
+ |
+std::ostream& operator<<(std::ostream& stream, const DiskEvent& disk_event) { |
+ return stream << disk_event.DebugString(); |
+} |
+ |
+std::ostream& operator<<(std::ostream& stream, |
+ const FormatEvent& format_event) { |
+ return stream << format_event.DebugString(); |
+} |
+ |
+std::ostream& operator<<(std::ostream& stream, const MountEvent& mount_event) { |
+ return stream << mount_event.DebugString(); |
+} |
+ |
+std::ostream& operator<<(std::ostream& stream, const ObserverEvent& event) { |
+ return stream << event.DebugString(); |
+} |
+ |
// Expect |is_read_only| value of a disk object keyed by |source_path|. |
void ExpectDiskReadOnly(const DiskMountManager* manager, |
const std::string& source_path, |
@@ -264,7 +615,7 @@ class DiskMountManagerTest : public testing::Test { |
protected: |
chromeos::FakeCrosDisksClient* fake_cros_disks_client_; |
- MockDiskMountManagerObserver observer_; |
+ GmockDiskMountManagerObserver observer_; |
base::MessageLoopForUI message_loop_; |
}; |