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 "chrome/browser/ui/cocoa/share_menu_controller.h" | |
6 | |
7 #import "base/mac/scoped_nsobject.h" | |
8 #import "base/path_service.h" | |
9 #include "base/strings/sys_string_conversions.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/browser_list.h" | |
12 #include "chrome/browser/ui/browser_window.h" | |
13 #include "chrome/browser/ui/cocoa/test/cocoa_profile_test.h" | |
14 #import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h" | |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "chrome/grit/generated_resources.h" | |
18 #include "chrome/test/base/in_process_browser_test.h" | |
19 #include "net/base/mac/url_conversions.h" | |
20 #include "testing/gtest_mac.h" | |
21 #include "ui/base/l10n/l10n_util_mac.h" | |
22 | |
23 // Mock sharing service for sensing shared items. | |
24 @interface MockSharingService : NSSharingService | |
25 | |
26 // Weak since both this object and the shared item | |
27 // should only live in the scope of the test. | |
28 @property(nonatomic, assign) id sharedItem; | |
29 | |
30 @end | |
31 | |
32 @implementation MockSharingService | |
33 | |
34 // The real one is backed by SHKSharingService parameters which | |
35 // don't appear to be present when inheriting from vanilla | |
36 // |NSSharingService|. | |
37 @synthesize subject; | |
38 @synthesize sharedItem = sharedItem_; | |
39 | |
40 - (void)performWithItems:(NSArray*)items { | |
41 [self setSharedItem:[items firstObject]]; | |
42 } | |
43 | |
44 @end | |
45 | |
46 class ShareMenuControllerTest : public InProcessBrowserTest { | |
47 public: | |
48 ShareMenuControllerTest() {} | |
49 | |
50 void SetUpOnMainThread() override { | |
51 base::FilePath test_data_dir; | |
52 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | |
53 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | |
54 ASSERT_TRUE(embedded_test_server()->Start()); | |
55 | |
56 url_ = embedded_test_server()->GetURL("/title2.html"); | |
57 AddTabAtIndex(0, url_, ui::PAGE_TRANSITION_TYPED); | |
58 controller_.reset([[ShareMenuController alloc] init]); | |
59 } | |
60 | |
61 protected: | |
62 // Create a menu item for |service| and trigger it using | |
63 // the target/action of real menu items created by | |
64 // |controller_| | |
65 void PerformShare(NSSharingService* service) { | |
66 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Share"]); | |
67 | |
68 [controller_ menuNeedsUpdate:menu]; | |
69 | |
70 NSMenuItem* mockMenuItem = | |
71 [[NSMenuItem alloc] initWithTitle:@"test" action:nil keyEquivalent:@""]; | |
Robert Sesek
2017/06/28 15:37:55
This is leaked. (Use scoped_nsobject).
lgrey
2017/06/29 21:11:50
Done.
| |
72 [mockMenuItem setRepresentedObject:service]; | |
73 | |
74 NSMenuItem* firstMenuItem = [menu itemAtIndex:0]; | |
75 id target = [firstMenuItem target]; | |
76 SEL action = [firstMenuItem action]; | |
77 [target performSelector:action withObject:mockMenuItem]; | |
78 } | |
79 GURL url_; | |
80 base::scoped_nsobject<ShareMenuController> controller_; | |
81 }; | |
82 | |
83 IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, PopulatesMenu) { | |
84 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Share"]); | |
85 NSArray* sharingServicesForURL = [NSSharingService | |
Robert Sesek
2017/06/28 15:37:55
Nit (and throughout this file): Since this is a C+
| |
86 sharingServicesForItems:@[ [NSURL URLWithString:@"http://example.com"] ]]; | |
87 EXPECT_GT([sharingServicesForURL count], 0U); | |
88 | |
89 [controller_ menuNeedsUpdate:menu]; | |
90 | |
91 // -1 for reading list, +1 for "More..." if it's showing. | |
92 NSInteger expectedCount = [sharingServicesForURL count]; | |
93 if (![ShareMenuController shouldShowMoreItem]) { | |
94 expectedCount--; | |
95 } | |
96 EXPECT_EQ([menu numberOfItems], expectedCount); | |
97 | |
98 NSSharingService* readingListService = [NSSharingService | |
99 sharingServiceNamed:NSSharingServiceNameAddToSafariReadingList]; | |
100 | |
101 NSUInteger i = 0; | |
102 // Ensure there's a menu item for each service besides reading list. | |
103 for (NSSharingService* service in sharingServicesForURL) { | |
104 if ([service isEqual:readingListService]) | |
105 continue; | |
106 NSMenuItem* menuItem = [menu itemAtIndex:i]; | |
107 EXPECT_NSEQ([menuItem representedObject], service); | |
108 EXPECT_EQ([menuItem target], static_cast<id>(controller_)); | |
109 i++; | |
110 } | |
111 | |
112 // Ensure the menu is cleared between updates. | |
113 [controller_ menuNeedsUpdate:menu]; | |
114 EXPECT_EQ([menu numberOfItems], expectedCount); | |
115 } | |
116 | |
117 IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, AddsMoreButton) { | |
118 if (![ShareMenuController shouldShowMoreItem]) { | |
119 return; | |
120 } | |
121 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Share"]); | |
122 [controller_ menuNeedsUpdate:menu]; | |
123 | |
124 NSInteger numberOfItems = [menu numberOfItems]; | |
125 EXPECT_GT(numberOfItems, 0); | |
126 NSMenuItem* lastItem = [menu itemAtIndex:numberOfItems - 1]; | |
127 EXPECT_NSEQ(lastItem.title, l10n_util::GetNSString(IDS_SHARING_MORE_MAC)); | |
128 } | |
129 | |
130 IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, ActionPerformsShare) { | |
131 base::scoped_nsobject<MockSharingService> service( | |
132 [[MockSharingService alloc] init]); | |
133 EXPECT_FALSE([service sharedItem]); | |
134 | |
135 PerformShare(service); | |
136 | |
137 EXPECT_NSEQ([service sharedItem], net::NSURLWithGURL(url_)); | |
138 // Title of chrome/test/data/title2.html | |
139 EXPECT_NSEQ([service subject], @"Title Of Awesomeness"); | |
140 EXPECT_EQ([service delegate], | |
141 static_cast<id<NSSharingServiceDelegate>>(controller_)); | |
142 } | |
143 | |
144 IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, SharingDelegate) { | |
145 NSURL* url = [NSURL URLWithString:@"http://google.com"]; | |
146 base::scoped_nsobject<NSSharingService> service( | |
147 [[NSSharingService alloc] init]); | |
148 | |
149 PerformShare(service); | |
150 | |
151 NSWindow* browserWindow = browser()->window()->GetNativeWindow(); | |
152 EXPECT_FALSE(NSEqualRects([controller_ sharingService:service.get() | |
153 sourceFrameOnScreenForShareItem:url], | |
154 NSZeroRect)); | |
155 NSSharingContentScope scope = NSSharingContentScopeItem; | |
156 EXPECT_NSEQ([controller_ sharingService:service.get() | |
157 sourceWindowForShareItems:@[ url ] | |
158 sharingContentScope:&scope], | |
159 browserWindow); | |
160 EXPECT_EQ(scope, NSSharingContentScopeFull); | |
161 NSRect contentRect; | |
162 EXPECT_FALSE([controller_ sharingService:service.get() | |
163 transitionImageForShareItem:url | |
164 contentRect:&contentRect] == nil); | |
165 } | |
OLD | NEW |