Skip to content

Commit

Permalink
Fill out more data types on OSExchangeDataProviderAuraX11.
Browse files Browse the repository at this point in the history
This also makes OSExchangeDataProvider unit tests that are cross platform
actually cross platform.

BUG=130806

Review URL: https://chromiumcodereview.appspot.com/17776002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@208985 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
erg@chromium.org committed Jun 27, 2013
1 parent f8b23b4 commit b17e31d
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 95 deletions.
2 changes: 2 additions & 0 deletions ui/base/dragdrop/os_exchange_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class UI_EXPORT OSExchangeData {
Provider() {}
virtual ~Provider() {}

virtual Provider* Clone() const = 0;

virtual void SetString(const base::string16& data) = 0;
virtual void SetURL(const GURL& url, const base::string16& title) = 0;
virtual void SetFilename(const base::FilePath& path) = 0;
Expand Down
17 changes: 17 additions & 0 deletions ui/base/dragdrop/os_exchange_data_provider_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ OSExchangeDataProviderAura::OSExchangeDataProviderAura()

OSExchangeDataProviderAura::~OSExchangeDataProviderAura() {}

OSExchangeData::Provider* OSExchangeDataProviderAura::Clone() const {
OSExchangeDataProviderAura* ret = new OSExchangeDataProviderAura();
ret->formats_ = formats_;
ret->string_ = string_;
ret->url_ = url_;
ret->title_ = title_;
ret->filenames_ = filenames_;
ret->pickle_data_ = pickle_data_;
// We skip copying the drag images.
ret->html_ = html_;
ret->base_url_ = base_url_;

return ret;
}

void OSExchangeDataProviderAura::SetString(const base::string16& data) {
string_ = data;
formats_ |= OSExchangeData::STRING;
Expand All @@ -28,6 +43,8 @@ void OSExchangeDataProviderAura::SetURL(const GURL& url,
url_ = url;
title_ = title;
formats_ |= OSExchangeData::URL;

SetString(UTF8ToUTF16(url.spec()));
}

void OSExchangeDataProviderAura::SetFilename(const base::FilePath& path) {
Expand Down
1 change: 1 addition & 0 deletions ui/base/dragdrop/os_exchange_data_provider_aura.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class UI_EXPORT OSExchangeDataProviderAura
virtual ~OSExchangeDataProviderAura();

// Overridden from OSExchangeData::Provider:
virtual Provider* Clone() const OVERRIDE;
virtual void SetString(const base::string16& data) OVERRIDE;
virtual void SetURL(const GURL& url, const base::string16& title) OVERRIDE;
virtual void SetFilename(const base::FilePath& path) OVERRIDE;
Expand Down
70 changes: 63 additions & 7 deletions ui/base/dragdrop/os_exchange_data_provider_aurax11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ SelectionFormatMap OSExchangeDataProviderAuraX11::GetFormatMap() const {
return selection_owner_.selection_format_map();
}

OSExchangeData::Provider* OSExchangeDataProviderAuraX11::Clone() const {
OSExchangeDataProviderAuraX11* ret = new OSExchangeDataProviderAuraX11();
ret->format_map_ = format_map_;
return ret;
}

void OSExchangeDataProviderAuraX11::SetString(const base::string16& text_data) {
std::string utf8 = UTF16ToUTF8(text_data);
scoped_refptr<base::RefCountedMemory> mem(
Expand All @@ -115,7 +121,21 @@ void OSExchangeDataProviderAuraX11::SetString(const base::string16& text_data) {

void OSExchangeDataProviderAuraX11::SetURL(const GURL& url,
const base::string16& title) {
NOTIMPLEMENTED();
// Mozilla's URL format: (UTF16: URL, newline, title)
if (url.is_valid()) {
string16 spec = UTF8ToUTF16(url.spec());

std::vector<unsigned char> data;
ui::AddString16ToVector(spec, &data);
ui::AddString16ToVector(ASCIIToUTF16("\n"), &data);
ui::AddString16ToVector(title, &data);
scoped_refptr<base::RefCountedMemory> mem(
base::RefCountedBytes::TakeVector(&data));

format_map_.Insert(atom_cache_.GetAtom(kMimeTypeMozillaURL), mem);

SetString(spec);
}
}

void OSExchangeDataProviderAuraX11::SetFilename(const base::FilePath& path) {
Expand All @@ -129,8 +149,16 @@ void OSExchangeDataProviderAuraX11::SetFilenames(

void OSExchangeDataProviderAuraX11::SetPickledData(
const OSExchangeData::CustomFormat& format,
const Pickle& data) {
NOTIMPLEMENTED();
const Pickle& pickle) {
const unsigned char* data =
reinterpret_cast<const unsigned char*>(pickle.data());

std::vector<unsigned char> bytes;
bytes.insert(bytes.end(), data, data + pickle.size());
scoped_refptr<base::RefCountedMemory> mem(
base::RefCountedBytes::TakeVector(&bytes));

format_map_.Insert(atom_cache_.GetAtom(format.ToString().c_str()), mem);
}

bool OSExchangeDataProviderAuraX11::GetString(base::string16* result) const {
Expand Down Expand Up @@ -212,8 +240,19 @@ bool OSExchangeDataProviderAuraX11::GetFilenames(

bool OSExchangeDataProviderAuraX11::GetPickledData(
const OSExchangeData::CustomFormat& format,
Pickle* data) const {
NOTIMPLEMENTED();
Pickle* pickle) const {
std::vector< ::Atom> requested_types;
requested_types.push_back(atom_cache_.GetAtom(format.ToString().c_str()));

ui::SelectionData data(format_map_.GetFirstOf(requested_types));
if (data.IsValid()) {
// Note that the pickle object on the right hand side of the assignment
// only refers to the bytes in |data|. The assignment copies the data.
*pickle = Pickle(reinterpret_cast<const char*>(data.GetData()),
static_cast<int>(data.GetSize()));
return true;
}

return false;
}

Expand Down Expand Up @@ -248,7 +287,16 @@ bool OSExchangeDataProviderAuraX11::HasCustomFormat(

void OSExchangeDataProviderAuraX11::SetHtml(const base::string16& html,
const GURL& base_url) {
NOTIMPLEMENTED();
std::vector<unsigned char> bytes;
// Manually jam a UTF16 BOM into bytes because otherwise, other programs will
// assume UTF-8.
bytes.push_back(0xFF);
bytes.push_back(0xFE);
ui::AddString16ToVector(html, &bytes);
scoped_refptr<base::RefCountedMemory> mem(
base::RefCountedBytes::TakeVector(&bytes));

format_map_.Insert(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML), mem);
}

bool OSExchangeDataProviderAuraX11::GetHtml(base::string16* html,
Expand Down Expand Up @@ -306,7 +354,15 @@ bool OSExchangeDataProviderAuraX11::Dispatch(const base::NativeEvent& event) {
}

bool OSExchangeDataProviderAuraX11::GetPlainTextURL(GURL* url) const {
NOTIMPLEMENTED();
string16 text;
if (GetString(&text)) {
GURL test_url(text);
if (test_url.is_valid()) {
*url = test_url;
return true;
}
}

return false;
}

Expand Down
5 changes: 3 additions & 2 deletions ui/base/dragdrop/os_exchange_data_provider_aurax11.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,21 @@ class UI_EXPORT OSExchangeDataProviderAuraX11
SelectionFormatMap GetFormatMap() const;

// Overridden from OSExchangeData::Provider:
virtual Provider* Clone() const OVERRIDE;
virtual void SetString(const base::string16& data) OVERRIDE;
virtual void SetURL(const GURL& url, const base::string16& title) OVERRIDE;
virtual void SetFilename(const base::FilePath& path) OVERRIDE;
virtual void SetFilenames(
const std::vector<OSExchangeData::FileInfo>& filenames) OVERRIDE;
virtual void SetPickledData(const OSExchangeData::CustomFormat& format,
const Pickle& data) OVERRIDE;
const Pickle& pickle) OVERRIDE;
virtual bool GetString(base::string16* data) const OVERRIDE;
virtual bool GetURLAndTitle(GURL* url, base::string16* title) const OVERRIDE;
virtual bool GetFilename(base::FilePath* path) const OVERRIDE;
virtual bool GetFilenames(
std::vector<OSExchangeData::FileInfo>* filenames) const OVERRIDE;
virtual bool GetPickledData(const OSExchangeData::CustomFormat& format,
Pickle* data) const OVERRIDE;
Pickle* pickle) const OVERRIDE;
virtual bool HasString() const OVERRIDE;
virtual bool HasURL() const OVERRIDE;
virtual bool HasFile() const OVERRIDE;
Expand Down
4 changes: 4 additions & 0 deletions ui/base/dragdrop/os_exchange_data_provider_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ OSExchangeDataProviderWin::OSExchangeDataProviderWin()
OSExchangeDataProviderWin::~OSExchangeDataProviderWin() {
}

OSExchangeData::Provider* OSExchangeDataProviderWin::Clone() const {
return new OSExchangeDataProviderWin(data_object());
}

void OSExchangeDataProviderWin::SetString(const base::string16& data) {
STGMEDIUM* storage = GetStorageForString(data);
data_->contents_.push_back(new DataObjectImpl::StoredDataInfo(
Expand Down
1 change: 1 addition & 0 deletions ui/base/dragdrop/os_exchange_data_provider_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class UI_EXPORT OSExchangeDataProviderWin : public OSExchangeData::Provider {
IDataObjectAsyncCapability* async_operation() const { return data_.get(); }

// OSExchangeData::Provider methods.
virtual Provider* Clone() const;
virtual void SetString(const base::string16& data);
virtual void SetURL(const GURL& url, const base::string16& title);
virtual void SetFilename(const base::FilePath& path);
Expand Down
98 changes: 98 additions & 0 deletions ui/base/dragdrop/os_exchange_data_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2013 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.

#include "base/message_loop.h"
#include "base/pickle.h"
#include "base/strings/utf_string_conversions.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "ui/base/dragdrop/os_exchange_data.h"

namespace ui {

class OSExchangeDataTest : public PlatformTest {
private:
base::MessageLoopForUI message_loop_;
};

TEST_F(OSExchangeDataTest, StringDataGetAndSet) {
OSExchangeData data;
string16 input = ASCIIToUTF16("I can has cheezburger?");
data.SetString(input);

OSExchangeData data2(data.provider().Clone());
string16 output;
EXPECT_TRUE(data2.GetString(&output));
EXPECT_EQ(input, output);
std::string url_spec = "http://www.goats.com/";
GURL url(url_spec);
string16 title;
EXPECT_FALSE(data2.GetURLAndTitle(&url, &title));
// No URLs in |data|, so url should be untouched.
EXPECT_EQ(url_spec, url.spec());
}

TEST_F(OSExchangeDataTest, TestURLExchangeFormats) {
OSExchangeData data;
std::string url_spec = "http://www.google.com/";
GURL url(url_spec);
string16 url_title = ASCIIToUTF16("www.google.com");
data.SetURL(url, url_title);
string16 output;

OSExchangeData data2(data.provider().Clone());

// URL spec and title should match
GURL output_url;
string16 output_title;
EXPECT_TRUE(data2.GetURLAndTitle(&output_url, &output_title));
EXPECT_EQ(url_spec, output_url.spec());
EXPECT_EQ(url_title, output_title);
string16 output_string;

// URL should be the raw text response
EXPECT_TRUE(data2.GetString(&output_string));
EXPECT_EQ(url_spec, UTF16ToUTF8(output_string));
}

TEST_F(OSExchangeDataTest, TestPickledData) {
const OSExchangeData::CustomFormat kTestFormat =
ui::Clipboard::GetFormatType("application/vnd.chromium.test");

Pickle saved_pickle;
saved_pickle.WriteInt(1);
saved_pickle.WriteInt(2);
OSExchangeData data;
data.SetPickledData(kTestFormat, saved_pickle);

OSExchangeData copy(data.provider().Clone());
EXPECT_TRUE(copy.HasCustomFormat(kTestFormat));

Pickle restored_pickle;
EXPECT_TRUE(copy.GetPickledData(kTestFormat, &restored_pickle));
PickleIterator iterator(restored_pickle);
int value;
EXPECT_TRUE(restored_pickle.ReadInt(&iterator, &value));
EXPECT_EQ(1, value);
EXPECT_TRUE(restored_pickle.ReadInt(&iterator, &value));
EXPECT_EQ(2, value);
}

TEST_F(OSExchangeDataTest, TestHTML) {
OSExchangeData data;
GURL url("http://www.google.com/");
string16 html = ASCIIToUTF16(
"<HTML>\n<BODY>\n"
"<b>bold.</b> <i><b>This is bold italic.</b></i>\n"
"</BODY>\n</HTML>");
data.SetHtml(html, url);

OSExchangeData copy(data.provider().Clone());
string16 read_html;
EXPECT_TRUE(copy.GetHtml(&read_html, &url));
EXPECT_EQ(html, read_html);
}

} // namespace ui
Loading

0 comments on commit b17e31d

Please sign in to comment.