Commit 31c81b78 authored by andresantoso's avatar andresantoso Committed by Commit bot

MacViews: Get OSExchangeDataTest to pass.

Add filenames support. DO_NOT_CONVERT_FILENAMES policy is not
supported because writing NSFilenamesPboardType to NSPasteboard always
populates the URL types.

Modify SetURL so that it doesn't overwrite previous SetString.

The following tests now passes:
OSExchangeDataTest.URLAndString
OSExchangeDataTest.TestFileToURLConversion

BUG=378134

Review URL: https://codereview.chromium.org/542203004

Cr-Commit-Position: refs/heads/master@{#296444}
parent 7671d4cc
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "base/pickle.h" #include "base/pickle.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "third_party/mozilla/NSPasteboard+Utils.h" #import "third_party/mozilla/NSPasteboard+Utils.h"
#import "ui/base/dragdrop/cocoa_dnd_util.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace ui { namespace ui {
...@@ -45,12 +44,16 @@ void OSExchangeDataProviderMac::SetString(const base::string16& string) { ...@@ -45,12 +44,16 @@ void OSExchangeDataProviderMac::SetString(const base::string16& string) {
void OSExchangeDataProviderMac::SetURL(const GURL& url, void OSExchangeDataProviderMac::SetURL(const GURL& url,
const base::string16& title) { const base::string16& title) {
[pasteboard_ setDataForURL:base::SysUTF8ToNSString(url.spec()) NSURL* ns_url = [NSURL URLWithString:base::SysUTF8ToNSString(url.spec())];
title:base::SysUTF16ToNSString(title)]; [pasteboard_ writeObjects:@[ ns_url ]];
[pasteboard_ setString:base::SysUTF16ToNSString(title)
forType:kCorePasteboardFlavorType_urln];
} }
void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) { void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) {
NOTIMPLEMENTED(); [pasteboard_ setPropertyList:@[ base::SysUTF8ToNSString(path.value()) ]
forType:NSFilenamesPboardType];
} }
void OSExchangeDataProviderMac::SetFilenames( void OSExchangeDataProviderMac::SetFilenames(
...@@ -80,13 +83,39 @@ bool OSExchangeDataProviderMac::GetURLAndTitle( ...@@ -80,13 +83,39 @@ bool OSExchangeDataProviderMac::GetURLAndTitle(
OSExchangeData::FilenameToURLPolicy policy, OSExchangeData::FilenameToURLPolicy policy,
GURL* url, GURL* url,
base::string16* title) const { base::string16* title) const {
return PopulateURLAndTitleFromPasteboard( DCHECK(url);
url, title, pasteboard_, policy == OSExchangeData::CONVERT_FILENAMES); DCHECK(title);
NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSURL class] ]
options:@{ }];
if ([items count] == 0)
return false;
NSURL* ns_url = [items objectAtIndex:0];
if (policy == OSExchangeData::DO_NOT_CONVERT_FILENAMES) {
// If the URL matches a filename, assume that it came from SetFilename().
// Don't return it if we are not supposed to convert filename to URL.
NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType];
NSString* url_path = [[ns_url path] stringByStandardizingPath];
for (NSString* path in paths) {
if ([[path stringByStandardizingPath] isEqualToString:url_path])
return false;
}
}
*url = GURL([[ns_url absoluteString] UTF8String]);
*title = base::SysNSStringToUTF16(
[pasteboard_ stringForType:kCorePasteboardFlavorType_urln]);
return true;
} }
bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const { bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const {
NOTIMPLEMENTED(); NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType];
return false; if ([paths count] == 0)
return false;
*path = base::FilePath([[paths objectAtIndex:0] UTF8String]);
return true;
} }
bool OSExchangeDataProviderMac::GetFilenames( bool OSExchangeDataProviderMac::GetFilenames(
...@@ -120,8 +149,7 @@ bool OSExchangeDataProviderMac::HasURL( ...@@ -120,8 +149,7 @@ bool OSExchangeDataProviderMac::HasURL(
} }
bool OSExchangeDataProviderMac::HasFile() const { bool OSExchangeDataProviderMac::HasFile() const {
NOTIMPLEMENTED(); return [[pasteboard_ types] containsObject:NSFilenamesPboardType];
return false;
} }
bool OSExchangeDataProviderMac::HasCustomFormat( bool OSExchangeDataProviderMac::HasCustomFormat(
......
...@@ -104,6 +104,7 @@ TEST_F(OSExchangeDataTest, TestFileToURLConversion) { ...@@ -104,6 +104,7 @@ TEST_F(OSExchangeDataTest, TestFileToURLConversion) {
ASSERT_TRUE(base::GetCurrentDirectory(&current_directory)); ASSERT_TRUE(base::GetCurrentDirectory(&current_directory));
data.SetFilename(current_directory); data.SetFilename(current_directory);
{ {
EXPECT_FALSE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES)); EXPECT_FALSE(data.HasURL(OSExchangeData::DO_NOT_CONVERT_FILENAMES));
GURL actual_url; GURL actual_url;
...@@ -113,6 +114,7 @@ TEST_F(OSExchangeDataTest, TestFileToURLConversion) { ...@@ -113,6 +114,7 @@ TEST_F(OSExchangeDataTest, TestFileToURLConversion) {
EXPECT_EQ(GURL(), actual_url); EXPECT_EQ(GURL(), actual_url);
EXPECT_EQ(base::string16(), actual_title); EXPECT_EQ(base::string16(), actual_title);
} }
{ {
// Filename to URL conversion is not implemented on ChromeOS or on non-X11 Linux // Filename to URL conversion is not implemented on ChromeOS or on non-X11 Linux
// builds. // builds.
...@@ -130,7 +132,9 @@ TEST_F(OSExchangeDataTest, TestFileToURLConversion) { ...@@ -130,7 +132,9 @@ TEST_F(OSExchangeDataTest, TestFileToURLConversion) {
expected_success, expected_success,
data.GetURLAndTitle( data.GetURLAndTitle(
OSExchangeData::CONVERT_FILENAMES, &actual_url, &actual_title)); OSExchangeData::CONVERT_FILENAMES, &actual_url, &actual_title));
EXPECT_EQ(expected_url, actual_url); // Some Mac OS versions return the URL in file://localhost form instead
// of file:///, so we compare the url's path not its absolute string.
EXPECT_EQ(expected_url.path(), actual_url.path());
EXPECT_EQ(base::string16(), actual_title); EXPECT_EQ(base::string16(), actual_title);
} }
EXPECT_TRUE(data.HasFile()); EXPECT_TRUE(data.HasFile());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment