| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 #import "ios/web/web_state/context_menu_params_utils.h" |
| 6 |
| 7 #include "base/strings/sys_string_conversions.h" |
| 8 #include "components/url_formatter/url_formatter.h" |
| 9 #include "ios/web/public/referrer_util.h" |
| 10 #import "ios/web/public/web_state/context_menu_params.h" |
| 11 #import "net/base/mac/url_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #import "testing/gtest_mac.h" |
| 14 #include "testing/platform_test.h" |
| 15 |
| 16 namespace { |
| 17 // Text values for the tapped element triggering the context menu. |
| 18 const char* kLinkUrl = "http://link.url/"; |
| 19 const char* kSrcUrl = "http://src.url/"; |
| 20 const char* kTitle = "title"; |
| 21 const char* kReferrerPolicy = "always"; |
| 22 const char* kLinkText = "link text"; |
| 23 const char* kJavaScriptLinkUrl = "javascript://src.url/"; |
| 24 const char* kDataUrl = "data://foo.bar/"; |
| 25 } |
| 26 |
| 27 // Test fixture for error translation testing. |
| 28 typedef PlatformTest ContextMenuParamsUtilsTest; |
| 29 |
| 30 // Tests the empty contructor. |
| 31 TEST_F(ContextMenuParamsUtilsTest, EmptyParams) { |
| 32 web::ContextMenuParams params; |
| 33 EXPECT_EQ(params.menu_title.get(), nil); |
| 34 EXPECT_FALSE(params.link_url.is_valid()); |
| 35 EXPECT_FALSE(params.src_url.is_valid()); |
| 36 EXPECT_EQ(params.referrer_policy, web::ReferrerPolicyDefault); |
| 37 EXPECT_EQ(params.view.get(), nil); |
| 38 EXPECT_TRUE(CGPointEqualToPoint(params.location, CGPointZero)); |
| 39 EXPECT_EQ(params.link_text.get(), nil); |
| 40 } |
| 41 |
| 42 // Tests the the parsing of the element NSDictionary. |
| 43 TEST_F(ContextMenuParamsUtilsTest, DictionaryConstructorTest) { |
| 44 web::ContextMenuParams params = web::ContextMenuParamsFromElementDictionary(@{ |
| 45 @"href" : @(kLinkUrl), |
| 46 @"src" : @(kSrcUrl), |
| 47 @"title" : @(kTitle), |
| 48 @"referrerPolicy" : @(kReferrerPolicy), |
| 49 @"innerText" : @(kLinkText), |
| 50 }); |
| 51 |
| 52 EXPECT_NSEQ(params.menu_title.get(), @(kTitle)); |
| 53 EXPECT_EQ(params.link_url, GURL(kLinkUrl)); |
| 54 EXPECT_EQ(params.src_url, GURL(kSrcUrl)); |
| 55 EXPECT_NSEQ(params.link_text.get(), @(kLinkText)); |
| 56 EXPECT_EQ(params.referrer_policy, |
| 57 web::ReferrerPolicyFromString(kReferrerPolicy)); |
| 58 |
| 59 EXPECT_EQ(params.view.get(), nil); |
| 60 EXPECT_TRUE(CGPointEqualToPoint(params.location, CGPointZero)); |
| 61 } |
| 62 |
| 63 // Tests title is set as the formatted URL there is no title. |
| 64 TEST_F(ContextMenuParamsUtilsTest, DictionaryConstructorTestNoTitle) { |
| 65 web::ContextMenuParams params = web::ContextMenuParamsFromElementDictionary(@{ |
| 66 @"href" : @(kLinkUrl), |
| 67 }); |
| 68 base::string16 urlText = url_formatter::FormatUrl(GURL(kLinkUrl)); |
| 69 NSString* title = base::SysUTF16ToNSString(urlText); |
| 70 |
| 71 EXPECT_NSEQ(params.menu_title.get(), title); |
| 72 } |
| 73 |
| 74 // Tests title is set to "JavaScript" if there is no title and "href" links to |
| 75 // JavaScript URL. |
| 76 TEST_F(ContextMenuParamsUtilsTest, DictionaryConstructorTestJavascriptTitle) { |
| 77 web::ContextMenuParams params = web::ContextMenuParamsFromElementDictionary(@{ |
| 78 @"href" : @(kJavaScriptLinkUrl), |
| 79 }); |
| 80 EXPECT_NSEQ(params.menu_title.get(), @"JavaScript"); |
| 81 } |
| 82 |
| 83 // Tests title is set to |src_url| if there is no title. |
| 84 TEST_F(ContextMenuParamsUtilsTest, DictionaryConstructorTestSrcTitle) { |
| 85 web::ContextMenuParams params = web::ContextMenuParamsFromElementDictionary(@{ |
| 86 @"src" : @(kSrcUrl), |
| 87 }); |
| 88 EXPECT_EQ(params.src_url, GURL(kSrcUrl)); |
| 89 EXPECT_NSEQ(params.menu_title.get(), @(kSrcUrl)); |
| 90 } |
| 91 |
| 92 // Tests title is set to nil if there is no title and src is a data URL. |
| 93 TEST_F(ContextMenuParamsUtilsTest, DictionaryConstructorTestDataTitle) { |
| 94 web::ContextMenuParams params = web::ContextMenuParamsFromElementDictionary(@{ |
| 95 @"src" : @(kDataUrl), |
| 96 }); |
| 97 EXPECT_EQ(params.src_url, GURL(kDataUrl)); |
| 98 EXPECT_NSEQ(params.menu_title.get(), nil); |
| 99 } |
| OLD | NEW |