|
| 1 | +// Created by Bryn Bodayle on 6/20/25. |
| 2 | +// Copyright © 2025 Airbnb Inc. All rights reserved. |
| 3 | + |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +import XCTest |
| 17 | +@testable import MagazineLayout |
| 18 | + |
| 19 | +final class ContentInsetAdjustingContentOffsetTests: XCTestCase { |
| 20 | + |
| 21 | + func testContentOffsetIsNotAdjustedForTopInsetChangeWithToTopBottomLayout() { |
| 22 | + let layout = MagazineLayout(verticalLayoutDirection: .topToBottom) |
| 23 | + let collectionView = StubCollectionView( |
| 24 | + frame: .zero, |
| 25 | + collectionViewLayout: layout) |
| 26 | + let context = MagazineLayoutInvalidationContext() |
| 27 | + layout.invalidateLayout(with: context) |
| 28 | + XCTAssertEqual(context.contentOffsetAdjustment, .zero) |
| 29 | + |
| 30 | + collectionView.stubAdjustedContentInset = .init(top: 50, left: 0, bottom: 50, right: 0) |
| 31 | + layout.invalidateLayout(with: context) |
| 32 | + XCTAssertEqual(context.contentOffsetAdjustment, .zero) |
| 33 | + } |
| 34 | + |
| 35 | + func testContentOffsetIsAdjustedForTopInsetChangeWithBottomToTopLayout() { |
| 36 | + let layout = MagazineLayout(verticalLayoutDirection: .bottomToTop) |
| 37 | + let collectionView = StubCollectionView( |
| 38 | + frame: .zero, |
| 39 | + collectionViewLayout: layout) |
| 40 | + let context = MagazineLayoutInvalidationContext() |
| 41 | + layout.invalidateLayout(with: context) |
| 42 | + XCTAssertEqual(context.contentOffsetAdjustment, .zero) |
| 43 | + |
| 44 | + collectionView.stubAdjustedContentInset = .init(top: 50, left: 0, bottom: 0, right: 0) |
| 45 | + layout.invalidateLayout(with: context) |
| 46 | + XCTAssertEqual(context.contentOffsetAdjustment, .init(x: 0, y: 50)) |
| 47 | + } |
| 48 | + |
| 49 | + func testContentOffsetIsAdjustedForBottomInsetChangeWithBottomToTopLayout() { |
| 50 | + let layout = MagazineLayout(verticalLayoutDirection: .bottomToTop) |
| 51 | + let collectionView = StubCollectionView( |
| 52 | + frame: .zero, |
| 53 | + collectionViewLayout: layout) |
| 54 | + let context = MagazineLayoutInvalidationContext() |
| 55 | + layout.invalidateLayout(with: context) |
| 56 | + XCTAssertEqual(context.contentOffsetAdjustment, .zero) |
| 57 | + |
| 58 | + collectionView.stubAdjustedContentInset = .init(top: 0, left: 0, bottom: 75, right: 0) |
| 59 | + layout.invalidateLayout(with: context) |
| 60 | + XCTAssertEqual(context.contentOffsetAdjustment, .init(x: 0, y: 75)) |
| 61 | + } |
| 62 | + |
| 63 | + func testContentOffsetIsAdjustedForTopAndBottomInsetChangesWithBottomToTopLayout() { |
| 64 | + let layout = MagazineLayout(verticalLayoutDirection: .bottomToTop) |
| 65 | + let collectionView = StubCollectionView( |
| 66 | + frame: .zero, |
| 67 | + collectionViewLayout: layout) |
| 68 | + let context = MagazineLayoutInvalidationContext() |
| 69 | + layout.invalidateLayout(with: context) |
| 70 | + XCTAssertEqual(context.contentOffsetAdjustment, .zero) |
| 71 | + |
| 72 | + collectionView.stubAdjustedContentInset = .init(top: 100, left: 0, bottom: 100, right: 0) |
| 73 | + layout.invalidateLayout(with: context) |
| 74 | + XCTAssertEqual(context.contentOffsetAdjustment, .init(x: 0, y: 200)) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +private class StubCollectionView: UICollectionView { |
| 79 | + |
| 80 | + var stubAdjustedContentInset: UIEdgeInsets = .zero |
| 81 | + override var adjustedContentInset: UIEdgeInsets { |
| 82 | + stubAdjustedContentInset |
| 83 | + } |
| 84 | +} |
0 commit comments