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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageInsets.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * 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. | ||
| // Both checks allow 1pt of slack: within 1pt of the screen edge counts as flush, and up to 1pt past | ||
| // the safe area edge still counts as inside. | ||
| // 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 | ||
55 changes: 55 additions & 0 deletions
55
iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageInsets.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
iOS_SDK/OneSignalSDK/OneSignalInAppMessagesTests/OSInAppMessageInsetsTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| 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() { | ||
|
fadi-george marked this conversation as resolved.
Resolved
|
||
| 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() { | ||
|
fadi-george marked this conversation as resolved.
Resolved
|
||
| let region = CGRect(x: 10, y: 100, width: 50, height: 80) | ||
| XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 84, bottom: 0, right: 47)) | ||
| } | ||
|
|
||
| func testRightRegionWithinOnePointOfSafeAreaEdge_addsClearance() { | ||
| let region = CGRect(x: 752, y: 100, width: 30, height: 80) | ||
| XCTAssertEqual(insets(regions: [region]), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 72)) | ||
| } | ||
|
|
||
| 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 testTwoRegionsOnLeftSide_keepLargerClearance() { | ||
| let regions = [CGRect(x: 10, y: 100, width: 40, height: 80), CGRect(x: 10, y: 200, width: 30, height: 80)] | ||
| XCTAssertEqual(insets(regions: regions), UIEdgeInsets(top: 0, left: 74, bottom: 0, right: 47)) | ||
| XCTAssertEqual(insets(regions: regions.reversed()), UIEdgeInsets(top: 0, left: 74, bottom: 0, right: 47)) | ||
| } | ||
|
|
||
| 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)) | ||
| XCTAssertEqual(insets(regions: regions.reversed()), UIEdgeInsets(top: 0, left: 59, bottom: 0, right: 64)) | ||
| } | ||
|
fadi-george marked this conversation as resolved.
Resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.