Repository navigation
refactor: [SDK-5661] extract IAM horizontal inset math and unit test it #1791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * Modified MIT License | ||
| * | ||
| * Copyright 2026 OneSignal | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * 1. The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * 2. All copies of substantial portions of the Software may only be used in connection | ||
| * with services provided by OneSignal. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| #import <UIKit/UIKit.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| @interface OSInAppMessageInsets : NSObject | ||
|
|
||
| // Each side is the larger of the margin and its safe area inset. Region frames are in the scene | ||
| // view's coordinates; one sitting inside a side safe area, not flush with the edge, adds clearance. | ||
| // Full screen messages ignore the safe area and regions. | ||
| + (UIEdgeInsets)horizontalInsetsWithMargin:(CGFloat)margin | ||
| safeArea:(UIEdgeInsets)safeArea | ||
| width:(CGFloat)width | ||
| regionFrames:(NSArray<NSValue *> *)regionFrames | ||
| isFullscreen:(BOOL)isFullscreen; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * Modified MIT License | ||
| * | ||
| * Copyright 2026 OneSignal | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * 1. The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * 2. All copies of substantial portions of the Software may only be used in connection | ||
| * with services provided by OneSignal. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| #import "OSInAppMessageInsets.h" | ||
|
|
||
| @implementation OSInAppMessageInsets | ||
|
|
||
| + (UIEdgeInsets)horizontalInsetsWithMargin:(CGFloat)margin | ||
| safeArea:(UIEdgeInsets)safeArea | ||
| width:(CGFloat)width | ||
| regionFrames:(NSArray<NSValue *> *)regionFrames | ||
| isFullscreen:(BOOL)isFullscreen { | ||
| if (isFullscreen) { | ||
| return UIEdgeInsetsMake(0, margin, 0, margin); | ||
| } | ||
| CGFloat left = MAX(margin, safeArea.left); | ||
| CGFloat right = MAX(margin, safeArea.right); | ||
| // Region frames already include UIKit's interactive margins; the message margin is added on top. | ||
| for (NSValue *value in regionFrames) { | ||
| CGRect frame = value.CGRectValue; | ||
| CGFloat minX = CGRectGetMinX(frame); | ||
| CGFloat maxX = CGRectGetMaxX(frame); | ||
| if (minX >= 1 && maxX <= safeArea.left + 1) { | ||
| left = MAX(left, maxX + margin); | ||
| } else if (maxX <= width - 1 && minX >= width - safeArea.right - 1) { | ||
| right = MAX(right, width - minX + margin); | ||
| } | ||
| } | ||
| return UIEdgeInsetsMake(0, left, 0, right); | ||
| } | ||
|
|
||
| @end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| Modified MIT License | ||
|
|
||
| Copyright 2026 OneSignal | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| 1. The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| 2. All copies of substantial portions of the Software may only be used in connection | ||
| with services provided by OneSignal. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| */ | ||
|
|
||
| import XCTest | ||
|
|
||
| final class OSInAppMessageInsetsTests: XCTestCase { | ||
| private let width: CGFloat = 800 | ||
| private let margin: CGFloat = 24 | ||
| // Landscape with a side safe area on each side, e.g. a camera housing on the left. | ||
| private let safeArea = UIEdgeInsets(top: 0, left: 59, bottom: 21, right: 47) | ||
|
|
||
| private func insets(margin: CGFloat? = nil, | ||
| safeArea: UIEdgeInsets? = nil, | ||
| regions: [CGRect] = [], | ||
| isFullscreen: Bool = false) -> UIEdgeInsets { | ||
| OSInAppMessageInsets.horizontalInsets(withMargin: margin ?? self.margin, | ||
| safeArea: safeArea ?? self.safeArea, | ||
| width: width, | ||
| regionFrames: regions.map { NSValue(cgRect: $0) }, | ||
| isFullscreen: isFullscreen) | ||
| } | ||
|
|
||
| func testNoRegions_usesSafeAreaWhenLargerThanMargin() { | ||
| XCTAssertEqual(insets(), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 47)) | ||
| } | ||
|
|
||
| func testMarginAndSafeArea_takesMaxNotSum() { | ||
| let result = insets(safeArea: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 30)) | ||
| XCTAssertEqual(result, UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 30)) | ||
| } | ||
|
|
||
| func testNoSafeArea_usesMargin() { | ||
| XCTAssertEqual(insets(safeArea: .zero), UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)) | ||
| } | ||
|
|
||
| func testNoWidthMargin_usesSafeAreaOnly() { | ||
| let result = insets(margin: 0, safeArea: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)) | ||
| XCTAssertEqual(result, UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)) | ||
| } | ||
|
|
||
| func testFullscreen_ignoresSafeAreaAndRegions() { | ||
| let region = CGRect(x: 10, y: 100, width: 40, height: 80) | ||
| XCTAssertEqual(insets(regions: [region], isFullscreen: true), | ||
| UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)) | ||
| XCTAssertEqual(insets(margin: 0, regions: [region], isFullscreen: true), .zero) | ||
| } | ||
|
|
||
| func testRegionInsideLeftSafeArea_addsClearance() { | ||
| let region = CGRect(x: 10, y: 100, width: 40, height: 80) | ||
| XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 74, bottom: 0, right: 47)) | ||
| } | ||
|
|
||
| func testRegionInsideRightSafeArea_addsClearance() { | ||
| let region = CGRect(x: 760, y: 100, width: 30, height: 80) | ||
| XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 64)) | ||
| } | ||
|
|
||
| func testRegionsOnBothSides_addClearanceToEach() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-blocking: Nothing checks two regions on the same side. Two left regions with maxX 50 and 40, in either order, should both give left 74. |
||
| let left = CGRect(x: 10, y: 100, width: 40, height: 80) | ||
| let right = CGRect(x: 760, y: 100, width: 30, height: 80) | ||
| XCTAssertEqual(insets(regions: [left, right]), UIEdgeInsets(top: 0, left: 74, bottom: 0, right: 64)) | ||
| } | ||
|
|
||
| func testRegionClearanceSmallerThanSafeArea_keepsSafeArea() { | ||
| let region = CGRect(x: 2, y: 100, width: 20, height: 80) | ||
| XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 47)) | ||
| } | ||
|
|
||
| func testRegionFlushWithEdge_isIgnored() { | ||
| let left = CGRect(x: 0, y: 0, width: 59, height: 400) | ||
| let right = CGRect(x: 753, y: 0, width: 47, height: 400) | ||
| XCTAssertEqual(insets(regions: [left, right]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 47)) | ||
| } | ||
|
|
||
| func testRegionStraddlingSafeAreaEdge_isIgnored() { | ||
| let left = CGRect(x: 40, y: 100, width: 40, height: 80) | ||
| let right = CGRect(x: 740, y: 100, width: 40, height: 80) | ||
| XCTAssertEqual(insets(regions: [left, right]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 47)) | ||
| } | ||
|
|
||
| func testRegionWithinOnePointOfSafeAreaEdge_addsClearance() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-blocking: Could you mirror this on the right? A region at x 752, width 30 should give right 72. Right now changing the right side's |
||
| let region = CGRect(x: 10, y: 100, width: 50, height: 80) | ||
| XCTAssertEqual(insets(regions: [region]).left, 84) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These three close the surviving mutations (each verified to go red against the corresponding mutant): func testRegionStartingAtOnePoint_addsClearance() {
let region = CGRect(x: 1, y: 100, width: 40, height: 80)
XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 65, bottom: 0, right: 47))
}
func testRightRegionWithinOnePointOfSafeAreaEdge_addsClearance() {
let region = CGRect(x: 752, y: 100, width: 20, height: 80)
XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 72))
}
func testTwoRegionsOnRightSide_keepLargerClearance() {
let regions = [CGRect(x: 760, y: 100, width: 30, height: 80), CGRect(x: 790, y: 100, width: 5, height: 80)]
XCTAssertEqual(insets(regions: regions), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 64))
}Small nit while here: |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth naming the 1pt slack here (within 1pt of the screen edge counts as flush; up to 1pt past the safe area still counts as inside), since that is the part of the contract the suite only half-defends today.