کتابخانه آزمایشی «کنشهای برنامه» (AATL) قابلیتهایی را برای توسعهدهندگان فراهم میکند تا بتوانند اجرای «کنش برنامه» را بهصورت برنامهنویسیشده آزمایش کنند و آزمایشی را که معمولاً بااستفاده از پُرسمانهای گفتاری واقعی یا ابزار آزمایشی «کنشهای برنامه» انجام میشود خودکارسازی کنند.
این کتابخانه کمک میکند مطمئن شوید پیکربندی shortcut.xml درست است و
فراخوانی هدف Android شرحدادهشده موفقیتآمیز است. «کتابخانه آزمایشی کنشهای برنامه»
سازوکاری برای آزمایش توانایی برنامهتان در برآورده کردن هدف و پارامترهای «دستیار Google»
ارائه میدهد، با تبدیل کردن آنها به پیوند عمیق Android یا
هدف Android، که میتوان آن را تأیید کرد و برای نمونهسازی فعالیت Android
استفاده کرد.
آزمایش بهصورت واحد Robolectric یا آزمایشهای ابزاری در محیط Android انجام میشود. این کار به توسعهدهندگان اجازه میدهد تا با شبیهسازی رفتار واقعی برنامه، برنامهشان را بهطور جامع آزمایش کنند. برای آزمایش کردن «هدفهای درونساخت»، هدفهای سفارشی، یا تحقق پیوند عمیق، میتوان از هر چارچوب آزمایش ابزاردار استفاده کرد (UI Automator، Espresso، JUnit4، Appium، Detox، Calabash).
اگر برنامه چندزبانه باشد، توسعهدهندگان میتوانند تأیید کنند که عملکرد برنامه در مناطق مختلف بهدرستی کار میکند.
روش کار
برای ادغام کردن «کتابخانه آزمایشی کنشهای برنامه» در محیط آزمایشی برنامه، توسعهدهندگان باید
آزمایشهای Robolectric یا آزمایشهای ابزاری جدیدی ایجاد کنند یا آزمایشهای موجود را در
واحد app برنامه بهروز کنند.
کد آزمایشی شامل بخشهای زیر است:
- مقداردهی اولیه نمونه کتابخانه، در روش راهاندازی مشترک یا در چارچوبهای تست فردی.
- هر آزمایش جداگانه روش
fulfillنمونه کتابخانه را فراخوانی میکند تا نتیجه ایجاد قصد را تولید کند. - سپس توسعهدهنده پیوند عمیق را تأیید میکند یا «تکمیل برنامه» را راهاندازی میکند، و اعتبارسنجی سفارشی را در وضعیت برنامه اجرا میکند.
شرایط لازم برای راهاندازی
برای استفاده از کتابخانه آزمایشی، قبلاز افزودن آزمایشها به برنامهتان، باید برخی پیکربندیهای اولیه برنامه را انجام دهید.
پیکربندی
برای استفاده از «کتابخانه آزمایشی کنشهای برنامه»، مطمئن شوید برنامهتان بهصورت زیر پیکربندی شده باشد:
- افزایه Android Gradle (AGP) را نصب کنید
- فایل
shortcuts.xmlرا در پوشهres/xmlدر واحدappبگنجانید. - مطمئن شوید
AndroidManifest.xmlشامل<meta-data android:name="android.app.shortcuts" android:resource=”@xml/shortcuts” />در یکی از این موارد باشد:- برچسب
<application> - برچسب
<activity>راهانداز
- برچسب
- عنصر
<capability>را در عنصر<shortcuts>درshortcuts.xmlقرار دهید
افزودن وابستگیهای «کتابخانه آزمایشی کنشهای برنامه»
مخزن Google را به فهرست مخزنهای پروژه در
settings.gradleاضافه کنید:allprojects { repositories { … google() } }در فایل
build.gradleواحد برنامه، وابستگیهای AATL را اضافه کنید:androidTestImplementation 'com.google.assistant.appactions:testing:1.0.0'حتماً از شماره نسخه کتابخانهای که بارگیری کردهاید استفاده کنید.
ایجاد آزمایشهای یکپارچهسازی
آزمایشهای جدیدی در
app/src/androidTestایجاد کنید. برای آزمایشهای Robolectric، آنها را درapp/src/testایجاد کنید:کاتلین
import android.content.Context import android.content.Intent import android.widget.TextView import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ActivityScenario import com.google.assistant.appactions.testing.aatl.AppActionsTestManager import com.google.assistant.appactions.testing.aatl.fulfillment.AppActionsFulfillmentIntentResult import com.google.assistant.appactions.testing.aatl.fulfillment.AppActionsFulfillmentResult import com.google.assistant.appactions.testing.aatl.fulfillment.FulfillmentType import com.google.common.collect.ImmutableMap import org.junit.Assert.assertEquals import org.junit.Before import org.junit.runner.RunWith import org.junit.Test import org.robolectric.RobolectricTestRunner … @Test fun IntentTestExample() { val intentParams = mapOf("feature" to "settings") val intentName = "actions.intent.OPEN_APP_FEATURE" val result = aatl.fulfill(intentName, intentParams) assertEquals(FulfillmentType.INTENT, result.getFulfillmentType()) val intentResult = result as AppActionsFulfillmentIntentResult val intent = intentResult.intent // Developer can choose to assert different relevant properties of the returned intent, such as the action, activity, package, scheme and so on assertEquals("youtube", intent.scheme) assertEquals("settings", intent.getStringExtra("featureParam")) assertEquals("actions.intent.OPEN_APP_FEATURE", intent.action) assertEquals("com.google.android.youtube/.MainActivity", intent.component.flattenToShortString()) assertEquals("com.google.myapp", intent.package) // Developers can choose to use returned Android Intent to launch and assess the activity. Below are examples for how it will look like for Robolectric and Espresso tests. // Please note that the below part is just a possible example of how Android tests are validating Activity functionality correctness for given Android Intent. // Robolectric example: val activity = Robolectric.buildActivity(MainActivity::class.java, intentResult.intent).create().resume().get() val title: TextView = activity.findViewById(R.id.startActivityTitle) assertEquals(title?.text?.toString(), "Launching…") }
جاوا
import android.content.Context; import android.content.Intent; import android.widget.TextView; import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ActivityScenario; import com.google.assistant.appactions.testing.aatl.AppActionsTestManager; import com.google.assistant.appactions.testing.aatl.fulfillment.AppActionsFulfillmentIntentResult; import com.google.assistant.appactions.testing.aatl.fulfillment.AppActionsFulfillmentResult; import com.google.assistant.appactions.testing.aatl.fulfillment.FulfillmentType; import com.google.common.collect.ImmutableMap; import org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.runner.RunWith; import org.junit.Test; import org.robolectric.RobolectricTestRunner; ... @Test public void IntentTestExample() throws Exception { Map<String, String> intentParams = ImmutableMap.of("feature", "settings"); String intentName = "actions.intent.OPEN_APP_FEATURE"; AppActionsFulfillmentResult result = aatl.fulfill(intentName, intentParams); assertEquals(FulfillmentType.INTENT, result.getFulfillmentType()); AppActionsFulfillmentIntentResult intentResult = (AppActionsFulfillmentIntentResult) result; Intent intent = intentResult.getIntent(); // Developer can choose to assert different relevant properties of the returned intent, such as the action, activity, package, or scheme assertEquals("settings", intent.getStringExtra("featureParam")); assertEquals("actions.intent.OPEN_APP_FEATURE", intent.getAction()); assertEquals("com.google.android.youtube/.MainActivity", intent.getComponent().flattenToShortString()); assertEquals("com.google.myapp", intent.getPackage()); // Developers can choose to use returned Android Intent to launch and assess the activity. Below are examples for how it will look like for Robolectric and Espresso tests. // Please note that the below part is just a possible example of how Android tests are validating Activity functionality correctness for given Android Intent. // Robolectric example: MainActivity activity = Robolectric.buildActivity(MainActivity.class,intentResult.intent).create().resume().get(); TextView title: TextView = activity.findViewById(R.id.startActivityTitle) assertEquals(title?.getText()?.toString(), "Launching…") }
اگر از Espresso استفاده میکنید، باید نحوه راهاندازی «فعالیت» را براساس نتایج AATL اصلاح کنید. در اینجا مثالی برای Espresso بااستفاده از روش
ActivityScenarioآورده شده است:کاتلین
ActivityScenario.launch<MainActivity>(intentResult.intent); Espresso.onView(ViewMatchers.withId(R.id.startActivityTitle)) .check(ViewAssertions.matches(ViewMatchers.withText("Launching…")))
جاوا
ActivityScenario.launch<MainActivity>(intentResult.intent); Espresso.onView(ViewMatchers.withId(R.id.startActivityTitle)) .check(ViewAssertions.matches(ViewMatchers.withText("Launching…")))
نام و ویژگیهای کلیدی در نگاشتهای پارامتر با پارامترهای BII مطابقت داشته باشد. برای مثال،
exercisePlan.forExercise.nameبا مستندات پارامتر درGET_EXERCISE_PLANمطابقت دارد.نمونه میانای برنامهسازی کاربردی را با پارامتر «بافت Android» (دریافتشده از
ApplicationProviderیاInstrumentationRegistry) نمونهسازی کنید:- معماری برنامه تکواحدی:
کاتلین
private lateinit var aatl: AppActionsTestManager @Before fun init() { val appContext = ApplicationProvider.getApplicationContext() aatl = AppActionsTestManager(appContext) }
جاوا
private AppActionsTestManager aatl; @Before public void init() { Context appContext = ApplicationProvider.getApplicationContext(); aatl = new AppActionsTestManager(appContext); }
- معماری برنامه چندماژولی:
کاتلین
private lateinit var aatl: AppActionsTestManager @Before fun init() { val appContext = ApplicationProvider.getApplicationContext() val lookupPackages = listOf("com.myapp.mainapp", "com.myapp.resources") aatl = AppActionsTestManager(appContext, lookupPackages) }
جاوا
private AppActionsTestManager aatl; @Before public void init() throws Exception { Context appContext = ApplicationProvider.getApplicationContext(); List<String> lookupPackages = Arrays.asList("com.myapp.mainapp","com.myapp.resources"); aatl = new AppActionsTestManager(appContext, Optional.of(lookupPackages)); }
روش
fulfillمیانای برنامهسازی کاربردی را اجرا کنید و شیءAppActionsFulfillmentResultرا دریافت کنید.
انجام ادعاها
روش توصیهشده برای تأیید «کتابخانه آزمایشی کنشهای برنامه» عبارت است از:
- نوع برآوردهسازی
AppActionsFulfillmentResultرا تأیید کنید. بایدFulfillmentType.INTENTیاFulfillmentType.UNFULFILLEDباشد تا بتوان نحوه رفتار برنامه را درصورت درخواستهای غیرمنتظره «هدفهای داخلی برنامه» آزمایش کرد. - دو نوع اجرای دستور وجود دارد: اجرای دستور
INTENTوDEEPLINK.- معمولاً توسعهدهنده میتواند با نگاه کردن به برچسب قصد در
shortcuts.xmlکه با راهاندازی کتابخانه درحال برآورده کردن آن است، بین برآورده کردنINTENTوDEEPLINKتمایز قائل شود. - اگر برچسب url-template زیر برچسب intent وجود داشته باشد، این نشان میدهد
که
DEEPLINKاین هدف را برآورده میکند. - اگر روش
getData()قصد نتیجه شیء غیرتهی برگرداند، این نیز نشاندهندهDEEPLINKبرآوردهسازی است. بههمین ترتیب، اگرgetDatanullرا برگرداند، یعنی این یک اجرایINTENTاست.
- معمولاً توسعهدهنده میتواند با نگاه کردن به برچسب قصد در
- برای مورد
INTENT،AppActionsFulfillmentResultرا بهAppActionsIntentFulfillmentResultتبدیل نوع کنید، Android Intent را با فراخوانی روشgetIntentواکشی کنید و یکی از کارهای زیر را انجام دهید:- فیلدهای تکی Android Intent را تأیید کنید.
- Assert the uri of an intent that is accessed through intent.getData.getHost method.
- برای مورد
DEEPLINK،AppActionsFulfillmentResultرا بهAppActionsIntentFulfillmentResult(همانند سناریوINTENTدر بالا) تبدیل کنید، Android Intent را با فراخوانی روشgetIntentواکشی کنید و نشانی وب پیوند عمیق (دسترسی ازطریقintent.getData.getHost) را تأیید کنید. - برای هر دو
INTENTوDEEPLINK، میتوانید از هدف حاصل برای راهاندازی فعالیت با چارچوب آزمایشی Android انتخابشده استفاده کنید.
بینالمللیسازی
اگر برنامهتان چند منطقه زبانی دارد، میتوانید آزمایشها را برای اجرا در منطقه زبانی خاصی که تحت آزمایش است پیکربندی کنید. یا اینکه میتوانید مستقیماً منطقه زبانی را تغییر دهید:
کاتلین
import android.content.res.Configuration import java.util.Locale ... val newLocale = Locale("es") val conf = context.resources.configuration conf = Configuration(conf) conf.setLocale(newLocale)
جاوا
Locale newLocale = new Locale("es"); Configuration conf = context.getResources().getConfiguration(); conf = new Configuration(conf); conf.setLocale(newLocale);
در اینجا نمونهای از آزمایش AATL پیکربندیشده برای منطقه زبانی اسپانیایی (ES) آورده شده است:
کاتلین
import com.google.common.truth.Truth.assertThat import org.junit.Assert.assertEquals import android.content.Context import android.content.res.Configuration import androidx.test.platform.app.InstrumentationRegistry import com.google.assistant.appactions.testing.aatl.AppActionsTestManager import com.google.assistant.appactions.testing.aatl.fulfillment.AppActionsFulfillmentIntentResult import com.google.assistant.appactions.testing.aatl.fulfillment.AppActionsFulfillmentResult import com.google.assistant.appactions.testing.aatl.fulfillment.FulfillmentType import com.google.common.collect.ImmutableMap import java.util.Locale import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @RunWith(RobolectricTestRunner::class) class ShortcutForDifferentLocaleTest { @Before fun setUp() { val context = InstrumentationRegistry.getInstrumentation().getContext() // change the device locale to 'es' val newLocale = Locale("es") val conf = context.resources.configuration conf = Configuration(conf) conf.setLocale(newLocale) val localizedContext = context.createConfigurationContext(conf) } @Test fun shortcutForDifferentLocale_succeeds() { val aatl = AppActionsTestManager(localizedContext) val intentName = "actions.intent.GET_EXERCISE_PLAN" val intentParams = ImmutableMap.of("exercisePlan.forExercise.name", "Running") val result = aatl.fulfill(intentName, intentParams) assertThat(result.getFulfillmentType()).isEqualTo(FulfillmentType.INTENT) val intentResult = result as AppActionsFulfillmentIntentResult assertThat(intentResult.getIntent().getData().toString()) .isEqualTo("myexercise://browse?plan=running_weekly") } }
جاوا
import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import android.content.Context; import android.content.res.Configuration; import androidx.test.platform.app.InstrumentationRegistry; import com.google.assistant.appactions.testing.aatl.AppActionsTestManager; import com.google.assistant.appactions.testing.aatl.fulfillment.AppActionsFulfillmentIntentResult; import com.google.assistant.appactions.testing.aatl.fulfillment.AppActionsFulfillmentResult; import com.google.assistant.appactions.testing.aatl.fulfillment.FulfillmentType; import com.google.common.collect.ImmutableMap; import java.util.Locale; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; @Test public void shortcutForDifferentLocale_succeeds() throws Exception { Context context = InstrumentationRegistry.getInstrumentation().getContext(); // change the device locale to 'es' Locale newLocale = new Locale("es"); Configuration conf = context.getResources().getConfiguration(); conf = new Configuration(conf); conf.setLocale(newLocale); Context localizedContext = context.createConfigurationContext(conf); AppActionsTestManager aatl = new AppActionsTestManager(localizedContext); String intentName = "actions.intent.GET_EXERCISE_PLAN"; ImmutableMap<String, String> intentParams = ImmutableMap.of("exercisePlan.forExercise.name", "Running"); AppActionsFulfillmentResult result = aatl.fulfill(intentName, intentParams); assertThat(result.getFulfillmentType()).isEqualTo(FulfillmentType.INTENT); AppActionsFulfillmentIntentResult intentResult = (AppActionsFulfillmentIntentResult) result; assertThat(intentResult.getIntent().getData().toString()) .isEqualTo("myexercise://browse?plan=running_weekly"); }
عیبیابی
اگر آزمایش ادغام شما بهطور غیرمنتظرهای ناموفق بود، میتوانید پیامهای گزارش AATL را در پنجره logcat در Android Studio جستجو کنید تا پیام سطح هشدار یا خطا را دریافت کنید. همچنین میتوانید سطح ثبت گزارش را افزایش دهید تا خروجی بیشتری از کتابخانه ضبط کنید.
محدودیتها
محدودیتهای فعلی «کتابخانه آزمایشی کنشهای برنامه» اینها هستند :
- AATL ویژگیهای «درک زبان طبیعی» (NLU) یا «گفتار به نوشتار» (STT) را آزمایش نمیکند.
- وقتی آزمایشها در واحدهایی غیراز واحد برنامه پیشفرض باشند، AATL کار نمیکند.
- AATL فقط با Android 7.0 «Nougat» (سطح میانای برنامهسازی کاربردی ۲۴) و نسخههای جدیدتر سازگار است.