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