File: WebKitCommunicationBridgeTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (95 lines) | stat: -rw-r--r-- 4,429 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2021 Apple Inc. and the Swift project authors
 Licensed under Apache License v2.0 with Runtime Library Exception

 See https://swift.org/LICENSE.txt for license information
 See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Foundation
#if canImport(WebKit)
import WebKit
#endif
import XCTest
@testable import SwiftDocC

class WebKitCommunicationBridgeTests: XCTestCase {
    func testMessagesCanBeSent() throws {
        #if canImport(WebKit)
        try assertMessageIsSent(message: .rendered())
        try assertMessageIsSent(message: .codeColors(CodeColors.testValue))
        #endif
    }
    
    func testMessagesCanBeReceived() throws {
        #if canImport(WebKit)
        let message = Message.codeColors(CodeColors.testValue)
        
        let userContentController = WKUserContentController()
        let expectation = XCTestExpectation(description: "Message has been received.")
        let bridge = WebKitCommunicationBridge(with: userContentController) { _message in
            XCTAssertEqual(message.type.rawValue, _message.type.rawValue)
            XCTAssertEqual(message.identifier, _message.identifier)
            XCTAssertEqual(message.data?.value as! CodeColors, _message.data?.value as! CodeColors)
            expectation.fulfill()
        }
        
        let json = try JSONSerialization.jsonObject(with: try! JSONEncoder().encode(message))
        bridge.onReceiveMessageData(messageBody: json)
        
        wait(for: [expectation], timeout: 5.0)
        #endif
    }
    
    func assertMessageIsSent(message: Message) throws {
        #if canImport(WebKit)
        let encodedMessage = try JSONEncoder().encode(message)
        let json = try XCTUnwrap(JSONSerialization.jsonObject(with: encodedMessage) as? NSDictionary)
        
        let didEvaluateJavaScript = expectation(description: "Did evaluate JavaScript")
        let evaluateJavaScript: (String, ((Any?, Error?) -> ())?) -> () = { string, _ in
            defer { didEvaluateJavaScript.fulfill() }
            
            XCTAssert(string.hasPrefix("window.bridge.receive("))
            XCTAssert(string.hasSuffix(")"))
            
            let jsonString = String(string.dropFirst("window.bridge.receive(".count).dropLast())
            guard let jsonData = jsonString.data(using: .utf8),
                  let decodedMessage = try? JSONSerialization.jsonObject(with: jsonData) as? NSDictionary
            else {
                XCTFail("Unable to decode \(jsonString) as communication bridge Message")
                return
            }
            
            XCTAssertEqual(json, decodedMessage)
        }
        
        let bridge = WebKitCommunicationBridge()
        XCTAssertNoThrow(try bridge.send(message, using: evaluateJavaScript))
        waitForExpectations(timeout: 1.0)
        #endif
    }
}

private extension CodeColors {
    static var testValue: CodeColors {
        return .init(
            background: SRGBColor(red: UInt8(1), green: UInt8(2), blue: UInt8(3), alpha: 0.1),
            text: SRGBColor(red: UInt8(4), green: UInt8(5), blue: UInt8(6), alpha: 0.2),
            keyword: SRGBColor(red: UInt8(7), green: UInt8(8), blue: UInt8(9), alpha: 0.3),
            identifier: SRGBColor(red: UInt8(10), green: UInt8(11), blue: UInt8(12), alpha: 0.4),
            parameterName: SRGBColor(red: UInt8(13), green: UInt8(14), blue: UInt8(15), alpha: 0.5),
            numberLiteral: SRGBColor(red: UInt8(16), green: UInt8(17), blue: UInt8(18), alpha: 0.6),
            stringLiteral: SRGBColor(red: UInt8(19), green: UInt8(20), blue: UInt8(21), alpha: 0.7),
            typeAnnotation: SRGBColor(red: UInt8(22), green: UInt8(23), blue: UInt8(24), alpha: 0.8),
            docComment: SRGBColor(red: UInt8(25), green: UInt8(26), blue: UInt8(27), alpha: 0.9),
            docCommentField: SRGBColor(red: UInt8(28), green: UInt8(29), blue: UInt8(30), alpha: 0.91),
            comment: SRGBColor(red: UInt8(31), green: UInt8(32), blue: UInt8(33), alpha: 0.92),
            commentURL: SRGBColor(red: UInt8(34), green: UInt8(35), blue: UInt8(36), alpha: 0.93),
            buildConfigKeyword: SRGBColor(red: UInt8(37), green: UInt8(38), blue: UInt8(39), alpha: 0.94),
            buildConfigId: SRGBColor(red: UInt8(40), green: UInt8(41), blue: UInt8(42), alpha: 0.95)
        )
    }
}