Configurare i test delle unità
Questa sezione fornisce esempi di come configurare i test delle unità per verificare che la tua app interagisca con gli stati dell'app con chiave come previsto.
Passaggio 1: configura i tuoi corsi in modo che accettino KeyedAppStatesReporter
come parametro
Anziché chiamare direttamente create()
, modifica le tue classi per accettare KeyedAppStatesReporter
come parametro, come nell'esempio della classe BatteryManager
riportata di seguito:
Kotlin
class BatteryManager(val reporter:KeyedAppStatesReporter) { fun lowBattery(battery:Int) { reporter.setStatesImmediate( hashSetOf(KeyedAppState.builder() .setKey("battery") .setSeverity(KeyedAppState.SEVERITY_INFO) .setMessage("Battery is low") .setData(battery.toString()) .build())) } }
Java
public class BatteryManager { private final KeyedAppStatesReporter reporter; public BatteryManager(KeyedAppStatesReporter reporter) { this.reporter = reporter; } public void lowBattery(int battery) { final Collectionstates = new HashSet<>(); states.add(KeyedAppState.builder() .setKey("battery") .setSeverity(KeyedAppState.SEVERITY_INFO) .setMessage("Battery is low") .setData(Integer.toString(battery)) .build(); reporter.setStatesImmediate(states); } }
Quindi, utilizza KeyedAppStatesReporter.create
per ottenere un'istanza da passare
ovunque venga creato BatteryManager
.
Passaggio 2: aggiungi la libreria di test del feedback aziendale al file build.gradle
Aggiungi la seguente dipendenza al file build.gradle
della tua app:
dependencies { testImplementation 'androidx.enterprise:enterprise-feedback-testing:1.0.0' }
Passaggio 3: crea un FakeKeyedAppStatesReporter
e trasmettilo al corso
Kotlin
val reporter = FakeKeyedAppStatesReporter(); val batteryManager = BatteryManager(reporter);
Java
FakeKeyedAppStatesReporter reporter = new FakeKeyedAppStatesReporter(); BatteryManager batteryManager = new BatteryManager(reporter);
Passaggio 4: dichiara le interazioni con FakeKeyedAppStatesReporter
Ad esempio, per verificare che non sia stato impostato alcuno stato:
Kotlin
assertThat(reporter.keyedAppStates).isEmpty();
Java
assertThat(reporter.getKeyedAppStates()).isEmpty();
Oppure che è stato richiesto di caricare un determinato stato:
Kotlin
assertThat(reporter.uploadedKeyedAppStatesByKey["battery"]).isNotNull()
Java
assertThat(reporter.getUploadedKeyedAppStatesByKey().get("battery")).isNotNull();
Invia feedback di test al DPC di test
Un controller dei criteri dei dispositivi di esempio, chiamato DPC Test, è in grado di ricevere feedback sull'app ed è disponibile per il download.
Passaggio 1: installa il DPC di prova
Installa la versione più recente del Test DPC dal Play Store. Successivamente, imposta DPC di prova come amministratore del dispositivo:
adb shell dpm set-device-owner com.afwsamples.testdpc/.DeviceAdminReceiver
Passaggio 2: attiva le notifiche di feedback delle app
Nel menu di Test DPC, attiva Notifiche di feedback app.
Attiva un evento che imposta uno stato dell'app con chiave. In caso di esito positivo, Test DPC mostrerà il feedback nelle notifiche: