summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPNUITests/Pages/DNSSettingsPage.swift
blob: 115fafa140a7e8401d1aa5453f1b822826a25cf2 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
//  DNSSettingsPage.swift
//  MullvadVPNUITests
//
//  Created by Niklas Berglund on 2024-03-19.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import XCTest

class DNSSettingsPage: Page {
    @discardableResult override init(_ app: XCUIApplication) {
        super.init(app)

        self.pageElement = app.tables[.dnsSettingsTableView]
        waitForPageToBeShown()
    }

    private func assertSwitchOn(accessibilityIdentifier: AccessibilityIdentifier) -> Self {
        let switchElement = app.cells[accessibilityIdentifier]
            .switches[AccessibilityIdentifier.customSwitch]

        guard let switchValue = switchElement.value as? String else {
            XCTFail("Failed to read switch state")
            return self
        }

        XCTAssertEqual(switchValue, "1")

        return self
    }

    @discardableResult func tapBackButton() -> Self {
        // Workaround for setting accessibility identifier on navigation bar button being non-trivial
        app.buttons.matching(identifier: "VPN settings").allElementsBoundByIndex.last?.tap()
        return self
    }

    @discardableResult func tapDNSContentBlockersHeaderExpandButton() -> Self {
        let headerView = app.otherElements[AccessibilityIdentifier.dnsContentBlockersHeaderView]
        let expandButton = headerView.buttons[AccessibilityIdentifier.expandButton]
        expandButton.tap()

        return self
    }

    @discardableResult func tapUseCustomDNSSwitch() -> Self {
        app.cells[AccessibilityIdentifier.dnsSettingsUseCustomDNSCell]
            .switches[AccessibilityIdentifier.customSwitch]
            .tap()

        return self
    }

    @discardableResult func tapBlockAdsSwitch() -> Self {
        app.cells[AccessibilityIdentifier.blockAdvertising]
            .switches[AccessibilityIdentifier.customSwitch]
            .tap()

        return self
    }

    @discardableResult func tapBlockAdsSwitchIfOn() -> Self {
        let blockAdsSwitch = app.cells[AccessibilityIdentifier.blockAdvertising]
            .switches[AccessibilityIdentifier.customSwitch]

        if blockAdsSwitch.value as? String == "1" {
            tapBlockAdsSwitch()
        }
        return self
    }

    @discardableResult func tapBlockTrackerSwitch() -> Self {
        app.cells[AccessibilityIdentifier.blockTracking]
            .switches[AccessibilityIdentifier.customSwitch]
            .tap()

        return self
    }

    @discardableResult func tapBlockMalwareSwitch() -> Self {
        app.cells[AccessibilityIdentifier.blockMalware]
            .switches[AccessibilityIdentifier.customSwitch]
            .tap()

        return self
    }

    @discardableResult func tapBlockAdultContentSwitch() -> Self {
        app.cells[AccessibilityIdentifier.blockAdultContent]
            .switches[AccessibilityIdentifier.customSwitch]
            .tap()

        return self
    }

    @discardableResult func tapBlockGamblingSwitch() -> Self {
        app.cells[AccessibilityIdentifier.blockGambling]
            .switches[AccessibilityIdentifier.customSwitch]
            .tap()

        return self
    }

    @discardableResult func tapBlockSocialMediaSwitch() -> Self {
        app.cells[AccessibilityIdentifier.blockSocialMedia]
            .switches[AccessibilityIdentifier.customSwitch]
            .tap()

        return self
    }

    @discardableResult func tapEditButton() -> Self {
        app.buttons[AccessibilityIdentifier.dnsSettingsEditButton]
            .tap()
        return self
    }

    @discardableResult func tapDoneButton() -> Self {
        return self.tapEditButton()
    }

    @discardableResult func tapAddAServer() -> Self {
        app.cells[AccessibilityIdentifier.dnsSettingsAddServerCell]
            .tap()
        return self
    }

    @discardableResult func tapEnterIPAddressTextField() -> Self {
        app.textFields[AccessibilityIdentifier.dnsSettingsEnterIPAddressTextField]
            .tap()
        return self
    }

    @discardableResult func verifyBlockAdsSwitchOn() -> Self {
        return assertSwitchOn(accessibilityIdentifier: .blockAdvertising)
    }

    @discardableResult func verifyBlockTrackerSwitchOn() -> Self {
        return assertSwitchOn(accessibilityIdentifier: .blockTracking)
    }

    @discardableResult func verifyBlockMalwareSwitchOn() -> Self {
        return assertSwitchOn(accessibilityIdentifier: .blockMalware)
    }

    @discardableResult func verifyBlockAdultContentSwitchOn() -> Self {
        return assertSwitchOn(accessibilityIdentifier: .blockAdultContent)
    }

    @discardableResult func verifyBlockGamblingSwitchOn() -> Self {
        return assertSwitchOn(accessibilityIdentifier: .blockGambling)
    }

    @discardableResult func verifyBlockSocialMediaSwitchOn() -> Self {
        return assertSwitchOn(accessibilityIdentifier: .blockSocialMedia)
    }

    @discardableResult func verifyUseCustomDNSSwitchOn() -> Self {
        return assertSwitchOn(accessibilityIdentifier: .dnsSettingsUseCustomDNSCell)
    }

    /// Verify that the UI shows stored DNS server IP address same as `ipAddress`. Note that this function assumes there is only one custom DNS server IP address stored.
    @discardableResult func verifyCustomDNSIPAddress(_ ipAddress: String) -> Self {
        let textField = app.textFields[AccessibilityIdentifier.dnsSettingsEnterIPAddressTextField]

        guard let settingIPAddress = textField.value as? String else {
            XCTFail("Failed to read configured DNS IP address")
            return self
        }

        XCTAssertEqual(ipAddress, settingIPAddress)
        return self
    }
}