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

Side by Side Diff: chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac_unittest.mm

Issue 11416089: [Media Galleries] Filesystem interface for Mac PTP/MTP devices using ImageCaptureCore (part 3) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Review fixes 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
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 #import <Foundation/Foundation.h>
6 #import <ImageCaptureCore/ImageCaptureCore.h>
7
8 #include "base/mac/foundation_util.h"
9 #include "base/memory/scoped_nsobject.h"
10 #include "base/message_loop.h"
11 #include "base/sys_string_conversions.h"
12 #include "base/system_monitor/system_monitor.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.h"
15 #include "chrome/browser/system_monitor/image_capture_device_manager.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "webkit/fileapi/file_system_file_util.h"
20
21 namespace {
22
23 const char kDeviceId[] = "id";
24 const char kTestFileContents[] = "test";
25
26 } // namespace
27
28 @interface MockMTPICCameraDevice : ICCameraDevice {
29 @private
30 scoped_nsobject<NSMutableArray> allMediaFiles_;
31 }
32
33 - (void)addMediaFile:(ICCameraFile*)file;
34
35 @end
36
37 @implementation MockMTPICCameraDevice
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 alloc] init]);
68 //rrayWithCapacity:1]);
sail 2013/01/03 23:10:43 remove
Greg Billock 2013/01/04 18:05:05 Done.
69 [allMediaFiles_ addObject:file];
70 }
71
72 - (void)requestDownloadFile:(ICCameraFile*)file
73 options:(NSDictionary*)options
74 downloadDelegate:(id<ICCameraDeviceDownloadDelegate>)downloadDelegate
75 didDownloadSelector:(SEL)selector
76 contextInfo:(void*)contextInfo {
77 FilePath saveDir(base::SysNSStringToUTF8(
78 [[options objectForKey:ICDownloadsDirectoryURL] path]));
79 std::string saveAsFilename =
80 base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]);
81 // It appears that the ImageCapture library adds an extension to the requested
82 // filename. Do that here to require a rename.
83 saveAsFilename += ".jpg";
84 FilePath toBeSaved = saveDir.Append(saveAsFilename);
85 ASSERT_EQ(static_cast<int>(strlen(kTestFileContents)),
86 file_util::WriteFile(toBeSaved, kTestFileContents,
87 strlen(kTestFileContents)));
88
89 NSMutableDictionary* returnOptions =
90 [NSMutableDictionary dictionaryWithDictionary:options];
91 [returnOptions setObject:base::SysUTF8ToNSString(saveAsFilename)
92 forKey:ICSavedFilename];
93
94 [downloadDelegate didDownloadFile:file
95 error:nil
96 options:returnOptions
97 contextInfo:contextInfo];
98 }
99
100 @end
101
102 @interface MockMTPICCameraFile : ICCameraFile {
103 @private
104 scoped_nsobject<NSString> name_;
105 scoped_nsobject<NSDate> date_;
106 }
107
108 - (id)init:(NSString*)name;
109
110 @end
111
112 @implementation MockMTPICCameraFile
113
114 - (id)init:(NSString*)name {
115 if ((self = [super init])) {
116 name_.reset([NSString stringWithString:name]);
sail 2013/01/03 23:10:43 retain actually, you can just do [name retain]
Greg Billock 2013/01/04 18:05:05 Done.
117 date_.reset([[NSDate dateWithNaturalLanguageString:@"12/12/12"] retain]);
118 }
119 return self;
120 }
121
122 - (NSString*)name {
123 return name_.get();
124 }
125
126 - (NSString*)UTI {
127 return base::mac::CFToNSCast(kUTTypeImage);
128 }
129
130 - (NSDate*)modificationDate {
131 return date_.get();
132 }
133
134 - (NSDate*)creationDate {
135 return date_.get();
136 }
137
138 - (off_t)fileSize {
139 return 1000;
140 }
141
142 @end
143
144 class MTPDeviceDelegateImplMacTest : public testing::Test {
145 public:
146 MTPDeviceDelegateImplMacTest() : camera_(NULL), delegate_(NULL) {}
147
148 virtual void SetUp() OVERRIDE {
149 base::SystemMonitor::AllocateSystemIOPorts();
150 system_monitor_.reset(new base::SystemMonitor());
151 ui_thread_.reset(new content::TestBrowserThread(
152 content::BrowserThread::UI, &message_loop_));
153
154 camera_ = [MockMTPICCameraDevice alloc];
155 id<ICDeviceBrowserDelegate> delegate = manager_.device_browser();
156 [delegate deviceBrowser:nil didAddDevice:camera_ moreComing:NO];
157
158 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
159 task_runner_ = pool->GetSequencedTaskRunner(
160 pool->GetNamedSequenceToken("token-name"));
161 delegate_ = new chrome::MTPDeviceDelegateImplMac(
162 "/ic:id", task_runner_.get());
163 }
164
165 virtual void TearDown() OVERRIDE {
166 id<ICDeviceBrowserDelegate> delegate = manager_.device_browser();
167 [delegate deviceBrowser:nil didRemoveDevice:camera_ moreGoing:NO];
168
169 task_runner_->PostTask(FROM_HERE,
170 base::Bind(&chrome::MTPDeviceDelegateImplMac::
171 CancelPendingTasksAndDeleteDelegate,
172 base::Unretained(delegate_)));
173 }
174
175 protected:
176 MessageLoopForUI message_loop_;
177 scoped_ptr<content::TestBrowserThread> ui_thread_;
178 scoped_ptr<base::SystemMonitor> system_monitor_;
179 chrome::ImageCaptureDeviceManager manager_;
180 ICCameraDevice* camera_;
181 scoped_refptr<base::SequencedTaskRunner> task_runner_;
182
183 // This object needs special deletion inside the above |task_runner_|.
184 chrome::MTPDeviceDelegateImplMac* delegate_;
185 };
sail 2013/01/03 23:10:43 DISALLOW copy/assign
Greg Billock 2013/01/04 18:05:05 Done.
186
187 TEST_F(MTPDeviceDelegateImplMacTest, TestGetRootFileInfo) {
188 base::PlatformFileInfo info;
189 // Making a fresh delegate should have a single file entry for the synthetic
190 // root directory, with the name equal to the device id string.
191 EXPECT_EQ(base::PLATFORM_FILE_OK,
192 delegate_->GetFileInfo(FilePath("/ic:id"), &info));
193 EXPECT_TRUE(info.is_directory);
194 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
195 delegate_->GetFileInfo(FilePath("/nonexistent"), &info));
196
197 // Signal the delegate that no files are coming.
198 delegate_->NoMoreItems();
199
200 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> enumerator =
201 delegate_->CreateFileEnumerator(FilePath("/ic:id"), true);
202 EXPECT_TRUE(enumerator->Next().empty());
203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698