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

Side by Side Diff: chrome/browser/media_gallery/media_device_notifications_chromeos_unittest.cc

Issue 10919051: Move device notification impl out of chrome/browser/media_gallery (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: modify for chromeos and win Created 8 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
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 // chromeos::MediaDeviceNotifications unit tests.
6
7 #include "chrome/browser/media_gallery/media_device_notifications_chromeos.h"
8
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop.h"
13 #include "base/scoped_temp_dir.h"
14 #include "base/system_monitor/system_monitor.h"
15 #include "base/test/mock_devices_changed_observer.h"
16 #include "base/utf_string_conversions.h"
17 #include "chrome/browser/media_gallery/media_storage_util.h"
18 #include "chromeos/disks/mock_disk_mount_manager.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace chromeos {
23
24 namespace {
25
26 using content::BrowserThread;
27 using disks::DiskMountManager;
28 using testing::_;
29
30 const char kDevice1[] = "/dev/d1";
31 const char kDevice2[] = "/dev/disk/d2";
32 const char kDevice1Name[] = "d1";
33 const char kDevice2Name[] = "d2";
34 const char kMountPointA[] = "mnt_a";
35 const char kMountPointB[] = "mnt_b";
36
37 std::string GetDCIMDeviceId(const std::string& unique_id) {
38 return chrome::MediaStorageUtil::MakeDeviceId(
39 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id);
40 }
41
42 class MediaDeviceNotificationsTest : public testing::Test {
43 public:
44 MediaDeviceNotificationsTest()
45 : ui_thread_(BrowserThread::UI, &ui_loop_),
46 file_thread_(BrowserThread::FILE) {
47 }
48 virtual ~MediaDeviceNotificationsTest() {}
49
50 protected:
51 virtual void SetUp() {
52 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
53 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
54 file_thread_.Start();
55
56 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver);
57 system_monitor_.AddDevicesChangedObserver(
58 mock_devices_changed_observer_.get());
59
60 disk_mount_manager_mock_ = new disks::MockDiskMountManager();
61 DiskMountManager::InitializeForTesting(disk_mount_manager_mock_);
62 disk_mount_manager_mock_->SetupDefaultReplies();
63
64 // Initialize the test subject.
65 notifications_ = new MediaDeviceNotifications();
66 }
67
68 virtual void TearDown() {
69 notifications_ = NULL;
70 disk_mount_manager_mock_ = NULL;
71 DiskMountManager::Shutdown();
72 system_monitor_.RemoveDevicesChangedObserver(
73 mock_devices_changed_observer_.get());
74 WaitForFileThread();
75 }
76
77 base::MockDevicesChangedObserver& observer() {
78 return *mock_devices_changed_observer_;
79 }
80
81 void MountDevice(MountError error_code,
82 const DiskMountManager::MountPointInfo& mount_info,
83 const std::string& unique_id) {
84 if (error_code == MOUNT_ERROR_NONE) {
85 disk_mount_manager_mock_->CreateDiskEntryForMountDevice(
86 mount_info, unique_id);
87 }
88 notifications_->MountCompleted(disks::DiskMountManager::MOUNTING,
89 error_code,
90 mount_info);
91 WaitForFileThread();
92 }
93
94 void UnmountDevice(MountError error_code,
95 const DiskMountManager::MountPointInfo& mount_info) {
96 notifications_->MountCompleted(disks::DiskMountManager::UNMOUNTING,
97 error_code,
98 mount_info);
99 if (error_code == MOUNT_ERROR_NONE) {
100 disk_mount_manager_mock_->RemoveDiskEntryForMountDevice(
101 mount_info);
102 }
103 WaitForFileThread();
104 }
105
106 // Create a directory named |dir| relative to the test directory.
107 // Set |with_dcim_dir| to true if the created directory will have a "DCIM"
108 // subdirectory.
109 // Returns the full path to the created directory on success, or an empty
110 // path on failure.
111 FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) {
112 FilePath return_path(scoped_temp_dir_.path());
113 return_path = return_path.AppendASCII(dir);
114 FilePath path(return_path);
115 if (with_dcim_dir)
116 path = path.AppendASCII("DCIM");
117 if (!file_util::CreateDirectory(path))
118 return FilePath();
119 return return_path;
120 }
121
122 static void PostQuitToUIThread() {
123 BrowserThread::PostTask(BrowserThread::UI,
124 FROM_HERE,
125 MessageLoop::QuitClosure());
126 }
127
128 static void WaitForFileThread() {
129 BrowserThread::PostTask(BrowserThread::FILE,
130 FROM_HERE,
131 base::Bind(&PostQuitToUIThread));
132 MessageLoop::current()->Run();
133 }
134
135 private:
136 // The message loops and threads to run tests on.
137 MessageLoop ui_loop_;
138 content::TestBrowserThread ui_thread_;
139 content::TestBrowserThread file_thread_;
140
141 // Temporary directory for created test data.
142 ScopedTempDir scoped_temp_dir_;
143
144 // Objects that talks with MediaDeviceNotifications.
145 base::SystemMonitor system_monitor_;
146 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_;
147 // Owned by DiskMountManager.
148 disks::MockDiskMountManager* disk_mount_manager_mock_;
149
150 scoped_refptr<MediaDeviceNotifications> notifications_;
151
152 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsTest);
153 };
154
155 // Simple test case where we attach and detach a media device.
156 TEST_F(MediaDeviceNotificationsTest, BasicAttachDetach) {
157 testing::Sequence mock_sequence;
158 FilePath mount_path1 = CreateMountPoint(kMountPointA, true);
159 ASSERT_FALSE(mount_path1.empty());
160 DiskMountManager::MountPointInfo mount_info(kDevice1,
161 mount_path1.value(),
162 MOUNT_TYPE_DEVICE,
163 disks::MOUNT_CONDITION_NONE);
164 const std::string kUniqueId0 = "FFFF-FFFF";
165 EXPECT_CALL(observer(),
166 OnRemovableStorageAttached(GetDCIMDeviceId(kUniqueId0),
167 ASCIIToUTF16(kDevice1Name),
168 mount_path1.value()))
169 .InSequence(mock_sequence);
170 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId0);
171
172 EXPECT_CALL(observer(),
173 OnRemovableStorageDetached(GetDCIMDeviceId(kUniqueId0)))
174 .InSequence(mock_sequence);
175 UnmountDevice(MOUNT_ERROR_NONE, mount_info);
176
177 FilePath mount_path2 = CreateMountPoint(kMountPointB, true);
178 ASSERT_FALSE(mount_path2.empty());
179 DiskMountManager::MountPointInfo mount_info2(kDevice2,
180 mount_path2.value(),
181 MOUNT_TYPE_DEVICE,
182 disks::MOUNT_CONDITION_NONE);
183 const std::string kUniqueId1 = "FFF0-FFF0";
184
185 EXPECT_CALL(observer(),
186 OnRemovableStorageAttached(GetDCIMDeviceId(kUniqueId1),
187 ASCIIToUTF16(kDevice2Name),
188 mount_path2.value()))
189 .InSequence(mock_sequence);
190 MountDevice(MOUNT_ERROR_NONE, mount_info2, kUniqueId1);
191
192 EXPECT_CALL(observer(),
193 OnRemovableStorageDetached(GetDCIMDeviceId(kUniqueId1)))
194 .InSequence(mock_sequence);
195 UnmountDevice(MOUNT_ERROR_NONE, mount_info2);
196 }
197
198 // Only mount points with DCIM directories are recognized.
199 TEST_F(MediaDeviceNotificationsTest, DCIM) {
200 testing::Sequence mock_sequence;
201 FilePath mount_path = CreateMountPoint(kMountPointA, false);
202 const std::string kUniqueId = "FFFF-FFFF";
203 ASSERT_FALSE(mount_path.empty());
204 DiskMountManager::MountPointInfo mount_info(kDevice1,
205 mount_path.value(),
206 MOUNT_TYPE_DEVICE,
207 disks::MOUNT_CONDITION_NONE);
208 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
209 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId);
210 }
211
212 // Non device mounts and mount errors are ignored.
213 TEST_F(MediaDeviceNotificationsTest, Ignore) {
214 testing::Sequence mock_sequence;
215 FilePath mount_path = CreateMountPoint(kMountPointA, true);
216 const std::string kUniqueId = "FFFF-FFFF";
217 ASSERT_FALSE(mount_path.empty());
218
219 // Mount error.
220 DiskMountManager::MountPointInfo mount_info(kDevice1,
221 mount_path.value(),
222 MOUNT_TYPE_DEVICE,
223 disks::MOUNT_CONDITION_NONE);
224 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
225 MountDevice(MOUNT_ERROR_UNKNOWN, mount_info, kUniqueId);
226
227 // Not a device
228 mount_info.mount_type = MOUNT_TYPE_ARCHIVE;
229 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
230 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId);
231
232 // Unsupported file system.
233 mount_info.mount_type = MOUNT_TYPE_DEVICE;
234 mount_info.mount_condition = disks::MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM;
235 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
236 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId);
237 }
238
239 } // namespace
240
241 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698