| OLD | NEW |
| (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 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(static_cast<int>(strlen(kTestFileContents)), |
| 85 file_util::WriteFile(toBeSaved, kTestFileContents, |
| 86 strlen(kTestFileContents))); |
| 87 |
| 88 NSMutableDictionary* returnOptions = |
| 89 [NSMutableDictionary dictionaryWithDictionary:options]; |
| 90 [returnOptions setObject:base::SysUTF8ToNSString(saveAsFilename) |
| 91 forKey:ICSavedFilename]; |
| 92 |
| 93 [downloadDelegate didDownloadFile:file |
| 94 error:nil |
| 95 options:returnOptions |
| 96 contextInfo:contextInfo]; |
| 97 } |
| 98 |
| 99 @end |
| 100 |
| 101 @interface MockMTPICCameraFile : ICCameraFile { |
| 102 @private |
| 103 scoped_nsobject<NSString> name_; |
| 104 scoped_nsobject<NSDate> date_; |
| 105 } |
| 106 |
| 107 - (id)init:(NSString*)name; |
| 108 |
| 109 @end |
| 110 |
| 111 @implementation MockMTPICCameraFile |
| 112 |
| 113 - (id)init:(NSString*)name { |
| 114 if ((self = [super init])) { |
| 115 name_.reset([NSString stringWithString:name]); |
| 116 date_.reset([[NSDate dateWithNaturalLanguageString:@"12/12/12"] retain]); |
| 117 } |
| 118 return self; |
| 119 } |
| 120 |
| 121 - (NSString*)name { |
| 122 return name_.get(); |
| 123 } |
| 124 |
| 125 - (NSString*)UTI { |
| 126 return base::mac::CFToNSCast(kUTTypeImage); |
| 127 } |
| 128 |
| 129 - (NSDate*)modificationDate { |
| 130 return date_.get(); |
| 131 } |
| 132 |
| 133 - (NSDate*)creationDate { |
| 134 return date_.get(); |
| 135 } |
| 136 |
| 137 - (off_t)fileSize { |
| 138 return 1000; |
| 139 } |
| 140 |
| 141 @end |
| 142 |
| 143 class MTPDeviceDelegateImplMacTest : public testing::Test { |
| 144 public: |
| 145 virtual void SetUp() OVERRIDE { |
| 146 base::SystemMonitor::AllocateSystemIOPorts(); |
| 147 system_monitor_.reset(new base::SystemMonitor()); |
| 148 ui_thread_.reset(new content::TestBrowserThread( |
| 149 content::BrowserThread::UI, &message_loop_)); |
| 150 |
| 151 camera_ = [MockMTPICCameraDevice alloc]; |
| 152 id<ICDeviceBrowserDelegate> delegate = manager_.device_browser(); |
| 153 [delegate deviceBrowser:nil didAddDevice:camera_ moreComing:NO]; |
| 154 |
| 155 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| 156 task_runner_ = pool->GetSequencedTaskRunner( |
| 157 pool->GetNamedSequenceToken("token-name")); |
| 158 delegate_ = new chrome::MTPDeviceDelegateImplMac( |
| 159 "/ic:id", task_runner_.get()); |
| 160 } |
| 161 |
| 162 virtual void TearDown() OVERRIDE { |
| 163 id<ICDeviceBrowserDelegate> delegate = manager_.device_browser(); |
| 164 [delegate deviceBrowser:nil didRemoveDevice:camera_ moreGoing:NO]; |
| 165 |
| 166 task_runner_->PostTask(FROM_HERE, |
| 167 base::Bind(&chrome::MTPDeviceDelegateImplMac:: |
| 168 CancelPendingTasksAndDeleteDelegate, |
| 169 base::Unretained(delegate_))); |
| 170 } |
| 171 |
| 172 protected: |
| 173 MessageLoopForUI message_loop_; |
| 174 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 175 scoped_ptr<base::SystemMonitor> system_monitor_; |
| 176 chrome::ImageCaptureDeviceManager manager_; |
| 177 ICCameraDevice* camera_; |
| 178 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 179 |
| 180 // This object needs special deletion inside the above |task_runner_|. |
| 181 chrome::MTPDeviceDelegateImplMac* delegate_; |
| 182 }; |
| 183 |
| 184 TEST_F(MTPDeviceDelegateImplMacTest, TestGetRootFileInfo) { |
| 185 base::PlatformFileInfo info; |
| 186 // Making a fresh delegate should have a single file entry for the synthetic |
| 187 // root directory, with the name equal to the device id string. |
| 188 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 189 delegate_->GetFileInfo(FilePath("/ic:id"), &info)); |
| 190 EXPECT_TRUE(info.is_directory); |
| 191 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 192 delegate_->GetFileInfo(FilePath("/nonexistent"), &info)); |
| 193 |
| 194 // Signal the delegate that no files are coming. |
| 195 delegate_->NoMoreItems(); |
| 196 |
| 197 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> enumerator = |
| 198 delegate_->CreateFileEnumerator(FilePath("/ic:id"), true); |
| 199 EXPECT_TRUE(enumerator->Next().empty()); |
| 200 } |
| OLD | NEW |