Index: ios/chrome/browser/ui/context_menu/context_menu_coordinator_unittest.mm |
diff --git a/ios/chrome/browser/ui/context_menu/context_menu_coordinator_unittest.mm b/ios/chrome/browser/ui/context_menu/context_menu_coordinator_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c2429e0cbcfc3abbd1dd2051e770ae1e09085721 |
--- /dev/null |
+++ b/ios/chrome/browser/ui/context_menu/context_menu_coordinator_unittest.mm |
@@ -0,0 +1,142 @@ |
+// Copyright 2016 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 "ios/chrome/browser/ui/context_menu/context_menu_coordinator.h" |
+ |
+#import <UIKit/UIKit.h> |
+ |
+#import "base/mac/scoped_nsobject.h" |
+#import "ios/web/public/web_state/context_menu_params.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "testing/platform_test.h" |
+ |
+// Fixture to test ContextMenuCoordinator. |
+class ContextMenuCoordinatorTest : public PlatformTest { |
+ public: |
+ ContextMenuCoordinatorTest() { |
+ window_.reset( |
+ [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]); |
+ [window_ makeKeyAndVisible]; |
marq (ping after 24h)
2016/05/21 09:39:58
Does the destructor need to restore the previous k
michaeldo
2016/05/23 21:19:07
It looks like the last window which was created an
|
+ view_controller_.reset([[UIViewController alloc] init]); |
+ [window_ setRootViewController:view_controller_]; |
+ } |
+ |
+ protected: |
+ base::scoped_nsobject<ContextMenuCoordinator> menu_coordinator_; |
+ base::scoped_nsobject<UIWindow> window_; |
+ base::scoped_nsobject<UIViewController> view_controller_; |
+}; |
+ |
+// Tests the context menu reports as visible after presenting. |
+TEST_F(ContextMenuCoordinatorTest, ValidateIsVisible) { |
+ web::ContextMenuParams params; |
+ params.location = CGPointZero; |
+ params.view.reset([[view_controller_ view] retain]); |
+ menu_coordinator_.reset([[ContextMenuCoordinator alloc] |
+ initWithViewController:view_controller_ |
+ params:params]); |
+ [menu_coordinator_ start]; |
+ |
+ EXPECT_TRUE([menu_coordinator_ isVisible]); |
+} |
+ |
+// Tests the context menu dismissal. |
marq (ping after 24h)
2016/05/21 09:39:58
Also test that destroying the coordinator without
michaeldo
2016/05/23 21:19:08
Done.
|
+TEST_F(ContextMenuCoordinatorTest, ValidateDismissal) { |
+ web::ContextMenuParams params; |
+ params.location = CGPointZero; |
+ params.view.reset([[view_controller_ view] retain]); |
+ menu_coordinator_.reset([[ContextMenuCoordinator alloc] |
+ initWithViewController:view_controller_ |
+ params:params]); |
+ [menu_coordinator_ start]; |
+ |
+ [menu_coordinator_ stop]; |
+ |
+ EXPECT_FALSE([menu_coordinator_ isVisible]); |
+} |
+ |
+// Tests that only the expected actions are present on the context menu. |
+TEST_F(ContextMenuCoordinatorTest, ValidateActions) { |
+ web::ContextMenuParams params; |
+ params.location = CGPointZero; |
+ params.view.reset([[view_controller_ view] retain]); |
+ menu_coordinator_.reset([[ContextMenuCoordinator alloc] |
+ initWithViewController:view_controller_ |
+ params:params]); |
+ |
+ NSArray* menu_titles = @[ @"foo", @"bar" ]; |
+ for (NSString* title in menu_titles) { |
+ [menu_coordinator_ addItemWithTitle:title |
+ action:^{ |
+ }]; |
+ } |
+ |
+ [menu_coordinator_ start]; |
+ |
+ EXPECT_TRUE([[view_controller_ presentedViewController] |
+ isKindOfClass:UIAlertController.class]); |
Eugene But (OOO till 7-30)
2016/05/20 21:26:46
[UIAlertController class]
michaeldo
2016/05/23 21:19:08
Done.
|
+ UIAlertController* alert_controller = static_cast<UIAlertController*>( |
Eugene But (OOO till 7-30)
2016/05/20 21:26:46
ObjCCastStrict
michaeldo
2016/05/23 21:19:08
Done.
|
+ [view_controller_ presentedViewController]); |
+ |
+ base::scoped_nsobject<NSMutableArray> remaining_titles( |
+ [menu_titles mutableCopy]); |
+ [alert_controller.actions |
Eugene But (OOO till 7-30)
2016/05/20 21:26:45
Why not a for loop?
michaeldo
2016/05/23 21:19:08
great question! switched to simpler for loop.
|
+ enumerateObjectsUsingBlock:^(UIAlertAction* action, NSUInteger idx, |
+ BOOL* stop) { |
+ if (action.style != UIAlertActionStyleCancel) { |
+ EXPECT_TRUE([remaining_titles containsObject:action.title]); |
+ [remaining_titles removeObject:action.title]; |
+ } |
+ }]; |
+ |
+ EXPECT_EQ(0LU, [remaining_titles count]); |
+} |
+ |
+// Validates that the cancel action is present on the context menu. |
+TEST_F(ContextMenuCoordinatorTest, CancelButtonExists) { |
+ web::ContextMenuParams params; |
+ params.location = CGPointZero; |
+ params.view.reset([[view_controller_ view] retain]); |
+ menu_coordinator_.reset([[ContextMenuCoordinator alloc] |
+ initWithViewController:view_controller_ |
+ params:params]); |
+ |
+ [menu_coordinator_ start]; |
+ |
+ EXPECT_TRUE([[view_controller_ presentedViewController] |
+ isKindOfClass:UIAlertController.class]); |
+ UIAlertController* alert_controller = static_cast<UIAlertController*>( |
+ [view_controller_ presentedViewController]); |
+ |
+ EXPECT_EQ(1LU, alert_controller.actions.count); |
+ EXPECT_EQ(UIAlertActionStyleCancel, |
+ [[alert_controller.actions firstObject] style]); |
Eugene But (OOO till 7-30)
2016/05/20 21:26:46
NIT: alert_controller.actions.firstObject for cons
michaeldo
2016/05/23 21:19:08
Done.
|
+} |
+ |
+// Test that the ContextMenuParams are used to display context menu. |
Eugene But (OOO till 7-30)
2016/05/20 21:26:46
s/Test/Tests
michaeldo
2016/05/23 21:19:07
Done.
|
+TEST_F(ContextMenuCoordinatorTest, ValidateContextMenuParams) { |
+ CGPoint location = CGPointMake(100.0, 125.0); |
+ NSString* title = @"Context Menu Title"; |
+ |
+ web::ContextMenuParams params; |
+ params.location = location; |
+ params.menu_title.reset(title); |
+ params.view.reset([[view_controller_ view] retain]); |
+ menu_coordinator_.reset([[ContextMenuCoordinator alloc] |
+ initWithViewController:view_controller_ |
+ params:params]); |
+ [menu_coordinator_ start]; |
+ |
+ EXPECT_TRUE([[view_controller_ presentedViewController] |
+ isKindOfClass:UIAlertController.class]); |
+ UIAlertController* alert_controller = static_cast<UIAlertController*>( |
+ [view_controller_ presentedViewController]); |
+ |
+ EXPECT_EQ(title, alert_controller.title); |
+ |
+ CGPoint presentedLocation = |
+ alert_controller.popoverPresentationController.sourceRect.origin; |
+ EXPECT_EQ(location.x, presentedLocation.x); |
+ EXPECT_EQ(location.y, presentedLocation.y); |
+} |