Skip to content

Commit 3e4dcd1

Browse files
author
“Akshay
committed
orientation listener calculation code testing
1 parent 598a159 commit 3e4dcd1

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
213213
public void onOrientationChanged(int orientation) {
214214
if (loaded && webView != null) {
215215
// Only trigger on significant orientation changes (90 degree increments)
216-
int currentOrientation = ((orientation + 45) / 90) * 90;
216+
int currentOrientation = roundToNearest90Degrees(orientation);
217217
if (currentOrientation != lastOrientation && lastOrientation != -1) {
218218
lastOrientation = currentOrientation;
219219

@@ -687,6 +687,20 @@ int getVerticalLocation(Rect padding) {
687687
return gravity;
688688
}
689689

690+
/**
691+
* Rounds an orientation value to the nearest 90-degree increment.
692+
* This is used to detect significant orientation changes (portrait/landscape).
693+
*
694+
* The calculation uses integer division: ((orientation + 45) / 90) * 90
695+
* This rounds to the nearest multiple of 90 by adding 45 before dividing.
696+
*
697+
* @param orientation The orientation value in degrees (typically 0-359 from OrientationEventListener)
698+
* @return The orientation rounded to the nearest 90-degree increment (0, 90, 180, 270, or 360)
699+
*/
700+
static int roundToNearest90Degrees(int orientation) {
701+
return ((orientation + 45) / 90) * 90;
702+
}
703+
690704
/**
691705
* Sets the window gravity based on inset padding
692706
*

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,82 @@ public void testOrientationChange_PortraitToLandscape() throws Exception {
305305
// If runResizeScript wasn't called, we would have seen validation errors or exceptions
306306
// The fact that we get here without exceptions means the orientation change handling worked
307307
}
308+
309+
// ===== Orientation Rounding Tests =====
310+
311+
@Test
312+
public void testRoundToNearest90Degrees_Zero() {
313+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(0));
314+
}
315+
316+
@Test
317+
public void testRoundToNearest90Degrees_StandardOrientations() {
318+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(0));
319+
assertEquals(90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(90));
320+
assertEquals(180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(180));
321+
assertEquals(270, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(270));
322+
assertEquals(360, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(360));
323+
}
324+
325+
@Test
326+
public void testRoundToNearest90Degrees_BoundaryValues() {
327+
// Values that round down to 0 (0-44)
328+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(44));
329+
330+
// Values that round up to 90 (45-134)
331+
assertEquals(90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(45));
332+
assertEquals(90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(89));
333+
assertEquals(90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(90));
334+
assertEquals(90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(134));
335+
336+
// Values that round up to 180 (135-224)
337+
assertEquals(180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(135));
338+
assertEquals(180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(180));
339+
assertEquals(180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(224));
340+
341+
// Values that round up to 270 (225-314)
342+
assertEquals(270, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(225));
343+
assertEquals(270, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(270));
344+
assertEquals(270, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(314));
345+
346+
// Values that round up to 360 (315-359)
347+
assertEquals(360, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(315));
348+
assertEquals(360, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(359));
349+
}
350+
351+
@Test
352+
public void testRoundToNearest90Degrees_NearZero() {
353+
// Test values very close to 0
354+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(1));
355+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-1));
356+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-44));
357+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-45));
358+
}
359+
360+
@Test
361+
public void testRoundToNearest90Degrees_NegativeValues() {
362+
// Test negative values (though OrientationEventListener typically returns 0-359)
363+
// These test the integer division behavior with negative numbers
364+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-1));
365+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-45));
366+
assertEquals(-90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-46));
367+
assertEquals(-90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-90));
368+
assertEquals(-90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-134));
369+
assertEquals(-180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(-135));
370+
}
371+
372+
@Test
373+
public void testRoundToNearest90Degrees_EdgeCases() {
374+
// Test edge cases around boundaries
375+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(0));
376+
assertEquals(0, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(44));
377+
assertEquals(90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(45));
378+
assertEquals(90, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(134));
379+
assertEquals(180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(135));
380+
assertEquals(180, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(224));
381+
assertEquals(270, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(225));
382+
assertEquals(270, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(314));
383+
assertEquals(360, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(315));
384+
assertEquals(360, IterableInAppFragmentHTMLNotification.roundToNearest90Degrees(359));
385+
}
308386
}

0 commit comments

Comments
 (0)