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

Side by Side Diff: chrome/browser/system_monitor/image_capture_device_manager_unittest.mm

Issue 11817036: Revert 175938 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 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 | Annotate | Revision Log
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
6 #import <Foundation/Foundation.h>
7 #import <ImageCaptureCore/ImageCaptureCore.h>
8
9 #include "base/file_path.h"
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/mac/foundation_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop.h"
15 #include "base/system_monitor/system_monitor.h"
16 #include "chrome/browser/system_monitor/image_capture_device.h"
17 #include "chrome/browser/system_monitor/image_capture_device_manager.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace {
22
23 const char kDeviceId[] = "id";
24 const char kTestFileContents[] = "test";
25
26 } // namespace
27
28 // Private ICCameraDevice method needed to properly initialize the object.
29 @interface NSObject (PrivateAPIICCameraDevice)
30 - (id)initWithDictionary:(id)properties;
31 @end
32
33 @interface MockICCameraDevice : ICCameraDevice {
34 @private
35 scoped_nsobject<NSMutableArray> allMediaFiles_;
36 }
37
38 - (void)addMediaFile:(ICCameraFile*)file;
39
40 @end
41
42 @implementation MockICCameraDevice
43
44 - (id)init {
45 if ((self = [super initWithDictionary:[NSDictionary dictionary]])) {
46 }
47 return self;
48 }
49
50 - (NSString*)mountPoint {
51 return @"mountPoint";
52 }
53
54 - (NSString*)name {
55 return @"name";
56 }
57
58 - (NSString*)UUIDString {
59 return base::SysUTF8ToNSString(kDeviceId);
60 }
61
62 - (ICDeviceType)type {
63 return ICDeviceTypeCamera;
64 }
65
66 - (void)requestOpenSession {
67 }
68
69 - (void)requestCloseSession {
70 }
71
72 - (NSArray*)mediaFiles {
73 return allMediaFiles_;
74 }
75
76 - (void)addMediaFile:(ICCameraFile*)file {
77 if (!allMediaFiles_.get())
78 allMediaFiles_.reset([[NSMutableArray alloc] init]);
79 [allMediaFiles_ addObject:file];
80 }
81
82 // This method does approximately what the internal ImageCapture platform
83 // library is observed to do: take the download save-as filename and mangle
84 // it to attach an extension, then return that new filename to the caller
85 // in the options.
86 - (void)requestDownloadFile:(ICCameraFile*)file
87 options:(NSDictionary*)options
88 downloadDelegate:(id<ICCameraDeviceDownloadDelegate>)downloadDelegate
89 didDownloadSelector:(SEL)selector
90 contextInfo:(void*)contextInfo {
91 FilePath saveDir(base::SysNSStringToUTF8(
92 [[options objectForKey:ICDownloadsDirectoryURL] path]));
93 std::string saveAsFilename =
94 base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]);
95 // It appears that the ImageCapture library adds an extension to the requested
96 // filename. Do that here to require a rename.
97 saveAsFilename += ".jpg";
98 FilePath toBeSaved = saveDir.Append(saveAsFilename);
99 ASSERT_EQ(static_cast<int>(strlen(kTestFileContents)),
100 file_util::WriteFile(toBeSaved, kTestFileContents,
101 strlen(kTestFileContents)));
102
103 NSMutableDictionary* returnOptions =
104 [NSMutableDictionary dictionaryWithDictionary:options];
105 [returnOptions setObject:base::SysUTF8ToNSString(saveAsFilename)
106 forKey:ICSavedFilename];
107
108 [downloadDelegate didDownloadFile:file
109 error:nil
110 options:returnOptions
111 contextInfo:contextInfo];
112 }
113
114 @end
115
116 @interface MockICCameraFile : ICCameraFile {
117 @private
118 scoped_nsobject<NSString> name_;
119 scoped_nsobject<NSDate> date_;
120 }
121
122 - (id)init:(NSString*)name;
123
124 @end
125
126 @implementation MockICCameraFile
127
128 - (id)init:(NSString*)name {
129 if ((self = [super init])) {
130 name_.reset([name retain]);
131 date_.reset([[NSDate dateWithNaturalLanguageString:@"12/12/12"] retain]);
132 }
133 return self;
134 }
135
136 - (NSString*)name {
137 return name_.get();
138 }
139
140 - (NSString*)UTI {
141 return base::mac::CFToNSCast(kUTTypeImage);
142 }
143
144 - (NSDate*)modificationDate {
145 return date_.get();
146 }
147
148 - (NSDate*)creationDate {
149 return date_.get();
150 }
151
152 - (off_t)fileSize {
153 return 1000;
154 }
155
156 @end
157
158 class TestCameraListener
159 : public ImageCaptureDeviceListener,
160 public base::SupportsWeakPtr<TestCameraListener> {
161 public:
162 TestCameraListener()
163 : completed_(false),
164 removed_(false),
165 last_error_(base::PLATFORM_FILE_ERROR_INVALID_URL) {}
166 virtual ~TestCameraListener() {}
167
168 virtual void ItemAdded(const std::string& name,
169 const base::PlatformFileInfo& info) OVERRIDE {
170 items_.push_back(name);
171 }
172
173 virtual void NoMoreItems() OVERRIDE {
174 completed_ = true;
175 }
176
177 virtual void DownloadedFile(const std::string& name,
178 base::PlatformFileError error) OVERRIDE {
179 EXPECT_TRUE(content::BrowserThread::CurrentlyOn(
180 content::BrowserThread::UI));
181 downloads_.push_back(name);
182 last_error_ = error;
183 }
184
185 virtual void DeviceRemoved() OVERRIDE {
186 removed_ = true;
187 }
188
189 std::vector<std::string> items() const { return items_; }
190 std::vector<std::string> downloads() const { return downloads_; }
191 bool completed() const { return completed_; }
192 bool removed() const { return removed_; }
193 base::PlatformFileError last_error() const { return last_error_; }
194
195 private:
196 std::vector<std::string> items_;
197 std::vector<std::string> downloads_;
198 bool completed_;
199 bool removed_;
200 base::PlatformFileError last_error_;
201 };
202
203 class ImageCaptureDeviceManagerTest : public testing::Test {
204 public:
205 virtual void SetUp() OVERRIDE {
206 base::SystemMonitor::AllocateSystemIOPorts();
207 system_monitor_.reset(new base::SystemMonitor());
208 ui_thread_.reset(new content::TestBrowserThread(
209 content::BrowserThread::UI, &message_loop_));
210 }
211
212 MockICCameraDevice* AttachDevice(
213 chrome::ImageCaptureDeviceManager* manager) {
214 // Ownership will be passed to the device browser delegate.
215 scoped_nsobject<MockICCameraDevice> device(
216 [[MockICCameraDevice alloc] init]);
217 id<ICDeviceBrowserDelegate> delegate = manager->device_browser();
218 [delegate deviceBrowser:nil didAddDevice:device moreComing:NO];
219 return device.autorelease();
220 }
221
222 void DetachDevice(chrome::ImageCaptureDeviceManager* manager,
223 ICCameraDevice* device) {
224 id<ICDeviceBrowserDelegate> delegate = manager->device_browser();
225 [delegate deviceBrowser:nil didRemoveDevice:device moreGoing:NO];
226 }
227
228 protected:
229 MessageLoopForUI message_loop_;
230 scoped_ptr<content::TestBrowserThread> ui_thread_;
231 scoped_ptr<base::SystemMonitor> system_monitor_;
232 TestCameraListener listener_;
233 };
234
235 TEST_F(ImageCaptureDeviceManagerTest, TestAttachDetach) {
236 chrome::ImageCaptureDeviceManager manager;
237 ICCameraDevice* device = AttachDevice(&manager);
238 std::vector<base::SystemMonitor::RemovableStorageInfo> devices =
239 system_monitor_->GetAttachedRemovableStorage();
240
241 ASSERT_EQ(1U, devices.size());
242 EXPECT_EQ(std::string("ic:") + kDeviceId, devices[0].device_id);
243
244 DetachDevice(&manager, device);
245 devices = system_monitor_->GetAttachedRemovableStorage();
246 ASSERT_EQ(0U, devices.size());
247 };
248
249 TEST_F(ImageCaptureDeviceManagerTest, OpenCamera) {
250 chrome::ImageCaptureDeviceManager manager;
251 ICCameraDevice* device = AttachDevice(&manager);
252
253 EXPECT_FALSE(chrome::ImageCaptureDeviceManager::deviceForUUID(
254 "nonexistent"));
255
256 scoped_nsobject<ImageCaptureDevice> camera(
257 [chrome::ImageCaptureDeviceManager::deviceForUUID(kDeviceId)
258 retain]);
259
260 [camera setListener:listener_.AsWeakPtr()];
261 [camera open];
262
263 scoped_nsobject<MockICCameraFile> picture1(
264 [[MockICCameraFile alloc] init:@"pic1"]);
265 [camera cameraDevice:nil didAddItem:picture1];
266 scoped_nsobject<MockICCameraFile> picture2(
267 [[MockICCameraFile alloc] init:@"pic2"]);
268 [camera cameraDevice:nil didAddItem:picture2];
269 ASSERT_EQ(2U, listener_.items().size());
270 EXPECT_EQ("pic1", listener_.items()[0]);
271 EXPECT_EQ("pic2", listener_.items()[1]);
272 EXPECT_FALSE(listener_.completed());
273
274 [camera deviceDidBecomeReadyWithCompleteContentCatalog:nil];
275 ASSERT_EQ(2U, listener_.items().size());
276 EXPECT_TRUE(listener_.completed());
277
278 [camera close];
279 DetachDevice(&manager, device);
280 EXPECT_FALSE(chrome::ImageCaptureDeviceManager::deviceForUUID(
281 kDeviceId));
282 }
283
284 TEST_F(ImageCaptureDeviceManagerTest, RemoveCamera) {
285 chrome::ImageCaptureDeviceManager manager;
286 ICCameraDevice* device = AttachDevice(&manager);
287
288 scoped_nsobject<ImageCaptureDevice> camera(
289 [chrome::ImageCaptureDeviceManager::deviceForUUID(kDeviceId)
290 retain]);
291
292 [camera setListener:listener_.AsWeakPtr()];
293 [camera open];
294
295 [camera didRemoveDevice:device];
296 EXPECT_TRUE(listener_.removed());
297 }
298
299 TEST_F(ImageCaptureDeviceManagerTest, DownloadFile) {
300 scoped_ptr<content::TestBrowserThread> file_thread_(
301 new content::TestBrowserThread(
302 content::BrowserThread::FILE, &message_loop_));
303
304 chrome::ImageCaptureDeviceManager manager;
305 MockICCameraDevice* device = AttachDevice(&manager);
306
307 scoped_nsobject<ImageCaptureDevice> camera(
308 [chrome::ImageCaptureDeviceManager::deviceForUUID(kDeviceId)
309 retain]);
310
311 [camera setListener:listener_.AsWeakPtr()];
312 [camera open];
313
314 std::string kTestFileName("pic1");
315
316 scoped_nsobject<MockICCameraFile> picture1(
317 [[MockICCameraFile alloc]
318 init:base::SysUTF8ToNSString(kTestFileName)]);
319 [device addMediaFile:picture1];
320 [camera cameraDevice:nil didAddItem:picture1];
321
322 base::ScopedTempDir temp_dir;
323 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
324
325 EXPECT_EQ(0U, listener_.downloads().size());
326
327 // Test that a nonexistent file we ask to be downloaded will
328 // return us a not-found error.
329 FilePath temp_file = temp_dir.path().Append("tempfile");
330 [camera downloadFile:std::string("nonexistent") localPath:temp_file];
331 message_loop_.RunUntilIdle();
332 ASSERT_EQ(1U, listener_.downloads().size());
333 EXPECT_EQ("nonexistent", listener_.downloads()[0]);
334 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, listener_.last_error());
335
336 // Test that an existing file we ask to be downloaded will end up in
337 // the location we specify. The mock system will copy testing file
338 // contents to a separate filename, mimicking the ImageCaptureCore
339 // library behavior. Our code then renames the file onto the requested
340 // destination.
341 [camera downloadFile:kTestFileName localPath:temp_file];
342 message_loop_.RunUntilIdle();
343
344 ASSERT_EQ(2U, listener_.downloads().size());
345 EXPECT_EQ(kTestFileName, listener_.downloads()[1]);
346 ASSERT_EQ(base::PLATFORM_FILE_OK, listener_.last_error());
347 char file_contents[5];
348 ASSERT_EQ(4, file_util::ReadFile(temp_file, file_contents,
349 strlen(kTestFileContents)));
350 EXPECT_EQ(kTestFileContents,
351 std::string(file_contents, strlen(kTestFileContents)));
352
353 [camera didRemoveDevice:device];
354 }
OLDNEW
« no previous file with comments | « chrome/browser/system_monitor/image_capture_device_manager.mm ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698