Skip to content

Commit 04852ae

Browse files
author
“Akshay
committed
Test fix
1 parent 38bfdfc commit 04852ae

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppFragmentHTMLNotification.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,22 @@ int getVerticalLocation(Rect padding) {
692692
* This is used to detect significant orientation changes (portrait/landscape).
693693
*
694694
* The calculation rounds to the nearest multiple of 90 by adding 45 before dividing.
695-
* Uses floating point division to correctly handle negative numbers.
695+
* For positive numbers, uses integer division (which truncates toward zero).
696+
* For negative numbers, uses floor division to correctly round toward negative infinity.
696697
*
697698
* @param orientation The orientation value in degrees (typically 0-359 from OrientationEventListener)
698699
* @return The orientation rounded to the nearest 90-degree increment (0, 90, 180, 270, or 360)
699700
*/
700701
static int roundToNearest90Degrees(int orientation) {
701-
return (int) (Math.round((orientation + 45.0) / 90.0) * 90);
702+
if (orientation >= 0) {
703+
// For positive numbers, integer division truncates toward zero
704+
// (0 + 45) / 90 = 0, (44 + 45) / 90 = 0, (45 + 45) / 90 = 1
705+
return ((orientation + 45) / 90) * 90;
706+
} else {
707+
// For negative numbers, use floor division to round toward negative infinity
708+
// Math.floor((-46 + 45) / 90.0) = Math.floor(-1/90.0) = -1, so -1 * 90 = -90
709+
return (int) (Math.floor((orientation + 45.0) / 90.0) * 90);
710+
}
702711
}
703712

704713
/**

iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppHTMLNotificationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ public void testRoundToNearest90Degrees_NegativeValues() {
363363
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-45));
364364
assertEquals(-90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-46));
365365
assertEquals(-90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-90));
366-
assertEquals(-90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-134));
367-
assertEquals(-180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-135));
366+
assertEquals(-90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-135));
367+
assertEquals(-180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-136));
368368
}
369369

370370
@Test

0 commit comments

Comments
 (0)