-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #624 from OpenSRP/kigamba-tt-11-08-2020
Add tests for SettingsSyncedCheck
- Loading branch information
Showing
10 changed files
with
285 additions
and
22 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
opensrp-app/src/test/java/org/smartregister/TestP2pApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.smartregister; | ||
|
||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.repository.AllSharedPreferences; | ||
|
||
import static android.preference.PreferenceManager.getDefaultSharedPreferences; | ||
|
||
/** | ||
* Created by Ephraim Kigamba - nek.eam@gmail.com on 18-08-2020. | ||
*/ | ||
public class TestP2pApplication extends TestApplication { | ||
|
||
@Override | ||
public void onCreate() { | ||
mInstance = this; | ||
context = Context.getInstance(); | ||
context.updateApplicationContext(getApplicationContext()); | ||
|
||
AllSharedPreferences allSharedPreferences = new AllSharedPreferences(getDefaultSharedPreferences(context.applicationContext())); | ||
allSharedPreferences.updateANMUserName("demo"); | ||
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); | ||
CoreLibrary.init(context, null, 1588062490000l, new P2POptions(true)); | ||
|
||
|
||
setTheme(R.style.Theme_AppCompat_NoActionBar); //or just R.style.Theme_AppCompat | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
opensrp-app/src/test/java/org/smartregister/multitenant/check/SettingsSyncedCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.smartregister.multitenant.check; | ||
|
||
import org.json.JSONException; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.BaseRobolectricUnitTest; | ||
import org.smartregister.DristhiConfiguration; | ||
import org.smartregister.exception.PreResetAppOperationException; | ||
import org.smartregister.repository.AllSettings; | ||
import org.smartregister.shadows.ShadowSyncSettingsServiceHelper; | ||
import org.smartregister.view.activity.DrishtiApplication; | ||
|
||
/** | ||
* Created by Ephraim Kigamba - nek.eam@gmail.com on 11-08-2020. | ||
*/ | ||
public class SettingsSyncedCheckTest extends BaseRobolectricUnitTest { | ||
|
||
public SettingsSyncedCheck settingsSyncedCheck; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
settingsSyncedCheck = Mockito.spy(new SettingsSyncedCheck()); | ||
} | ||
|
||
@Test | ||
public void isCheckOk() { | ||
Mockito.doReturn(true).when(settingsSyncedCheck).isSettingsSynced(DrishtiApplication.getInstance()); | ||
|
||
settingsSyncedCheck.isCheckOk(DrishtiApplication.getInstance()); | ||
|
||
Mockito.verify(settingsSyncedCheck).isSettingsSynced(DrishtiApplication.getInstance()); | ||
} | ||
|
||
@Config(shadows = {ShadowSyncSettingsServiceHelper.class}) | ||
@Test | ||
public void performPreResetAppOperations() throws PreResetAppOperationException, JSONException { | ||
DristhiConfiguration dristhiConfiguration = Mockito.spy(DrishtiApplication.getInstance().getContext().configuration()); | ||
Mockito.doReturn("https://someurl.com").when(dristhiConfiguration).dristhiBaseURL(); | ||
ReflectionHelpers.setField(DrishtiApplication.getInstance().getContext(), "configuration", dristhiConfiguration); | ||
|
||
settingsSyncedCheck.performPreResetAppOperations(DrishtiApplication.getInstance()); | ||
|
||
Assert.assertEquals(1, ShadowSyncSettingsServiceHelper.processIntent); | ||
} | ||
|
||
@Test | ||
public void isSettingsSynced() { | ||
AllSettings allSettings = Mockito.spy(DrishtiApplication.getInstance().getContext().allSettings()); | ||
ReflectionHelpers.setField(DrishtiApplication.getInstance().getContext(), "allSettings", allSettings); | ||
|
||
Assert.assertTrue(settingsSyncedCheck.isSettingsSynced(DrishtiApplication.getInstance())); | ||
|
||
Mockito.verify(allSettings).getUnsyncedSettingsCount(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
opensrp-app/src/test/java/org/smartregister/shadows/ShadowSyncSettingsServiceHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.smartregister.shadows; | ||
|
||
import org.json.JSONException; | ||
import org.mockito.Mockito; | ||
import org.robolectric.annotation.Implementation; | ||
import org.robolectric.annotation.Implements; | ||
import org.robolectric.annotation.RealObject; | ||
import org.robolectric.shadow.api.Shadow; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.CoreLibrary; | ||
import org.smartregister.repository.AllSharedPreferences; | ||
import org.smartregister.service.HTTPAgent; | ||
import org.smartregister.sync.helper.SyncSettingsServiceHelper; | ||
|
||
/** | ||
* Created by Ephraim Kigamba - nek.eam@gmail.com on 11-08-2020. | ||
*/ | ||
@Implements(SyncSettingsServiceHelper.class) | ||
public class ShadowSyncSettingsServiceHelper { | ||
|
||
private static SyncSettingsServiceHelper instance; | ||
|
||
@RealObject | ||
private SyncSettingsServiceHelper realObject; | ||
|
||
public static int processIntent; | ||
|
||
@Implementation | ||
public SyncSettingsServiceHelper __constructor__(String baseUrl, HTTPAgent httpAgent) { | ||
|
||
ReflectionHelpers.setField(realObject, "httpAgent", httpAgent); | ||
ReflectionHelpers.setField(realObject, "baseUrl", baseUrl); | ||
AllSharedPreferences sharedPreferences = CoreLibrary.getInstance().context().allSharedPreferences(); | ||
ReflectionHelpers.setField(realObject, "sharedPreferences", sharedPreferences); | ||
|
||
realObject = Mockito.spy(realObject); | ||
instance = realObject; | ||
|
||
return realObject; | ||
} | ||
|
||
public static SyncSettingsServiceHelper getLastInstance() { | ||
return instance; | ||
} | ||
|
||
@Implementation | ||
public int processIntent() throws JSONException { | ||
processIntent++; | ||
return Shadow.directlyOn(instance, SyncSettingsServiceHelper.class).processIntent(); | ||
} | ||
|
||
} |
Oops, something went wrong.