This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFireWindowTests.swift
348 lines (296 loc) · 14.9 KB
/
FireWindowTests.swift
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// FireWindowTests.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
class FireWindowTests: XCTestCase {
private var app: XCUIApplication!
private var settingsGeneralButton: XCUIElement!
private var reopenAllWindowsFromLastSessionPreference: XCUIElement!
override class func setUp() {
UITests.firstRun()
}
override func setUpWithError() throws {
continueAfterFailure = false
app = XCUIApplication()
app.launchEnvironment["UITEST_MODE"] = "1"
settingsGeneralButton = app.buttons["PreferencesSidebar.generalButton"]
reopenAllWindowsFromLastSessionPreference = app.radioButtons["PreferencesGeneralView.stateRestorePicker.reopenAllWindowsFromLastSession"]
app.launch()
app.typeKey("w", modifierFlags: [.command, .option, .shift]) // Let's enforce a single window
}
func testFireWindowDoesNotStoreHistory() {
openFireWindow()
openSite(pageTitle: "Some site")
openNormalWindow()
assertSiteIsNotShowingInNormalWindowHistory()
}
func testFireWindowStateIsNotSavedAfterRestart() {
openNormalWindow()
app.typeKey(",", modifierFlags: [.command]) // Open settings
settingsGeneralButton.click(forDuration: 0.5, thenDragTo: settingsGeneralButton)
reopenAllWindowsFromLastSessionPreference.clickAfterExistenceTestSucceeds()
openThreeSitesOnNormalWindow()
openFireWindow()
openThreeSitesOnFireWindow()
app.terminate()
app.launch()
assertSitesOpenedInNormalWindowAreRestored()
assertSitesOpenedOnFireWindowAreNotRestored()
}
func testFireWindowDoNotShowPinnedTabs() {
openNormalWindow()
openSite(pageTitle: "Page #1")
app.menuItems["Pin Tab"].tap()
app.openNewTab()
openSite(pageTitle: "Page #2")
app.menuItems["Pin Tab"].tap()
openFireWindow()
assertFireWindowDoesNotHavePinnedTabs()
}
func testFireWindowTabsCannotBeDragged() {
openFireWindow()
openSite(pageTitle: "Page #1")
app.openNewTab()
openSite(pageTitle: "Page #2")
dragFirstTabOutsideOfFireWindow()
/// Assert that Page #1 is still on the fire window after the drag
app.typeKey("]", modifierFlags: [.command, .shift])
XCTAssertTrue(app.staticTexts["Sample text for Page #2"].exists)
app.typeKey("[", modifierFlags: [.command, .shift])
XCTAssertTrue(app.staticTexts["Sample text for Page #1"].exists)
}
func testFireWindowsSignInDoesNotShowCredentialsPopup() {
openFireWindow()
hoverMouseOutsideTabSoPreviewIsNotShown()
openSignUpSite()
fillCredentials()
finishSignUp()
assertSavePasswordPopupIsNotShown()
}
func testCrendentialsAreAutoFilledInFireWindows() {
openNormalWindow()
hoverMouseOutsideTabSoPreviewIsNotShown()
openLoginSite()
signIn()
saveCredentials()
/// Here we start the same flow but in the fire window, but we use the autofill credentials saved in the step before.
openFireWindow()
hoverMouseOutsideTabSoPreviewIsNotShown()
openLoginSite()
signInUsingAutoFill()
}
// MARK: - Utilities
private func hoverMouseOutsideTabSoPreviewIsNotShown() {
let window = app.windows.firstMatch
let coordinate = window.coordinate(withNormalizedOffset: CGVector(dx: -100, dy: -100))
coordinate.hover()
}
private func signInUsingAutoFill() {
if areTestsRunningOnMacos13() {
let webViewFire = app.webViews.firstMatch
let webViewCoordinate = webViewFire.coordinate(withNormalizedOffset: CGVector(dx: 5, dy: 5))
webViewCoordinate.tap()
app.typeKey("\t", modifierFlags: [])
sleep(1)
let autoFillPopup = webViewFire.buttons["test@duck.com privacy-test-pages.site"]
let coordinate = autoFillPopup.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
coordinate.tap()
/// On macOS 13 there are some issues when accessing web view elements so we do not check the value of the email text field.
/// If we can access the `test@duck.com privacy-test-pages.site` button means that auto fill is working correctly in the fire window.
/// Checking that the email is being filled correctly is more an autofill test that fire window, so we are okay to skip it.
///
/// We do run this test on macOS 14 and above.
} else {
let webViewFire = app.webViews.firstMatch
webViewFire.tap()
let emailTextFieldFire = webViewFire.textFields["Email"].firstMatch
emailTextFieldFire.click()
let autoFillPopup = webViewFire.buttons["test@duck.com privacy-test-pages.site"]
let coordinate = autoFillPopup.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
coordinate.tap()
// Use an expectation to wait for the value to update
let expectedValue = "test@duck.com"
let valuePredicate = NSPredicate(format: "value == %@", expectedValue)
let expectation = XCTNSPredicateExpectation(predicate: valuePredicate, object: emailTextFieldFire)
let result = XCTWaiter().wait(for: [expectation], timeout: UITests.Timeouts.elementExistence)
XCTAssertEqual(result, .completed, "The email text field value did not update as expected.")
XCTAssertEqual(emailTextFieldFire.value as? String, expectedValue)
}
}
private func saveCredentials() {
let saveButton = app.buttons["Save"]
saveButton.tap()
}
private func signIn() {
if areTestsRunningOnMacos13() {
let webView = app.webViews.firstMatch
let webViewCoordinate = webView.coordinate(withNormalizedOffset: CGVector(dx: 5, dy: 5))
webViewCoordinate.tap()
app.typeKey("\t", modifierFlags: [])
app.typeText("test@duck.com")
app.typeKey("\t", modifierFlags: [])
app.typeText("pa$$word")
} else {
let webView = app.webViews.firstMatch
webView.tap()
let emailTextField = webView.textFields["Email"].firstMatch
emailTextField.click()
emailTextField.typeText("test@duck.com")
app.typeKey("\t", modifierFlags: [])
app.typeText("pa$$word")
}
let signInButton = app.webViews.firstMatch.buttons["Sign in"].firstMatch
signInButton.click()
}
private func openLoginSite() {
let addressBarTextField = app.windows.firstMatch.textFields["AddressBarViewController.addressBarTextField"].firstMatch
XCTAssertTrue(
addressBarTextField.waitForExistence(timeout: UITests.Timeouts.elementExistence),
"The address bar text field didn't become available in a reasonable timeframe."
)
addressBarTextField.typeURL(URL(string: "https://privacy-test-pages.site/autofill/autoprompt/1-standard-login-form.html")!)
XCTAssertTrue(
app.windows.firstMatch.webViews["Autofill autoprompt for signin forms"].waitForExistence(timeout: UITests.Timeouts.elementExistence),
"Visited site didn't load with the expected title in a reasonable timeframe."
)
}
private func assertSavePasswordPopupIsNotShown() {
let credentialsPopup = app.popovers["Save password in DuckDuckGo?"]
XCTAssertFalse(credentialsPopup.exists)
}
private func finishSignUp() {
let signUpButton = app.webViews.firstMatch.buttons["Sign up"].firstMatch
signUpButton.click()
}
private func fillCredentials() {
if areTestsRunningOnMacos13() {
/// On macOS 13 we tap in the webview coordinate and we use tabs to make it work given that it doesn't find web view elements
let webView = app.webViews.firstMatch
let webViewCoordinate = webView.coordinate(withNormalizedOffset: CGVector(dx: 5, dy: 5))
webViewCoordinate.tap()
app.typeKey("\t", modifierFlags: [])
app.typeText("test@duck.com")
app.typeKey("\t", modifierFlags: [])
app.typeText("pa$$word")
app.typeKey("\t", modifierFlags: [])
app.typeText("pa$$word")
} else {
let webView = app.webViews.firstMatch
webView.tap()
let emailTextField = webView.textFields["Email"].firstMatch
emailTextField.click()
emailTextField.typeText("test@duck.com")
let password = webView.secureTextFields["Password"].firstMatch
password.click()
password.typeText("pa$$word")
app.typeKey("\t", modifierFlags: [])
app.typeText("pa$$word")
}
}
private func openSignUpSite() {
let addressBarTextField = app.windows.firstMatch.textFields["AddressBarViewController.addressBarTextField"].firstMatch
XCTAssertTrue(
addressBarTextField.waitForExistence(timeout: UITests.Timeouts.elementExistence),
"The address bar text field didn't become available in a reasonable timeframe."
)
addressBarTextField.typeURL(URL(string: "https://privacy-test-pages.site/autofill/signup.html")!)
XCTAssertTrue(
app.windows.firstMatch.webViews["Password generation during signup"].waitForExistence(timeout: UITests.Timeouts.elementExistence),
"Visited site didn't load with the expected title in a reasonable timeframe."
)
}
private func dragFirstTabOutsideOfFireWindow() {
let toolbar = app.toolbars.firstMatch
let toolbarCoordinate = toolbar.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
let startPoint = toolbarCoordinate.withOffset(CGVector(dx: 120, dy: 15))
let endPoint = toolbarCoordinate.withOffset(CGVector(dx: -100, dy: -100))
startPoint.press(forDuration: 0.5, thenDragTo: endPoint)
}
private func assertFireWindowDoesNotHavePinnedTabs() {
let existsPredicate = NSPredicate(format: "exists == true")
let staticTextExistsExpectation = expectation(for: existsPredicate, evaluatedWith: app.windows.firstMatch.staticTexts.element(boundBy: 0), handler: nil)
// Wait up to 10 seconds for the static texts to be available
let result = XCTWaiter().wait(for: [staticTextExistsExpectation], timeout: 10)
XCTAssertEqual(result, .completed, "No static texts were found in the app")
// After confirming static texts are available, iterate through them
for staticText in app.staticTexts.allElementsBoundByIndex where staticText.exists {
XCTAssertFalse(staticText.label.contains("Page #1"), "Unwanted string found in static text: \(staticText.label)")
XCTAssertFalse(staticText.label.contains("Page #2"), "Unwanted string found in static text: \(staticText.label)")
}
}
private func assertSitesOpenedInNormalWindowAreRestored() {
XCTAssertTrue(app.staticTexts["Sample text for Page #3"].waitForExistence(timeout: UITests.Timeouts.elementExistence), "Page #3 should exist.")
app.typeKey("[", modifierFlags: [.command, .shift])
XCTAssertTrue(app.staticTexts["Sample text for Page #2"].waitForExistence(timeout: UITests.Timeouts.elementExistence), "Page #2 should exist.")
app.typeKey("[", modifierFlags: [.command, .shift])
XCTAssertTrue(app.staticTexts["Sample text for Page #1"].waitForExistence(timeout: UITests.Timeouts.elementExistence), "Page #1 should exist.")
}
private func assertSitesOpenedOnFireWindowAreNotRestored() {
let existsPredicate = NSPredicate(format: "exists == true")
let staticTextExistsExpectation = expectation(for: existsPredicate, evaluatedWith: app.staticTexts.element(boundBy: 0), handler: nil)
// Wait up to 10 seconds for the static texts to be available
let result = XCTWaiter().wait(for: [staticTextExistsExpectation], timeout: 10)
XCTAssertEqual(result, .completed, "No static texts were found in the app")
// After confirming static texts are available, iterate through them
for staticText in app.staticTexts.allElementsBoundByIndex where staticText.exists {
XCTAssertFalse(staticText.label.contains("Page #4"), "Unwanted string found in static text: \(staticText.label)")
XCTAssertFalse(staticText.label.contains("Page #5"), "Unwanted string found in static text: \(staticText.label)")
XCTAssertFalse(staticText.label.contains("Page #6"), "Unwanted string found in static text: \(staticText.label)")
}
}
private func openThreeSitesOnNormalWindow() {
app.openNewTab()
openSite(pageTitle: "Page #1")
app.openNewTab()
openSite(pageTitle: "Page #2")
app.openNewTab()
openSite(pageTitle: "Page #3")
}
private func openThreeSitesOnFireWindow() {
openSite(pageTitle: "Page #4")
app.openNewTab()
openSite(pageTitle: "Page #5")
app.openNewTab()
openSite(pageTitle: "Page #6")
}
private func assertSiteIsNotShowingInNormalWindowHistory() {
let siteMenuItemInHistory = app.menuItems["Some site"]
XCTAssertFalse(siteMenuItemInHistory.exists, "Menu item should not exist because it was not stored in history.")
}
private func openFireWindow() {
app.typeKey("n", modifierFlags: [.command, .shift])
}
private func openNormalWindow() {
app.typeKey("n", modifierFlags: .command)
}
private func openSite(pageTitle: String) {
let url = UITests.simpleServedPage(titled: pageTitle)
let addressBarTextField = app.windows.firstMatch.textFields["AddressBarViewController.addressBarTextField"].firstMatch
XCTAssertTrue(
addressBarTextField.waitForExistence(timeout: UITests.Timeouts.elementExistence),
"The address bar text field didn't become available in a reasonable timeframe."
)
addressBarTextField.typeURL(url)
XCTAssertTrue(
app.windows.firstMatch.webViews[pageTitle].waitForExistence(timeout: UITests.Timeouts.elementExistence),
"Visited site didn't load with the expected title in a reasonable timeframe."
)
}
private func areTestsRunningOnMacos13() -> Bool {
return ProcessInfo.processInfo.operatingSystemVersion.majorVersion == 13
}
}