Chromium Code Reviews| Index: chrome/browser/ui/cocoa/share_menu_controller_browsertest.mm |
| diff --git a/chrome/browser/ui/cocoa/share_menu_controller_browsertest.mm b/chrome/browser/ui/cocoa/share_menu_controller_browsertest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c8ff4baf320f60e342192f5ef061a99ba1df8b5b |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/share_menu_controller_browsertest.mm |
| @@ -0,0 +1,165 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "chrome/browser/ui/cocoa/share_menu_controller.h" |
| + |
| +#import "base/mac/scoped_nsobject.h" |
| +#import "base/path_service.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/cocoa/test/cocoa_profile_test.h" |
| +#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "net/base/mac/url_conversions.h" |
| +#include "testing/gtest_mac.h" |
| +#include "ui/base/l10n/l10n_util_mac.h" |
| + |
| +// Mock sharing service for sensing shared items. |
| +@interface MockSharingService : NSSharingService |
| + |
| +// Weak since both this object and the shared item |
| +// should only live in the scope of the test. |
| +@property(nonatomic, assign) id sharedItem; |
| + |
| +@end |
| + |
| +@implementation MockSharingService |
| + |
| +// The real one is backed by SHKSharingService parameters which |
| +// don't appear to be present when inheriting from vanilla |
| +// |NSSharingService|. |
| +@synthesize subject; |
| +@synthesize sharedItem = sharedItem_; |
| + |
| +- (void)performWithItems:(NSArray*)items { |
| + [self setSharedItem:[items firstObject]]; |
| +} |
| + |
| +@end |
| + |
| +class ShareMenuControllerTest : public InProcessBrowserTest { |
| + public: |
| + ShareMenuControllerTest() {} |
| + |
| + void SetUpOnMainThread() override { |
| + base::FilePath test_data_dir; |
| + PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| + embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| + ASSERT_TRUE(embedded_test_server()->Start()); |
| + |
| + url_ = embedded_test_server()->GetURL("/title2.html"); |
| + AddTabAtIndex(0, url_, ui::PAGE_TRANSITION_TYPED); |
| + controller_.reset([[ShareMenuController alloc] init]); |
| + } |
| + |
| + protected: |
| + // Create a menu item for |service| and trigger it using |
| + // the target/action of real menu items created by |
| + // |controller_| |
| + void PerformShare(NSSharingService* service) { |
| + base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Share"]); |
| + |
| + [controller_ menuNeedsUpdate:menu]; |
| + |
| + NSMenuItem* mockMenuItem = |
| + [[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.
|
| + [mockMenuItem setRepresentedObject:service]; |
| + |
| + NSMenuItem* firstMenuItem = [menu itemAtIndex:0]; |
| + id target = [firstMenuItem target]; |
| + SEL action = [firstMenuItem action]; |
| + [target performSelector:action withObject:mockMenuItem]; |
| + } |
| + GURL url_; |
| + base::scoped_nsobject<ShareMenuController> controller_; |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, PopulatesMenu) { |
| + base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Share"]); |
| + NSArray* sharingServicesForURL = [NSSharingService |
|
Robert Sesek
2017/06/28 15:37:55
Nit (and throughout this file): Since this is a C+
|
| + sharingServicesForItems:@[ [NSURL URLWithString:@"http://example.com"] ]]; |
| + EXPECT_GT([sharingServicesForURL count], 0U); |
| + |
| + [controller_ menuNeedsUpdate:menu]; |
| + |
| + // -1 for reading list, +1 for "More..." if it's showing. |
| + NSInteger expectedCount = [sharingServicesForURL count]; |
| + if (![ShareMenuController shouldShowMoreItem]) { |
| + expectedCount--; |
| + } |
| + EXPECT_EQ([menu numberOfItems], expectedCount); |
| + |
| + NSSharingService* readingListService = [NSSharingService |
| + sharingServiceNamed:NSSharingServiceNameAddToSafariReadingList]; |
| + |
| + NSUInteger i = 0; |
| + // Ensure there's a menu item for each service besides reading list. |
| + for (NSSharingService* service in sharingServicesForURL) { |
| + if ([service isEqual:readingListService]) |
| + continue; |
| + NSMenuItem* menuItem = [menu itemAtIndex:i]; |
| + EXPECT_NSEQ([menuItem representedObject], service); |
| + EXPECT_EQ([menuItem target], static_cast<id>(controller_)); |
| + i++; |
| + } |
| + |
| + // Ensure the menu is cleared between updates. |
| + [controller_ menuNeedsUpdate:menu]; |
| + EXPECT_EQ([menu numberOfItems], expectedCount); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, AddsMoreButton) { |
| + if (![ShareMenuController shouldShowMoreItem]) { |
| + return; |
| + } |
| + base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Share"]); |
| + [controller_ menuNeedsUpdate:menu]; |
| + |
| + NSInteger numberOfItems = [menu numberOfItems]; |
| + EXPECT_GT(numberOfItems, 0); |
| + NSMenuItem* lastItem = [menu itemAtIndex:numberOfItems - 1]; |
| + EXPECT_NSEQ(lastItem.title, l10n_util::GetNSString(IDS_SHARING_MORE_MAC)); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, ActionPerformsShare) { |
| + base::scoped_nsobject<MockSharingService> service( |
| + [[MockSharingService alloc] init]); |
| + EXPECT_FALSE([service sharedItem]); |
| + |
| + PerformShare(service); |
| + |
| + EXPECT_NSEQ([service sharedItem], net::NSURLWithGURL(url_)); |
| + // Title of chrome/test/data/title2.html |
| + EXPECT_NSEQ([service subject], @"Title Of Awesomeness"); |
| + EXPECT_EQ([service delegate], |
| + static_cast<id<NSSharingServiceDelegate>>(controller_)); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ShareMenuControllerTest, SharingDelegate) { |
| + NSURL* url = [NSURL URLWithString:@"http://google.com"]; |
| + base::scoped_nsobject<NSSharingService> service( |
| + [[NSSharingService alloc] init]); |
| + |
| + PerformShare(service); |
| + |
| + NSWindow* browserWindow = browser()->window()->GetNativeWindow(); |
| + EXPECT_FALSE(NSEqualRects([controller_ sharingService:service.get() |
| + sourceFrameOnScreenForShareItem:url], |
| + NSZeroRect)); |
| + NSSharingContentScope scope = NSSharingContentScopeItem; |
| + EXPECT_NSEQ([controller_ sharingService:service.get() |
| + sourceWindowForShareItems:@[ url ] |
| + sharingContentScope:&scope], |
| + browserWindow); |
| + EXPECT_EQ(scope, NSSharingContentScopeFull); |
| + NSRect contentRect; |
| + EXPECT_FALSE([controller_ sharingService:service.get() |
| + transitionImageForShareItem:url |
| + contentRect:&contentRect] == nil); |
| +} |