1// Copyright (c) 2011 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#include "chrome/browser/chromeos/cros/mock_mount_library.h"
6
7#include "base/message_loop.h"
8#include "base/string_util.h"
9#include "chrome/browser/chromeos/cros/cros_library.h"
10#include "content/browser/browser_thread.h"
11
12namespace chromeos {
13
14using testing::_;
15using testing::Invoke;
16
17const char* kTestSystemPath = "/this/system/path";
18const char* kTestDevicePath = "/this/device/path";
19const char* kTestMountPath = "/media/foofoo";
20const char* kTestFilePath = "/this/file/path";
21const char* kTestDeviceLabel = "A label";
22const char* kTestDriveLabel = "Another label";
23const char* kTestParentPath = "/this/is/my/parent";
24
25void MockMountLibrary::AddObserverInternal(MountLibrary::Observer* observer) {
26  observers_.AddObserver(observer);
27}
28
29void MockMountLibrary::RemoveObserverInternal(
30    MountLibrary::Observer* observer) {
31  observers_.RemoveObserver(observer);
32}
33
34MockMountLibrary::MockMountLibrary() {
35  ON_CALL(*this, AddObserver(_))
36      .WillByDefault(Invoke(this, &MockMountLibrary::AddObserverInternal));
37  ON_CALL(*this, RemoveObserver(_))
38      .WillByDefault(Invoke(this, &MockMountLibrary::RemoveObserverInternal));
39  ON_CALL(*this, disks())
40      .WillByDefault(Invoke(this, &MockMountLibrary::disksInternal));
41}
42
43MockMountLibrary::~MockMountLibrary() {
44
45}
46
47void MockMountLibrary::FireDeviceInsertEvents() {
48
49  scoped_ptr<MountLibrary::Disk> disk1(new MountLibrary::Disk(
50      std::string(kTestDevicePath),
51      std::string(),
52      std::string(kTestSystemPath),
53      std::string(kTestFilePath),
54      std::string(),
55      std::string(kTestDriveLabel),
56      std::string(kTestParentPath),
57      FLASH,
58      4294967295U,
59      false,
60      false,
61      true,
62      false));
63
64  disks_.clear();
65  disks_.insert(std::pair<std::string, MountLibrary::Disk*>(
66      std::string(kTestDevicePath), disk1.get()));
67
68  // Device Added
69  chromeos::MountLibraryEventType evt;
70  evt = chromeos::MOUNT_DEVICE_ADDED;
71  UpdateDeviceChanged(evt, kTestSystemPath);
72
73  // Disk Added
74  evt = chromeos::MOUNT_DISK_ADDED;
75  UpdateDiskChanged(evt, disk1.get());
76
77  // Disk Changed
78  scoped_ptr<MountLibrary::Disk> disk2(new MountLibrary::Disk(
79      std::string(kTestDevicePath),
80      std::string(kTestMountPath),
81      std::string(kTestSystemPath),
82      std::string(kTestFilePath),
83      std::string(kTestDeviceLabel),
84      std::string(kTestDriveLabel),
85      std::string(kTestParentPath),
86      FLASH,
87      1073741824,
88      false,
89      false,
90      true,
91      false));
92  disks_.clear();
93  disks_.insert(std::pair<std::string, MountLibrary::Disk*>(
94      std::string(kTestDevicePath), disk2.get()));
95  evt = chromeos::MOUNT_DISK_CHANGED;
96  UpdateDiskChanged(evt, disk2.get());
97}
98
99void MockMountLibrary::FireDeviceRemoveEvents() {
100  scoped_ptr<MountLibrary::Disk> disk(new MountLibrary::Disk(
101      std::string(kTestDevicePath),
102      std::string(kTestMountPath),
103      std::string(kTestSystemPath),
104      std::string(kTestFilePath),
105      std::string(kTestDeviceLabel),
106      std::string(kTestDriveLabel),
107      std::string(kTestParentPath),
108      FLASH,
109      1073741824,
110      false,
111      false,
112      true,
113      false));
114  disks_.clear();
115  disks_.insert(std::pair<std::string, MountLibrary::Disk*>(
116      std::string(kTestDevicePath), disk.get()));
117  UpdateDiskChanged(chromeos::MOUNT_DISK_REMOVED, disk.get());
118}
119
120void MockMountLibrary::UpdateDiskChanged(MountLibraryEventType evt,
121                                         const MountLibrary::Disk* disk) {
122  // Make sure we run on UI thread.
123  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
124
125  FOR_EACH_OBSERVER(Observer, observers_, DiskChanged(evt, disk));
126}
127
128
129void MockMountLibrary::UpdateDeviceChanged(MountLibraryEventType evt,
130                                           const std::string& path) {
131  // Make sure we run on UI thread.
132  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
133
134  FOR_EACH_OBSERVER(Observer, observers_, DeviceChanged(evt, path));
135}
136
137}  // namespace chromeos
138