키 앱 상태 형태로 의견을 보낼 수 있도록 앱을 업데이트한 후에는 다음을 사용할 수 있습니다.
단위 테스트를 설정하고 테스트 기기 정책에 테스트 의견을 보내는 방법에 관한 이 페이지의 안내
컨트롤러 (DPC)
단위 테스트 설정
이 섹션에서는 단위 테스트를 설정하여 앱이
확인합니다.
1단계: KeyedAppStatesReporter를 매개변수로 허용하도록 클래스 설정
create()를 직접 호출하는 대신 수락하도록 클래스를 수정합니다.
매개변수인 KeyedAppStatesReporter(BatteryManager 예)
클래스를 참조하세요.
Kotlin
classBatteryManager(valreporter:KeyedAppStatesReporter){funlowBattery(battery:Int){reporter.setStatesImmediate(hashSetOf(KeyedAppState.builder().setKey("battery").setSeverity(KeyedAppState.SEVERITY_INFO).setMessage("Battery is low").setData(battery.toString()).build()))}}
자바
publicclassBatteryManager{privatefinalKeyedAppStatesReporterreporter;publicBatteryManager(KeyedAppStatesReporterreporter){this.reporter=reporter;}publicvoidlowBattery(intbattery){finalCollectionstates=newHashSet<>();states.add(KeyedAppState.builder().setKey("battery").setSeverity(KeyedAppState.SEVERITY_INFO).setMessage("Battery is low").setData(Integer.toString(battery)).build();reporter.setStatesImmediate(states);}}
다음으로 KeyedAppStatesReporter.create를 사용하여
BatteryManager가 생성되는 모든 위치
키가 있는 앱 상태를 설정하는 이벤트를 트리거합니다. 성공하면 Test DPC가 표시됩니다.
알림의 피드백:
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-26(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-26(UTC)"],[],[],null,["# Test app feedback\n\n| **Note:** To learn more about how to send feedback from your app, see [Send feedback to EMMs](/work/app-feedback/overview).\nAfter updating your app to support sending feedback in the form of keyed app states, you can use the guidance on this page to set up unit tests and send test feedback to a test device policy controller (DPC).\n\nSet up unit tests\n-----------------\n\nThis section provides examples of how to set up unit tests to check that your app interacts with\nkeyed app states as expected.\n\n### Step 1: Set up your classes to accept `KeyedAppStatesReporter` as a parameter\n\nInstead of calling `create()` directly, modify your classes to accept `KeyedAppStatesReporter` as a parameter like in the example `BatteryManager` class below: \n\n### Kotlin\n\n```kotlin\nclass BatteryManager(val reporter:KeyedAppStatesReporter) {\n fun lowBattery(battery:Int) {\n reporter.setStatesImmediate(\n hashSetOf(KeyedAppState.builder()\n .setKey(\"battery\")\n .setSeverity(KeyedAppState.SEVERITY_INFO)\n .setMessage(\"Battery is low\")\n .setData(battery.toString())\n .build()))\n }\n}\n```\n\n### Java\n\n```java\npublic class BatteryManager {\n private final KeyedAppStatesReporter reporter;\n public BatteryManager(KeyedAppStatesReporter reporter) {\n this.reporter = reporter;\n }\n\n public void lowBattery(int battery) {\n final Collection states = new HashSet\u003c\u003e();\n states.add(KeyedAppState.builder()\n .setKey(\"battery\")\n .setSeverity(KeyedAppState.SEVERITY_INFO)\n .setMessage(\"Battery is low\")\n .setData(Integer.toString(battery))\n .build();\n reporter.setStatesImmediate(states);\n }\n}\n```\n\nNext, use `KeyedAppStatesReporter.create` to get an instance to pass\nwherever `BatteryManager` is created.\n\n### Step 2: Add the enterprise feedback testing library to your `build.gradle` file\n\nAdd the following dependency to your app's\n[`build.gradle`](/studio/build#module-level) file: \n\n```\ndependencies {\n testImplementation 'androidx.enterprise:enterprise-feedback-testing:1.0.0'\n}\n```\n\n### Step 3: Create a `FakeKeyedAppStatesReporter` and pass it into your class\n\n### Kotlin\n\n```kotlin\nval reporter = FakeKeyedAppStatesReporter();\nval batteryManager = BatteryManager(reporter);\n```\n\n### Java\n\n```java\nFakeKeyedAppStatesReporter reporter = new FakeKeyedAppStatesReporter();\nBatteryManager batteryManager = new BatteryManager(reporter);\n```\n\n### Step 4: Assert interactions with `FakeKeyedAppStatesReporter`\n\n| **Note:** To view all the options for verifying expected interactions, see [`FakeKeyedAppStatesReporter`](https://developer.android.google.cn/reference/androidx/enterprise/feedback/FakeKeyedAppStatesReporter).\n\nFor example, to check that no states have been set: \n\n### Kotlin\n\n```kotlin\nassertThat(reporter.keyedAppStates).isEmpty();\n```\n\n### Java\n\n```java\nassertThat(reporter.getKeyedAppStates()).isEmpty();\n```\n\nOr that a particular state has been requested to be uploaded: \n\n### Kotlin\n\n```kotlin\nassertThat(reporter.uploadedKeyedAppStatesByKey[\"battery\"]).isNotNull()\n```\n\n### Java\n\n```java\nassertThat(reporter.getUploadedKeyedAppStatesByKey().get(\"battery\")).isNotNull();\n```\n\nSend test feedback to Test DPC\n------------------------------\n\nA sample [device policy controller](https://developers.google.com/android/work/terminology#device_policy_controller_dpc),\ncalled Test DPC, is capable of receiving app feedback and is available for\ndownload.\n\n### Step 1: Install Test DPC\n\nInstall the latest version of [Test DPC](https://play.google.com/store/apps/details?id=com.afwsamples.testdpc)\nfrom the Play Store. Next, set Test DPC as the admin of the device: \n\n```\nadb shell dpm set-device-owner com.afwsamples.testdpc/.DeviceAdminReceiver\n```\n\n### Step 2: Enable App feedback notifications\n\nIn Test DPC's menu, enable **App feedback notifications**.\n\nTrigger an event that set a keyed app state. If successful, Test DPC will show\nthe feedback in notifications:"]]