InAppMessageManager { @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) public void onPrepare(final @NonNull String scheduleId, final @NonNull InAppMessage inAppMessage, final @NonNull AutomationDriver.PrepareScheduleCallback callback) { final AdapterWrapper adapter = createAdapterWrapper(scheduleId, inAppMessage); if (adapter == null) { callback.onFinish(AutomationDriver.PREPARE_RESULT_PENALIZE); return; } RetryingExecutor.Operation prepareAssets = new RetryingExecutor.Operation() { @Override public int run() { int result = assetManager.onPrepare(scheduleId, adapter.message); switch (result) { case AssetManager.PREPARE_RESULT_OK: Logger.debug("InAppMessageManager - Assets prepared for schedule %s.", scheduleId); return RetryingExecutor.RESULT_FINISHED; case AssetManager.PREPARE_RESULT_RETRY: Logger.debug("InAppMessageManager - Assets failed to prepare for schedule %s. Will retry.", scheduleId); return RetryingExecutor.RESULT_RETRY; case AssetManager.PREPARE_RESULT_CANCEL: default: Logger.debug("InAppMessageManager - Assets failed to prepare. Cancelling display for schedule %s.", scheduleId); assetManager.onDisplayFinished(scheduleId, adapter.message); callback.onFinish(AutomationDriver.PREPARE_RESULT_CANCEL); return RetryingExecutor.RESULT_CANCEL; } } }; RetryingExecutor.Operation prepareAdapter = new RetryingExecutor.Operation() { @Override public int run() { int result = adapter.prepare(context, assetManager.getAssets(scheduleId)); switch (result) { case InAppMessageAdapter.OK: Logger.debug("InAppMessageManager - Adapter prepared schedule %s.", scheduleId); adapterWrappers.put(scheduleId, adapter); callback.onFinish(AutomationDriver.PREPARE_RESULT_CONTINUE); return RetryingExecutor.RESULT_FINISHED; case InAppMessageAdapter.RETRY: Logger.debug("InAppMessageManager - Adapter failed to prepare schedule %s. Will retry.", scheduleId); return RetryingExecutor.RESULT_RETRY; case InAppMessageAdapter.CANCEL: default: Logger.debug("InAppMessageManager - Adapter failed to prepare. Cancelling display for schedule %s.", scheduleId); callback.onFinish(AutomationDriver.PREPARE_RESULT_CANCEL); return RetryingExecutor.RESULT_CANCEL; } } }; executor.execute(prepareAssets, prepareAdapter); } @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)  InAppMessageManager(@NonNull Context context,
                               @NonNull PreferenceDataStore dataStore,
                               @NonNull Analytics analytics,
                               @NonNull Delegate delegate); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)  InAppMessageManager(@NonNull Context context,
                        @NonNull PreferenceDataStore dataStore,
                        @NonNull Analytics analytics,
                        @NonNull RetryingExecutor executor,
                        @NonNull ActionRunRequestFactory runRequestFactory,
                        @NonNull AssetManager assetManager,
                        @NonNull Delegate delegate); void setAdapterFactory(@NonNull @InAppMessage.DisplayType String displayType, @Nullable InAppMessageAdapter.Factory factory); void setDisplayInterval(@IntRange(from = 0) long time, @NonNull TimeUnit timeUnit); long getDisplayInterval(); @NonNull AssetManager getAssetManager(); void addListener(@NonNull InAppMessageListener listener); void removeListener(@NonNull InAppMessageListener listener); void setMessageExtender(@Nullable InAppMessageExtender extender); void setOnRequestDisplayCoordinatorCallback(@Nullable OnRequestDisplayCoordinatorCallback callback); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void onAirshipReady(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void onPrepare(final @NonNull String scheduleId,
                          final @NonNull InAppMessage inAppMessage,
                          final @NonNull AutomationDriver.PrepareScheduleCallback callback); @MainThread @AutomationDriver.ReadyResult @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) int onCheckExecutionReadiness(@NonNull String scheduleId); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @MainThread void onExecute(@NonNull String scheduleId, @NonNull AutomationDriver.ExecutionCallback callback); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void onExecutionInvalidated(@NonNull final String scheduleId); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void onMessageScheduleFinished(@NonNull final String scheduleId); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void onNewMessageSchedule(@NonNull final String scheduleId, @NonNull final InAppMessage message); static final long DEFAULT_DISPLAY_INTERVAL_MS; }
@Test public void testRetryAdapterPrepareMessage() { when(mockAssetManager.onPrepare(scheduleId, message)).thenReturn(AssetManager.PREPARE_RESULT_OK); when(mockAdapter.isReady(any(Context.class))).thenReturn(true); when(mockAdapter.onPrepare(any(Context.class), any(Assets.class))).thenReturn(InAppMessageAdapter.RETRY); AutomationDriver.PrepareScheduleCallback mockPrepareCallback = mock(AutomationDriver.PrepareScheduleCallback.class); manager.onPrepare(scheduleId, message, mockPrepareCallback); verify(mockAdapter).onPrepare(any(Context.class), any(Assets.class)); verify(mockAdapter, times(1)).onPrepare(any(Context.class), any(Assets.class)); Looper mainLooper = Looper.getMainLooper(); Shadows.shadowOf(mainLooper).runToEndOfTasks(); verify(mockAdapter, times(2)).onPrepare(any(Context.class), any(Assets.class)); verifyZeroInteractions(mockPrepareCallback); }
@Test public void testRetryAssetsPrepare() { when(mockAssetManager.onPrepare(scheduleId, message)).thenReturn(AssetManager.PREPARE_RESULT_RETRY); AutomationDriver.PrepareScheduleCallback mockPrepareCallback = mock(AutomationDriver.PrepareScheduleCallback.class); manager.onPrepare(scheduleId, message, mockPrepareCallback); verify(mockAssetManager, times(1)).onPrepare(scheduleId, message); Looper mainLooper = Looper.getMainLooper(); Shadows.shadowOf(mainLooper).runToEndOfTasks(); verify(mockAssetManager, times(2)).onPrepare(scheduleId, message); verifyZeroInteractions(mockPrepareCallback); }
@Test public void testCancelAdapterPrepare() { when(mockAssetManager.onPrepare(scheduleId, message)).thenReturn(AssetManager.PREPARE_RESULT_OK); when(mockAdapter.onPrepare(any(Context.class), any(Assets.class))).thenReturn(InAppMessageAdapter.CANCEL); AutomationDriver.PrepareScheduleCallback mockPrepareCallback = mock(AutomationDriver.PrepareScheduleCallback.class); manager.onPrepare(scheduleId, message, mockPrepareCallback); verify(mockAdapter).onPrepare(any(Context.class), any(Assets.class)); verify(mockAdapter, times(1)).onPrepare(any(Context.class), any(Assets.class)); verify(mockPrepareCallback).onFinish(AutomationDriver.PREPARE_RESULT_CANCEL); }
@Test public void testCancelAssetsPrepare() { when(mockAssetManager.onPrepare(scheduleId, message)).thenReturn(AssetManager.PREPARE_RESULT_CANCEL); when(mockAdapter.onPrepare(any(Context.class), any(Assets.class))).thenReturn(InAppMessageAdapter.OK); AutomationDriver.PrepareScheduleCallback mockPrepareCallback = mock(AutomationDriver.PrepareScheduleCallback.class); manager.onPrepare(scheduleId, message, mockPrepareCallback); verify(mockAssetManager, times(1)).onPrepare(scheduleId, message); verify(mockPrepareCallback).onFinish(AutomationDriver.PREPARE_RESULT_CANCEL); }