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
.isNull();
39 return !m_htmlText
.isNull();
41 case FormatSmartPaste
:
42 return m_writeSmartPaste
;
53 #if defined(OS_POSIX) && !defined(OS_MACOSX)
64 WebVector
<WebString
> MockWebClipboardImpl::readAvailableTypes(
66 bool* containsFilenames
) {
67 *containsFilenames
= false;
68 std::vector
<WebString
> results
;
69 if (!m_plainText
.isEmpty()) {
70 results
.push_back(WebString("text/plain"));
72 if (!m_htmlText
.isEmpty()) {
73 results
.push_back(WebString("text/html"));
75 if (!m_image
.isNull()) {
76 results
.push_back(WebString("image/png"));
78 for (std::map
<base::string16
, base::string16
>::const_iterator it
=
80 it
!= m_customData
.end(); ++it
) {
81 CHECK(std::find(results
.begin(), results
.end(), it
->first
) ==
83 results
.push_back(it
->first
);
88 blink::WebString
MockWebClipboardImpl::readPlainText(
89 blink::WebClipboard::Buffer buffer
) {
93 // TODO(wtc): set output argument *url.
94 blink::WebString
MockWebClipboardImpl::readHTML(
95 blink::WebClipboard::Buffer buffer
,
97 unsigned* fragmentStart
,
98 unsigned* fragmentEnd
) {
100 *fragmentEnd
= static_cast<unsigned>(m_htmlText
.length());
104 blink::WebData
MockWebClipboardImpl::readImage(
105 blink::WebClipboard::Buffer buffer
) {
107 std::vector
<unsigned char> encoded_image
;
108 // TODO(dcheng): Verify that we can assume the image is ARGB8888. Note that
109 // for endianess reasons, it will be BGRA8888 on Windows.
110 const SkBitmap
& bitmap
= m_image
.getSkBitmap();
111 SkAutoLockPixels
lock(bitmap
);
112 gfx::PNGCodec::Encode(static_cast<unsigned char*>(bitmap
.getPixels()),
113 #if defined(OS_ANDROID)
114 gfx::PNGCodec::FORMAT_RGBA
,
116 gfx::PNGCodec::FORMAT_BGRA
,
118 gfx::Size(bitmap
.width(), bitmap
.height()),
120 false /* discard_transparency */,
121 std::vector
<gfx::PNGCodec::Comment
>(),
123 data
.assign(reinterpret_cast<char*>(vector_as_array(&encoded_image
)),
124 encoded_image
.size());
128 blink::WebString
MockWebClipboardImpl::readCustomData(
129 blink::WebClipboard::Buffer buffer
,
130 const blink::WebString
& type
) {
131 std::map
<base::string16
, base::string16
>::const_iterator it
=
132 m_customData
.find(type
);
133 if (it
!= m_customData
.end())
135 return blink::WebString();
138 void MockWebClipboardImpl::writeHTML(const blink::WebString
& htmlText
,
139 const blink::WebURL
& url
,
140 const blink::WebString
& plainText
,
141 bool writeSmartPaste
) {
144 m_htmlText
= htmlText
;
145 m_plainText
= plainText
;
146 m_writeSmartPaste
= writeSmartPaste
;
149 void MockWebClipboardImpl::writePlainText(const blink::WebString
& plain_text
) {
152 m_plainText
= plain_text
;
155 void MockWebClipboardImpl::writeURL(const blink::WebURL
& url
,
156 const blink::WebString
& title
) {
159 m_htmlText
= WebString::fromUTF8(URLToMarkup(url
, title
));
160 m_plainText
= url
.spec().utf16();
163 void MockWebClipboardImpl::writeImage(const blink::WebImage
& image
,
164 const blink::WebURL
& url
,
165 const blink::WebString
& title
) {
166 if (!image
.isNull()) {
169 m_plainText
= m_htmlText
;
170 m_htmlText
= WebString::fromUTF8(URLToImageMarkup(url
, title
));
175 void MockWebClipboardImpl::writeDataObject(const WebDragData
& data
) {
178 const WebVector
<WebDragData::Item
>& itemList
= data
.items();
179 for (size_t i
= 0; i
< itemList
.size(); ++i
) {
180 const WebDragData::Item
& item
= itemList
[i
];
181 switch (item
.storageType
) {
182 case WebDragData::Item::StorageTypeString
: {
183 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeText
)) {
184 m_plainText
= item
.stringData
;
187 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeHTML
)) {
188 m_htmlText
= item
.stringData
;
191 m_customData
.insert(std::make_pair(item
.stringType
, item
.stringData
));
194 case WebDragData::Item::StorageTypeFilename
:
195 case WebDragData::Item::StorageTypeBinaryData
:
196 NOTREACHED(); // Currently unused by the clipboard implementation.
201 void MockWebClipboardImpl::clear() {
202 m_plainText
= WebString();
203 m_htmlText
= WebString();
205 m_customData
.clear();
206 m_writeSmartPaste
= false;
209 } // namespace content