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