با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
API تست واحد Glance به شما این امکان را می دهد که کد Glance خود را بدون افزایش نماها یا نیاز به خودکار UI آزمایش کنید. برای مثال، واحد تست API به شما امکان میدهد با استفاده از تطبیقهایی مانند hasContentDescriptionEqualTo یا isChecked ، شرایط را تأیید کنید، مانند اینکه آیا عناصر در یک لیست هستند یا کادرها علامت زده شدهاند.
این API سبک است و به تنظیمات کمتری نیاز دارد، بنابراین میتوانید همزمان با توسعه تکههای ویجت خود و سازماندهی آنها برای بهبود استفاده مجدد از کد، توسعه آزمایشی را انجام دهید.
راه اندازی
وابستگی های مورد نیاز برای استفاده از کتابخانه آزمون واحد در مثال های زیر نشان داده شده است:
// Other Glance and Compose runtime dependencies....testImplementation'androidx.glance:glance-testing:1.1.1'testImplementation'androidx.glance:glance-appwidget-testing:1.1.1'testImplementation'org.robolectric:robolectric:4.11.1'...// You may include additional dependencies, such as Robolectric, if your test// needs to set a LocalContext.
ساختار تست
برای فعال کردن استفاده مجدد کد و تست واحد، توابع قابل ترکیب را خارج از کلاس GlanceAppWidget سازماندهی کنید. پیچیدگی واحدهای تحت آزمایش خود را تا حد امکان کاهش دهید.
classMyGlanceComposableTest{@TestfunmyNewsItemComposable_largeSize_hasAuthorAsSubtitle()=runGlanceAppWidgetUnitTest{// Prepare inputs and statesetAppWidgetSize(100.dp,100.dp)// Set the composable under testprovideComposable{MyNewsItemComposable(TEST_NEWS_ITEM)}// Perform assertionsonNode(hasTestTag("subTitle")).assertHasText(TEST_NEWS_ITEM.authorName)}}
زمینه و اندازه را برای آزمون تنظیم کنید
اگر تابع composable شما متن را با استفاده از متد LocalContext.current() می خواند، باید یک متن را با استفاده از setContext() تنظیم کنید. در غیر این صورت این مرحله اختیاری است.
شما می توانید از هر چارچوب تست واحد اندروید مبتنی بر JVM، مانند Roboletric، برای ارائه زمینه استفاده کنید.
اگر تابع composable شما به LocalSize دسترسی دارد، قبل از ارائه یک composable در آزمایش، اندازه مورد نظر را برای آزمایش تنظیم کنید. اندازه پیشفرض 349.dp x 455.dp است که معادل یک ویجت 5x4 است که در دستگاه Pixel 4 در حالت عمودی نشان داده میشود.
اگر AppWidget شما از sizeMode == Single استفاده میکند، میتوانید آن را روی minWidth و minHeight در فایل info.xml ویجت خود تنظیم کنید.
اگر AppWidget شما از sizeMode == Exact استفاده میکند، میتوانید اندازهها را برای آزمایش به روشی مشابه با نحوه تعیین اندازه برای ویجت خود شناسایی کنید و اندازههای افقی و عمودی را که ویجت شما ممکن است روی آن ظاهر شود شناسایی کنید و آنها را آزمایش کنید.
اگر AppWidget شما از sizeMode == Responsive استفاده می کند، می توانید آن را روی یکی از اندازه های لیستی که هنگام تعیین sizeMode ارائه می کنید، تنظیم کنید.
مدت زمان پیشفرض برای وقفه آزمایشی 1 ثانیه است، اما اگر زیرساخت آزمایش شما زمانبندی متفاوتی را اعمال کند، میتوانید مدت زمان سفارشی را به عنوان آرگومان به روش runGlanceAppWidgetUnitTest ارسال کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-08-21 بهوقت ساعت هماهنگ جهانی."],[],[],null,["The Glance unit test API let you test your Glance code without inflating views\nor needing a UI automator. For example, the unit test API lets you verify\nconditions, such as whether elements are in a list or whether boxes have been\nchecked, using matchers such as `hasContentDescriptionEqualTo` or `isChecked`.\n\nThis API is lightweight and requires less setup, so you can perform test driven\ndevelopment as you develop individual pieces of your widget and organize them to\nimprove code reuse.\n| **Note:** The test doesn't render the composables under test, so it doesn't let you perform clicks. Instead, it uses matchers, which let you perform assertions on actions in clickables such as starting a service or activity.\n\nSetup\n\nThe dependencies required to use the unit test library are shown in the\nfollowing examples: \n\n // Other Glance and Compose runtime dependencies.\n ...\n testImplementation 'androidx.glance:glance-testing:1.1.1'\n testImplementation 'androidx.glance:glance-appwidget-testing:1.1.1'\n testImplementation 'org.robolectric:robolectric:4.11.1'\n ...\n // You may include additional dependencies, such as Robolectric, if your test\n // needs to set a LocalContext.\n\nTest structure\n\nOrganize composable functions outside of the `GlanceAppWidget` class to enable\ncode reuse and unit testing. Reduce the complexity of your units under test as\nmuch as possible. \n\n class MyGlanceComposableTest {\n @Test\n fun myNewsItemComposable_largeSize_hasAuthorAsSubtitle() = runGlanceAppWidgetUnitTest {\n // Prepare inputs and state\n setAppWidgetSize(100.dp, 100.dp)\n\n // Set the composable under test\n provideComposable {\n MyNewsItemComposable(TEST_NEWS_ITEM)\n }\n\n // Perform assertions\n onNode(hasTestTag(\"subTitle\"))\n .assertHasText(TEST_NEWS_ITEM.authorName)\n }\n }\n\nSet context and size for the test\n\nIf your composable function reads context using the `LocalContext.current()`\nmethod, you must set a context using `setContext()`. Otherwise, this step is\noptional.\n\nYou can use any JVM-based Android unit testing framework, such as Roboletric, to\nprovide the context.\n\nIf your composable function accesses `LocalSize`, set the intended size\nfor the test before providing a composable in the test. The default size is\n349.dp x 455.dp, which is equivalent to a 5x4 widget shown on a Pixel 4 device\nin portrait mode.\n\n- If your AppWidget uses `sizeMode == Single`, you can set this to the `minWidth` and `minHeight` in your widget's `info.xml` file.\n- If your AppWidget uses `sizeMode == Exact`, you can identify the sizes to test in a similar way to how you [determine a size for your widget](/develop/ui/views/appwidgets/layouts#anatomy_determining_size) and identify landscape and portrait sizes that your widget may appear on and test for them.\n- If your AppWidget uses `sizeMode == Responsive`, you can set this to one of the sizes from the list that you provide when specifying the `sizeMode`.\n\nThe default duration for a test timeout is 1 second, but you can pass a custom\nduration as an argument to the `runGlanceAppWidgetUnitTest` method if your test\ninfrastructure enforces a different timeout.\n\nFor more information and code samples, see the reference documentation for\n[`runGlanceAppWidgetUnitTest`](/reference/kotlin/androidx/glance/appwidget/testing/unit/package-summary#runGlanceAppWidgetUnitTest(kotlin.time.Duration,kotlin.Function1))."]]