Skip to content

Commit

Permalink
fix(#376): fix issue of OpenSettingsDialogFragment class not being ab…
Browse files Browse the repository at this point in the history
…le to find calling Activity by saving the state of the Fragment (#378)
  • Loading branch information
sugat009 authored Oct 23, 2024
1 parent 1ce2a9c commit 91ff670
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public boolean onTouch(View view, MotionEvent event) {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
View view = getActivity().findViewById(R.id.wbvMain);
view.setOnTouchListener(onTouchListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
Expand Down Expand Up @@ -229,4 +230,60 @@ public void onTouch_withTapTimeout_doesNotOpenSettingsDialog() {
verify(activity, never()).finish();
}
}

@Test
public void onCreate_setsRetainInstanceTrue_preservesStateAfterRecreation() {
//> GIVEN
Bundle savedState = new Bundle();
MotionEvent eventTap = mock(MotionEvent.class);
when(eventTap.getPointerCount()).thenReturn(1);
when(eventTap.getActionMasked()).thenReturn(MotionEvent.ACTION_DOWN);

// First creation
openSettingsDialogFragment.onCreate(savedState);
OnTouchListener firstListener = argsOnTouch.getValue();
// Set up initial clock mock
Clock initialTime = Clock.fixed(Instant.ofEpochMilli(1000), ZoneOffset.UTC);
// Update clock time for subsequent taps
Clock laterTime = Clock.fixed(Instant.ofEpochMilli(1200), ZoneOffset.UTC);

try (MockedStatic<Clock> mockClock = mockStatic(Clock.class)) {
mockClock.when(Clock::systemUTC).thenReturn(initialTime);

// Simulate initial taps
tap(firstListener, eventTap, 3);

// Simulate fragment recreation (e.g., due to configuration change)
Activity newActivity = mock(Activity.class, RETURNS_SMART_NULLS);
View newView = mock(View.class);
ArgumentCaptor<OnTouchListener> newArgsOnTouch = ArgumentCaptor.forClass(OnTouchListener.class);
doNothing().when(newView).setOnTouchListener(newArgsOnTouch.capture());
when(openSettingsDialogFragment.getActivity()).thenReturn(newActivity);
when(newActivity.findViewById(R.id.wbvMain)).thenReturn(newView);

//> WHEN
openSettingsDialogFragment.onCreate(savedState);
OnTouchListener recreatedListener = newArgsOnTouch.getValue();

mockClock.when(Clock::systemUTC).thenReturn(laterTime);

// Continue taps after recreation
tap(recreatedListener, eventTap, 3);

// Try to trigger settings with swipe
MotionEvent eventSwipe = mock(MotionEvent.class);
when(eventSwipe.getPointerCount()).thenReturn(2);

when(eventSwipe.getActionMasked()).thenReturn(MotionEvent.ACTION_POINTER_DOWN);
positionPointers(recreatedListener, eventSwipe, (float) 261.81, (float) 264.99);

when(eventSwipe.getActionMasked()).thenReturn(MotionEvent.ACTION_MOVE);
positionPointers(recreatedListener, eventSwipe, (float) 800.90, (float) 850.13);

//> THEN
Intent intent = argsStartActivity.getValue();
assertEquals(SettingsDialogActivity.class.getName(), intent.getComponent().getClassName());
verify(newActivity).finish();
}
}
}

0 comments on commit 91ff670

Please sign in to comment.