Chromium Code Reviews| Index: ios/chrome/browser/ui/alert_coordinator/modal_alert_unittest.mm |
| diff --git a/ios/chrome/browser/ui/alert_coordinator/modal_alert_unittest.mm b/ios/chrome/browser/ui/alert_coordinator/modal_alert_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a2d764763cb3ff8e4c9792ce533fa2d72af1ea16 |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/alert_coordinator/modal_alert_unittest.mm |
| @@ -0,0 +1,117 @@ |
| +// 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/alert_coordinator/modal_alert.h" |
| + |
| +#import <UIKit/UIKit.h> |
| + |
| +#import "base/mac/scoped_nsobject.h" |
| +#import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "testing/platform_test.h" |
| +#import "third_party/ocmock/OCMock/OCMock.h" |
| +#include "third_party/ocmock/gtest_support.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/strings/grit/ui_strings.h" |
| + |
| +// Tests that ActionSheet ask for a default cancel button. |
|
lpromero
2016/07/18 12:11:36
s/ActionSheet/ModalAlert, or AlertDelegate.
|
| +TEST(ModalAlertTest, ShouldAddCancelButton) { |
| + // Setup. |
| + base::scoped_nsobject<ModalAlert> modalAlert( |
| + [[ModalAlert alloc] initWithTitle:@"title"]); |
| + |
| + // Test. |
| + EXPECT_FALSE([modalAlert alertCoordinatorShouldAddCancelButton:nil]); |
| +} |
| + |
| +// Tests that the ModalAlert let the Alert start and do not add a button if it |
| +// has at least one button. |
| +TEST(ModalAlertTest, AlertCoordinatorShouldStart) { |
| + // Setup. |
| + base::scoped_nsobject<UIWindow> window( |
| + [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]); |
| + [window makeKeyAndVisible]; |
| + base::scoped_nsobject<UIViewController> viewController( |
| + [[UIViewController alloc] init]); |
| + [window setRootViewController:viewController]; |
| + |
| + id alertCoordinator = [OCMockObject mockForClass:[AlertCoordinator class]]; |
| + [[[alertCoordinator stub] andReturn:viewController] baseViewController]; |
| + [[[alertCoordinator stub] andReturnValue:@(1)] actionsCount]; |
| + |
| + base::scoped_nsobject<ModalAlert> modalAlert( |
| + [[ModalAlert alloc] initWithTitle:@"title"]); |
| + |
| + // Test. |
| + ASSERT_TRUE([modalAlert alertCoordinatorShouldStart:alertCoordinator]); |
|
lpromero
2016/07/18 12:11:36
EXPECT here and below.
|
| +} |
| + |
| +// Tests that the ModalAlert let the Alert start and do not add a button if it |
|
lpromero
2016/07/18 12:11:36
Update the comments. It tests that it adds.
|
| +// has at least one button. |
| +TEST(ModalAlertTest, AlertCoordinatorShouldStartAndAddButton) { |
| + // Setup. |
| + base::scoped_nsobject<UIWindow> window( |
| + [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]); |
| + [window makeKeyAndVisible]; |
| + base::scoped_nsobject<UIViewController> viewController( |
| + [[UIViewController alloc] init]); |
| + [window setRootViewController:viewController]; |
| + |
| + id alertCoordinator = [OCMockObject mockForClass:[AlertCoordinator class]]; |
| + [[[alertCoordinator stub] andReturn:viewController] baseViewController]; |
| + [[[alertCoordinator stub] andReturnValue:@(0)] actionsCount]; |
| + [[alertCoordinator expect] addItemWithTitle:l10n_util::GetNSString(IDS_APP_OK) |
| + action:nil |
| + style:UIAlertActionStyleDefault]; |
| + |
| + base::scoped_nsobject<ModalAlert> modalAlert( |
| + [[ModalAlert alloc] initWithTitle:@"title"]); |
| + |
| + // Action. |
| + BOOL result = [modalAlert alertCoordinatorShouldStart:alertCoordinator]; |
| + |
| + // Test. |
| + ASSERT_TRUE(result); |
| + ASSERT_OCMOCK_VERIFY(alertCoordinator); |
|
lpromero
2016/07/18 12:11:36
Isn't is automatically done? I was under that impr
gambard
2016/07/18 12:40:20
No. You have to call verify (I just tested it).
lpromero
2016/07/18 12:55:31
Acknowledged.
|
| +} |
| + |
| +// Tests that the ModalAlert do not let the Alert start if the window is not |
| +// visible. |
| +TEST(ModalAlertTest, AlertCoordinatorShouldNotStart) { |
| + // Setup. |
| + base::scoped_nsobject<UIWindow> window( |
| + [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]); |
| + base::scoped_nsobject<UIViewController> viewController( |
| + [[UIViewController alloc] init]); |
| + [window setRootViewController:viewController]; |
| + |
| + id alertCoordinator = [OCMockObject mockForClass:[AlertCoordinator class]]; |
| + [[[alertCoordinator stub] andReturn:viewController] baseViewController]; |
| + base::scoped_nsobject<ModalAlert> modalAlert( |
| + [[ModalAlert alloc] initWithTitle:@"title"]); |
| + |
| + // Test. |
| + ASSERT_FALSE([modalAlert alertCoordinatorShouldStart:alertCoordinator]); |
| +} |
| + |
| +// Tests that the ModalAlert let the Alert start and do not add a button if it |
| +// has at least one button. |
|
lpromero
2016/07/18 12:11:36
Update comment?
|
| +TEST(ModalAlertTest, AlertControllerCreation) { |
| + // Setup. |
| + NSString* title = @"Title text"; |
| + NSString* message = @"Message text"; |
| + base::scoped_nsobject<ModalAlert> modalAlert( |
| + [[ModalAlert alloc] initWithTitle:title]); |
| + id alertCoordinator = [OCMockObject mockForClass:[AlertCoordinator class]]; |
| + [[[alertCoordinator stub] andReturn:message] message]; |
| + |
| + // Action. |
| + UIAlertController* alertController = |
| + [modalAlert alertCoordinatorUIAlertController:alertCoordinator]; |
| + |
| + // Test. |
| + ASSERT_EQ(title, alertController.title); |
| + ASSERT_EQ(message, alertController.message); |
| + ASSERT_EQ(UIAlertControllerStyleAlert, alertController.preferredStyle); |
| +} |