|
| 1 | +/* |
| 2 | + Modified MIT License |
| 3 | + |
| 4 | + Copyright 2026 OneSignal |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | + 1. The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | + |
| 16 | + 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | +with services provided by OneSignal. |
| 18 | + |
| 19 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +import XCTest |
| 29 | + |
| 30 | +final class OSInAppMessageInsetsTests: XCTestCase { |
| 31 | + private let width: CGFloat = 800 |
| 32 | + private let margin: CGFloat = 24 |
| 33 | + // Landscape with a side safe area on each side, e.g. a camera housing on the left. |
| 34 | + private let safeArea = UIEdgeInsets(top: 0, left: 59, bottom: 21, right: 47) |
| 35 | + |
| 36 | + private func insets(margin: CGFloat? = nil, |
| 37 | + safeArea: UIEdgeInsets? = nil, |
| 38 | + regions: [CGRect] = [], |
| 39 | + isFullscreen: Bool = false) -> UIEdgeInsets { |
| 40 | + OSInAppMessageInsets.horizontalInsets(withMargin: margin ?? self.margin, |
| 41 | + safeArea: safeArea ?? self.safeArea, |
| 42 | + width: width, |
| 43 | + regionFrames: regions.map { NSValue(cgRect: $0) }, |
| 44 | + isFullscreen: isFullscreen) |
| 45 | + } |
| 46 | + |
| 47 | + func testNoRegions_usesSafeAreaWhenLargerThanMargin() { |
| 48 | + XCTAssertEqual(insets(), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 47)) |
| 49 | + } |
| 50 | + |
| 51 | + func testMarginAndSafeArea_takesMaxNotSum() { |
| 52 | + let result = insets(safeArea: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 30)) |
| 53 | + XCTAssertEqual(result, UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 30)) |
| 54 | + } |
| 55 | + |
| 56 | + func testNoSafeArea_usesMargin() { |
| 57 | + XCTAssertEqual(insets(safeArea: .zero), UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)) |
| 58 | + } |
| 59 | + |
| 60 | + func testNoWidthMargin_usesSafeAreaOnly() { |
| 61 | + let result = insets(margin: 0, safeArea: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)) |
| 62 | + XCTAssertEqual(result, UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)) |
| 63 | + } |
| 64 | + |
| 65 | + func testFullscreen_ignoresSafeAreaAndRegions() { |
| 66 | + let region = CGRect(x: 10, y: 100, width: 40, height: 80) |
| 67 | + XCTAssertEqual(insets(regions: [region], isFullscreen: true), |
| 68 | + UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)) |
| 69 | + XCTAssertEqual(insets(margin: 0, regions: [region], isFullscreen: true), .zero) |
| 70 | + } |
| 71 | + |
| 72 | + func testRegionInsideLeftSafeArea_addsClearance() { |
| 73 | + let region = CGRect(x: 10, y: 100, width: 40, height: 80) |
| 74 | + XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 74, bottom: 0, right: 47)) |
| 75 | + } |
| 76 | + |
| 77 | + func testRegionInsideRightSafeArea_addsClearance() { |
| 78 | + let region = CGRect(x: 760, y: 100, width: 30, height: 80) |
| 79 | + XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 64)) |
| 80 | + } |
| 81 | + |
| 82 | + func testRegionsOnBothSides_addClearanceToEach() { |
| 83 | + let left = CGRect(x: 10, y: 100, width: 40, height: 80) |
| 84 | + let right = CGRect(x: 760, y: 100, width: 30, height: 80) |
| 85 | + XCTAssertEqual(insets(regions: [left, right]), UIEdgeInsets(top: 0, left: 74, bottom: 0, right: 64)) |
| 86 | + } |
| 87 | + |
| 88 | + func testRegionClearanceSmallerThanSafeArea_keepsSafeArea() { |
| 89 | + let region = CGRect(x: 2, y: 100, width: 20, height: 80) |
| 90 | + XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 47)) |
| 91 | + } |
| 92 | + |
| 93 | + func testRegionFlushWithEdge_isIgnored() { |
| 94 | + let left = CGRect(x: 0, y: 0, width: 59, height: 400) |
| 95 | + let right = CGRect(x: 753, y: 0, width: 47, height: 400) |
| 96 | + XCTAssertEqual(insets(regions: [left, right]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 47)) |
| 97 | + } |
| 98 | + |
| 99 | + func testRegionStraddlingSafeAreaEdge_isIgnored() { |
| 100 | + let left = CGRect(x: 40, y: 100, width: 40, height: 80) |
| 101 | + let right = CGRect(x: 740, y: 100, width: 40, height: 80) |
| 102 | + XCTAssertEqual(insets(regions: [left, right]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 47)) |
| 103 | + } |
| 104 | + |
| 105 | + func testRegionWithinOnePointOfSafeAreaEdge_addsClearance() { |
| 106 | + let region = CGRect(x: 10, y: 100, width: 50, height: 80) |
| 107 | + XCTAssertEqual(insets(regions: [region]).left, 84) |
| 108 | + } |
| 109 | +} |
0 commit comments