summaryrefslogtreecommitdiffstats
path: root/chromeos/disks/mock_disk_mount_manager.cc
blob: 610efbbf8e5a22de4e3842dd25b1827ef1c49a78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (c) 2012 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.

#include "chromeos/disks/mock_disk_mount_manager.h"

#include <utility>

#include "base/message_loop.h"
#include "base/stl_util.h"
#include "base/string_util.h"

using testing::_;
using testing::AnyNumber;
using testing::Invoke;
using testing::ReturnRef;

namespace chromeos {
namespace disks {

namespace {

const char* kTestSystemPath = "/this/system/path";
const char* kTestSystemPathPrefix = "/this/system";
const char* kTestDevicePath = "/this/device/path";
const char* kTestMountPath = "/media/foofoo";
const char* kTestFilePath = "/this/file/path";
const char* kTestDeviceLabel = "A label";
const char* kTestDriveLabel = "Another label";
const char* kTestVendorId = "0123";
const char* kTestVendorName = "A vendor";
const char* kTestProductId = "abcd";
const char* kTestProductName = "A product";
const char* kTestUuid = "FFFF-FFFF";

}  // namespace

void MockDiskMountManager::AddObserverInternal(
    DiskMountManager::Observer* observer) {
  observers_.AddObserver(observer);
}

void MockDiskMountManager::RemoveObserverInternal(
    DiskMountManager::Observer* observer) {
  observers_.RemoveObserver(observer);
}

MockDiskMountManager::MockDiskMountManager() {
  ON_CALL(*this, AddObserver(_))
      .WillByDefault(Invoke(this, &MockDiskMountManager::AddObserverInternal));
  ON_CALL(*this, RemoveObserver(_))
      .WillByDefault(Invoke(this,
                            &MockDiskMountManager::RemoveObserverInternal));
  ON_CALL(*this, disks())
      .WillByDefault(Invoke(this, &MockDiskMountManager::disksInternal));
  ON_CALL(*this, mount_points())
      .WillByDefault(Invoke(this, &MockDiskMountManager::mountPointsInternal));
  ON_CALL(*this, FindDiskBySourcePath(_))
      .WillByDefault(Invoke(
          this, &MockDiskMountManager::FindDiskBySourcePathInternal));
}

MockDiskMountManager::~MockDiskMountManager() {
  STLDeleteContainerPairSecondPointers(disks_.begin(), disks_.end());
  disks_.clear();
}

void MockDiskMountManager::NotifyDeviceInsertEvents() {
  scoped_ptr<DiskMountManager::Disk> disk1(new DiskMountManager::Disk(
      std::string(kTestDevicePath),
      std::string(),
      std::string(kTestSystemPath),
      std::string(kTestFilePath),
      std::string(),
      std::string(kTestDriveLabel),
      std::string(kTestVendorId),
      std::string(kTestVendorName),
      std::string(kTestProductId),
      std::string(kTestProductName),
      std::string(kTestUuid),
      std::string(kTestSystemPathPrefix),
      DEVICE_TYPE_USB,
      4294967295U,
      false,  // is_parent
      false,  // is_read_only
      true,  // has_media
      false,  // on_boot_device
      false));  // is_hidden

  disks_.clear();
  disks_.insert(std::pair<std::string, DiskMountManager::Disk*>(
      std::string(kTestDevicePath), disk1.get()));

  // Device Added
  DiskMountManagerEventType event;
  event = MOUNT_DEVICE_ADDED;
  NotifyDeviceChanged(event, kTestSystemPath);

  // Disk Added
  event = MOUNT_DISK_ADDED;
  NotifyDiskChanged(event, disk1.get());

  // Disk Changed
  scoped_ptr<DiskMountManager::Disk> disk2(new DiskMountManager::Disk(
      std::string(kTestDevicePath),
      std::string(kTestMountPath),
      std::string(kTestSystemPath),
      std::string(kTestFilePath),
      std::string(kTestDeviceLabel),
      std::string(kTestDriveLabel),
      std::string(kTestVendorId),
      std::string(kTestVendorName),
      std::string(kTestProductId),
      std::string(kTestProductName),
      std::string(kTestUuid),
      std::string(kTestSystemPathPrefix),
      DEVICE_TYPE_MOBILE,
      1073741824,
      false,  // is_parent
      false,  // is_read_only
      true,  // has_media
      false,  // on_boot_device
      false));  // is_hidden
  disks_.clear();
  disks_.insert(std::pair<std::string, DiskMountManager::Disk*>(
      std::string(kTestDevicePath), disk2.get()));
  event = MOUNT_DISK_CHANGED;
  NotifyDiskChanged(event, disk2.get());
}

void MockDiskMountManager::NotifyDeviceRemoveEvents() {
  scoped_ptr<DiskMountManager::Disk> disk(new DiskMountManager::Disk(
      std::string(kTestDevicePath),
      std::string(kTestMountPath),
      std::string(kTestSystemPath),
      std::string(kTestFilePath),
      std::string(kTestDeviceLabel),
      std::string(kTestDriveLabel),
      std::string(kTestVendorId),
      std::string(kTestVendorName),
      std::string(kTestProductId),
      std::string(kTestProductName),
      std::string(kTestUuid),
      std::string(kTestSystemPathPrefix),
      DEVICE_TYPE_SD,
      1073741824,
      false,  // is_parent
      false,  // is_read_only
      true,  // has_media
      false,  // on_boot_device
      false));  // is_hidden
  disks_.clear();
  disks_.insert(std::pair<std::string, DiskMountManager::Disk*>(
      std::string(kTestDevicePath), disk.get()));
  NotifyDiskChanged(MOUNT_DISK_REMOVED, disk.get());
}

void MockDiskMountManager::SetupDefaultReplies() {
  EXPECT_CALL(*this, AddObserver(_))
      .Times(AnyNumber());
  EXPECT_CALL(*this, RemoveObserver(_))
      .Times(AnyNumber());
  EXPECT_CALL(*this, disks())
      .WillRepeatedly(ReturnRef(disks_));
  EXPECT_CALL(*this, mount_points())
      .WillRepeatedly(ReturnRef(mount_points_));
  EXPECT_CALL(*this, FindDiskBySourcePath(_))
      .Times(AnyNumber());
  EXPECT_CALL(*this, RequestMountInfoRefresh())
      .Times(AnyNumber());
  EXPECT_CALL(*this, MountPath(_, _, _, _))
      .Times(AnyNumber());
  EXPECT_CALL(*this, UnmountPath(_))
      .Times(AnyNumber());
  EXPECT_CALL(*this, FormatUnmountedDevice(_))
      .Times(AnyNumber());
  EXPECT_CALL(*this, FormatMountedDevice(_))
      .Times(AnyNumber());
  EXPECT_CALL(*this, UnmountDeviceRecursive(_, _, _))
      .Times(AnyNumber());
}

void MockDiskMountManager::CreateDiskEntryForMountDevice(
    const DiskMountManager::MountPointInfo& mount_info,
    const std::string& device_id) {
  Disk* disk = new DiskMountManager::Disk(std::string(mount_info.source_path),
                                          std::string(mount_info.mount_path),
                                          std::string(),  // system_path
                                          std::string(),  // file_path
                                          std::string(),  // device_label
                                          std::string(),  // drive_label
                                          std::string(),  // vendor_id
                                          std::string(),  // vendor_name
                                          std::string(),  // product_id
                                          std::string(),  // product_name
                                          device_id,  // fs_uuid
                                          std::string(),  // system_path_prefix
                                          DEVICE_TYPE_USB,  // device_type
                                          1073741824,  // total_size_in_bytes
                                          false,  // is_parent
                                          false,  // is_read_only
                                          true,  // has_media
                                          false,  // on_boot_device
                                          false);  // is_hidden
  DiskMountManager::DiskMap::iterator it = disks_.find(mount_info.source_path);
  if (it == disks_.end()) {
    disks_.insert(std::make_pair(std::string(mount_info.source_path), disk));
  } else {
    delete it->second;
    it->second = disk;
  }
}

void MockDiskMountManager::RemoveDiskEntryForMountDevice(
    const DiskMountManager::MountPointInfo& mount_info) {
  DiskMountManager::DiskMap::iterator it = disks_.find(mount_info.source_path);
  if (it != disks_.end()) {
    delete it->second;
    disks_.erase(it);
  }
}

const DiskMountManager::MountPointMap&
MockDiskMountManager::mountPointsInternal() const {
  return mount_points_;
}

const DiskMountManager::Disk*
MockDiskMountManager::FindDiskBySourcePathInternal(
    const std::string& source_path) const {
  DiskMap::const_iterator disk_it = disks_.find(source_path);
  return disk_it == disks_.end() ? NULL : disk_it->second;
}

void MockDiskMountManager::NotifyDiskChanged(
    DiskMountManagerEventType event,
    const DiskMountManager::Disk* disk) {
  FOR_EACH_OBSERVER(Observer, observers_, DiskChanged(event, disk));
}

void MockDiskMountManager::NotifyDeviceChanged(DiskMountManagerEventType event,
                                               const std::string& path) {
  FOR_EACH_OBSERVER(Observer, observers_, DeviceChanged(event, path));
}

}  // namespace disks
}  // namespace chromeos