এসপ্রেসো ডিভাইস API দিয়ে স্ক্রিন কনফিগারেশন পরিবর্তনের বিরুদ্ধে পরীক্ষা করুন
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
আপনার অ্যাপটি পরীক্ষা করার জন্য Espresso ডিভাইস API ব্যবহার করুন যখন ডিভাইসটি সাধারণ কনফিগারেশন পরিবর্তনের মধ্য দিয়ে যায়, যেমন ঘূর্ণন এবং স্ক্রিন খোলা। এসপ্রেসো ডিভাইস API আপনাকে একটি ভার্চুয়াল ডিভাইসে এই কনফিগারেশন পরিবর্তনগুলি অনুকরণ করতে দেয় এবং আপনার পরীক্ষাগুলি সিঙ্ক্রোনাসভাবে চালায়, তাই একটি সময়ে শুধুমাত্র একটি UI অ্যাকশন বা দাবি ঘটে এবং আপনার পরীক্ষার ফলাফলগুলি আরও নির্ভরযোগ্য। আপনি যদি Espresso-এর সাথে UI পরীক্ষা লেখার জন্য নতুন হন, তাহলে এর ডকুমেন্টেশন দেখুন।
এসপ্রেসো ডিভাইস API ব্যবহার করতে, আপনার নিম্নলিখিতগুলি প্রয়োজন:
- অ্যান্ড্রয়েড স্টুডিও ইগুয়ানা বা উচ্চতর
- Android Gradle প্লাগইন 8.3 বা উচ্চতর
- Android এমুলেটর 33.1.10 বা উচ্চতর
- Android ভার্চুয়াল ডিভাইস যা API স্তর 24 বা উচ্চতর চালায়
Espresso ডিভাইস API এর জন্য আপনার প্রকল্প সেট আপ করুন
আপনার প্রকল্প সেট আপ করতে যাতে এটি এসপ্রেসো ডিভাইস API সমর্থন করে, নিম্নলিখিতগুলি করুন:
টেস্ট ডিভাইসে টেস্ট পাস কমান্ড দিতে, androidTest
সোর্স সেটের ম্যানিফেস্ট ফাইলে INTERNET
এবং ACCESS_NETWORK_STATE
অনুমতি যোগ করুন:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
gradle.properties
ফাইলে enableEmulatorControl
পরীক্ষামূলক পতাকা সক্ষম করুন:
android.experimental.androidTest.enableEmulatorControl=true
মডিউল-স্তরের বিল্ড স্ক্রিপ্টে emulatorControl
বিকল্পটি সক্ষম করুন:
কোটলিন
testOptions {
emulatorControl {
enable = true
}
}
গ্রোভি
testOptions {
emulatorControl {
enable = true
}
}
মডিউল-স্তরের বিল্ড স্ক্রিপ্টে, আপনার প্রকল্পে এসপ্রেসো ডিভাইস লাইব্রেরি আমদানি করুন:
কোটলিন
dependencies {
androidTestImplementation("androidx.test.espresso:espresso-device:1.0.1")
}
গ্রোভি
dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-device:1.0.1'
}
সাধারণ কনফিগারেশন পরিবর্তনের বিরুদ্ধে পরীক্ষা করুন
এসপ্রেসো ডিভাইস API-এর একাধিক স্ক্রিন ওরিয়েন্টেশন এবং ভাঁজযোগ্য অবস্থা রয়েছে যা আপনি ডিভাইস কনফিগারেশন পরিবর্তনগুলি অনুকরণ করতে ব্যবহার করতে পারেন।
পর্দা ঘূর্ণন বিরুদ্ধে পরীক্ষা
ডিভাইসের স্ক্রিন ঘোরার সময় আপনার অ্যাপে কী ঘটবে তা পরীক্ষা করার একটি উদাহরণ এখানে দেওয়া হল:
প্রথমত, একটি সামঞ্জস্যপূর্ণ প্রারম্ভিক অবস্থার জন্য ডিভাইসটিকে প্রতিকৃতি মোডে সেট করুন:
import androidx.test.espresso.device.action.ScreenOrientation
import androidx.test.espresso.device.rules.ScreenOrientationRule
...
@get:Rule
val screenOrientationRule: ScreenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)
একটি পরীক্ষা তৈরি করুন যা পরীক্ষা সম্পাদনের সময় ডিভাইসটিকে ল্যান্ডস্কেপ অভিযোজনে সেট করে:
@Test
fun myRotationTest() {
...
// Sets the device to landscape orientation during test execution.
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
...
}
স্ক্রিন ঘোরার পরে, UI আশানুরূপ নতুন লেআউটের সাথে খাপ খায় কিনা তা পরীক্ষা করুন।
@Test
fun myRotationTest() {
...
// Sets the device to landscape orientation during test execution.
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist()
}
পর্দা উন্মোচন বিরুদ্ধে পরীক্ষা
আপনার অ্যাপটি যদি ভাঁজ করা যায় এমন ডিভাইসে থাকে এবং স্ক্রিনটি উন্মোচিত হয় তবে কীভাবে তা পরীক্ষা করবেন তার একটি উদাহরণ এখানে দেওয়া হল:
প্রথমে, onDevice().setClosedMode()
কল করে ডিভাইসটিকে ভাঁজ অবস্থায় নিয়ে পরীক্ষা করুন। নিশ্চিত করুন যে আপনার অ্যাপের লেআউট কমপ্যাক্ট স্ক্রিনের প্রস্থের সাথে খাপ খায়।
@Test
fun myUnfoldedTest() {
onDevice().setClosedMode()
composeTestRule.onNodeWithTag("BottomBar").assetIsDisplayed()
composeTestRule.onNodeWithTag("NavRail").assetDoesNotExist()
...
}
সম্পূর্ণরূপে উন্মোচিত অবস্থায় রূপান্তর করতে, onDevice().setFlatMode()
কল করুন। অ্যাপের লেআউটটি প্রসারিত আকারের ক্লাসের সাথে খাপ খায় কিনা তা পরীক্ষা করুন।
@Test
fun myUnfoldedTest() {
onDevice().setClosedMode()
...
onDevice().setFlatMode()
composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("BottomBar").assetDoesNotExist()
}
আপনার পরীক্ষার জন্য কোন ডিভাইসের প্রয়োজন তা উল্লেখ করুন
আপনি যদি এমন একটি পরীক্ষা চালাচ্ছেন যা ভাঁজ করা যায় না এমন একটি ডিভাইসে ভাঁজ করার ক্রিয়া সম্পাদন করে, তাহলে পরীক্ষাটি সম্ভবত ব্যর্থ হবে। চলমান ডিভাইসের সাথে প্রাসঙ্গিক পরীক্ষাগুলি চালানোর জন্য, @RequiresDeviceMode
টীকাটি ব্যবহার করুন। টেস্ট রানার স্বয়ংক্রিয়ভাবে এমন ডিভাইসগুলিতে চলমান পরীক্ষাগুলি এড়িয়ে যায় যা পরীক্ষা করা কনফিগারেশনকে সমর্থন করে না। আপনি প্রতিটি পরীক্ষা বা একটি সম্পূর্ণ পরীক্ষার ক্লাসে ডিভাইসের প্রয়োজনীয়তার নিয়ম যোগ করতে পারেন।
উদাহরণস্বরূপ, নির্দিষ্ট করার জন্য যে একটি পরীক্ষা শুধুমাত্র সেই ডিভাইসগুলিতে চালানো উচিত যা একটি ফ্ল্যাট কনফিগারেশনে উন্মোচন সমর্থন করে, আপনার পরীক্ষায় নিম্নলিখিত @RequiresDeviceMode
কোড যোগ করুন:
@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
...
}
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-09-04 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-09-04 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["Use the Espresso Device API to test your app when the device undergoes common\nconfiguration changes, such as rotation and screen unfolding. The Espresso\nDevice API lets you simulate these configuration changes on a virtual device and\nexecutes your tests synchronously, so only one UI action or assertion happens at\na time and your test results are more reliable. If you're new to writing UI\ntests with Espresso, see its [documentation](/training/testing/espresso).\n\nTo use the Espresso Device API, you need the following:\n\n- Android Studio Iguana or higher\n- Android Gradle plugin 8.3 or higher\n- Android Emulator 33.1.10 or higher\n- Android virtual device that runs API level 24 or higher\n\nSet up your project for the Espresso Device API\n\nTo set up your project so it supports the Espresso Device API, do the following:\n\n1. To let the test pass commands to the test device, add the\n `INTERNET` and `ACCESS_NETWORK_STATE` permissions to the manifest file in the `androidTest` source set:\n\n ```\n \u003cuses-permission android:name=\"android.permission.INTERNET\" /\u003e\n \u003cuses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" /\u003e\n \n ```\n2. Enable the `enableEmulatorControl` experimental flag in the\n `gradle.properties` file:\n\n ```\n android.experimental.androidTest.enableEmulatorControl=true\n \n ```\n3. Enable the `emulatorControl` option in the module-level build\n script:\n\n Kotlin \n\n ```kotlin\n testOptions {\n emulatorControl {\n enable = true\n }\n }\n \n ```\n\n Groovy \n\n ```groovy\n testOptions {\n emulatorControl {\n enable = true\n }\n }\n \n ```\n4. In the module-level build script, import the Espresso Device library\n into your project:\n\n Kotlin \n\n ```kotlin\n dependencies {\n androidTestImplementation(\"androidx.test.espresso:espresso-device:1.0.1\")\n }\n \n ```\n\n Groovy \n\n ```groovy\n dependencies {\n androidTestImplementation 'androidx.test.espresso:espresso-device:1.0.1'\n }\n \n ```\n\nTest against common configuration changes\n\nThe Espresso Device API has multiple screen orientation and foldable states that\nyou can use to simulate device configuration changes.\n\nTest against screen rotation\n\nHere's an example of how to test what happens to your app when the device screen\nrotates:\n\n1. First, for a consistent starting state set the device to portrait\n mode:\n\n ```kotlin\n import androidx.test.espresso.device.action.ScreenOrientation\n import androidx.test.espresso.device.rules.ScreenOrientationRule\n ...\n @get:Rule\n val screenOrientationRule: ScreenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)\n \n ```\n2. Create a test that sets the device to landscape orientation during test\n execution:\n\n ```kotlin\n @Test\n fun myRotationTest() {\n ...\n // Sets the device to landscape orientation during test execution.\n onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)\n ...\n }\n \n ```\n3. After the screen rotates, check that the UI adapts to the new layout as expected.\n\n ```kotlin\n @Test\n fun myRotationTest() {\n ...\n // Sets the device to landscape orientation during test execution.\n onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)\n composeTestRule.onNodeWithTag(\"NavRail\").assertIsDisplayed()\n composeTestRule.onNodeWithTag(\"BottomBar\").assertDoesNotExist()\n }\n \n ```\n\nTest against screen unfolding\n\nHere's an example of how to test what happens to your app if it's on a foldable\ndevice and the screen unfolds:\n\n1. First, test with the device in the folded state by calling\n `onDevice().setClosedMode()`. Make sure that your app's layout\n adapts to the compact screen width.\n\n ```kotlin\n @Test\n fun myUnfoldedTest() {\n onDevice().setClosedMode()\n composeTestRule.onNodeWithTag(\"BottomBar\").assetIsDisplayed()\n composeTestRule.onNodeWithTag(\"NavRail\").assetDoesNotExist()\n ...\n }\n \n ```\n2. To transition to a fully unfolded state, call\n `onDevice().setFlatMode()`. Check that the app's layout adapts to\n the expanded size class.\n\n ```kotlin\n @Test\n fun myUnfoldedTest() {\n onDevice().setClosedMode()\n ...\n onDevice().setFlatMode()\n composeTestRule.onNodeWithTag(\"NavRail\").assertIsDisplayed()\n composeTestRule.onNodeWithTag(\"BottomBar\").assetDoesNotExist()\n }\n \n ```\n\nSpecify what devices your tests need\n\nIf you're running a test that performs folding actions on a device that isn't\nfoldable, the test will likely fail. To execute only the tests that are relevant\nto the running device, use the `@RequiresDeviceMode` annotation. The test runner\nautomatically skips running tests on devices that don't support the\nconfiguration being tested. You can add the device requirement rule to each test\nor an entire test class.\n\nFor example, to specify that a test should only be run on devices that support\nunfolding to a flat configuration, add the following `@RequiresDeviceMode` code\nto your test: \n\n @Test\n @RequiresDeviceMode(mode = FLAT)\n fun myUnfoldedTest() {\n ...\n }"]]