1 // Copyright 2013 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.
5 #include "content/test/mock_webclipboard_impl.h"
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/renderer/clipboard_utils.h"
14 #include "third_party/WebKit/public/platform/WebCommon.h"
15 #include "third_party/WebKit/public/platform/WebDragData.h"
16 #include "third_party/WebKit/public/platform/WebImage.h"
17 #include "third_party/WebKit/public/platform/WebURL.h"
18 #include "ui/base/clipboard/clipboard.h"
19 #include "ui/gfx/codec/png_codec.h"
20 #include "ui/gfx/size.h"
22 using blink::WebDragData
;
23 using blink::WebString
;
25 using blink::WebVector
;
29 MockWebClipboardImpl::MockWebClipboardImpl() {}
31 MockWebClipboardImpl::~MockWebClipboardImpl() {}
33 bool MockWebClipboardImpl::isFormatAvailable(Format format
, Buffer buffer
) {
36 return !m_plainText
.is_null();
39 return !m_htmlText
.is_null();
41 case FormatSmartPaste
:
42 return m_writeSmartPaste
;
50 WebVector
<WebString
> MockWebClipboardImpl::readAvailableTypes(
52 bool* containsFilenames
) {
53 *containsFilenames
= false;
54 std::vector
<WebString
> results
;
55 if (!m_plainText
.string().empty()) {
56 results
.push_back(WebString("text/plain"));
58 if (!m_htmlText
.string().empty()) {
59 results
.push_back(WebString("text/html"));
61 if (!m_image
.isNull()) {
62 results
.push_back(WebString("image/png"));
64 for (std::map
<base::string16
, base::string16
>::const_iterator it
=
66 it
!= m_customData
.end(); ++it
) {
67 CHECK(std::find(results
.begin(), results
.end(), it
->first
) ==
69 results
.push_back(it
->first
);
74 blink::WebString
MockWebClipboardImpl::readPlainText(
75 blink::WebClipboard::Buffer buffer
) {
79 // TODO(wtc): set output argument *url.
80 blink::WebString
MockWebClipboardImpl::readHTML(
81 blink::WebClipboard::Buffer buffer
,
83 unsigned* fragmentStart
,
84 unsigned* fragmentEnd
) {
86 *fragmentEnd
= static_cast<unsigned>(m_htmlText
.string().length());
90 blink::WebData
MockWebClipboardImpl::readImage(
91 blink::WebClipboard::Buffer buffer
) {
93 std::vector
<unsigned char> encoded_image
;
94 // TODO(dcheng): Verify that we can assume the image is ARGB8888. Note that
95 // for endianess reasons, it will be BGRA8888 on Windows.
96 const SkBitmap
& bitmap
= m_image
.getSkBitmap();
97 SkAutoLockPixels
lock(bitmap
);
98 gfx::PNGCodec::Encode(static_cast<unsigned char*>(bitmap
.getPixels()),
99 #if defined(OS_ANDROID)
100 gfx::PNGCodec::FORMAT_RGBA
,
102 gfx::PNGCodec::FORMAT_BGRA
,
104 gfx::Size(bitmap
.width(), bitmap
.height()),
105 static_cast<int>(bitmap
.rowBytes()),
106 false /* discard_transparency */,
107 std::vector
<gfx::PNGCodec::Comment
>(),
109 data
.assign(reinterpret_cast<char*>(vector_as_array(&encoded_image
)),
110 encoded_image
.size());
114 blink::WebString
MockWebClipboardImpl::readCustomData(
115 blink::WebClipboard::Buffer buffer
,
116 const blink::WebString
& type
) {
117 std::map
<base::string16
, base::string16
>::const_iterator it
=
118 m_customData
.find(type
);
119 if (it
!= m_customData
.end())
121 return blink::WebString();
124 void MockWebClipboardImpl::writeHTML(const blink::WebString
& htmlText
,
125 const blink::WebURL
& url
,
126 const blink::WebString
& plainText
,
127 bool writeSmartPaste
) {
130 m_htmlText
= htmlText
;
131 m_plainText
= plainText
;
132 m_writeSmartPaste
= writeSmartPaste
;
135 void MockWebClipboardImpl::writePlainText(const blink::WebString
& plain_text
) {
138 m_plainText
= plain_text
;
141 void MockWebClipboardImpl::writeURL(const blink::WebURL
& url
,
142 const blink::WebString
& title
) {
145 m_htmlText
= WebString::fromUTF8(URLToMarkup(url
, title
));
146 m_plainText
= url
.spec().utf16();
149 void MockWebClipboardImpl::writeImage(const blink::WebImage
& image
,
150 const blink::WebURL
& url
,
151 const blink::WebString
& title
) {
152 if (!image
.isNull()) {
155 m_plainText
= m_htmlText
;
156 m_htmlText
= WebString::fromUTF8(URLToImageMarkup(url
, title
));
161 void MockWebClipboardImpl::writeDataObject(const WebDragData
& data
) {
164 const WebVector
<WebDragData::Item
>& itemList
= data
.items();
165 for (size_t i
= 0; i
< itemList
.size(); ++i
) {
166 const WebDragData::Item
& item
= itemList
[i
];
167 switch (item
.storageType
) {
168 case WebDragData::Item::StorageTypeString
: {
169 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeText
)) {
170 m_plainText
= item
.stringData
;
173 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeHTML
)) {
174 m_htmlText
= item
.stringData
;
177 m_customData
.insert(std::make_pair(item
.stringType
, item
.stringData
));
181 // Currently other types are unused by the clipboard implementation.
187 void MockWebClipboardImpl::clear() {
188 m_plainText
= base::NullableString16();
189 m_htmlText
= base::NullableString16();
191 m_customData
.clear();
192 m_writeSmartPaste
= false;
195 } // namespace content