• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "base/message_loop/message_loop.h"
6 #include "base/pickle.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "testing/platform_test.h"
10 #include "ui/base/dragdrop/os_exchange_data.h"
11 #include "url/gurl.h"
12 
13 namespace ui {
14 
15 class OSExchangeDataTest : public PlatformTest {
16  private:
17   base::MessageLoopForUI message_loop_;
18 };
19 
TEST_F(OSExchangeDataTest,StringDataGetAndSet)20 TEST_F(OSExchangeDataTest, StringDataGetAndSet) {
21   OSExchangeData data;
22   string16 input = ASCIIToUTF16("I can has cheezburger?");
23   data.SetString(input);
24 
25   OSExchangeData data2(data.provider().Clone());
26   string16 output;
27   EXPECT_TRUE(data2.GetString(&output));
28   EXPECT_EQ(input, output);
29   std::string url_spec = "http://www.goats.com/";
30   GURL url(url_spec);
31   string16 title;
32   EXPECT_FALSE(
33       data2.GetURLAndTitle(OSExchangeData::CONVERT_FILENAMES, &url, &title));
34   // No URLs in |data|, so url should be untouched.
35   EXPECT_EQ(url_spec, url.spec());
36 }
37 
TEST_F(OSExchangeDataTest,TestURLExchangeFormats)38 TEST_F(OSExchangeDataTest, TestURLExchangeFormats) {
39   OSExchangeData data;
40   std::string url_spec = "http://www.google.com/";
41   GURL url(url_spec);
42   string16 url_title = ASCIIToUTF16("www.google.com");
43   data.SetURL(url, url_title);
44   string16 output;
45 
46   OSExchangeData data2(data.provider().Clone());
47 
48   // URL spec and title should match
49   GURL output_url;
50   string16 output_title;
51   EXPECT_TRUE(data2.GetURLAndTitle(
52       OSExchangeData::CONVERT_FILENAMES, &output_url, &output_title));
53   EXPECT_EQ(url_spec, output_url.spec());
54   EXPECT_EQ(url_title, output_title);
55   string16 output_string;
56 
57   // URL should be the raw text response
58   EXPECT_TRUE(data2.GetString(&output_string));
59   EXPECT_EQ(url_spec, UTF16ToUTF8(output_string));
60 }
61 
TEST_F(OSExchangeDataTest,TestPickledData)62 TEST_F(OSExchangeDataTest, TestPickledData) {
63   const OSExchangeData::CustomFormat kTestFormat =
64       ui::Clipboard::GetFormatType("application/vnd.chromium.test");
65 
66   Pickle saved_pickle;
67   saved_pickle.WriteInt(1);
68   saved_pickle.WriteInt(2);
69   OSExchangeData data;
70   data.SetPickledData(kTestFormat, saved_pickle);
71 
72   OSExchangeData copy(data.provider().Clone());
73   EXPECT_TRUE(copy.HasCustomFormat(kTestFormat));
74 
75   Pickle restored_pickle;
76   EXPECT_TRUE(copy.GetPickledData(kTestFormat, &restored_pickle));
77   PickleIterator iterator(restored_pickle);
78   int value;
79   EXPECT_TRUE(restored_pickle.ReadInt(&iterator, &value));
80   EXPECT_EQ(1, value);
81   EXPECT_TRUE(restored_pickle.ReadInt(&iterator, &value));
82   EXPECT_EQ(2, value);
83 }
84 
TEST_F(OSExchangeDataTest,TestHTML)85 TEST_F(OSExchangeDataTest, TestHTML) {
86   OSExchangeData data;
87   GURL url("http://www.google.com/");
88   string16 html = ASCIIToUTF16(
89       "<HTML>\n<BODY>\n"
90       "<b>bold.</b> <i><b>This is bold italic.</b></i>\n"
91       "</BODY>\n</HTML>");
92   data.SetHtml(html, url);
93 
94   OSExchangeData copy(data.provider().Clone());
95   string16 read_html;
96   EXPECT_TRUE(copy.GetHtml(&read_html, &url));
97   EXPECT_EQ(html, read_html);
98 }
99 
100 }  // namespace ui
101