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 pathPinnedTabsTests.swift
177 lines (151 loc) · 6.79 KB
/
PinnedTabsTests.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
//
// PinnedTabsTests.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 PinnedTabsTests: UITestCase {
private static let failureObserver = TestFailureObserver()
private var app: XCUIApplication!
override func setUpWithError() throws {
continueAfterFailure = false
app = XCUIApplication()
app.launchEnvironment["UITEST_MODE"] = "1"
app.launch()
app.typeKey("n", modifierFlags: .command)
}
override class func setUp() {
super.setUp()
UITests.firstRun()
}
func testPinnedTabsFunctionality() {
openThreeSitesOnSameWindow()
openNewWindowAndLoadSite()
moveBackToPreviousWindows()
waitForSite(pageTitle: "Page #3")
pinsPageOne()
pinsPageTwo()
assertsPageTwoIsPinned()
assertsPageOneIsPinned()
dragsPageTwoPinnedTabToTheFirstPosition()
assertsCommandWFunctionality()
assertWindowTwoHasTheTwoPinnedTabFromWindowsOne()
app.terminate()
assertPinnedTabsRestoredState()
}
// MARK: - Utilities
private func openThreeSitesOnSameWindow() {
openSite(pageTitle: "Page #1")
app.openNewTab()
openSite(pageTitle: "Page #2")
app.openNewTab()
openSite(pageTitle: "Page #3")
}
private func openNewWindowAndLoadSite() {
app.typeKey("n", modifierFlags: .command)
openSite(pageTitle: "Page #4")
}
private func moveBackToPreviousWindows() {
let menuItem = app.menuItems["Page #3"].firstMatch
XCTAssertTrue(
menuItem.waitForExistence(timeout: UITests.Timeouts.elementExistence),
"Reset bookmarks menu item didn't become available in a reasonable timeframe."
)
menuItem.hover()
app.typeKey(XCUIKeyboardKey.return, modifierFlags: [])
}
private func openSite(pageTitle: String) {
let url = UITests.simpleServedPage(titled: pageTitle)
let addressBarTextField = app.windows.firstMatch.textFields["AddressBarViewController.addressBarTextField"]
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 pinsPageOne() {
app.typeKey("[", modifierFlags: [.command, .shift])
app.typeKey("[", modifierFlags: [.command, .shift])
app.menuItems["Pin Tab"].tap()
}
private func pinsPageTwo() {
app.typeKey("]", modifierFlags: [.command, .shift])
app.menuItems["Pin Tab"].tap()
}
private func assertsPageTwoIsPinned() {
XCTAssertTrue(app.menuItems["Unpin Tab"].firstMatch.waitForExistence(timeout: UITests.Timeouts.elementExistence))
XCTAssertTrue(app.menuItems["Unpin Tab"].firstMatch.exists)
XCTAssertFalse(app.menuItems["Pin Tab"].firstMatch.exists)
}
private func assertsPageOneIsPinned() {
app.typeKey("[", modifierFlags: [.command, .shift])
XCTAssertTrue(app.menuItems["Unpin Tab"].firstMatch.exists)
XCTAssertFalse(app.menuItems["Pin Tab"].firstMatch.exists)
}
private func dragsPageTwoPinnedTabToTheFirstPosition() {
app.typeKey("]", modifierFlags: [.command, .shift])
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: 0, dy: 0))
startPoint.press(forDuration: 0, thenDragTo: endPoint)
sleep(1)
/// Asserts the re-order worked by moving to the next tab and checking is Page #1
app.typeKey("]", modifierFlags: [.command, .shift])
XCTAssertTrue(app.staticTexts["Sample text for Page #1"].exists)
}
private func assertsCommandWFunctionality() {
app.typeKey("w", modifierFlags: .command)
XCTAssertTrue(app.staticTexts["Sample text for Page #3"].exists)
}
private func assertWindowTwoHasTheTwoPinnedTabFromWindowsOne() {
let items = app.menuItems.matching(identifier: "Page #4")
let pageFourMenuItem = items.element(boundBy: 1)
XCTAssertTrue(
pageFourMenuItem.waitForExistence(timeout: UITests.Timeouts.elementExistence),
"Reset bookmarks menu item didn't become available in a reasonable timeframe."
)
pageFourMenuItem.hover()
app.typeKey(XCUIKeyboardKey.return, modifierFlags: [])
sleep(2)
/// Goes to Page #2 to check the state
app.typeKey("[", modifierFlags: [.command, .shift])
app.typeKey("[", modifierFlags: [.command, .shift])
XCTAssertTrue(app.staticTexts["Sample text for Page #2"].exists)
/// Goes to Page #1 to check the state
app.typeKey("]", modifierFlags: [.command, .shift])
XCTAssertTrue(app.staticTexts["Sample text for Page #1"].exists)
}
private func assertPinnedTabsRestoredState() {
let newApp = XCUIApplication()
newApp.launch()
newApp.typeKey("n", modifierFlags: .command)
sleep(10) // This was increased from two to ten, because slower VMs needed more time to re-launch the app.
/// Goes to Page #2 to check the state
newApp.typeKey("[", modifierFlags: [.command, .shift])
newApp.typeKey("[", modifierFlags: [.command, .shift])
XCTAssertTrue(newApp.staticTexts["Sample text for Page #2"].waitForExistence(timeout: UITests.Timeouts.elementExistence))
/// Goes to Page #1 to check the state
newApp.typeKey("]", modifierFlags: [.command, .shift])
XCTAssertTrue(newApp.staticTexts["Sample text for Page #1"].waitForExistence(timeout: UITests.Timeouts.elementExistence))
}
private func waitForSite(pageTitle: String) {
XCTAssertTrue(app.windows.webViews[pageTitle].waitForExistence(timeout: UITests.Timeouts.elementExistence))
}
}