Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Prev Previous commit
Next Next commit
run swift-format
  • Loading branch information
hellohuanlin committed Nov 15, 2022
commit 3bff5245650b7389101c7c5a8babc16d30ba2cb8
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

import Flutter
import XCTest

@testable import quick_actions_ios

class ShortcutStateManagerTests: XCTestCase {

func testSetShortcutItems_shouldSetItem() {
let rawItem = [
"type" : "SearchTheThing",
"localizedTitle" : "Search the thing",
"icon" : "search_the_thing.png",
"type": "SearchTheThing",
"localizedTitle": "Search the thing",
"icon": "search_the_thing.png",
]

let expectedItem = UIApplicationShortcutItem(
Expand All @@ -33,9 +34,9 @@ class ShortcutStateManagerTests: XCTestCase {

func testSetShortcutItems_shouldSetItemWithoutIcon() {
let rawItem: [String: Any] = [
"type" : "SearchTheThing",
"localizedTitle" : "Search the thing",
"icon" : NSNull(),
"type": "SearchTheThing",
"localizedTitle": "Search the thing",
"icon": NSNull(),
]

let expectedItem = UIApplicationShortcutItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import Foundation

@testable import quick_actions_ios

final class MockMethodChannel: MethodChannel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
final class MockShortcutStateManager: ShortcutStateManaging {
var setShortcutItemsStub: (([[String: Any]]) -> Void)?

func setShortcutItems(_ items: [[String : Any]]) {
func setShortcutItems(_ items: [[String: Any]]) {
setShortcutItemsStub?(items)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@

import Flutter
import XCTest

@testable import quick_actions_ios

class QuickActionsPluginTests: XCTestCase {

func testHandleMethodCall_setShortcutItems() {
let rawItem = [
"type" : "SearchTheThing",
"localizedTitle" : "Search the thing",
"icon" : "search_the_thing.png",
"type": "SearchTheThing",
"localizedTitle": "Search the thing",
"icon": "search_the_thing.png",
]
let call = FlutterMethodCall(methodName: "setShortcutItems", arguments: [rawItem])

let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let setShortcutItemsExpectation = expectation(description: "setShortcutItems must be called.")
mockShortcutStateManager.setShortcutItemsStub = { items in
XCTAssertEqual(items as? [[String:String]], [rawItem])
XCTAssertEqual(items as? [[String: String]], [rawItem])
setShortcutItemsExpectation.fulfill()
}

Expand All @@ -39,7 +41,8 @@ class QuickActionsPluginTests: XCTestCase {
let call = FlutterMethodCall(methodName: "clearShortcutItems", arguments: nil)
let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let setShortcutItemsExpectation = expectation(description: "setShortcutItems must be called.")
mockShortcutStateManager.setShortcutItemsStub = { items in
Expand All @@ -61,7 +64,8 @@ class QuickActionsPluginTests: XCTestCase {

let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let resultExpectation = expectation(description: "result block must be called.")
plugin.handle(call) { result in
Expand All @@ -77,12 +81,15 @@ class QuickActionsPluginTests: XCTestCase {

let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let resultExpectation = expectation(description: "result block must be called.")

plugin.handle(call) { result in
XCTAssertEqual(result as? NSObject, FlutterMethodNotImplemented, "result block must be called with FlutterMethodNotImplemented")
XCTAssertEqual(
result as? NSObject, FlutterMethodNotImplemented,
"result block must be called with FlutterMethodNotImplemented")
resultExpectation.fulfill()
}

Expand All @@ -92,7 +99,8 @@ class QuickActionsPluginTests: XCTestCase {
func testApplicationPerformActionForShortcutItem() {
let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let item = UIApplicationShortcutItem(
type: "SearchTheThing",
Expand All @@ -110,7 +118,8 @@ class QuickActionsPluginTests: XCTestCase {

let actionResult = plugin.application(
UIApplication.shared,
performActionFor: item) { success in /* no-op */}
performActionFor: item
) { success in /* no-op */ }

XCTAssert(actionResult, "performActionForShortcutItem must return true.")

Expand All @@ -120,7 +129,8 @@ class QuickActionsPluginTests: XCTestCase {
func testApplicationDidFinishLaunchingWithOptions_launchWithShortcut() {
let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let item = UIApplicationShortcutItem(
type: "SearchTheThing",
Expand All @@ -129,32 +139,39 @@ class QuickActionsPluginTests: XCTestCase {
icon: UIApplicationShortcutIcon(templateImageName: "search_the_thing.png"),
userInfo: nil)

let launchResult = plugin.application(UIApplication.shared, didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey.shortcutItem: item])
XCTAssertFalse(launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.")
let launchResult = plugin.application(
UIApplication.shared,
didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey.shortcutItem: item])
XCTAssertFalse(
launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.")

}

func testApplicationDidFinishLaunchingWithOptions_launchWithoutShortcut() {
let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let launchResult = plugin.application(UIApplication.shared, didFinishLaunchingWithOptions: [:])
XCTAssert(launchResult, "didFinishLaunchingWithOptions must return true if not launched from shortcut.")
XCTAssert(
launchResult, "didFinishLaunchingWithOptions must return true if not launched from shortcut.")

}

func testApplicationDidBecomeActive_launchWithoutShortcut() {
let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

mockChannel.invokeMethodStub = { _, _ in
XCTFail("invokeMethod should not be called if launch without shortcut.")
}

let launchResult = plugin.application(UIApplication.shared, didFinishLaunchingWithOptions: [:])
XCTAssert(launchResult, "didFinishLaunchingWithOptions must return true if not launched from shortcut.")
XCTAssert(
launchResult, "didFinishLaunchingWithOptions must return true if not launched from shortcut.")

plugin.applicationDidBecomeActive(UIApplication.shared)

Expand All @@ -170,7 +187,8 @@ class QuickActionsPluginTests: XCTestCase {

let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let invokeMethodExpectation = expectation(description: "invokeMethod must be called.")
mockChannel.invokeMethodStub = { method, arguments in
Expand All @@ -179,9 +197,12 @@ class QuickActionsPluginTests: XCTestCase {
invokeMethodExpectation.fulfill()
}

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.")
XCTAssertFalse(
launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.")

plugin.applicationDidBecomeActive(UIApplication.shared)
waitForExpectations(timeout: 1)
Expand All @@ -197,7 +218,8 @@ class QuickActionsPluginTests: XCTestCase {

let mockChannel = MockMethodChannel()
let mockShortcutStateManager = MockShortcutStateManager()
let plugin = QuickActionsPlugin(channel: mockChannel, shortcutStateManager: mockShortcutStateManager)
let plugin = QuickActionsPlugin(
channel: mockChannel, shortcutStateManager: mockShortcutStateManager)

let invokeMethodExpectation = expectation(description: "invokeMethod must be called.")

Expand All @@ -207,9 +229,12 @@ class QuickActionsPluginTests: XCTestCase {
invokeMethodExpectation.fulfill()
}

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.")
XCTAssertFalse(
launchResult, "didFinishLaunchingWithOptions must return false if launched from shortcut.")

plugin.applicationDidBecomeActive(UIApplication.shared)
waitForExpectations(timeout: 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ protocol ShortcutItemServicing: AnyObject {
var shortcutItems: [UIApplicationShortcutItem]? { get set }
}

/// A default implementation of the `ShortcutItemServicing` protocol.
/// A default implementation of the `ShortcutItemServicing` protocol.
extension UIApplication: ShortcutItemServicing {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protocol ShortcutStateManaging {
/// Sets the list of shortcut items.
///
/// - Parameter items the list of shortcut items to be parsed and set.
func setShortcutItems(_ items: [[String:Any]])
func setShortcutItems(_ items: [[String: Any]])
}

/// A default implementation of `ShortcutStateManaging` protocol.
Expand All @@ -24,11 +24,12 @@ final class DefaultShortcutStateManager: ShortcutStateManaging {
self.service = service
}

func setShortcutItems(_ items: [[String : Any]]) {
service.shortcutItems = items.compactMap { deserializeShortcutItem(with:$0) }
func setShortcutItems(_ items: [[String: Any]]) {
service.shortcutItems = items.compactMap { deserializeShortcutItem(with: $0) }
}

private func deserializeShortcutItem(with serialized: [String: Any]) -> UIApplicationShortcutItem {
private func deserializeShortcutItem(with serialized: [String: Any]) -> UIApplicationShortcutItem
{

let icon = (serialized["icon"] as? String).map {
UIApplicationShortcutIcon(templateImageName: $0)
Expand Down