Skip to content

Commit

Permalink
Wrap device details for devices that don't have them
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Feb 12, 2020
1 parent 98bc38e commit 8a34018
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public void when_scrollQuestionsList_should_questionsNotDisappear() {
.startBlankForm("3403_ODK Version 1.23.3 Tester")
.clickOnText("New Farmer Registration")
.scrollToAndClickText("Insemination")
.scrollToAndCheckIsDisplayed("New Farmer Registration");
.scrollToAndAssertText("New Farmer Registration");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;
Expand All @@ -14,19 +13,24 @@
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.odk.collect.android.activities.MainMenuActivity;
import org.odk.collect.android.injection.config.AppDependencyModule;
import org.odk.collect.android.metadata.SharedPreferencesInstallIDProvider;
import org.odk.collect.android.support.CollectHelpers;
import org.odk.collect.android.support.CopyFormRule;
import org.odk.collect.android.support.ResetStateRule;
import org.odk.collect.android.support.pages.GeneralSettingsPage;
import org.odk.collect.android.support.pages.MainMenuPage;
import org.odk.collect.android.support.pages.UserAndDeviceIdentitySettingsPage;
import org.odk.collect.android.utilities.DeviceDetailsProvider;

import static android.preference.PreferenceManager.getDefaultSharedPreferences;
import static org.odk.collect.android.preferences.GeneralKeys.KEY_INSTALL_ID;

@RunWith(AndroidJUnit4.class)
public class FormMetadataTest {

private final DeviceDetailsProvider deviceDetailsProvider = new FakeDeviceDetailsProvider();

@Rule
public RuleChain copyFormChain = RuleChain
.outerRule(GrantPermissionRule.grant(
Expand All @@ -38,7 +42,19 @@ public class FormMetadataTest {
.around(new CopyFormRule("metadata.xml"));

@Rule
public ActivityTestRule<MainMenuActivity> rule = new ActivityTestRule<>(MainMenuActivity.class);
public ActivityTestRule<MainMenuActivity> rule = new ActivityTestRule<MainMenuActivity>(MainMenuActivity.class) {
@Override
protected void beforeActivityLaunched() {
super.beforeActivityLaunched();

CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() {
@Override
public DeviceDetailsProvider providesDeviceDetailsProvider(Context context) {
return deviceDetailsProvider;
}
});
}
};

@Test
public void settingMetadata_letsThemBeIncludedInForm() {
Expand Down Expand Up @@ -70,28 +86,46 @@ public void deviceIdentifiersAreDisplayedInSettings() {
.clickGeneralSettings()
.clickUserAndDeviceIdentity()
.clickFormMetadata()
.assertText(getTelephonyManager().getDeviceId())
.assertText(getTelephonyManager().getSubscriberId())
.assertText(getTelephonyManager().getSimSerialNumber())
.assertText(deviceDetailsProvider.getDeviceId())
.assertText(deviceDetailsProvider.getSubscriberId())
.assertText(deviceDetailsProvider.getSimSerialNumber())
.assertText(getInstallID());
}

@Test
public void deviceIdentifiersCanBeIncludedInForm() {
new MainMenuPage(rule)
.startBlankForm("Metadata")
.assertText(getTelephonyManager().getDeviceId())
.assertText(getTelephonyManager().getSubscriberId())
.assertText(getTelephonyManager().getSimSerialNumber());
.scrollToAndAssertText(deviceDetailsProvider.getDeviceId())
.scrollToAndAssertText(deviceDetailsProvider.getSubscriberId())
.scrollToAndAssertText(deviceDetailsProvider.getSimSerialNumber());
}

private String getInstallID() {
SharedPreferences sharedPreferences = getDefaultSharedPreferences(rule.getActivity());
return new SharedPreferencesInstallIDProvider(sharedPreferences, KEY_INSTALL_ID).getInstallID();
}

private TelephonyManager getTelephonyManager() {
Context context = rule.getActivity();
return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
class FakeDeviceDetailsProvider implements DeviceDetailsProvider {

@Override
public String getDeviceId() {
return "deviceID";
}

@Override
public String getLine1Number() {
return "line1Number";
}

@Override
public String getSubscriberId() {
return "subscriberID";
}

@Override
public String getSimSerialNumber() {
return "simSerialNumber";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public T scrollToAndClickText(String text) {
return (T) this;
}

public T scrollToAndCheckIsDisplayed(String text) {
public T scrollToAndAssertText(String text) {
onView(withText(text)).perform(nestedScrollTo());
onView(withText(text)).check(matches(isDisplayed()));
return (T) this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.odk.collect.android.injection.config;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.webkit.MimeTypeMap;

import org.javarosa.core.reference.ReferenceManager;
Expand All @@ -26,6 +28,7 @@
import org.odk.collect.android.tasks.sms.contracts.SmsSubmissionManagerContract;
import org.odk.collect.android.utilities.ActivityAvailability;
import org.odk.collect.android.utilities.AndroidUserAgent;
import org.odk.collect.android.utilities.DeviceDetailsProvider;
import org.odk.collect.android.utilities.FormListDownloader;
import org.odk.collect.android.utilities.PermissionUtils;
import org.odk.collect.android.utilities.WebCredentialsUtils;
Expand Down Expand Up @@ -166,4 +169,36 @@ InstallIDProvider providesInstallIDProvider(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return new SharedPreferencesInstallIDProvider(preferences, KEY_INSTALL_ID);
}

@Provides
public DeviceDetailsProvider providesDeviceDetailsProvider(Context context) {
TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

return new DeviceDetailsProvider() {

@Override
@SuppressLint("MissingPermission")
public String getDeviceId() {
return telMgr.getDeviceId();
}

@Override
@SuppressLint("MissingPermission")
public String getLine1Number() {
return telMgr.getLine1Number();
}

@Override
@SuppressLint("MissingPermission")
public String getSubscriberId() {
return telMgr.getSubscriberId();
}

@Override
@SuppressLint("MissingPermission")
public String getSimSerialNumber() {
return telMgr.getSimSerialNumber();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import org.javarosa.core.services.IPropertyManager;
import org.javarosa.core.services.properties.IPropertyRules;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.events.ReadPhoneStatePermissionRxEvent;
import org.odk.collect.android.events.RxEventBus;
import org.odk.collect.android.utilities.DeviceDetailsProvider;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -71,6 +71,9 @@ public class PropertyManager implements IPropertyManager {
@Inject
RxEventBus eventBus;

@Inject
DeviceDetailsProvider deviceDetailsProvider;

public String getName() {
return "Property Manager";
}
Expand All @@ -91,12 +94,11 @@ public PropertyManager(Context context) {
Collect.getInstance().getComponent().inject(this);
try {
// Device-defined properties
TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
IdAndPrefix idp = findDeviceId(context, telMgr);
IdAndPrefix idp = findDeviceId(context, deviceDetailsProvider);
putProperty(PROPMGR_DEVICE_ID, idp.prefix, idp.id);
putProperty(PROPMGR_PHONE_NUMBER, SCHEME_TEL, telMgr.getLine1Number());
putProperty(PROPMGR_SUBSCRIBER_ID, SCHEME_IMSI, telMgr.getSubscriberId());
putProperty(PROPMGR_SIM_SERIAL, SCHEME_SIMSERIAL, telMgr.getSimSerialNumber());
putProperty(PROPMGR_PHONE_NUMBER, SCHEME_TEL, deviceDetailsProvider.getLine1Number());
putProperty(PROPMGR_SUBSCRIBER_ID, SCHEME_IMSI, deviceDetailsProvider.getSubscriberId());
putProperty(PROPMGR_SIM_SERIAL, SCHEME_SIMSERIAL, deviceDetailsProvider.getSimSerialNumber());
} catch (SecurityException e) {
Timber.e(e);
}
Expand All @@ -109,9 +111,9 @@ public PropertyManager(Context context) {
}

// telephonyManager.getDeviceId() requires permission READ_PHONE_STATE (ISSUE #2506). Permission should be handled or exception caught.
private IdAndPrefix findDeviceId(Context context, TelephonyManager telephonyManager) throws SecurityException {
private IdAndPrefix findDeviceId(Context context, DeviceDetailsProvider deviceDetailsProvider) throws SecurityException {
final String androidIdName = Settings.Secure.ANDROID_ID;
String deviceId = telephonyManager.getDeviceId();
String deviceId = deviceDetailsProvider.getDeviceId();
String scheme = null;

if (deviceId != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.odk.collect.android.utilities;

public interface DeviceDetailsProvider {

@Deprecated
String getDeviceId();

String getLine1Number();

String getSubscriberId();

String getSimSerialNumber();
}

0 comments on commit 8a34018

Please sign in to comment.