|
| 1 | +// Copyright 2013 The Flutter 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 Flutter; |
| 6 | +@import quick_actions_ios; |
| 7 | +@import quick_actions_ios.Test; |
| 8 | +@import XCTest; |
| 9 | +#import <OCMock/OCMock.h> |
| 10 | + |
| 11 | +@interface FLTQuickActionsPluginTests : XCTestCase |
| 12 | + |
| 13 | +@end |
| 14 | + |
| 15 | +@implementation FLTQuickActionsPluginTests |
| 16 | + |
| 17 | +// A dummy `UIApplicationShortcutItem`. |
| 18 | +- (UIApplicationShortcutItem *)searchTheThingShortcutItem { |
| 19 | + return [[UIApplicationShortcutItem alloc] |
| 20 | + initWithType:@"SearchTheThing" |
| 21 | + localizedTitle:@"Search the thing" |
| 22 | + localizedSubtitle:nil |
| 23 | + icon:[UIApplicationShortcutIcon |
| 24 | + iconWithTemplateImageName:@"search_the_thing.png"] |
| 25 | + userInfo:nil]; |
| 26 | +} |
| 27 | + |
| 28 | +// A dummy raw shortcut item. |
| 29 | +- (NSDictionary<NSString *, NSString *> *)searchTheThingRawItem { |
| 30 | + return @{ |
| 31 | + @"type" : @"SearchTheThing", |
| 32 | + @"localizedTitle" : @"Search the thing", |
| 33 | + @"icon" : @"search_the_thing.png", |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +- (void)testHandleMethodCall_setShortcutItems { |
| 38 | + FlutterMethodCall *call = |
| 39 | + [FlutterMethodCall methodCallWithMethodName:@"setShortcutItems" |
| 40 | + arguments:@[ [self searchTheThingRawItem] ]]; |
| 41 | + |
| 42 | + FLTQuickActionsPlugin *plugin = |
| 43 | + [[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])]; |
| 44 | + XCTestExpectation *resultExpectation = |
| 45 | + [self expectationWithDescription:@"result block must be called."]; |
| 46 | + [plugin handleMethodCall:call |
| 47 | + result:^(id _Nullable result) { |
| 48 | + XCTAssertNil(result, @"result block must be called with nil."); |
| 49 | + XCTAssertEqualObjects(UIApplication.sharedApplication.shortcutItems, |
| 50 | + @[ [self searchTheThingShortcutItem] ], |
| 51 | + @"shortcut items should be set correctly."); |
| 52 | + [resultExpectation fulfill]; |
| 53 | + }]; |
| 54 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 55 | +} |
| 56 | + |
| 57 | +- (void)testHandleMethodCall_clearShortcutItems { |
| 58 | + FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"clearShortcutItems" |
| 59 | + arguments:nil]; |
| 60 | + |
| 61 | + FLTQuickActionsPlugin *plugin = |
| 62 | + [[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])]; |
| 63 | + XCTestExpectation *resultExpectation = |
| 64 | + [self expectationWithDescription:@"result block must be called."]; |
| 65 | + [plugin handleMethodCall:call |
| 66 | + result:^(id _Nullable result) { |
| 67 | + XCTAssertNil(result, @"result block must be called with nil."); |
| 68 | + XCTAssertEqual(UIApplication.sharedApplication.shortcutItems.count, 0, |
| 69 | + @"shortcut items should be cleared"); |
| 70 | + [resultExpectation fulfill]; |
| 71 | + }]; |
| 72 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 73 | +} |
| 74 | + |
| 75 | +- (void)testHandleMethodCall_getLaunchAction { |
| 76 | + FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"getLaunchAction" |
| 77 | + arguments:nil]; |
| 78 | + |
| 79 | + FLTQuickActionsPlugin *plugin = |
| 80 | + [[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])]; |
| 81 | + XCTestExpectation *resultExpectation = |
| 82 | + [self expectationWithDescription:@"result block must be called."]; |
| 83 | + [plugin handleMethodCall:call |
| 84 | + result:^(id _Nullable result) { |
| 85 | + XCTAssertNil(result, @"result block must be called with nil."); |
| 86 | + [resultExpectation fulfill]; |
| 87 | + }]; |
| 88 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 89 | +} |
| 90 | + |
| 91 | +- (void)testHandleMethodCall_nonExistMethods { |
| 92 | + FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"nonExist" arguments:nil]; |
| 93 | + |
| 94 | + FLTQuickActionsPlugin *plugin = |
| 95 | + [[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])]; |
| 96 | + XCTestExpectation *resultExpectation = |
| 97 | + [self expectationWithDescription:@"result must be called."]; |
| 98 | + [plugin |
| 99 | + handleMethodCall:call |
| 100 | + result:^(id _Nullable result) { |
| 101 | + XCTAssertEqual(result, FlutterMethodNotImplemented, |
| 102 | + @"result block must be called with FlutterMethodNotImplemented"); |
| 103 | + [resultExpectation fulfill]; |
| 104 | + }]; |
| 105 | + |
| 106 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 107 | +} |
| 108 | + |
| 109 | +- (void)testApplicationPerformActionForShortcutItem { |
| 110 | + id mockChannel = OCMClassMock([FlutterMethodChannel class]); |
| 111 | + FLTQuickActionsPlugin *plugin = [[FLTQuickActionsPlugin alloc] initWithChannel:mockChannel]; |
| 112 | + |
| 113 | + UIApplicationShortcutItem *item = [self searchTheThingShortcutItem]; |
| 114 | + |
| 115 | + BOOL actionResult = [plugin application:[UIApplication sharedApplication] |
| 116 | + performActionForShortcutItem:item |
| 117 | + completionHandler:^(BOOL succeeded){/* no-op */}]; |
| 118 | + XCTAssert(actionResult, @"performActionForShortcutItem must return true."); |
| 119 | + OCMVerify([mockChannel invokeMethod:@"launch" arguments:item.type]); |
| 120 | +} |
| 121 | + |
| 122 | +- (void)testApplicationDidFinishLaunchingWithOptions_launchWithShortcut { |
| 123 | + FLTQuickActionsPlugin *plugin = |
| 124 | + [[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])]; |
| 125 | + |
| 126 | + UIApplicationShortcutItem *item = [self searchTheThingShortcutItem]; |
| 127 | + |
| 128 | + BOOL launchResult = [plugin application:[UIApplication sharedApplication] |
| 129 | + didFinishLaunchingWithOptions:@{UIApplicationLaunchOptionsShortcutItemKey : item}]; |
| 130 | + |
| 131 | + XCTAssertFalse(launchResult, |
| 132 | + @"didFinishLaunchingWithOptions must return false if launched from shortcut."); |
| 133 | +} |
| 134 | + |
| 135 | +- (void)testApplicationDidFinishLaunchingWithOptions_launchWithoutShortcut { |
| 136 | + FLTQuickActionsPlugin *plugin = |
| 137 | + [[FLTQuickActionsPlugin alloc] initWithChannel:OCMClassMock([FlutterMethodChannel class])]; |
| 138 | + BOOL launchResult = [plugin application:[UIApplication sharedApplication] |
| 139 | + didFinishLaunchingWithOptions:@{}]; |
| 140 | + XCTAssertTrue(launchResult, |
| 141 | + @"didFinishLaunchingWithOptions must return true if not launched from shortcut."); |
| 142 | +} |
| 143 | + |
| 144 | +- (void)testApplicationDidBecomeActive { |
| 145 | + UIApplicationShortcutItem *item = [self searchTheThingShortcutItem]; |
| 146 | + id mockChannel = OCMClassMock([FlutterMethodChannel class]); |
| 147 | + FLTQuickActionsPlugin *plugin = [[FLTQuickActionsPlugin alloc] initWithChannel:mockChannel]; |
| 148 | + plugin.launchingShortcutType = item.type; |
| 149 | + [plugin applicationDidBecomeActive:[UIApplication sharedApplication]]; |
| 150 | + |
| 151 | + OCMVerify([mockChannel invokeMethod:@"launch" arguments:item.type]); |
| 152 | + XCTAssertNil(plugin.launchingShortcutType, |
| 153 | + @"Must reset launchingShortcutType to nil after being used."); |
| 154 | +} |
| 155 | + |
| 156 | +@end |
0 commit comments