Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
do not crash if no type or title
Browse files Browse the repository at this point in the history
  • Loading branch information
hellohuanlin committed Nov 14, 2022
1 parent cc00bd7 commit fd9968e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest

class DefaultShortcutItemParserTests: XCTestCase {

func testparseShortcutItems() {
func testParseShortcutItems() {
let rawItem = [
"type": "SearchTheThing",
"localizedTitle": "Search the thing",
Expand All @@ -27,7 +27,7 @@ class DefaultShortcutItemParserTests: XCTestCase {
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [expectedItem])
}

func testparseShortcutItems_noIcon() {
func testParseShortcutItems_noIcon() {
let rawItem: [String: Any] = [
"type": "SearchTheThing",
"localizedTitle": "Search the thing",
Expand All @@ -44,4 +44,24 @@ class DefaultShortcutItemParserTests: XCTestCase {
let parser = DefaultShortcutItemParser()
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [expectedItem])
}

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

let parser = DefaultShortcutItemParser()
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [])
}

func testParseShortcutItems_noLocalizedTitle() {
let rawItem = [
"type": "SearchTheThing",
"icon": "search_the_thing.png",
]

let parser = DefaultShortcutItemParser()
XCTAssertEqual(parser.parseShortcutItems([rawItem]), [])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ final class DefaultShortcutItemParser: ShortcutItemParser {
return items.compactMap { deserializeShortcutItem(with: $0) }
}

private func deserializeShortcutItem(with serialized: [String: Any]) -> UIApplicationShortcutItem
private func deserializeShortcutItem(with serialized: [String: Any]) -> UIApplicationShortcutItem?
{
guard
let type = serialized["type"] as? String,
let localizedTitle = serialized["localizedTitle"] as? String
else {
return nil
}

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

// type and localizedTitle are required.
return UIApplicationShortcutItem(
type: serialized["type"] as! String,
localizedTitle: serialized["localizedTitle"] as! String,
type: type,
localizedTitle: localizedTitle,
localizedSubtitle: nil,
icon: icon,
userInfo: nil)
Expand Down

0 comments on commit fd9968e

Please sign in to comment.