-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[quick_actions_ios] Migrate XCTest to Swift Testing #10767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| // found in the LICENSE file. | ||
|
|
||
| import Flutter | ||
| import XCTest | ||
| import Testing | ||
|
|
||
| @testable import quick_actions_ios | ||
|
|
||
|
|
@@ -19,9 +19,10 @@ class MockFlutterApi: IOSQuickActionsFlutterApiProtocol { | |
| } | ||
| } | ||
|
|
||
| class QuickActionsPluginTests: XCTestCase { | ||
| @MainActor | ||
| struct QuickActionsPluginTests { | ||
|
|
||
| func testHandleMethodCall_setShortcutItems() { | ||
| @Test func handleMethodCallSetShortcutItems() { | ||
| let rawItem = ShortcutItemMessage( | ||
| type: "SearchTheThing", | ||
| localizedTitle: "Search the thing", | ||
|
|
@@ -43,10 +44,10 @@ class QuickActionsPluginTests: XCTestCase { | |
| shortcutItemProvider: mockShortcutItemProvider) | ||
|
|
||
| plugin.setShortcutItems(itemsList: [rawItem]) | ||
| XCTAssertEqual(mockShortcutItemProvider.shortcutItems, [item], "Must set shortcut items.") | ||
| #expect(mockShortcutItemProvider.shortcutItems == [item]) | ||
| } | ||
|
|
||
| func testHandleMethodCall_clearShortcutItems() { | ||
| @Test func handleMethodCallClearShortcutItems() { | ||
| let item = UIApplicationShortcutItem( | ||
| type: "SearchTheThing", | ||
| localizedTitle: "Search the thing", | ||
|
|
@@ -65,11 +66,11 @@ class QuickActionsPluginTests: XCTestCase { | |
|
|
||
| plugin.clearShortcutItems() | ||
|
|
||
| XCTAssertEqual(mockShortcutItemProvider.shortcutItems, [], "Must clear shortcut items.") | ||
| #expect(mockShortcutItemProvider.shortcutItems == []) | ||
jmagman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
|
|
||
| func testApplicationPerformActionForShortcutItem() { | ||
| @Test func applicationPerformActionForShortcutItem() async { | ||
| let flutterApi: MockFlutterApi = MockFlutterApi() | ||
| let mockShortcutItemProvider = MockShortcutItemProvider() | ||
|
|
||
|
|
@@ -84,24 +85,24 @@ class QuickActionsPluginTests: XCTestCase { | |
| icon: UIApplicationShortcutIcon(templateImageName: "search_the_thing.png"), | ||
| userInfo: nil) | ||
|
|
||
| let invokeMethodExpectation = expectation(description: "invokeMethod must be called.") | ||
| flutterApi.launchActionCallback = { aString in | ||
| XCTAssertEqual(aString, item.type) | ||
| invokeMethodExpectation.fulfill() | ||
| } | ||
| await confirmation("invokeMethod must be called") { confirmed in | ||
| flutterApi.launchActionCallback = { aString in | ||
| #expect(aString == item.type) | ||
| confirmed() | ||
| } | ||
|
|
||
| let actionResult = plugin.application( | ||
| UIApplication.shared, | ||
| performActionFor: item | ||
| ) { success in | ||
| // noop | ||
| } | ||
| let actionResult = plugin.application( | ||
| UIApplication.shared, | ||
| performActionFor: item | ||
| ) { success in | ||
| // noop | ||
| } | ||
|
|
||
| XCTAssert(actionResult, "performActionForShortcutItem must return true.") | ||
| waitForExpectations(timeout: 1) | ||
| #expect(actionResult, "performActionForShortcutItem must return true.") | ||
| } | ||
| } | ||
|
|
||
| func testApplicationDidFinishLaunchingWithOptions_launchWithShortcut() { | ||
| @Test func applicationDidFinishLaunchingWithOptionsLaunchWithShortcut() { | ||
| let flutterApi: MockFlutterApi = MockFlutterApi() | ||
| let mockShortcutItemProvider = MockShortcutItemProvider() | ||
|
|
||
|
|
@@ -119,11 +120,11 @@ class QuickActionsPluginTests: XCTestCase { | |
| let launchResult = plugin.application( | ||
| UIApplication.shared, | ||
| didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey.shortcutItem: item]) | ||
| XCTAssertFalse( | ||
| launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.") | ||
| #expect( | ||
| !launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.") | ||
| } | ||
|
|
||
| func testApplicationDidFinishLaunchingWithOptions_launchWithoutShortcut() { | ||
| @Test func applicationDidFinishLaunchingWithOptionsLaunchWithoutShortcut() { | ||
| let flutterApi: MockFlutterApi = MockFlutterApi() | ||
| let mockShortcutItemProvider = MockShortcutItemProvider() | ||
|
|
||
|
|
@@ -132,11 +133,11 @@ class QuickActionsPluginTests: XCTestCase { | |
| shortcutItemProvider: mockShortcutItemProvider) | ||
|
|
||
| let launchResult = plugin.application(UIApplication.shared, didFinishLaunchingWithOptions: [:]) | ||
| XCTAssert( | ||
| #expect( | ||
| launchResult, "didFinishLaunchingWithOptions must return true if not launched from shortcut.") | ||
| } | ||
|
|
||
| func testApplicationDidBecomeActive_launchWithoutShortcut() { | ||
| @Test func applicationDidBecomeActiveLaunchWithoutShortcut() { | ||
| let flutterApi: MockFlutterApi = MockFlutterApi() | ||
| let mockShortcutItemProvider = MockShortcutItemProvider() | ||
|
|
||
|
|
@@ -145,13 +146,13 @@ class QuickActionsPluginTests: XCTestCase { | |
| shortcutItemProvider: mockShortcutItemProvider) | ||
|
|
||
| let launchResult = plugin.application(UIApplication.shared, didFinishLaunchingWithOptions: [:]) | ||
| XCTAssert( | ||
| #expect( | ||
| launchResult, "didFinishLaunchingWithOptions must return true if not launched from shortcut.") | ||
|
|
||
| plugin.applicationDidBecomeActive(UIApplication.shared) | ||
| } | ||
|
|
||
| func testApplicationDidBecomeActive_launchWithShortcut() { | ||
| @Test func applicationDidBecomeActiveLaunchWithShortcut() async { | ||
| let item = UIApplicationShortcutItem( | ||
| type: "SearchTheThing", | ||
| localizedTitle: "Search the thing", | ||
|
|
@@ -166,24 +167,24 @@ class QuickActionsPluginTests: XCTestCase { | |
| flutterApi: flutterApi, | ||
| shortcutItemProvider: mockShortcutItemProvider) | ||
|
|
||
| let invokeMethodExpectation = expectation(description: "invokeMethod must be called.") | ||
| flutterApi.launchActionCallback = { aString in | ||
| XCTAssertEqual(aString, item.type) | ||
| invokeMethodExpectation.fulfill() | ||
| } | ||
| await confirmation("invokeMethod must be called") { confirmed in | ||
| flutterApi.launchActionCallback = { aString in | ||
| #expect(aString == item.type) | ||
| confirmed() | ||
| } | ||
|
|
||
| let launchResult = plugin.application( | ||
| UIApplication.shared, | ||
| didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey.shortcutItem: item]) | ||
| let launchResult = plugin.application( | ||
| UIApplication.shared, | ||
| didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey.shortcutItem: item]) | ||
|
|
||
| XCTAssertFalse( | ||
| launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.") | ||
| #expect( | ||
| !launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.") | ||
|
|
||
| plugin.applicationDidBecomeActive(UIApplication.shared) | ||
| waitForExpectations(timeout: 1) | ||
| plugin.applicationDidBecomeActive(UIApplication.shared) | ||
| } | ||
| } | ||
|
|
||
| func testApplicationDidBecomeActive_launchWithShortcut_becomeActiveTwice() { | ||
| @Test func applicationDidBecomeActiveLaunchWithShortcutBecomeActiveTwice() async { | ||
| let item = UIApplicationShortcutItem( | ||
| type: "SearchTheThing", | ||
| localizedTitle: "Search the thing", | ||
|
|
@@ -198,25 +199,26 @@ class QuickActionsPluginTests: XCTestCase { | |
| flutterApi: flutterApi, | ||
| shortcutItemProvider: mockShortcutItemProvider) | ||
|
|
||
| let invokeMethodExpectation = expectation(description: "invokeMethod must be called.") | ||
|
|
||
| var invokeMethodCount = 0 | ||
| flutterApi.launchActionCallback = { aString in | ||
| XCTAssertEqual(aString, item.type) | ||
| invokeMethodCount += 1 | ||
| invokeMethodExpectation.fulfill() | ||
| await confirmation("invokeMethod must be called") { confirmed in | ||
| flutterApi.launchActionCallback = { aString in | ||
| #expect(aString == item.type) | ||
| invokeMethodCount += 1 | ||
| if invokeMethodCount == 1 { | ||
| confirmed() | ||
| } | ||
| } | ||
|
|
||
| let launchResult = plugin.application( | ||
| UIApplication.shared, | ||
| didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey.shortcutItem: item]) | ||
|
|
||
| #expect( | ||
| !launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.") | ||
|
|
||
| plugin.applicationDidBecomeActive(UIApplication.shared) | ||
| } | ||
|
|
||
| let launchResult = plugin.application( | ||
| UIApplication.shared, | ||
| didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey.shortcutItem: item]) | ||
|
|
||
| XCTAssertFalse( | ||
| launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.") | ||
|
|
||
| plugin.applicationDidBecomeActive(UIApplication.shared) | ||
| waitForExpectations(timeout: 1) | ||
|
|
||
| XCTAssertEqual(invokeMethodCount, 1, "shortcut should only be handled once per launch.") | ||
| #expect(invokeMethodCount == 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test's name implies that // Call a second time to ensure the shortcut is not handled again.
plugin.applicationDidBecomeActive(UIApplication.shared)
#expect(invokeMethodCount == 1, "shortcut should only be handled once per launch.") |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.