OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/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 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
10 #import "chrome/browser/ui/cocoa/tab_contents/web_drop_target.h" | |
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
12 #include "content/browser/tab_contents/test_tab_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 #include "webkit/glue/webdropdata.h" | |
17 | |
18 class WebDropTargetTest : public ChromeRenderViewHostTestHarness { | |
19 public: | |
20 virtual void SetUp() { | |
21 ChromeRenderViewHostTestHarness::SetUp(); | |
22 CocoaTest::BootstrapCocoa(); | |
23 drop_target_.reset([[WebDropTarget alloc] initWithTabContents:contents()]); | |
24 } | |
25 | |
26 void PutURLOnPasteboard(NSString* urlString, NSPasteboard* pboard) { | |
27 [pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType] | |
28 owner:nil]; | |
29 NSURL* url = [NSURL URLWithString:urlString]; | |
30 EXPECT_TRUE(url); | |
31 [url writeToPasteboard:pboard]; | |
32 } | |
33 | |
34 void PutCoreURLAndTitleOnPasteboard(NSString* urlString, NSString* title, | |
35 NSPasteboard* pboard) { | |
36 [pboard | |
37 declareTypes:[NSArray arrayWithObjects:kCorePasteboardFlavorType_url, | |
38 kCorePasteboardFlavorType_urln, | |
39 nil] | |
40 owner:nil]; | |
41 [pboard setString:urlString | |
42 forType:kCorePasteboardFlavorType_url]; | |
43 [pboard setString:title | |
44 forType:kCorePasteboardFlavorType_urln]; | |
45 } | |
46 | |
47 base::mac::ScopedNSAutoreleasePool pool_; | |
48 scoped_nsobject<WebDropTarget> drop_target_; | |
49 }; | |
50 | |
51 // Make sure nothing leaks. | |
52 TEST_F(WebDropTargetTest, Init) { | |
53 EXPECT_TRUE(drop_target_); | |
54 } | |
55 | |
56 // Test flipping of coordinates given a point in window coordinates. | |
57 TEST_F(WebDropTargetTest, Flip) { | |
58 NSPoint windowPoint = NSZeroPoint; | |
59 scoped_nsobject<NSWindow> window([[CocoaTestHelperWindow alloc] init]); | |
60 NSPoint viewPoint = | |
61 [drop_target_ flipWindowPointToView:windowPoint | |
62 view:[window contentView]]; | |
63 NSPoint screenPoint = | |
64 [drop_target_ flipWindowPointToScreen:windowPoint | |
65 view:[window contentView]]; | |
66 EXPECT_EQ(0, viewPoint.x); | |
67 EXPECT_EQ(600, viewPoint.y); | |
68 EXPECT_EQ(0, screenPoint.x); | |
69 // We can't put a value on the screen size since everyone will have a | |
70 // different one. | |
71 EXPECT_NE(0, screenPoint.y); | |
72 } | |
73 | |
74 TEST_F(WebDropTargetTest, URL) { | |
75 NSPasteboard* pboard = nil; | |
76 NSString* url = nil; | |
77 NSString* title = nil; | |
78 GURL result_url; | |
79 string16 result_title; | |
80 | |
81 // Put a URL on the pasteboard and check it. | |
82 pboard = [NSPasteboard pasteboardWithUniqueName]; | |
83 url = @"http://www.google.com/"; | |
84 PutURLOnPasteboard(url, pboard); | |
85 EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard( | |
86 &result_url, &result_title, pboard, NO)); | |
87 EXPECT_EQ(base::SysNSStringToUTF8(url), result_url.spec()); | |
88 [pboard releaseGlobally]; | |
89 | |
90 // Put a 'url ' and 'urln' on the pasteboard and check it. | |
91 pboard = [NSPasteboard pasteboardWithUniqueName]; | |
92 url = @"http://www.google.com/"; | |
93 title = @"Title of Awesomeness!", | |
94 PutCoreURLAndTitleOnPasteboard(url, title, pboard); | |
95 EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard( | |
96 &result_url, &result_title, pboard, NO)); | |
97 EXPECT_EQ(base::SysNSStringToUTF8(url), result_url.spec()); | |
98 EXPECT_EQ(base::SysNSStringToUTF16(title), result_title); | |
99 [pboard releaseGlobally]; | |
100 | |
101 // Also check that it passes file:// via 'url '/'urln' properly. | |
102 pboard = [NSPasteboard pasteboardWithUniqueName]; | |
103 url = @"file:///tmp/dont_delete_me.txt"; | |
104 title = @"very important"; | |
105 PutCoreURLAndTitleOnPasteboard(url, title, pboard); | |
106 EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard( | |
107 &result_url, &result_title, pboard, NO)); | |
108 EXPECT_EQ(base::SysNSStringToUTF8(url), result_url.spec()); | |
109 EXPECT_EQ(base::SysNSStringToUTF16(title), result_title); | |
110 [pboard releaseGlobally]; | |
111 | |
112 // And javascript:. | |
113 pboard = [NSPasteboard pasteboardWithUniqueName]; | |
114 url = @"javascript:open('http://www.youtube.com/')"; | |
115 title = @"kill some time"; | |
116 PutCoreURLAndTitleOnPasteboard(url, title, pboard); | |
117 EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard( | |
118 &result_url, &result_title, pboard, NO)); | |
119 EXPECT_EQ(base::SysNSStringToUTF8(url), result_url.spec()); | |
120 EXPECT_EQ(base::SysNSStringToUTF16(title), result_title); | |
121 [pboard releaseGlobally]; | |
122 | |
123 pboard = [NSPasteboard pasteboardWithUniqueName]; | |
124 url = @"/bin/sh"; | |
125 [pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] | |
126 owner:nil]; | |
127 [pboard setPropertyList:[NSArray arrayWithObject:url] | |
128 forType:NSFilenamesPboardType]; | |
129 EXPECT_FALSE(ui::PopulateURLAndTitleFromPasteboard( | |
130 &result_url, &result_title, pboard, NO)); | |
131 EXPECT_TRUE(ui::PopulateURLAndTitleFromPasteboard( | |
132 &result_url, &result_title, pboard, YES)); | |
133 EXPECT_EQ("file://localhost/bin/sh", result_url.spec()); | |
134 EXPECT_EQ("sh", UTF16ToUTF8(result_title)); | |
135 [pboard releaseGlobally]; | |
136 } | |
137 | |
138 TEST_F(WebDropTargetTest, Data) { | |
139 WebDropData data; | |
140 NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName]; | |
141 | |
142 PutURLOnPasteboard(@"http://www.google.com", pboard); | |
143 [pboard addTypes:[NSArray arrayWithObjects:NSHTMLPboardType, | |
144 NSStringPboardType, nil] | |
145 owner:nil]; | |
146 NSString* htmlString = @"<html><body><b>hi there</b></body></html>"; | |
147 NSString* textString = @"hi there"; | |
148 [pboard setString:htmlString forType:NSHTMLPboardType]; | |
149 [pboard setString:textString forType:NSStringPboardType]; | |
150 [drop_target_ populateWebDropData:&data fromPasteboard:pboard]; | |
151 EXPECT_EQ(data.url.spec(), "http://www.google.com/"); | |
152 EXPECT_EQ(base::SysNSStringToUTF16(textString), data.plain_text); | |
153 EXPECT_EQ(base::SysNSStringToUTF16(htmlString), data.text_html); | |
154 | |
155 [pboard releaseGlobally]; | |
156 } | |
OLD | NEW |