OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_mediator.h" | 5 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_mediator.h" |
6 | 6 |
7 #if !defined(__has_feature) || !__has_feature(objc_arc) | 7 #if !defined(__has_feature) || !__has_feature(objc_arc) |
8 #error "This file requires ARC support." | 8 #error "This file requires ARC support." |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_consumer.h" | 12 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_consumer.h" |
| 13 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_context_impl.h" |
| 14 #import "ios/web/public/web_state/context_menu_params.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "testing/gtest_mac.h" | 16 #include "testing/gtest_mac.h" |
15 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
| 18 #include "url/gurl.h" |
16 | 19 |
17 @interface TestConsumer : NSObject<ContextMenuConsumer> | 20 @interface TestConsumer : NSObject<ContextMenuConsumer> |
| 21 @property(nonatomic, strong) ContextMenuContext* context; |
18 @property(nonatomic, copy) NSString* title; | 22 @property(nonatomic, copy) NSString* title; |
19 @property(nonatomic, copy) NSArray<ContextMenuItem*>* items; | 23 @property(nonatomic, copy) NSArray<ContextMenuItem*>* items; |
| 24 @property(nonatomic, strong) ContextMenuItem* cancelItem; |
20 @end | 25 @end |
21 | 26 |
22 @implementation TestConsumer | 27 @implementation TestConsumer |
| 28 @synthesize context = _context; |
23 @synthesize title = _title; | 29 @synthesize title = _title; |
24 @synthesize items = _items; | 30 @synthesize items = _items; |
| 31 @synthesize cancelItem = _cancelItem; |
| 32 |
| 33 - (void)setContextMenuContext:(ContextMenuContext*)context { |
| 34 self.context = context; |
| 35 } |
25 | 36 |
26 - (void)setContextMenuTitle:(NSString*)title { | 37 - (void)setContextMenuTitle:(NSString*)title { |
27 self.title = title; | 38 self.title = title; |
28 } | 39 } |
29 | 40 |
30 - (void)setContextMenuItems:(NSArray<ContextMenuItem*>*)items { | 41 - (void)setContextMenuItems:(NSArray<ContextMenuItem*>*)items |
| 42 cancelItem:(ContextMenuItem*)cancelItem { |
31 self.items = items; | 43 self.items = items; |
| 44 self.cancelItem = cancelItem; |
32 } | 45 } |
33 | 46 |
34 @end | 47 @end |
35 | 48 |
36 TEST(ContextMenuMediatorTest, TestTitleAndItems) { | 49 // Tests that the menu title is set properly. |
37 // A minimal test -- that once initialized, the mediator has set a non-empty | 50 TEST(ContextMenuMediatorTest, MenuTitle) { |
38 // title on the consumer, and passed a non-empty list of items with non-empty | 51 // Create a context with |kMenuTitle|. |
39 // titles. | 52 NSString* kMenuTitle = @"Menu Title"; |
| 53 web::ContextMenuParams params; |
| 54 params.menu_title.reset(kMenuTitle); |
| 55 ContextMenuContextImpl* context = |
| 56 [[ContextMenuContextImpl alloc] initWithParams:params]; |
| 57 // Update the consumer and verify that |kMenuTitle| is set. |
40 TestConsumer* consumer = [[TestConsumer alloc] init]; | 58 TestConsumer* consumer = [[TestConsumer alloc] init]; |
41 ContextMenuMediator* mediator = | 59 [ContextMenuMediator updateConsumer:consumer withContext:context]; |
42 [[ContextMenuMediator alloc] initWithConsumer:consumer]; | 60 EXPECT_NSEQ(kMenuTitle, consumer.title); |
| 61 } |
43 | 62 |
44 EXPECT_NE(nil, mediator); | 63 // Tests that the script option is shown for javacript: scheme URLs. |
| 64 TEST(ContextMenuMediatorTest, ScriptItem) { |
| 65 // Create a context with |kJavaScriptURL|. |
| 66 GURL kJavaScriptURL("javascript:alert('test');"); |
| 67 web::ContextMenuParams params; |
| 68 params.link_url = kJavaScriptURL; |
| 69 ContextMenuContextImpl* context = |
| 70 [[ContextMenuContextImpl alloc] initWithParams:params]; |
| 71 // Update the consumer and verify that the script option was added. |
| 72 TestConsumer* consumer = [[TestConsumer alloc] init]; |
| 73 [ContextMenuMediator updateConsumer:consumer withContext:context]; |
| 74 ContextMenuItem* script_item = [consumer.items firstObject]; |
| 75 EXPECT_NSEQ(script_item.title, @"Execute Script"); |
| 76 } |
45 | 77 |
46 EXPECT_NE(0U, consumer.title.length); | 78 // Tests that the link options are shown for valid link URLs. |
47 EXPECT_NE(0U, consumer.items.count); | 79 TEST(ContextMenuMediatorTest, LinkItems) { |
48 | 80 // Create a context with a valid link URL. |
49 for (ContextMenuItem* item in consumer.items) { | 81 web::ContextMenuParams params; |
50 EXPECT_NE(0U, item.title.length); | 82 params.link_url = GURL("http://valid.url.com"); |
| 83 ; |
| 84 ContextMenuContextImpl* context = |
| 85 [[ContextMenuContextImpl alloc] initWithParams:params]; |
| 86 // Update the consumer and verify that the link options were added. |
| 87 TestConsumer* consumer = [[TestConsumer alloc] init]; |
| 88 [ContextMenuMediator updateConsumer:consumer withContext:context]; |
| 89 NSArray* link_option_titles = @[ |
| 90 @"Open In New Tab", |
| 91 @"Open In New Incognito Tab", |
| 92 @"Copy Link", |
| 93 ]; |
| 94 ASSERT_EQ(link_option_titles.count, consumer.items.count); |
| 95 for (NSUInteger i = 0; i < link_option_titles.count; ++i) { |
| 96 EXPECT_NSEQ(link_option_titles[i], consumer.items[i].title); |
51 } | 97 } |
52 } | 98 } |
| 99 |
| 100 // Tests that the image options are shown for valid image URLs. |
| 101 TEST(ContextMenuMediatorTest, ImageItems) { |
| 102 // Create a context with a valid image URL. |
| 103 web::ContextMenuParams params; |
| 104 params.src_url = GURL("http://valid.url.com"); |
| 105 ; |
| 106 ContextMenuContextImpl* context = |
| 107 [[ContextMenuContextImpl alloc] initWithParams:params]; |
| 108 // Update the consumer and verify that the image options were added. |
| 109 TestConsumer* consumer = [[TestConsumer alloc] init]; |
| 110 [ContextMenuMediator updateConsumer:consumer withContext:context]; |
| 111 NSArray* image_option_titles = @[ |
| 112 @"Save Image", |
| 113 @"Open Image", |
| 114 @"Open Image In New Tab", |
| 115 ]; |
| 116 ASSERT_EQ(image_option_titles.count, consumer.items.count); |
| 117 for (NSUInteger i = 0; i < image_option_titles.count; ++i) { |
| 118 EXPECT_NSEQ(image_option_titles[i], consumer.items[i].title); |
| 119 } |
| 120 } |
| 121 |
| 122 // Tests that the cancel item is always added. |
| 123 TEST(ContextMenuMediatorTest, CancelItem) { |
| 124 // Create an empty context; the cancel item should be added regardless. |
| 125 web::ContextMenuParams params; |
| 126 ContextMenuContextImpl* context = |
| 127 [[ContextMenuContextImpl alloc] initWithParams:params]; |
| 128 // Update the consumer and verify that the image options were added. |
| 129 TestConsumer* consumer = [[TestConsumer alloc] init]; |
| 130 [ContextMenuMediator updateConsumer:consumer withContext:context]; |
| 131 EXPECT_NSEQ(@"Cancel", consumer.cancelItem.title); |
| 132 } |
OLD | NEW |