Chromium Code Reviews| Index: chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac_unittest.mm |
| diff --git a/chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac_unittest.mm b/chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e6159300f913151c94554ff9671f69baf4ac59b3 |
| --- /dev/null |
| +++ b/chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac_unittest.mm |
| @@ -0,0 +1,203 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import <Foundation/Foundation.h> |
| +#import <ImageCaptureCore/ImageCaptureCore.h> |
| + |
| +#include "base/mac/foundation_util.h" |
| +#include "base/memory/scoped_nsobject.h" |
| +#include "base/message_loop.h" |
| +#include "base/sys_string_conversions.h" |
| +#include "base/system_monitor/system_monitor.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.h" |
| +#include "chrome/browser/system_monitor/image_capture_device_manager.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webkit/fileapi/file_system_file_util.h" |
| + |
| +namespace { |
| + |
| +const char kDeviceId[] = "id"; |
| +const char kTestFileContents[] = "test"; |
| + |
| +} // namespace |
| + |
| +@interface MockMTPICCameraDevice : ICCameraDevice { |
| + @private |
| + scoped_nsobject<NSMutableArray> allMediaFiles_; |
| +} |
| + |
| +- (void)addMediaFile:(ICCameraFile*)file; |
| + |
| +@end |
| + |
| +@implementation MockMTPICCameraDevice |
| + |
| +- (NSString*)mountPoint { |
| + return @"mountPoint"; |
| +} |
| + |
| +- (NSString*)name { |
| + return @"name"; |
| +} |
| + |
| +- (NSString*)UUIDString { |
| + return base::SysUTF8ToNSString(kDeviceId); |
| +} |
| + |
| +- (ICDeviceType)type { |
| + return ICDeviceTypeCamera; |
| +} |
| + |
| +- (void)requestOpenSession { |
| +} |
| + |
| +- (void)requestCloseSession { |
| +} |
| + |
| +- (NSArray*)mediaFiles { |
| + return allMediaFiles_; |
| +} |
| + |
| +- (void)addMediaFile:(ICCameraFile*)file { |
| + if (!allMediaFiles_.get()) |
| + allMediaFiles_.reset([[NSMutableArray alloc] init]); |
| +//rrayWithCapacity:1]); |
|
sail
2013/01/03 23:10:43
remove
Greg Billock
2013/01/04 18:05:05
Done.
|
| + [allMediaFiles_ addObject:file]; |
| +} |
| + |
| +- (void)requestDownloadFile:(ICCameraFile*)file |
| + options:(NSDictionary*)options |
| + downloadDelegate:(id<ICCameraDeviceDownloadDelegate>)downloadDelegate |
| + didDownloadSelector:(SEL)selector |
| + contextInfo:(void*)contextInfo { |
| + FilePath saveDir(base::SysNSStringToUTF8( |
| + [[options objectForKey:ICDownloadsDirectoryURL] path])); |
| + std::string saveAsFilename = |
| + base::SysNSStringToUTF8([options objectForKey:ICSaveAsFilename]); |
| + // It appears that the ImageCapture library adds an extension to the requested |
| + // filename. Do that here to require a rename. |
| + saveAsFilename += ".jpg"; |
| + FilePath toBeSaved = saveDir.Append(saveAsFilename); |
| + ASSERT_EQ(static_cast<int>(strlen(kTestFileContents)), |
| + file_util::WriteFile(toBeSaved, kTestFileContents, |
| + strlen(kTestFileContents))); |
| + |
| + NSMutableDictionary* returnOptions = |
| + [NSMutableDictionary dictionaryWithDictionary:options]; |
| + [returnOptions setObject:base::SysUTF8ToNSString(saveAsFilename) |
| + forKey:ICSavedFilename]; |
| + |
| + [downloadDelegate didDownloadFile:file |
| + error:nil |
| + options:returnOptions |
| + contextInfo:contextInfo]; |
| +} |
| + |
| +@end |
| + |
| +@interface MockMTPICCameraFile : ICCameraFile { |
| + @private |
| + scoped_nsobject<NSString> name_; |
| + scoped_nsobject<NSDate> date_; |
| +} |
| + |
| +- (id)init:(NSString*)name; |
| + |
| +@end |
| + |
| +@implementation MockMTPICCameraFile |
| + |
| +- (id)init:(NSString*)name { |
| + if ((self = [super init])) { |
| + 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.
|
| + date_.reset([[NSDate dateWithNaturalLanguageString:@"12/12/12"] retain]); |
| + } |
| + return self; |
| +} |
| + |
| +- (NSString*)name { |
| + return name_.get(); |
| +} |
| + |
| +- (NSString*)UTI { |
| + return base::mac::CFToNSCast(kUTTypeImage); |
| +} |
| + |
| +- (NSDate*)modificationDate { |
| + return date_.get(); |
| +} |
| + |
| +- (NSDate*)creationDate { |
| + return date_.get(); |
| +} |
| + |
| +- (off_t)fileSize { |
| + return 1000; |
| +} |
| + |
| +@end |
| + |
| +class MTPDeviceDelegateImplMacTest : public testing::Test { |
| + public: |
| + MTPDeviceDelegateImplMacTest() : camera_(NULL), delegate_(NULL) {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + base::SystemMonitor::AllocateSystemIOPorts(); |
| + system_monitor_.reset(new base::SystemMonitor()); |
| + ui_thread_.reset(new content::TestBrowserThread( |
| + content::BrowserThread::UI, &message_loop_)); |
| + |
| + camera_ = [MockMTPICCameraDevice alloc]; |
| + id<ICDeviceBrowserDelegate> delegate = manager_.device_browser(); |
| + [delegate deviceBrowser:nil didAddDevice:camera_ moreComing:NO]; |
| + |
| + base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| + task_runner_ = pool->GetSequencedTaskRunner( |
| + pool->GetNamedSequenceToken("token-name")); |
| + delegate_ = new chrome::MTPDeviceDelegateImplMac( |
| + "/ic:id", task_runner_.get()); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + id<ICDeviceBrowserDelegate> delegate = manager_.device_browser(); |
| + [delegate deviceBrowser:nil didRemoveDevice:camera_ moreGoing:NO]; |
| + |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&chrome::MTPDeviceDelegateImplMac:: |
| + CancelPendingTasksAndDeleteDelegate, |
| + base::Unretained(delegate_))); |
| + } |
| + |
| + protected: |
| + MessageLoopForUI message_loop_; |
| + scoped_ptr<content::TestBrowserThread> ui_thread_; |
| + scoped_ptr<base::SystemMonitor> system_monitor_; |
| + chrome::ImageCaptureDeviceManager manager_; |
| + ICCameraDevice* camera_; |
| + scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| + |
| + // This object needs special deletion inside the above |task_runner_|. |
| + chrome::MTPDeviceDelegateImplMac* delegate_; |
| +}; |
|
sail
2013/01/03 23:10:43
DISALLOW copy/assign
Greg Billock
2013/01/04 18:05:05
Done.
|
| + |
| +TEST_F(MTPDeviceDelegateImplMacTest, TestGetRootFileInfo) { |
| + base::PlatformFileInfo info; |
| + // Making a fresh delegate should have a single file entry for the synthetic |
| + // root directory, with the name equal to the device id string. |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, |
| + delegate_->GetFileInfo(FilePath("/ic:id"), &info)); |
| + EXPECT_TRUE(info.is_directory); |
| + EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| + delegate_->GetFileInfo(FilePath("/nonexistent"), &info)); |
| + |
| + // Signal the delegate that no files are coming. |
| + delegate_->NoMoreItems(); |
| + |
| + scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> enumerator = |
| + delegate_->CreateFileEnumerator(FilePath("/ic:id"), true); |
| + EXPECT_TRUE(enumerator->Next().empty()); |
| +} |