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

Side by Side 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, 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 | « chromeos/chromeos.gyp ('k') | chromeos/disks/mock_disk_mount_manager_observer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROMEOS_DISKS_MOCK_DISK_MOUNT_MANAGER_OBSERVER_H_
6 #define CHROMEOS_DISKS_MOCK_DISK_MOUNT_MANAGER_OBSERVER_H_
7
8 #include <memory>
9 #include <string>
10 #include <vector>
11
12 #include "chromeos/disks/disk_mount_manager.h"
13 using chromeos::disks::DiskMountManager;
14
15 namespace chromeos {
16 namespace disks {
17
18 // Event classes which represents each invocation of functions in |Observer|.
19 // OnDeviceEvent()
20 class DeviceEvent {
21 public:
22 DeviceEvent() {}
23 ~DeviceEvent() {}
24
25 DeviceEvent(DiskMountManager::DeviceEvent event,
26 const std::string& device_path)
27 : event_(event), device_path_(device_path) {}
28
29 DeviceEvent(const DeviceEvent& other)
30 : event_(other.event_), device_path_(other.device_path_) {}
31
32 bool operator==(const DeviceEvent& other) const {
33 return event_ == other.event_ && device_path_ == other.device_path_;
34 }
35
36 DiskMountManager::DeviceEvent GetEvent() const { return event_; }
37
38 const std::string& GetDevicePath() const { return device_path_; }
39
40 std::string DebugString() const;
41
42 private:
43 DiskMountManager::DeviceEvent event_;
44 std::string device_path_;
45 };
46
47 // OnDiskEvent()
48 class DiskEvent {
49 public:
50 DiskEvent() {}
51
52 DiskEvent(DiskMountManager::DiskEvent event,
53 const DiskMountManager::Disk* disk)
54 : event_(event), disk_(disk) {}
55
56 DiskEvent(const DiskEvent& other)
57 : event_(other.event_), disk_(other.disk_) {}
58
59 DiskEvent& operator=(const DiskEvent& other) {
60 event_ = other.event_;
61 disk_ = other.disk_;
62 return *this;
63 }
64
65 bool operator==(const DiskEvent& other) const {
66 return event_ == other.event_ && disk_ == other.disk_;
67 }
68
69 std::string DebugString() const;
70
71 private:
72 DiskMountManager::DiskEvent event_;
73 const DiskMountManager::Disk* disk_;
74 };
75
76 // OnFormatEvent()
77 class FormatEvent {
78 public:
79 FormatEvent() {}
80 FormatEvent(DiskMountManager::FormatEvent event,
81 chromeos::FormatError error_code,
82 const std::string& device_path)
83 : event_(event), error_code_(error_code), device_path_(device_path) {}
84
85 bool operator==(const FormatEvent& other) const {
86 return event_ == other.event_ && error_code_ == other.error_code_ &&
87 device_path_ == other.device_path_;
88 }
89
90 DiskMountManager::FormatEvent GetEvent() const { return event_; }
91
92 chromeos::FormatError GetErrorCode() const { return error_code_; }
93
94 const std::string& GetDevicePath() const { return device_path_; }
95
96 std::string DebugString() const;
97
98 private:
99 DiskMountManager::FormatEvent event_;
100 chromeos::FormatError error_code_;
101 std::string device_path_;
102 };
103
104 // OnMountEvent()
105 class MountEvent {
106 public:
107 DiskMountManager::MountEvent GetEvent() { return event_; }
108 chromeos::MountError GetErrorCode() { return error_code_; }
109 const DiskMountManager::MountPointInfo& GetMountPoint() {
110 return mount_point_;
111 }
112 MountEvent();
113 MountEvent(const MountEvent& other);
114 MountEvent(DiskMountManager::MountEvent event,
115 chromeos::MountError error_code,
116 const DiskMountManager::MountPointInfo& mount_point,
117 std::shared_ptr<DiskMountManager::Disk> disk);
118 ~MountEvent();
119 bool operator==(const MountEvent& other) const;
120 std::string DebugString() const;
121
122 private:
123 DiskMountManager::MountEvent event_;
124 chromeos::MountError error_code_;
125 DiskMountManager::MountPointInfo mount_point_;
126
127 // Not passed to callback, but read by handlers. So it's captured upon
128 // callback.
129 std::shared_ptr<DiskMountManager::Disk> disk_;
130 };
131
132 // Represents every event notified to |Observer|.
133 class ObserverEvent {
134 private:
135 struct Event {
136 DeviceEvent device;
137 DiskEvent disk;
138 FormatEvent format;
139 MountEvent mount;
140 Event();
141 Event(const Event& other);
142 ~Event() {}
143 } event_;
144 ObserverEvent() {}
145
146 public:
147 enum ObserverEventType {
148 DEVICE_EVENT,
149 DISK_EVENT,
150 FORMAT_EVENT,
151 MOUNT_EVENT
152 } type_;
153
154 static ObserverEvent FromDeviceEvent(DeviceEvent event);
155 static ObserverEvent FromDiskEvent(DiskEvent event);
156 static ObserverEvent FromFormatEvent(FormatEvent event);
157 static ObserverEvent FromMountEvent(MountEvent event);
158
159 ObserverEvent(const ObserverEvent& other) : type_(other.type_) {
160 switch (other.type_) {
161 case DEVICE_EVENT:
162 event_.device = other.event_.device;
163 break;
164 case DISK_EVENT:
165 event_.disk = other.event_.disk;
166 break;
167 case FORMAT_EVENT:
168 event_.format = other.event_.format;
169 break;
170 case MOUNT_EVENT:
171 event_.mount = other.event_.mount;
172 break;
173 }
174 }
175
176 Event GetEvent() { return event_; }
177
178 ObserverEventType GetType() { return type_; }
179 };
180
181 // A mock |Observer| class which records all invocation of the methods invoked
182 // from DiskMountManager and all the arguments passed to them.
183 class MockDiskMountManagerObserver : public DiskMountManager::Observer {
184 public:
185 MockDiskMountManagerObserver(const DiskMountManager* manager);
186 ~MockDiskMountManagerObserver() override;
187
188 // Mock notify methods.
189 void OnDeviceEvent(DiskMountManager::DeviceEvent event,
190 const std::string& device_path) override;
191
192 void OnDiskEvent(DiskMountManager::DiskEvent event,
193 const DiskMountManager::Disk* disk) override;
194
195 void OnMountEvent(
196 DiskMountManager::MountEvent event,
197 chromeos::MountError error_code,
198 const DiskMountManager::MountPointInfo& mount_point) override;
199
200 void OnFormatEvent(DiskMountManager::FormatEvent event,
201 chromeos::FormatError error_code,
202 const std::string& device_path) override;
203
204 // Get invocation history to be verified by testcases.
205 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
206 FormatEvent GetFormatEvent(size_t index);
207 DiskEvent GetDiskEvent(size_t index);
208 MountEvent GetMountEvent(size_t index);
209
210 size_t GetEventCount() { return events_.size(); }
211
212 ObserverEvent GetEvent(size_t index) { return events_.at(index); }
213
214 private:
215 // Pointer to the manager object to which this |Observer| is registered.
216 const DiskMountManager* manager_;
217
218 // Records all invocations.
219 std::vector<ObserverEvent> events_;
220 };
221
222 // Needed to print values in case of EXPECT_* failure in gtest.
223 std::ostream& operator<<(std::ostream& stream, const DeviceEvent& device_event);
224 std::ostream& operator<<(std::ostream& stream, const DiskEvent& disk_event);
225 std::ostream& operator<<(std::ostream& stream, const FormatEvent& format_event);
226 std::ostream& operator<<(std::ostream& stream, const MountEvent& mount_event);
227
228 } // namespace disks
229 } // namespace chromeos
230
231 #endif // CHROMEOS_DISKS_MOCK_DISK_MOUNT_MANAGER_OBSERVER_H_
OLDNEW
« 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