Siren { public void checkVersion(Activity activity, SirenVersionCheckType versionCheckType, String appDescriptionUrl) { mActivityRef = new WeakReference<>(activity); if (getSirenHelper().isEmpty(appDescriptionUrl)) { getSirenHelper().logError(getClass().getSimpleName(), "Please make sure you set correct path to app version description document"); return; } if (versionCheckType == SirenVersionCheckType.IMMEDIATELY) { performVersionCheck(appDescriptionUrl); } else if (versionCheckType.getValue() <= getSirenHelper().getDaysSinceLastCheck(mApplicationContext) ||getSirenHelper().getLastVerificationDate(mApplicationContext) == 0) { performVersionCheck(appDescriptionUrl); } } @VisibleForTesting protected  Siren(); static Siren getInstance(Context context); void checkVersion(Activity activity, SirenVersionCheckType versionCheckType, String appDescriptionUrl); void setMajorUpdateAlertType(@SuppressWarnings("SameParameterValue") SirenAlertType majorUpdateAlertType); void setMinorUpdateAlertType(SirenAlertType minorUpdateAlertType); void setPatchUpdateAlertType(SirenAlertType patchUpdateAlertType); void setRevisionUpdateAlertType(SirenAlertType revisionUpdateAlertType); void setSirenListener(ISirenListener sirenListener); void setVersionCodeUpdateAlertType(SirenAlertType versionCodeUpdateAlertType); void setLanguageLocalization(SirenSupportedLocales localization);  }
@Test public void onCheckVersion_noExceptionsShouldBeThrown() { siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); }
@Test public void onVersionNameSkipped_dialogShouldNotBeShownFurther() { siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); Mockito.when(sirenHelper.isVersionSkippedByUser(Mockito.any(Context.class), Mockito.anyString())).thenReturn(true); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); }
@Test public void onVersionCodeSkipped_dialogShouldNotBeShownFurther() { siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); mockResult(TestConstants.jsonVersionCodeUpdate); Mockito.when(sirenHelper.isVersionSkippedByUser(Mockito.any(Context.class), Mockito.anyString())).thenReturn(true); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); }
@Test public void onVersionNameCountNotEquals_verificationShouldBeSkipped() { Mockito.when(sirenHelper.getVersionName(activity)).thenReturn("1.1.1"); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.never()).show(); }
@Test public void onEmptyJsonUrl_warningShouldBePrintedToTheLog() { siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, ""); Mockito.verify(sirenHelper, Mockito.times(1)).logError(Mockito.anyString(), Mockito.anyString()); }
@Test public void onImmediateVerification_versionCheckShouldBePerformedEveryTime() { siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(2)).show(); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(3)).show(); }
@Test public void onDailyVerification_versionCheckShouldBePerformedOnceADay() { Mockito.when(sirenHelper.getDaysSinceLastCheck(activity)).thenReturn(0); siren.checkVersion(activity, SirenVersionCheckType.DAILY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); siren.checkVersion(activity, SirenVersionCheckType.DAILY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); Mockito.when(sirenHelper.getDaysSinceLastCheck(activity)).thenReturn(1); siren.checkVersion(activity, SirenVersionCheckType.DAILY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(2)).show(); }
@Test public void onWeeklyVerificationVersionCheckShouldBePerformedOnceAWeek() { Mockito.when(sirenHelper.getDaysSinceLastCheck(activity)).thenReturn(0); siren.checkVersion(activity, SirenVersionCheckType.WEEKLY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); siren.checkVersion(activity, SirenVersionCheckType.WEEKLY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); Mockito.when(sirenHelper.getDaysSinceLastCheck(activity)).thenReturn(5); siren.checkVersion(activity, SirenVersionCheckType.WEEKLY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); Mockito.when(sirenHelper.getDaysSinceLastCheck(activity)).thenReturn(7); siren.checkVersion(activity, SirenVersionCheckType.WEEKLY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(2)).show(); }
@Test public void onMalformedJson_verificationShouldBeIgnored() { mockResult(TestConstants.jsonMalformed); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.never()).show(); mockResult(TestConstants.jsonMalformed2); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.never()).show(); mockResult(TestConstants.jsonMalformed3); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.never()).show(); }
@Test public void onVersionCodeUpdate_dialogShouldBeDisplayed() { mockResult(TestConstants.jsonVersionCodeUpdate); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(1)).show(); }
@Test public void onOutdatedVersionCode_dialogShouldNotBeShown() { mockResult(TestConstants.jsonVersionCodeOutdated); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.never()).show(); }
@Test public void onVersionNameUpdate_dialogShouldBeDisplayed() { mockResult(TestConstants.jsonVersionNameMajorUpdate); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); mockResult(TestConstants.jsonVersionNameMinorUpdate); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); mockResult(TestConstants.jsonVersionNamePatchUpdate); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); mockResult(TestConstants.jsonVersionNameRevisionUpdate); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.times(4)).show(); }
@Test public void onOutdatedVersionName_dialogShouldNotBeDisplayed() { mockResult(TestConstants.jsonVersionNameOutdated); siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); Mockito.verify(alertWrapper, Mockito.never()).show(); }