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