Get foreground tab on Android
[chromium-blink-merge.git] / content / test / mock_webclipboard_impl.cc
blob06507145aa1c81d1a2cf5e852eddca1c8d1bd112
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"
7 #include <algorithm>
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;
24 using blink::WebURL;
25 using blink::WebVector;
27 namespace content {
29 MockWebClipboardImpl::MockWebClipboardImpl() {}
31 MockWebClipboardImpl::~MockWebClipboardImpl() {}
33 bool MockWebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
34 switch (format) {
35 case FormatPlainText:
36 return !m_plainText.isNull();
38 case FormatHTML:
39 return !m_htmlText.isNull();
41 case FormatSmartPaste:
42 return m_writeSmartPaste;
44 default:
45 NOTREACHED();
46 return false;
49 switch (buffer) {
50 case BufferStandard:
51 break;
52 case BufferSelection:
53 #if defined(OS_POSIX) && !defined(OS_MACOSX)
54 break;
55 #endif
56 default:
57 NOTREACHED();
58 return false;
61 return true;
64 WebVector<WebString> MockWebClipboardImpl::readAvailableTypes(
65 Buffer buffer,
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 =
79 m_customData.begin();
80 it != m_customData.end(); ++it) {
81 CHECK(std::find(results.begin(), results.end(), it->first) ==
82 results.end());
83 results.push_back(it->first);
85 return results;
88 blink::WebString MockWebClipboardImpl::readPlainText(
89 blink::WebClipboard::Buffer buffer) {
90 return m_plainText;
93 // TODO(wtc): set output argument *url.
94 blink::WebString MockWebClipboardImpl::readHTML(
95 blink::WebClipboard::Buffer buffer,
96 blink::WebURL* url,
97 unsigned* fragmentStart,
98 unsigned* fragmentEnd) {
99 *fragmentStart = 0;
100 *fragmentEnd = static_cast<unsigned>(m_htmlText.length());
101 return m_htmlText;
104 blink::WebData MockWebClipboardImpl::readImage(
105 blink::WebClipboard::Buffer buffer) {
106 std::string data;
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,
115 #else
116 gfx::PNGCodec::FORMAT_BGRA,
117 #endif
118 gfx::Size(bitmap.width(), bitmap.height()),
119 bitmap.rowBytes(),
120 false /* discard_transparency */,
121 std::vector<gfx::PNGCodec::Comment>(),
122 &encoded_image);
123 data.assign(reinterpret_cast<char*>(vector_as_array(&encoded_image)),
124 encoded_image.size());
125 return data;
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())
134 return it->second;
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) {
142 clear();
144 m_htmlText = htmlText;
145 m_plainText = plainText;
146 m_writeSmartPaste = writeSmartPaste;
149 void MockWebClipboardImpl::writePlainText(const blink::WebString& plain_text) {
150 clear();
152 m_plainText = plain_text;
155 void MockWebClipboardImpl::writeURL(const blink::WebURL& url,
156 const blink::WebString& title) {
157 clear();
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()) {
167 clear();
169 m_plainText = m_htmlText;
170 m_htmlText = WebString::fromUTF8(URLToImageMarkup(url, title));
171 m_image = image;
175 void MockWebClipboardImpl::writeDataObject(const WebDragData& data) {
176 clear();
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;
185 continue;
187 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeHTML)) {
188 m_htmlText = item.stringData;
189 continue;
191 m_customData.insert(std::make_pair(item.stringType, item.stringData));
192 continue;
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();
204 m_image.reset();
205 m_customData.clear();
206 m_writeSmartPaste = false;
209 } // namespace content