Skip to content

Commit 841472d

Browse files
committed
Add unit tests
1 parent 5b3285b commit 841472d

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

MagazineLayout.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
60432D952E05DB41001728F0 /* ContentInsetAdjustingContentOffsetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60432D942E05DB41001728F0 /* ContentInsetAdjustingContentOffsetTests.swift */; };
1011
9332FB0822969B5600483D99 /* RowOffsetTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9332FB0622969AB200483D99 /* RowOffsetTrackerTests.swift */; };
1112
93424B012256878B003D00C0 /* MagazineLayoutFooterVisibilityMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93424B002256878B003D00C0 /* MagazineLayoutFooterVisibilityMode.swift */; };
1213
93540AB0282E25D90008BD6F /* ScreenPixelAlignment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93540AAF282E25D90008BD6F /* ScreenPixelAlignment.swift */; };
@@ -58,6 +59,7 @@
5859
/* End PBXContainerItemProxy section */
5960

6061
/* Begin PBXFileReference section */
62+
60432D942E05DB41001728F0 /* ContentInsetAdjustingContentOffsetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentInsetAdjustingContentOffsetTests.swift; sourceTree = "<group>"; };
6163
9332FB0622969AB200483D99 /* RowOffsetTrackerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RowOffsetTrackerTests.swift; sourceTree = "<group>"; };
6264
93424B002256878B003D00C0 /* MagazineLayoutFooterVisibilityMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MagazineLayoutFooterVisibilityMode.swift; sourceTree = "<group>"; };
6365
93540AAF282E25D90008BD6F /* ScreenPixelAlignment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenPixelAlignment.swift; sourceTree = "<group>"; };
@@ -156,6 +158,7 @@
156158
9332FB0622969AB200483D99 /* RowOffsetTrackerTests.swift */,
157159
93A1C00E21ACED0100DED67D /* TestingSupport.swift */,
158160
93540AB1282E26340008BD6F /* ScreenPixelAlignmentTests.swift */,
161+
60432D942E05DB41001728F0 /* ContentInsetAdjustingContentOffsetTests.swift */,
159162
);
160163
path = Tests;
161164
sourceTree = "<group>";
@@ -382,6 +385,7 @@
382385
files = (
383386
FDE08E162B2CC47800C9D24D /* TargetContentOffsetAnchorTests.swift in Sources */,
384387
93A1C04B21ACED1100DED67D /* ModelStateInitiallSetUpTests.swift in Sources */,
388+
60432D952E05DB41001728F0 /* ContentInsetAdjustingContentOffsetTests.swift in Sources */,
385389
9332FB0822969B5600483D99 /* RowOffsetTrackerTests.swift in Sources */,
386390
93A1C04C21ACED1100DED67D /* ModelStateLayoutTests.swift in Sources */,
387391
93A1C04921ACED1100DED67D /* ModelStateEmptySectionLayoutTests.swift in Sources */,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)