OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MediaDeviceNotificationsLinux unit tests. | |
6 | |
7 #include "content/browser/media_device_notifications_linux.h" | |
8 | |
5 #include <mntent.h> | 9 #include <mntent.h> |
6 #include <stdio.h> | 10 #include <stdio.h> |
7 #include <string.h> | 11 #include <string> |
8 | 12 |
9 #include "base/file_util.h" | 13 #include "base/file_util.h" |
10 #include "base/logging.h" | 14 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
13 #include "base/scoped_temp_dir.h" | 17 #include "base/scoped_temp_dir.h" |
14 #include "base/system_monitor/system_monitor.h" | 18 #include "base/system_monitor/system_monitor.h" |
15 #include "base/test/mock_devices_changed_observer.h" | 19 #include "base/test/mock_devices_changed_observer.h" |
16 #include "content/browser/browser_thread_impl.h" | 20 #include "content/browser/browser_thread_impl.h" |
17 #include "content/browser/media_device_notifications_linux.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
19 | 22 |
23 namespace content { | |
24 | |
25 namespace { | |
26 | |
20 using testing::_; | 27 using testing::_; |
21 | 28 |
22 namespace { | 29 const char kValidFS[] = "vfat"; |
30 const char kInvalidFS[] = "invalidfs"; | |
23 | 31 |
24 const char* kValidFS = "vfat"; | 32 const char kInvalidPath[] = "invalid path does not exist"; |
25 const char* kInvalidFS = "invalidfs"; | |
26 | 33 |
27 const char* kInvalidPath = "invalid path does not exist"; | 34 const char kDevice1[] = "d1"; |
35 const char kDevice2[] = "d2"; | |
36 const char kDevice3[] = "d3"; | |
28 | 37 |
29 const char* kDevice1 = "d1"; | 38 const char kMountPointA[] = "mnt_a"; |
30 const char* kDevice2 = "d2"; | 39 const char kMountPointB[] = "mnt_b"; |
31 const char* kDevice3 = "d3"; | |
32 | 40 |
33 const char* kMountPointA = "mnt_a"; | 41 // TODO(thestig) Move this into base/string_util.h, or replace this with |
34 const char* kMountPointB = "mnt_b"; | 42 // strndup_with_new() if we find more uses for it. |
35 | 43 // Duplicate the content of |str| into a new char array. Caller takes ownership |
36 } // namespace | 44 // of the allocated array. Unlike std::string::c_str(), this returns a char* |
37 | 45 // instead of a const char*. |
38 namespace content { | 46 char* copy_string(const std::string& str) { |
47 const size_t len = str.length(); | |
48 char* ret = new char[len + 1]; | |
49 str.copy(ret, len, 0); | |
50 ret[len] = '\0'; | |
51 return ret; | |
52 } | |
39 | 53 |
40 class MediaDeviceNotificationsLinuxTestWrapper | 54 class MediaDeviceNotificationsLinuxTestWrapper |
41 : public MediaDeviceNotificationsLinux { | 55 : public MediaDeviceNotificationsLinux { |
42 public: | 56 public: |
43 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, | 57 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, |
44 MessageLoop* message_loop) | 58 MessageLoop* message_loop) |
45 : MediaDeviceNotificationsLinux(path), | 59 : MediaDeviceNotificationsLinux(path), |
46 message_loop_(message_loop) { | 60 message_loop_(message_loop) { |
47 } | 61 } |
48 | 62 |
49 protected: | 63 private: |
64 // Avoids code deleting the object while there are references to it. | |
65 // Aside from the base::RefCountedThreadSafe friend class, any attempts to | |
66 // call this dtor will result in a compile-time error. | |
50 ~MediaDeviceNotificationsLinuxTestWrapper() {} | 67 ~MediaDeviceNotificationsLinuxTestWrapper() {} |
51 | 68 |
52 virtual void OnFilePathChanged(const FilePath& path) { | 69 virtual void OnFilePathChanged(const FilePath& path) { |
53 MediaDeviceNotificationsLinux::OnFilePathChanged(path); | 70 MediaDeviceNotificationsLinux::OnFilePathChanged(path); |
54 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 71 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
55 } | 72 } |
56 | 73 |
57 private: | |
58 MessageLoop* message_loop_; | 74 MessageLoop* message_loop_; |
59 | 75 |
60 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTestWrapper); | 76 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTestWrapper); |
61 }; | 77 }; |
62 | 78 |
63 class MediaDeviceNotificationsLinuxTest : public testing::Test { | 79 class MediaDeviceNotificationsLinuxTest : public testing::Test { |
64 public: | 80 public: |
65 struct MtabTestData { | 81 struct MtabTestData { |
66 MtabTestData(const char* mount_device, | 82 MtabTestData(const std::string& mount_device, |
67 const char* mount_point, | 83 const std::string& mount_point, |
68 const char* mount_type) | 84 const std::string& mount_type) |
69 : mount_device(mount_device), | 85 : mount_device(mount_device), |
70 mount_point(mount_point), | 86 mount_point(mount_point), |
71 mount_type(mount_type) { | 87 mount_type(mount_type) { |
72 } | 88 } |
73 | 89 |
74 const char* mount_device; | 90 const std::string mount_device; |
75 const char* mount_point; | 91 const std::string mount_point; |
76 const char* mount_type; | 92 const std::string mount_type; |
77 }; | 93 }; |
78 | 94 |
79 MediaDeviceNotificationsLinuxTest() | 95 MediaDeviceNotificationsLinuxTest() |
80 : message_loop_(MessageLoop::TYPE_IO), | 96 : message_loop_(MessageLoop::TYPE_IO), |
81 file_thread_(BrowserThread::FILE, &message_loop_) { | 97 file_thread_(BrowserThread::FILE, &message_loop_) { |
82 system_monitor_.reset(new base::SystemMonitor()); | |
83 } | 98 } |
84 virtual ~MediaDeviceNotificationsLinuxTest() {} | 99 virtual ~MediaDeviceNotificationsLinuxTest() {} |
85 | 100 |
86 protected: | 101 protected: |
87 virtual void SetUp() { | 102 virtual void SetUp() { |
88 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); | 103 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); |
89 system_monitor_->AddDevicesChangedObserver( | 104 system_monitor_.AddDevicesChangedObserver( |
90 mock_devices_changed_observer_.get()); | 105 mock_devices_changed_observer_.get()); |
91 | 106 |
92 // Create and set up a temp dir with files for the test. | 107 // Create and set up a temp dir with files for the test. |
93 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | 108 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
94 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc"); | 109 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc"); |
95 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); | 110 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); |
96 mtab_file_ = test_dir.AppendASCII("test_mtab"); | 111 mtab_file_ = test_dir.AppendASCII("test_mtab"); |
97 struct MtabTestData initial_test_data[] = { | 112 MtabTestData initial_test_data[] = { |
98 MtabTestData("dummydevice", "dummydir", kInvalidFS), | 113 MtabTestData("dummydevice", "dummydir", kInvalidFS), |
99 }; | 114 }; |
100 WriteToMtab(initial_test_data, arraysize(initial_test_data), true); | 115 WriteToMtab(initial_test_data, |
116 arraysize(initial_test_data), | |
117 true /* overwrite */); | |
101 | 118 |
102 // Initialize the test subject. | 119 // Initialize the test subject. |
103 notifications_ = | 120 notifications_ = |
104 new MediaDeviceNotificationsLinuxTestWrapper(mtab_file_, | 121 new MediaDeviceNotificationsLinuxTestWrapper(mtab_file_, |
105 &message_loop_); | 122 &message_loop_); |
106 notifications_->Init(); | 123 notifications_->Init(); |
107 message_loop_.RunAllPending(); | 124 message_loop_.RunAllPending(); |
108 } | 125 } |
109 | 126 |
110 virtual void TearDown() { | 127 virtual void TearDown() { |
111 message_loop_.RunAllPending(); | 128 message_loop_.RunAllPending(); |
112 notifications_ = NULL; | 129 notifications_ = NULL; |
113 system_monitor_->RemoveDevicesChangedObserver( | 130 system_monitor_.RemoveDevicesChangedObserver( |
114 mock_devices_changed_observer_.get()); | 131 mock_devices_changed_observer_.get()); |
115 } | 132 } |
116 | 133 |
117 // Used to run tests. When the mtab file gets modified, the message loop | 134 // Append mtab entries from the |data| array of |data_size| to the mtab file, |
ajl
2012/04/11 02:10:30
This still isn't exactly right. You need to speci
Lei Zhang
2012/04/11 02:24:01
Done.
| |
118 // needs to run in order to react to the file modification. | 135 // and run the message loop. |
119 // See WriteToMtab for parameters. | 136 void AppendToMtabAndRunLoop(const MtabTestData* data, size_t data_size) { |
120 void WriteToMtabAndRunLoop(struct MtabTestData* data, | 137 WriteToMtab(data, data_size, false /* do not overwrite */); |
121 size_t data_size, | |
122 bool overwrite) { | |
123 WriteToMtab(data, data_size, overwrite); | |
124 message_loop_.Run(); | 138 message_loop_.Run(); |
125 } | 139 } |
126 | 140 |
141 // Overwrite the mtab file with mtab entries from the |data| array of | |
142 // |data_size|, and run the message loop. | |
143 void OverwriteMtabAndRunLoop(const MtabTestData* data, size_t data_size) { | |
144 WriteToMtab(data, data_size, true /* overwrite */); | |
145 message_loop_.Run(); | |
146 } | |
147 | |
148 // Simplied version of OverwriteMtabAndRunLoop() that just deletes all the | |
149 // entries in the mtab file. | |
150 void WriteEmptyMtabAndRunLoop() { | |
151 OverwriteMtabAndRunLoop(NULL, // No data. | |
152 0); // No data length. | |
153 } | |
154 | |
155 // Create a directory named |dir| relative to the test directory. | |
156 // It has a DCIM directory, so MediaDeviceNotificationsLinux recognizes it as | |
157 // a media directory. | |
158 FilePath CreateMountPointWithDCIMDir(const std::string& dir) { | |
159 return CreateMountPoint(dir, true /* create DCIM dir */); | |
160 } | |
161 | |
162 // Create a directory named |dir| relative to the test directory. | |
163 // It does not have a DCIM directory, so MediaDeviceNotificationsLinux does | |
164 // not recognizes it as a media directory. | |
165 FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) { | |
166 return CreateMountPoint(dir, false /* do not create DCIM dir */); | |
167 } | |
168 | |
169 base::MockDevicesChangedObserver& observer() { | |
170 return *mock_devices_changed_observer_; | |
171 } | |
172 | |
173 private: | |
127 // Create a directory named |dir| relative to the test directory. | 174 // Create a directory named |dir| relative to the test directory. |
128 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" | 175 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" |
129 // subdirectory. | 176 // subdirectory. |
130 // Returns the full path to the created directory on success, or an empty | 177 // Returns the full path to the created directory on success, or an empty |
131 // path on failure. | 178 // path on failure. |
132 FilePath CreateMountPoint(const char* dir, bool with_dcim_dir) { | 179 FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) { |
133 FilePath return_path(scoped_temp_dir_.path()); | 180 FilePath return_path(scoped_temp_dir_.path()); |
134 return_path = return_path.AppendASCII(dir); | 181 return_path = return_path.AppendASCII(dir); |
135 FilePath path(return_path); | 182 FilePath path(return_path); |
136 if (with_dcim_dir) | 183 if (with_dcim_dir) |
137 path = path.AppendASCII("DCIM"); | 184 path = path.AppendASCII("DCIM"); |
138 if (!file_util::CreateDirectory(path)) | 185 if (!file_util::CreateDirectory(path)) |
139 return FilePath(); | 186 return FilePath(); |
140 return return_path; | 187 return return_path; |
141 } | 188 } |
142 | 189 |
143 base::MockDevicesChangedObserver& observer() { | |
144 return *mock_devices_changed_observer_; | |
145 } | |
146 | |
147 private: | |
148 // Write the test mtab data to |mtab_file_|. | 190 // Write the test mtab data to |mtab_file_|. |
149 // |data| is an array of mtab entries. | 191 // |data| is an array of mtab entries. |
150 // |data_size| is the array size of |data|. | 192 // |data_size| is the array size of |data|. |
151 // |overwrite| specifies whether to overwrite |mtab_file_|. | 193 // |overwrite| specifies whether to overwrite |mtab_file_|. |
152 void WriteToMtab(struct MtabTestData* data, | 194 void WriteToMtab(const MtabTestData* data, |
153 size_t data_size, | 195 size_t data_size, |
154 bool overwrite) { | 196 bool overwrite) { |
155 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); | 197 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); |
156 ASSERT_TRUE(file); | 198 ASSERT_TRUE(file); |
157 | 199 |
158 struct mntent entry; | 200 mntent entry; |
159 entry.mnt_opts = strdup("rw"); | 201 scoped_array<char> mount_opts(copy_string("rw")); |
202 entry.mnt_opts = mount_opts.get(); | |
160 entry.mnt_freq = 0; | 203 entry.mnt_freq = 0; |
161 entry.mnt_passno = 0; | 204 entry.mnt_passno = 0; |
162 for (size_t i = 0; i < data_size; ++i) { | 205 for (size_t i = 0; i < data_size; ++i) { |
163 entry.mnt_fsname = strdup(data[i].mount_device); | 206 scoped_array<char> mount_device(copy_string(data[i].mount_device)); |
164 entry.mnt_dir = strdup(data[i].mount_point); | 207 scoped_array<char> mount_point(copy_string(data[i].mount_point)); |
165 entry.mnt_type = strdup(data[i].mount_type); | 208 scoped_array<char> mount_type(copy_string(data[i].mount_type)); |
166 int add_result = addmntent(file, &entry); | 209 entry.mnt_fsname = mount_device.get(); |
167 ASSERT_EQ(0, add_result); | 210 entry.mnt_dir = mount_point.get(); |
168 free(entry.mnt_fsname); | 211 entry.mnt_type = mount_type.get(); |
169 free(entry.mnt_dir); | 212 ASSERT_EQ(0, addmntent(file, &entry)); |
170 free(entry.mnt_type); | |
171 } | 213 } |
172 free(entry.mnt_opts); | 214 ASSERT_EQ(1, endmntent(file)); |
173 int end_result = endmntent(file); | |
174 ASSERT_EQ(1, end_result); | |
175 } | 215 } |
176 | 216 |
177 // The message loop and file thread to run tests on. | 217 // The message loop and file thread to run tests on. |
178 MessageLoop message_loop_; | 218 MessageLoop message_loop_; |
179 BrowserThreadImpl file_thread_; | 219 BrowserThreadImpl file_thread_; |
180 | 220 |
181 // SystemMonitor and DevicesChangedObserver to hook together to test. | 221 // SystemMonitor and DevicesChangedObserver to hook together to test. |
182 scoped_ptr<base::SystemMonitor> system_monitor_; | 222 base::SystemMonitor system_monitor_; |
183 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; | 223 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; |
184 | 224 |
185 // Temporary directory for created test data. | 225 // Temporary directory for created test data. |
186 ScopedTempDir scoped_temp_dir_; | 226 ScopedTempDir scoped_temp_dir_; |
187 // Path to the test mtab file. | 227 // Path to the test mtab file. |
188 FilePath mtab_file_; | 228 FilePath mtab_file_; |
189 | 229 |
190 scoped_refptr<MediaDeviceNotificationsLinuxTestWrapper> notifications_; | 230 scoped_refptr<MediaDeviceNotificationsLinuxTestWrapper> notifications_; |
191 | 231 |
192 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest); | 232 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest); |
193 }; | 233 }; |
194 | 234 |
235 // Simple test case where we attach and detach a media device. | |
195 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { | 236 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { |
196 testing::Sequence mock_sequence; | 237 testing::Sequence mock_sequence; |
197 FilePath test_path = CreateMountPoint(kMountPointA, true); | 238 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); |
198 ASSERT_FALSE(test_path.empty()); | 239 ASSERT_FALSE(test_path.empty()); |
199 struct MtabTestData test_data[] = { | 240 MtabTestData test_data[] = { |
200 MtabTestData(kDevice1, kInvalidPath, kValidFS), | 241 MtabTestData(kDevice1, kInvalidPath, kValidFS), |
201 MtabTestData(kDevice2, test_path.value().c_str(), kValidFS), | 242 MtabTestData(kDevice2, test_path.value(), kValidFS), |
202 }; | 243 }; |
244 // Only |kDevice2| should be attached, since |kDevice1| has a bad path. | |
203 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice2, test_path)) | 245 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice2, test_path)) |
204 .InSequence(mock_sequence); | 246 .InSequence(mock_sequence); |
205 WriteToMtabAndRunLoop(test_data, arraysize(test_data), false); | 247 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); |
206 | 248 |
249 // |kDevice2| should be detached here. | |
207 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); | 250 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); |
208 WriteToMtabAndRunLoop(NULL, 0, true); | 251 WriteEmptyMtabAndRunLoop(); |
209 } | 252 } |
210 | 253 |
211 // Only mount points with DCIM directories are recognized. | 254 // Only mount points with DCIM directories are recognized. |
212 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { | 255 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { |
213 testing::Sequence mock_sequence; | 256 testing::Sequence mock_sequence; |
214 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 257 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
215 ASSERT_FALSE(test_pathA.empty()); | 258 ASSERT_FALSE(test_path_a.empty()); |
216 struct MtabTestData test_data1[] = { | 259 MtabTestData test_data1[] = { |
217 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 260 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
218 }; | 261 }; |
219 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathA)) | 262 // |kDevice1| should be attached as expected. |
263 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_path_a)) | |
220 .InSequence(mock_sequence); | 264 .InSequence(mock_sequence); |
221 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false); | 265 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
222 | 266 |
223 FilePath test_pathB = CreateMountPoint(kMountPointB, false); | 267 // This should do nothing, since |kMountPointB| does not have a DCIM dir. |
224 ASSERT_FALSE(test_pathB.empty()); | 268 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); |
225 struct MtabTestData test_data2[] = { | 269 ASSERT_FALSE(test_path_b.empty()); |
226 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 270 MtabTestData test_data2[] = { |
271 MtabTestData(kDevice2, test_path_b.value(), kValidFS), | |
227 }; | 272 }; |
228 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 273 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
229 | 274 |
275 // |kDevice1| should be detached as expected. | |
230 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); | 276 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); |
231 WriteToMtabAndRunLoop(NULL, 0, true); | 277 WriteEmptyMtabAndRunLoop(); |
232 } | 278 } |
233 | 279 |
280 // More complicated test case with multiple devices on multiple mount points. | |
234 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { | 281 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { |
235 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 282 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
236 FilePath test_pathB = CreateMountPoint(kMountPointB, true); | 283 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
237 ASSERT_FALSE(test_pathA.empty()); | 284 ASSERT_FALSE(test_path_a.empty()); |
238 ASSERT_FALSE(test_pathB.empty()); | 285 ASSERT_FALSE(test_path_b.empty()); |
239 | 286 |
240 // Attach two devices. | 287 // Attach two devices. |
241 // kDevice1 -> kMountPointA | 288 // kDevice1 -> kMountPointA |
242 // kDevice2 -> kMountPointB | 289 // kDevice2 -> kMountPointB |
243 struct MtabTestData test_data1[] = { | 290 MtabTestData test_data1[] = { |
244 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 291 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
245 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 292 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
246 }; | 293 }; |
247 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); | 294 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); |
248 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 295 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
249 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false); | 296 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
250 | 297 |
251 // Attach |kDevice1| to |kMountPointB|. | 298 // Attach |kDevice1| to |kMountPointB|. |
252 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been | 299 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been |
253 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA. | 300 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA. |
254 // kDevice1 -> kMountPointA | 301 // kDevice1 -> kMountPointA |
255 // kDevice2 -> kMountPointB | 302 // kDevice2 -> kMountPointB |
256 // kDevice1 -> kMountPointB | 303 // kDevice1 -> kMountPointB |
257 struct MtabTestData test_data2[] = { | 304 MtabTestData test_data2[] = { |
258 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 305 MtabTestData(kDevice1, test_path_b.value(), kValidFS), |
259 }; | 306 }; |
260 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); | 307 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); |
261 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); | 308 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
262 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 309 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
263 | 310 |
264 // Attach |kDevice2| to |kMountPointA|. | 311 // Attach |kDevice2| to |kMountPointA|. |
265 // kDevice1 -> kMountPointA | 312 // kDevice1 -> kMountPointA |
266 // kDevice2 -> kMountPointB | 313 // kDevice2 -> kMountPointB |
267 // kDevice1 -> kMountPointB | 314 // kDevice1 -> kMountPointB |
268 // kDevice2 -> kMountPointA | 315 // kDevice2 -> kMountPointA |
269 struct MtabTestData test_data3[] = { | 316 MtabTestData test_data3[] = { |
270 MtabTestData(kDevice2, test_pathA.value().c_str(), kValidFS), | 317 MtabTestData(kDevice2, test_path_a.value(), kValidFS), |
271 }; | 318 }; |
272 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); | 319 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); |
273 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 320 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
274 WriteToMtabAndRunLoop(test_data3, arraysize(test_data3), false); | 321 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3)); |
275 | 322 |
276 // Detach |kDevice2| from |kMountPointA|. | 323 // Detach |kDevice2| from |kMountPointA|. |
277 // kDevice1 -> kMountPointA | 324 // kDevice1 -> kMountPointA |
278 // kDevice2 -> kMountPointB | 325 // kDevice2 -> kMountPointB |
279 // kDevice1 -> kMountPointB | 326 // kDevice1 -> kMountPointB |
280 struct MtabTestData test_data4[] = { | 327 MtabTestData test_data4[] = { |
281 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 328 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
282 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 329 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
283 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 330 MtabTestData(kDevice1, test_path_b.value(), kValidFS), |
284 }; | 331 }; |
285 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 332 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
286 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); | 333 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); |
287 WriteToMtabAndRunLoop(test_data4, arraysize(test_data4), true); | 334 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4)); |
288 | 335 |
289 // Detach |kDevice1| from |kMountPointB|. | 336 // Detach |kDevice1| from |kMountPointB|. |
290 // kDevice1 -> kMountPointA | 337 // kDevice1 -> kMountPointA |
291 // kDevice2 -> kMountPointB | 338 // kDevice2 -> kMountPointB |
292 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); | 339 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); |
293 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); | 340 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); |
294 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true); | 341 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); |
295 | 342 |
296 // Detach all devices. | 343 // Detach all devices. |
297 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 344 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
298 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); | 345 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
299 WriteToMtabAndRunLoop(NULL, 0, true); | 346 WriteEmptyMtabAndRunLoop(); |
300 } | 347 } |
301 | 348 |
349 // More complicated test case with multiple devices on one mount point. | |
302 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { | 350 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { |
303 testing::Sequence mock_sequence; | 351 testing::Sequence mock_sequence; |
304 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 352 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
305 FilePath test_pathB = CreateMountPoint(kMountPointB, true); | 353 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
306 ASSERT_FALSE(test_pathA.empty()); | 354 ASSERT_FALSE(test_path_a.empty()); |
307 ASSERT_FALSE(test_pathB.empty()); | 355 ASSERT_FALSE(test_path_b.empty()); |
308 | 356 |
309 // |kDevice1| is most recently mounted at |kMountPointB|. | 357 // |kDevice1| is most recently mounted at |kMountPointB|. |
310 // kDevice1 -> kMountPointA | 358 // kDevice1 -> kMountPointA |
311 // kDevice2 -> kMountPointB | 359 // kDevice2 -> kMountPointB |
312 // kDevice1 -> kMountPointB | 360 // kDevice1 -> kMountPointB |
313 struct MtabTestData test_data1[] = { | 361 MtabTestData test_data1[] = { |
314 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 362 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
315 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 363 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
316 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 364 MtabTestData(kDevice1, test_path_b.value(), kValidFS), |
317 }; | 365 }; |
318 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathB)) | 366 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_path_b)) |
319 .Times(1); | 367 .Times(1); |
320 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 368 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
321 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true); | 369 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); |
322 | 370 |
323 // Attach |kDevice3| to |kMountPointB|. | 371 // Attach |kDevice3| to |kMountPointB|. |
324 // |kDevice1| is inaccessible at its most recent mount point, so it is | 372 // |kDevice1| is inaccessible at its most recent mount point, so it is |
325 // detached and unavailable, even though it is still accessible via | 373 // detached and unavailable, even though it is still accessible via |
326 // |kMountPointA|. | 374 // |kMountPointA|. |
327 // kDevice1 -> kMountPointA | 375 // kDevice1 -> kMountPointA |
328 // kDevice2 -> kMountPointB | 376 // kDevice2 -> kMountPointB |
329 // kDevice1 -> kMountPointB | 377 // kDevice1 -> kMountPointB |
330 // kDevice3 -> kMountPointB | 378 // kDevice3 -> kMountPointB |
331 struct MtabTestData test_data2[] = { | 379 MtabTestData test_data2[] = { |
332 MtabTestData(kDevice3, test_pathB.value().c_str(), kValidFS), | 380 MtabTestData(kDevice3, test_path_b.value(), kValidFS), |
333 }; | 381 }; |
334 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).Times(1); | 382 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).Times(1); |
335 EXPECT_CALL(observer(), OnMediaDeviceAttached(1, kDevice3, test_pathB)) | 383 EXPECT_CALL(observer(), OnMediaDeviceAttached(1, kDevice3, test_path_b)) |
336 .Times(1); | 384 .Times(1); |
337 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 385 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
338 | 386 |
339 // Detach all devices. | 387 // Detach all devices. |
340 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 388 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
341 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); | 389 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); |
342 WriteToMtabAndRunLoop(NULL, 0, true); | 390 WriteEmptyMtabAndRunLoop(); |
343 } | 391 } |
344 | 392 |
393 } // namespace | |
394 | |
345 } // namespace content | 395 } // namespace content |
OLD | NEW |