Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: chrome/browser/ui/cocoa/share_menu_controller_browsertest.mm

Issue 2950403002: [Mac] Add system share menu
Patch Set: CL comments Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 base::scoped_nsobject<NSMenuItem> mock_menu_item([[NSMenuItem alloc]
71 initWithTitle:@"test"
72 action:nil
73 keyEquivalent:@""]);
74 [mock_menu_item setRepresentedObject:service];
75
76 NSMenuItem* first_menu_item = [menu itemAtIndex:0];
77 id target = [first_menu_item target];
78 SEL action = [first_menu_item action];
79 [target performSelector:action withObject:mock_menu_item.get()];
80 }
81 GURL url_;
82 base::scoped_nsobject<ShareMenuController> controller_;
83 };
84
85 IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, PopulatesMenu) {
86 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Share"]);
87 NSArray* sharing_services_for_url = [NSSharingService
88 sharingServicesForItems:@[ [NSURL URLWithString:@"http://example.com"] ]];
89 EXPECT_GT([sharing_services_for_url count], 0U);
90
91 [controller_ menuNeedsUpdate:menu];
92
93 // -1 for reading list, +1 for "More..." if it's showing.
94 NSInteger expected_count = [sharing_services_for_url count];
95 if (![ShareMenuController shouldShowMoreItem]) {
96 expected_count--;
97 }
98 EXPECT_EQ([menu numberOfItems], expected_count);
99
100 NSSharingService* reading_list_service = [NSSharingService
101 sharingServiceNamed:NSSharingServiceNameAddToSafariReadingList];
102
103 NSUInteger i = 0;
104 // Ensure there's a menu item for each service besides reading list.
105 for (NSSharingService* service in sharing_services_for_url) {
106 if ([service isEqual:reading_list_service])
107 continue;
108 NSMenuItem* menu_item = [menu itemAtIndex:i];
109 EXPECT_NSEQ([menu_item representedObject], service);
110 EXPECT_EQ([menu_item target], static_cast<id>(controller_));
111 i++;
112 }
113
114 // Ensure the menu is cleared between updates.
115 [controller_ menuNeedsUpdate:menu];
116 EXPECT_EQ([menu numberOfItems], expected_count);
117 }
118
119 IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, AddsMoreButton) {
120 if (![ShareMenuController shouldShowMoreItem]) {
121 return;
122 }
123 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Share"]);
124 [controller_ menuNeedsUpdate:menu];
125
126 NSInteger number_of_items = [menu numberOfItems];
127 EXPECT_GT(number_of_items, 0);
128 NSMenuItem* last_item = [menu itemAtIndex:number_of_items - 1];
129 EXPECT_NSEQ(last_item.title, l10n_util::GetNSString(IDS_SHARING_MORE_MAC));
130 }
131
132 IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, ActionPerformsShare) {
133 base::scoped_nsobject<MockSharingService> service(
134 [[MockSharingService alloc] init]);
135 EXPECT_FALSE([service sharedItem]);
136
137 PerformShare(service);
138
139 EXPECT_NSEQ([service sharedItem], net::NSURLWithGURL(url_));
140 // Title of chrome/test/data/title2.html
141 EXPECT_NSEQ([service subject], @"Title Of Awesomeness");
142 EXPECT_EQ([service delegate],
143 static_cast<id<NSSharingServiceDelegate>>(controller_));
144 }
145
146 IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, SharingDelegate) {
147 NSURL* url = [NSURL URLWithString:@"http://google.com"];
148 base::scoped_nsobject<NSSharingService> service(
149 [[NSSharingService alloc] init]);
150
151 PerformShare(service);
152
153 NSWindow* browser_window = browser()->window()->GetNativeWindow();
154 EXPECT_FALSE(NSEqualRects([controller_ sharingService:service.get()
155 sourceFrameOnScreenForShareItem:url],
156 NSZeroRect));
157 NSSharingContentScope scope = NSSharingContentScopeItem;
158 EXPECT_NSEQ([controller_ sharingService:service.get()
159 sourceWindowForShareItems:@[ url ]
160 sharingContentScope:&scope],
161 browser_window);
162 EXPECT_EQ(scope, NSSharingContentScopeFull);
163 NSRect contentRect;
164 EXPECT_FALSE([controller_ sharingService:service.get()
165 transitionImageForShareItem:url
166 contentRect:&contentRect] == nil);
167 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698