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

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

Issue 11442057: [Media Galleries] Add an ImageCaptureCore listener for Mac. (part 2) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add test for downloadFile Created 8 years 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 "base/thread_task_runner_handle.h"
17 #include "chrome/browser/system_monitor/image_capture_device.h"
18 #include "chrome/browser/system_monitor/image_capture_device_manager.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace {
23
24 const char kDeviceId[] = "id";
25
26 } // namespace
27
28 @interface MockICCameraDevice : ICCameraDevice {
29 @private
30 scoped_nsobject<NSMutableArray> allMediaFiles_;
31 }
32
33 - (void)addMediaFile:(ICCameraFile*)file;
34
35 @end
36
37 @implementation MockICCameraDevice
38
39 - (NSString*)mountPoint {
40 return @"mountPoint";
41 }
42
43 - (NSString*)name {
44 return @"name";
45 }
46
47 - (NSString*)UUIDString {
48 return base::SysUTF8ToNSString(kDeviceId);
49 }
50
51 - (ICDeviceType)type {
52 return ICDeviceTypeCamera;
53 }
54
55 - (void)requestOpenSession {
56 }
57
58 - (void)requestCloseSession {
59 }
60
61 - (NSArray*)mediaFiles {
62 return allMediaFiles_;
63 }
64
65 - (void)addMediaFile:(ICCameraFile*)file {
66 if (!allMediaFiles_.get())
67 allMediaFiles_.reset([NSMutableArray arrayWithCapacity:1]);
68 [allMediaFiles_ addObject:file];
69 }
70
71 - (void)requestDownloadFile:(ICCameraFile*)file
72 options:(NSDictionary*)options
73 downloadDelegate:(id<ICCameraDeviceDownloadDelegate>)downloadDelegate
74 didDownloadSelector:(SEL)selector
75 contextInfo:(void*)contextInfo {
76 FilePath saveDir(base::SysNSStringToUTF8(
77 [[options objectForKey:ICDownloadsDirectoryURL] path]));
78 std::string saveAsFilename =
79 base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]);
80 // It appears that the ImageCapture library adds an extension to the requested
81 // filename. Do that here to require a rename.
82 saveAsFilename += ".jpg";
83 FilePath toBeSaved = saveDir.Append(saveAsFilename);
84 ASSERT_EQ(4, file_util::WriteFile(toBeSaved, "test", 4));
85
86 NSMutableDictionary* returnOptions =
87 [NSMutableDictionary dictionaryWithDictionary:options];
88 [returnOptions setObject:base::SysUTF8ToNSString(saveAsFilename)
89 forKey:ICSavedFilename];
90
91 [downloadDelegate didDownloadFile:file
92 error:nil
93 options:returnOptions
94 contextInfo:contextInfo];
95 }
96
97 @end
98
99 @interface MockICCameraFile : ICCameraFile {
100 @private
101 scoped_nsobject<NSString> name_;
102 scoped_nsobject<NSDate> date_;
103 }
104
105 - (id)init:(NSString*)name;
106
107 @end
108
109 @implementation MockICCameraFile
110
111 - (id)init:(NSString*)name {
112 if ((self = [super init])) {
113 name_.reset(name);
114 date_.reset([[NSDate dateWithNaturalLanguageString:@"12/12/12"] retain]);
115 }
116 return self;
117 }
118
119 - (NSString*)name {
120 return name_.get();
121 }
122
123 - (NSString*)UTI {
124 return base::mac::CFToNSCast(kUTTypeImage);
125 }
126
127 - (NSDate*)modificationDate {
128 return date_.get();
129 }
130
131 - (NSDate*)creationDate {
132 return date_.get();
133 }
134
135 - (off_t)fileSize {
136 return 1000;
137 }
138
139 @end
140
141 class TestCameraListener
142 : public ImageCaptureDeviceListener,
143 public base::SupportsWeakPtr<TestCameraListener> {
144 public:
145 TestCameraListener() : completed_(false), removed_(false) {}
146 virtual ~TestCameraListener() {}
147
148 virtual void ItemAdded(const std::string& name,
149 const base::PlatformFileInfo& info) OVERRIDE {
150 items_.push_back(name);
151 }
152
153 virtual void NoMoreItems() OVERRIDE {
154 completed_ = true;
155 }
156
157 virtual void DownloadedFile(const std::string& name,
158 base::PlatformFileError error) OVERRIDE {
159 downloads_.push_back(name);
sail 2012/12/21 02:47:10 check the thread ID here?
Greg Billock 2012/12/21 20:05:38 Done.
160 last_error_ = error;
161 }
162
163 virtual void DeviceRemoved() OVERRIDE {
164 removed_ = true;
165 }
166
167 std::vector<std::string> items() { return items_; }
168 std::vector<std::string> downloads() { return downloads_; }
169 bool completed() { return completed_; }
170 bool removed() { return removed_; }
171 base::PlatformFileError last_error() { return last_error_; }
172
173 private:
174 std::vector<std::string> items_;
175 std::vector<std::string> downloads_;
176 bool completed_;
177 bool removed_;
178 base::PlatformFileError last_error_;
179 };
180
181 class ImageCaptureDeviceManagerTest : public testing::Test {
182 public:
183 virtual void SetUp() OVERRIDE {
184 base::SystemMonitor::AllocateSystemIOPorts();
185 system_monitor_.reset(new base::SystemMonitor());
186 ui_thread_.reset(new content::TestBrowserThread(
187 content::BrowserThread::UI, &message_loop_));
188 }
189
190 ICCameraDevice* AttachDevice(
191 chrome::ImageCaptureDeviceManager* manager) {
192 // Ownership will be passed to the device browser delegate.
193 ICCameraDevice* device = [MockICCameraDevice alloc];
sail 2012/12/21 02:47:10 should be MockICCameraDevice* device = [[[MockICCa
194 id<ICDeviceBrowserDelegate> delegate = manager->device_browser();
195 [delegate deviceBrowser:nil didAddDevice:device moreComing:NO];
196 return device;
197 }
198
199 void DetachDevice(chrome::ImageCaptureDeviceManager* manager,
200 ICCameraDevice* device) {
201 id<ICDeviceBrowserDelegate> delegate = manager->device_browser();
202 [delegate deviceBrowser:nil didRemoveDevice:device moreGoing:NO];
203 }
204
205 protected:
206 MessageLoopForUI message_loop_;
207 scoped_ptr<content::TestBrowserThread> ui_thread_;
208 scoped_ptr<base::SystemMonitor> system_monitor_;
209 TestCameraListener listener_;
210 };
211
212 TEST_F(ImageCaptureDeviceManagerTest, TestAttachDetach) {
213 chrome::ImageCaptureDeviceManager manager;
214 ICCameraDevice* device = AttachDevice(&manager);
215
216 std::vector<base::SystemMonitor::RemovableStorageInfo> devices =
217 system_monitor_->GetAttachedRemovableStorage();
218
219 ASSERT_EQ(1U, devices.size());
220 EXPECT_EQ(std::string("ic:") + kDeviceId, devices[0].device_id);
221
222 DetachDevice(&manager, device);
223 devices = system_monitor_->GetAttachedRemovableStorage();
224 ASSERT_EQ(0U, devices.size());
225 };
226
227 TEST_F(ImageCaptureDeviceManagerTest, OpenCamera) {
228 chrome::ImageCaptureDeviceManager manager;
229 ICCameraDevice* device = AttachDevice(&manager);
230
231 EXPECT_FALSE(chrome::ImageCaptureDeviceManager::deviceForUUID(
232 "nonexistent"));
233
234 scoped_nsobject<ImageCaptureDevice> camera(
235 [chrome::ImageCaptureDeviceManager::deviceForUUID(kDeviceId)
236 retain]);
237
238 [camera setListener:listener_.AsWeakPtr()];
239 [camera open];
240
241 scoped_nsobject<MockICCameraFile> picture1(
242 [[MockICCameraFile alloc] init:@"pic1"]);
243 [camera cameraDevice:nil didAddItem:picture1];
244 scoped_nsobject<MockICCameraFile> picture2(
245 [[MockICCameraFile alloc] init:@"pic2"]);
246 [camera cameraDevice:nil didAddItem:picture2];
247 ASSERT_EQ(2U, listener_.items().size());
248 EXPECT_EQ("pic1", listener_.items()[0]);
249 EXPECT_EQ("pic2", listener_.items()[1]);
250 EXPECT_FALSE(listener_.completed());
251
252 [camera deviceDidBecomeReadyWithCompleteContentCatalog:nil];
253 ASSERT_EQ(2U, listener_.items().size());
254 EXPECT_TRUE(listener_.completed());
255
256 [camera close];
257 DetachDevice(&manager, device);
258 EXPECT_FALSE(chrome::ImageCaptureDeviceManager::deviceForUUID(
259 kDeviceId));
260 }
261
262 TEST_F(ImageCaptureDeviceManagerTest, RemoveCamera) {
263 chrome::ImageCaptureDeviceManager manager;
264 ICCameraDevice* device = AttachDevice(&manager);
265
266 scoped_nsobject<ImageCaptureDevice> camera(
267 [chrome::ImageCaptureDeviceManager::deviceForUUID(kDeviceId)
268 retain]);
269
270 [camera setListener:listener_.AsWeakPtr()];
271 [camera open];
272
273 [camera didRemoveDevice:device];
274 EXPECT_TRUE(listener_.removed());
275 }
276
277 TEST_F(ImageCaptureDeviceManagerTest, DownloadFile) {
278 scoped_ptr<content::TestBrowserThread> file_thread_(
279 new content::TestBrowserThread(
280 content::BrowserThread::FILE, &message_loop_));
281
282 chrome::ImageCaptureDeviceManager manager;
283 ICCameraDevice* device = AttachDevice(&manager);
284
285 scoped_nsobject<ImageCaptureDevice> camera(
286 [chrome::ImageCaptureDeviceManager::deviceForUUID(kDeviceId)
287 retain]);
288
289 [camera setListener:listener_.AsWeakPtr()];
290 [camera open];
291
292 scoped_nsobject<MockICCameraFile> picture1(
293 [[MockICCameraFile alloc] init:@"pic1"]);
294 [base::mac::ObjCCastStrict<MockICCameraDevice>(device)
sail 2012/12/21 02:47:10 should remove the cast and have AttachedDevice ret
Greg Billock 2012/12/21 20:05:38 I wanted to do this, but when I did, the call to
sail 2012/12/26 18:52:37 Hm.. that's really weird, it shouldn't fail. What'
sail 2012/12/26 20:48:47 There's no strong typing on method dispatch. If do
Greg Billock 2013/01/03 20:59:55 OK, it must have been some other simultaneous chan
sail 2013/01/03 21:50:28 Ahh cool.
295 addMediaFile:picture1];
296 [camera cameraDevice:nil didAddItem:picture1];
297
298 base::ScopedTempDir temp_dir;
299 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
300
301 ASSERT_EQ(0U, listener_.downloads().size());
sail 2012/12/21 02:47:10 expect_eq
Greg Billock 2012/12/21 20:05:38 Done.
302
303 [camera downloadFile:std::string("nonexistent")
304 localPath:temp_dir.path().Append("tempfile")];
sail 2012/12/21 02:47:10 make tempfile a const above this line?
Greg Billock 2012/12/21 20:05:38 Done.
305 message_loop_.RunUntilIdle();
306 ASSERT_EQ(1U, listener_.downloads().size());
307 EXPECT_EQ("nonexistent", listener_.downloads()[0]);
308 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, listener_.last_error());
309
310 [camera downloadFile:std::string("pic1")
sail 2012/12/21 02:47:10 make pic1 a constant above this line
Greg Billock 2012/12/21 20:05:38 Done, kinda. I can't figure out how to make @"pic1
311 localPath:temp_dir.path().Append("tempfile")];
312 message_loop_.RunUntilIdle();
313
314 ASSERT_EQ(2U, listener_.downloads().size());
315 EXPECT_EQ("pic1", listener_.downloads()[1]);
316 ASSERT_EQ(base::PLATFORM_FILE_OK, listener_.last_error());
317 char file_contents[5];
318 ASSERT_EQ(4, file_util::ReadFile(temp_dir.path().Append("tempfile"),
319 file_contents, 4));
320 EXPECT_EQ("test", std::string(file_contents, 4));
sail 2012/12/21 02:47:10 make "test" a constant at the top of this file?
Greg Billock 2012/12/21 20:05:38 Done.
321 }
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