summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPNTests/MullvadVPN/View controllers/SelectLocation/LocationNodeTests.swift
blob: 49aa271b7ac04d3555e818d11acf8cc8ba7bc8d2 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
//
//  LocationNodeTests.swift
//  MullvadVPNTests
//
//  Created by Jon Petersson on 2024-02-28.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import MullvadSettings
import XCTest

class LocationNodeTests: XCTestCase {
    let listNode = CustomListLocationNode(
        name: "List",
        code: "list",
        showsChildren: false,
        customList: CustomList(name: "List", locations: [])
    )
    let countryNode = LocationNode(name: "Country", code: "country", showsChildren: false)
    let cityNode = LocationNode(name: "City", code: "city", showsChildren: false)
    let hostNode = LocationNode(name: "Host", code: "host", showsChildren: false)

    override func setUp() async throws {
        createNodeTree()
    }

    func testNodeTree() throws {
        XCTAssertEqual(listNode.children.first, countryNode)
        XCTAssertEqual(countryNode.children.first, cityNode)
        XCTAssertEqual(cityNode.children.first, hostNode)
        XCTAssertNil(hostNode.children.first)
    }

    func testTopmostAncestor() throws {
        XCTAssertEqual(hostNode.root, listNode)
    }

    func testAnscestors() throws {
        hostNode.forEachAncestor { node in
            node.showsChildren = true
        }

        XCTAssertTrue(listNode.showsChildren)
        XCTAssertTrue(countryNode.showsChildren)
        XCTAssertTrue(cityNode.showsChildren)
        XCTAssertFalse(hostNode.showsChildren)
    }

    func testDescendants() throws {
        listNode.forEachDescendant { node in
            node.showsChildren = true
        }

        XCTAssertFalse(listNode.showsChildren)
        XCTAssertTrue(countryNode.showsChildren)
        XCTAssertTrue(cityNode.showsChildren)
        XCTAssertTrue(hostNode.showsChildren)
    }

    func testCopyNode() throws {
        let hostNodeCopy = hostNode.copy()

        XCTAssertTrue(hostNode == hostNodeCopy)
        XCTAssertFalse(hostNode === hostNodeCopy)

        var numberOfDescendants = 0
        hostNode.forEachDescendant { _ in
            numberOfDescendants += 1
        }

        var numberOfCopyDescendants = 0
        hostNodeCopy.forEachDescendant { _ in
            numberOfCopyDescendants += 1
        }

        XCTAssertEqual(numberOfDescendants, numberOfCopyDescendants)
    }

    func testFindByCountryCode() {
        XCTAssertTrue(listNode.countryFor(code: countryNode.code) == countryNode)
    }

    func testFindByCityCode() {
        XCTAssertTrue(countryNode.cityFor(codes: [cityNode.code]) == cityNode)
    }

    func testFindByHostCode() {
        XCTAssertTrue(cityNode.hostFor(code: hostNode.code) == hostNode)
    }

    func testFindDescendantByNodeCode() {
        XCTAssertTrue(listNode.descendantNodeFor(codes: [hostNode.code]) == hostNode)
    }
}

extension LocationNodeTests {
    private func createNodeTree() {
        hostNode.parent = cityNode
        cityNode.children.append(hostNode)

        cityNode.parent = countryNode
        countryNode.children.append(cityNode)

        countryNode.parent = listNode
        listNode.children.append(countryNode)
    }
}