با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
پس از بهروزرسانی برنامهتان برای پشتیبانی از ارسال بازخورد در قالب حالتهای برنامه کلیددار، میتوانید از راهنمایی در این صفحه برای تنظیم تستهای واحد و ارسال بازخورد آزمایشی به کنترلکننده خطمشی دستگاه آزمایشی (DPC) استفاده کنید.
تست های واحد را تنظیم کنید
این بخش نمونههایی از نحوه راهاندازی تستهای واحد را برای بررسی اینکه آیا برنامه شما مطابق انتظار با حالتهای برنامه کلیددار تعامل دارد، ارائه میدهد.
مرحله 1: کلاس های خود را طوری تنظیم کنید که KeyedAppStatesReporter را به عنوان یک پارامتر بپذیرد
به جای فراخوانی مستقیم create() ، کلاسهای خود را تغییر دهید تا KeyedAppStatesReporter به عنوان پارامتری مانند مثال کلاس BatteryManager در زیر بپذیرد:
کاتلین
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 ایجاد شود، یک نمونه ارسال شود.
مرحله 2: کتابخانه تست بازخورد سازمانی را به فایل build.gradle خود اضافه کنید
وابستگی زیر را به فایل build.gradle برنامه خود اضافه کنید:
در منوی تست DPC، اعلانهای بازخورد برنامه را فعال کنید.
رویدادی را راهاندازی کنید که حالت برنامه کلیدی را تنظیم کند. در صورت موفقیت آمیز بودن، تست DPC بازخورد را در اعلان ها نشان می دهد:
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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:"]]