Broke ContentSettingBubbleModelTest.Plugins on Android.
[chromium-blink-merge.git] / content / browser / web_contents / web_drag_dest_mac_unittest.mm
blob76fafbd433089883f0e65347499a8da20f47060b
1 // Copyright (c) 2012 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 "base/mac/scoped_nsautorelease_pool.h"
6 #import "base/memory/scoped_nsobject.h"
7 #include "base/sys_string_conversions.h"
8 #include "base/utf_string_conversions.h"
9 #include "content/browser/renderer_host/test_render_view_host.h"
10 #include "content/browser/web_contents/test_web_contents.h"
11 #import "content/browser/web_contents/web_drag_dest_mac.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #import "third_party/mozilla/NSPasteboard+Utils.h"
14 #import "ui/base/dragdrop/cocoa_dnd_util.h"
15 #import "ui/base/test/ui_cocoa_test_helper.h"
16 #include "webkit/glue/webdropdata.h"
18 using content::RenderViewHostImplTestHarness;
20 namespace {
21 NSString* const kCrCorePasteboardFlavorType_url =
22     @"CorePasteboardFlavorType 0x75726C20"; // 'url '  url
23 NSString* const kCrCorePasteboardFlavorType_urln =
24     @"CorePasteboardFlavorType 0x75726C6E"; // 'urln'  title
25 }  // namespace
27 class WebDragDestTest : public RenderViewHostImplTestHarness {
28  public:
29   virtual void SetUp() {
30     RenderViewHostImplTestHarness::SetUp();
31     drag_dest_.reset([[WebDragDest alloc] initWithWebContentsImpl:contents()]);
32   }
34   void PutURLOnPasteboard(NSString* urlString, NSPasteboard* pboard) {
35     [pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType]
36                    owner:nil];
37     NSURL* url = [NSURL URLWithString:urlString];
38     EXPECT_TRUE(url);
39     [url writeToPasteboard:pboard];
40   }
42   void PutCoreURLAndTitleOnPasteboard(NSString* urlString, NSString* title,
43                                       NSPasteboard* pboard) {
44     [pboard
45         declareTypes:[NSArray arrayWithObjects:kCrCorePasteboardFlavorType_url,
46                                                kCrCorePasteboardFlavorType_urln,
47                                                nil]
48                owner:nil];
49     [pboard setString:urlString
50               forType:kCrCorePasteboardFlavorType_url];
51     [pboard setString:title
52               forType:kCrCorePasteboardFlavorType_urln];
53   }
55   base::mac::ScopedNSAutoreleasePool pool_;
56   scoped_nsobject<WebDragDest> drag_dest_;
59 // Make sure nothing leaks.
60 TEST_F(WebDragDestTest, Init) {
61   EXPECT_TRUE(drag_dest_);
64 // Test flipping of coordinates given a point in window coordinates.
65 TEST_F(WebDragDestTest, Flip) {
66   NSPoint windowPoint = NSZeroPoint;
67   scoped_nsobject<NSWindow> window([[CocoaTestHelperWindow alloc] init]);
68   NSPoint viewPoint =
69       [drag_dest_ flipWindowPointToView:windowPoint
70                                    view:[window contentView]];
71   NSPoint screenPoint =
72       [drag_dest_ flipWindowPointToScreen:windowPoint
73                                      view:[window contentView]];
74   EXPECT_EQ(0, viewPoint.x);
75   EXPECT_EQ(600, viewPoint.y);
76   EXPECT_EQ(0, screenPoint.x);
77   // We can't put a value on the screen size since everyone will have a
78   // different one.
79   EXPECT_NE(0, screenPoint.y);
82 TEST_F(WebDragDestTest, URL) {
83   NSPasteboard* pboard = nil;
84   NSString* url = nil;
85   NSString* title = nil;
86   GURL result_url;
87   string16 result_title;
89   // Put a URL on the pasteboard and check it.
90   pboard = [NSPasteboard pasteboardWithUniqueName];
91   url = @"http://www.google.com/";
92   PutURLOnPasteboard(url, pboard);
93   EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard(
94       &result_url, &result_title, pboard, NO));
95   EXPECT_EQ(base::SysNSStringToUTF8(url), result_url.spec());
96   [pboard releaseGlobally];
98   // Put a 'url ' and 'urln' on the pasteboard and check it.
99   pboard = [NSPasteboard pasteboardWithUniqueName];
100   url = @"http://www.google.com/";
101   title = @"Title of Awesomeness!",
102   PutCoreURLAndTitleOnPasteboard(url, title, pboard);
103   EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard(
104       &result_url, &result_title, pboard, NO));
105   EXPECT_EQ(base::SysNSStringToUTF8(url), result_url.spec());
106   EXPECT_EQ(base::SysNSStringToUTF16(title), result_title);
107   [pboard releaseGlobally];
109   // Also check that it passes file:// via 'url '/'urln' properly.
110   pboard = [NSPasteboard pasteboardWithUniqueName];
111   url = @"file:///tmp/dont_delete_me.txt";
112   title = @"very important";
113   PutCoreURLAndTitleOnPasteboard(url, title, pboard);
114   EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard(
115       &result_url, &result_title, pboard, NO));
116   EXPECT_EQ(base::SysNSStringToUTF8(url), result_url.spec());
117   EXPECT_EQ(base::SysNSStringToUTF16(title), result_title);
118   [pboard releaseGlobally];
120   // And javascript:.
121   pboard = [NSPasteboard pasteboardWithUniqueName];
122   url = @"javascript:open('http://www.youtube.com/')";
123   title = @"kill some time";
124   PutCoreURLAndTitleOnPasteboard(url, title, pboard);
125   EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard(
126       &result_url, &result_title, pboard, NO));
127   EXPECT_EQ(base::SysNSStringToUTF8(url), result_url.spec());
128   EXPECT_EQ(base::SysNSStringToUTF16(title), result_title);
129   [pboard releaseGlobally];
131   pboard = [NSPasteboard pasteboardWithUniqueName];
132   url = @"/bin/sh";
133   [pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
134                  owner:nil];
135   [pboard setPropertyList:[NSArray arrayWithObject:url]
136                   forType:NSFilenamesPboardType];
137   EXPECT_FALSE(ui::PopulateURLAndTitleFromPasteboard(
138       &result_url, &result_title, pboard, NO));
139   EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard(
140       &result_url, &result_title, pboard, YES));
141   EXPECT_EQ("file://localhost/bin/sh", result_url.spec());
142   EXPECT_EQ("sh", UTF16ToUTF8(result_title));
143   [pboard releaseGlobally];
146 TEST_F(WebDragDestTest, Data) {
147   WebDropData data;
148   NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName];
150   PutURLOnPasteboard(@"http://www.google.com", pboard);
151   [pboard addTypes:[NSArray arrayWithObjects:NSHTMLPboardType,
152                               NSStringPboardType, nil]
153              owner:nil];
154   NSString* htmlString = @"<html><body><b>hi there</b></body></html>";
155   NSString* textString = @"hi there";
156   [pboard setString:htmlString forType:NSHTMLPboardType];
157   [pboard setString:textString forType:NSStringPboardType];
158   [drag_dest_ populateWebDropData:&data fromPasteboard:pboard];
159   EXPECT_EQ(data.url.spec(), "http://www.google.com/");
160   EXPECT_EQ(base::SysNSStringToUTF16(textString), data.text.string());
161   EXPECT_EQ(base::SysNSStringToUTF16(htmlString), data.html.string());
163   [pboard releaseGlobally];