|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/ |
| 4 | + |
| 5 | +import Foundation |
| 6 | +import MozillaAppServices |
| 7 | +import Storage |
| 8 | +import XCTest |
| 9 | +import Glean |
| 10 | +import Common |
| 11 | +import Shared |
| 12 | + |
| 13 | +@testable import Client |
| 14 | + |
| 15 | +@MainActor |
| 16 | +final class BrowserTabScrollHandlerDelegateTest: BrowserViewControllerConstraintTestsBase { |
| 17 | + private let minimalHeaderOffset = BrowserViewController.UX.minimalHeaderOffset |
| 18 | + |
| 19 | + // MARK: - Top Toolbar |
| 20 | + |
| 21 | + func testHideToolbar_TopToolbar_headerMovesUp() { |
| 22 | + let subject = createSubject(isBottomSearchBar: false) |
| 23 | + subject.setupToolbarAnimator() |
| 24 | + |
| 25 | + let initialHeaderFrame = subject.header.frame |
| 26 | + |
| 27 | + subject.hideToolbar() |
| 28 | + |
| 29 | + // Current Y position should be less than initial position, the offset should be |
| 30 | + // the height plus UX.minimalHeaderOffset |
| 31 | + let minimalHeaderOffset = (initialHeaderFrame.minY - initialHeaderFrame.height) + minimalHeaderOffset |
| 32 | + XCTAssertLessThan(subject.header.frame.minY, initialHeaderFrame.minY) |
| 33 | + XCTAssertEqual(subject.header.frame.minY, minimalHeaderOffset) |
| 34 | + } |
| 35 | + |
| 36 | + func testShowToolbar_TopToolbar_afterHide_restoresHeaderPosition() { |
| 37 | + let subject = createSubject(isBottomSearchBar: false) |
| 38 | + subject.setupToolbarAnimator() |
| 39 | + let initialHeaderMinY = subject.header.frame.minY |
| 40 | + |
| 41 | + subject.hideToolbar() |
| 42 | + subject.showToolbar() |
| 43 | + |
| 44 | + XCTAssertEqual(subject.header.frame.minY, initialHeaderMinY) |
| 45 | + } |
| 46 | + |
| 47 | + // MARK: - Top Toolbar Transition |
| 48 | + |
| 49 | + func testUpdateToolbarTransition_TopToolbar_collapsing_movesHeaderUp() { |
| 50 | + let subject = createSubject(isBottomSearchBar: false) |
| 51 | + subject.setupToolbarAnimator() |
| 52 | + let initialHeaderMinY = subject.header.frame.minY |
| 53 | + |
| 54 | + subject.updateToolbarTransition(progress: 20, towards: .collapsed) |
| 55 | + |
| 56 | + XCTAssertLessThan(subject.header.frame.minY, initialHeaderMinY) |
| 57 | + } |
| 58 | + |
| 59 | + func testUpdateToolbarTransition_TopToolbar_expanding_resetsTransform() { |
| 60 | + let subject = createSubject(isBottomSearchBar: false) |
| 61 | + subject.setupToolbarAnimator() |
| 62 | + subject.updateToolbarTransition(progress: 20, towards: .collapsed) |
| 63 | + |
| 64 | + subject.updateToolbarTransition(progress: 0, towards: .expanded) |
| 65 | + |
| 66 | + XCTAssertEqual(subject.header.transform, .identity) |
| 67 | + } |
| 68 | + |
| 69 | + // MARK: - Top Toolbar + Reader Mode |
| 70 | + |
| 71 | + func testHideToolbar_ReaderModeActive_headerMovesUp() { |
| 72 | + let subject = createSubject(isBottomSearchBar: false) |
| 73 | + selectReaderModeTab() |
| 74 | + subject.setupToolbarAnimator() |
| 75 | + let initialHeaderFrame = subject.header.frame |
| 76 | + |
| 77 | + subject.hideToolbar() |
| 78 | + |
| 79 | + // Current Y position should be less than initial position, the offset should be the height |
| 80 | + let minimalHeaderOffset = initialHeaderFrame.minY - initialHeaderFrame.height |
| 81 | + XCTAssertLessThan(subject.header.frame.minY, initialHeaderFrame.minY) |
| 82 | + XCTAssertEqual(subject.header.frame.minY, minimalHeaderOffset) |
| 83 | + } |
| 84 | + |
| 85 | + func testShowToolbar_ReaderModeActive_afterHide_restoresHeaderPosition() { |
| 86 | + let subject = createSubject(isBottomSearchBar: false) |
| 87 | + selectReaderModeTab() |
| 88 | + subject.setupToolbarAnimator() |
| 89 | + let initialHeaderMinY = subject.header.frame.minY |
| 90 | + |
| 91 | + subject.hideToolbar() |
| 92 | + subject.showToolbar() |
| 93 | + |
| 94 | + XCTAssertEqual(subject.header.frame.minY, initialHeaderMinY) |
| 95 | + } |
| 96 | + |
| 97 | + // MARK: - Bottom Search Bar |
| 98 | + |
| 99 | + func testHideToolbar_BottomSearchBar_overKeyboardContainerMovesDown() { |
| 100 | + let subject = createSubject() |
| 101 | + guard subject.overKeyboardContainer.frame.height > 0 else { return } |
| 102 | + let initialMinY = subject.overKeyboardContainer.frame.minY |
| 103 | + |
| 104 | + setupAnimator(for: subject, overKeyboardContainerHeight: subject.overKeyboardContainer.frame.height) |
| 105 | + subject.toolbarAnimator?.hideToolbar() |
| 106 | + |
| 107 | + XCTAssertGreaterThan(subject.overKeyboardContainer.frame.minY, initialMinY) |
| 108 | + } |
| 109 | + |
| 110 | + func testShowToolbar_BottomSearchBar_afterHide_restoresOverKeyboardPosition() { |
| 111 | + let subject = createSubject() |
| 112 | + guard subject.overKeyboardContainer.frame.height > 0 else { return } |
| 113 | + let initialMinY = subject.overKeyboardContainer.frame.minY |
| 114 | + |
| 115 | + setupAnimator(for: subject, overKeyboardContainerHeight: subject.overKeyboardContainer.frame.height) |
| 116 | + subject.toolbarAnimator?.hideToolbar() |
| 117 | + subject.toolbarAnimator?.showToolbar() |
| 118 | + |
| 119 | + XCTAssertEqual(subject.overKeyboardContainer.frame.minY, initialMinY) |
| 120 | + } |
| 121 | + |
| 122 | + func testHideToolbar_BottomSearchBar_topHeaderDoesNotMove() { |
| 123 | + let subject = createSubject(isBottomSearchBar: true) |
| 124 | + subject.setupToolbarAnimator() |
| 125 | + let initialHeaderMinY = subject.header.frame.minY |
| 126 | + |
| 127 | + subject.hideToolbar() |
| 128 | + |
| 129 | + // For Bottom search bar header has height = 0 and should not be animated. |
| 130 | + XCTAssertEqual(subject.header.frame.minY, initialHeaderMinY) |
| 131 | + XCTAssertEqual(subject.header.frame.height, 0) |
| 132 | + } |
| 133 | + |
| 134 | + // MARK: - Animator Setup |
| 135 | + |
| 136 | + func testSetupToolbarAnimator_setsViewAndDelegate() { |
| 137 | + let subject = createSubject(isBottomSearchBar: false) |
| 138 | + |
| 139 | + subject.setupToolbarAnimator() |
| 140 | + |
| 141 | + XCTAssertNotNil(subject.toolbarAnimator) |
| 142 | + XCTAssertTrue(subject.toolbarAnimator?.view === subject) |
| 143 | + } |
| 144 | + |
| 145 | + func testSetupToolbarAnimator_calledTwice_replacesAnimator() { |
| 146 | + let subject = createSubject(isBottomSearchBar: false) |
| 147 | + subject.setupToolbarAnimator() |
| 148 | + let firstAnimator = subject.toolbarAnimator |
| 149 | + |
| 150 | + subject.setupToolbarAnimator() |
| 151 | + |
| 152 | + XCTAssertFalse(subject.toolbarAnimator === firstAnimator) |
| 153 | + } |
| 154 | + |
| 155 | + // MARK: - Private |
| 156 | + |
| 157 | + private func selectReaderModeTab() { |
| 158 | + let tab = Tab(profile: profile, windowUUID: .XCTestDefaultUUID) |
| 159 | + tab.url = URL(string: "http://localhost:6571/reader-mode/page?url=https://example.com") |
| 160 | + tabManager.selectedTab = tab |
| 161 | + } |
| 162 | + |
| 163 | + private func createToolbarAnimator(headerHeight: CGFloat = 60, |
| 164 | + bottomContainerHeight: CGFloat = 60, |
| 165 | + overKeyboardContainerHeight: CGFloat = 60) -> ToolbarAnimator { |
| 166 | + let context = ToolbarContext(overKeyboardContainerHeight: overKeyboardContainerHeight, |
| 167 | + bottomContainerHeight: bottomContainerHeight, |
| 168 | + headerHeight: headerHeight) |
| 169 | + return ToolbarAnimator(context: context) |
| 170 | + } |
| 171 | + |
| 172 | + /// Wires a ToolbarAnimator with an explicit context onto the subject, bypassing |
| 173 | + /// `updateToolbarContext()`. Use this when the calculated context would be 0 in the test environment |
| 174 | + private func setupAnimator(for subject: BrowserViewController, |
| 175 | + headerHeight: CGFloat = 60, |
| 176 | + bottomContainerHeight: CGFloat = 60, |
| 177 | + overKeyboardContainerHeight: CGFloat = 60) { |
| 178 | + let animator = createToolbarAnimator(headerHeight: headerHeight, |
| 179 | + bottomContainerHeight: bottomContainerHeight, |
| 180 | + overKeyboardContainerHeight: overKeyboardContainerHeight) |
| 181 | + animator.view = subject |
| 182 | + animator.delegate = subject |
| 183 | + subject.toolbarAnimator = animator |
| 184 | + } |
| 185 | + |
| 186 | + private func setupNimbusTabScrollRefactorTesting(isEnabled: Bool) { |
| 187 | + FxNimbus.shared.features.tabScrollRefactorFeature.with { _, _ in |
| 188 | + return TabScrollRefactorFeature(enabled: isEnabled) |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments