日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

如何使用Mock修改Android設(shè)備上的features

瀏覽:130日期:2022-09-19 16:30:36
背景

手工測試過程中有個測試場景需要刪除測試設(shè)備上某個Android feature,往上搜索了一圈沒找到有效的操作方法。獲取Android設(shè)備所有的feature可以通過adb命令pm list features或者Android代碼Context.getPackageManager().getSystemAvailableFeatures(),但都沒有對應(yīng)的修改方法。

既然feature是從Context獲取的,那能不能構(gòu)造一個只包含我想要的feature的Context呢。順著這個思路,有了下面的方案。

方案設(shè)計

單元測試常用的Mock技術(shù),就是來構(gòu)造假/模擬對象的。但如果完全構(gòu)造,又擔(dān)心和真實環(huán)境差別較大,測試結(jié)果不可靠。能不能從真實Android設(shè)備中獲取真實的Context,把不想要的feature去除,再給到被測試方法中呢?

答案是肯定的,通過選用流行的Mock組件Mockito ,官網(wǎng)上給出了下面2種Mock Java對象的方式:

mock()/@Mock: create mock spy()/@Spy: partial mocking, real methods are invoked but still can be verified and stubbed

可以看到Spy這種Mock方式可完美地解決我的需求。

實現(xiàn)(簡化版需求)業(yè)務(wù)需求說明

獲取所有Android Features并把feature name打印在日志中,使用adb命令的效果如下:

➜ study git:(master) ✗ adb shell pm list features feature:reqGlEsVersion=0x30002feature:android.hardware.audio.outputfeature:android.hardware.bluetoothfeature:android.hardware.bluetooth_lefeature:android.hardware.camerafeature:android.hardware.camera.anyfeature:android.hardware.camera.autofocusfeature:android.hardware.camera.capability.manual_post_processingfeature:android.hardware.camera.capability.manual_sensorfeature:android.hardware.camera.capability.rawfeature:android.hardware.camera.flashfeature:android.hardware.camera.frontfeature:android.hardware.camera.level.fullfeature:android.hardware.faketouchfeature:android.hardware.locationfeature:android.hardware.location.gpsfeature:android.hardware.location.networkfeature:android.hardware.microphonefeature:android.hardware.nfc.anyfeature:android.hardware.opengles.aepfeature:android.hardware.ram.normalfeature:android.hardware.screen.landscapefeature:android.hardware.screen.portraitfeature:android.hardware.sensor.accelerometerfeature:android.hardware.sensor.compassfeature:android.hardware.sensor.lightfeature:android.hardware.sensor.proximityfeature:android.hardware.sensor.stepcounterfeature:android.hardware.telephonyfeature:android.hardware.telephony.cdmafeature:android.hardware.telephony.gsmfeature:android.hardware.touchscreenfeature:android.hardware.touchscreen.multitouchfeature:android.hardware.touchscreen.multitouch.distinctfeature:android.hardware.touchscreen.multitouch.jazzhandfeature:android.hardware.usb.accessoryfeature:android.hardware.usb.hostfeature:android.hardware.vulkan.computefeature:android.hardware.vulkan.levelfeature:android.hardware.vulkan.version=4194307feature:android.hardware.wififeature:android.hardware.wifi.directfeature:android.software.activities_on_secondary_displaysfeature:android.software.app_widgetsfeature:android.software.autofillfeature:android.software.backupfeature:android.software.companion_device_setupfeature:android.software.connectionservicefeature:android.software.ctsfeature:android.software.device_adminfeature:android.software.file_based_encryptionfeature:android.software.input_methodsfeature:android.software.live_wallpaperfeature:android.software.managed_usersfeature:android.software.midifeature:android.software.picture_in_picturefeature:android.software.printfeature:android.software.securely_removes_usersfeature:android.software.sipfeature:android.software.sip.voipfeature:android.software.verified_bootfeature:android.software.voice_recognizersfeature:android.software.webview業(yè)務(wù)需求代碼實現(xiàn)

public class FeaturesUtil { private static final String TAG = 'FeaturesUtil'; public static void getFeatures(Context context) {PackageManager packageManager = context.getPackageManager();FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures();for (FeatureInfo featureInfo : featureInfos) { Log.i(TAG, 'feature: ' + featureInfo.name);} }}測試代碼實現(xiàn)(去除bluetooth feature)

@Spy Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();; @Spy PackageManager packageManager = context.getPackageManager();@Test public void getFeatures() {// 去除bluetooth featureFeatureInfo[] mockedFeatureInfos = removeFeature(packageManager.getSystemAvailableFeatures(),Arrays.asList('android.hardware.bluetooth', 'android.hardware.bluetooth_le'));when(packageManager.getSystemAvailableFeatures()).thenReturn(mockedFeatureInfos);when(context.getPackageManager()).thenReturn(packageManager);FeaturesUtil.getFeatures(context); } /** * 根據(jù)feature name刪除一個或多個feature * * @param featureInfos FeatureInfo數(shù)組 * @param featureNames 字符串列表,每個元素是要刪除的FeatureInfo的name * @return 刪除之后的FeatureInfo數(shù)組 */ private FeatureInfo[] removeFeature(FeatureInfo[] featureInfos, List<String> featureNames) {List<FeatureInfo> featureInfoList = new ArrayList();for (FeatureInfo featureInfo : featureInfos) { if (featureInfo.name != null && !(featureNames.contains(featureInfo.name))) {featureInfoList.add(featureInfo); }}return featureInfoList.toArray(new FeatureInfo[featureInfoList.size()]); }運(yùn)行測試代碼后Logcat輸出

可以看到bluetooth相關(guān)的feature已經(jīng)沒有了

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.sensor.proximity

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.sensor.accelerometer

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.faketouch

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.usb.accessory

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.telephony.cdma

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.backup

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.touchscreen

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.touchscreen.multitouch

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.print

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.activities_on_secondary_displays

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.voice_recognizers

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.picture_in_picture

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.opengles.aep

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera.autofocus

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.telephony.gsm

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.sip.voip

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.usb.host

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.audio.output

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.verified_boot

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera.flash

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera.front

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.screen.portrait

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.microphone

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.autofill

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.securely_removes_users

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.sensor.compass

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.hardware.touchscreen.multitouch.jazzhand

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.app_widgets

2021-04-07 13:23:39.266 16238-16268/? I/FeaturesUtil: feature: android.software.input_methods

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.sensor.light

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.vulkan.version

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.companion_device_setup

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.device_admin

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.screen.landscape

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.ram.normal

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.managed_users

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.webview

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.sensor.stepcounter

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera.capability.manual_post_processing

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera.any

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera.capability.raw

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.vulkan.compute

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.connectionservice

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.touchscreen.multitouch.distinct

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.location.network

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.cts

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.sip

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera.capability.manual_sensor

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.camera.level.full

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.wifi.direct

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.live_wallpaper

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.location.gps

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.software.midi

2021-04-07 13:23:39.267 16238-16268/? I/FeaturesUtil: feature: android.hardware.nfc.any

2021-04-07 13:23:39.268 16238-16268/? I/FeaturesUtil: feature: android.hardware.wifi

2021-04-07 13:23:39.268 16238-16268/? I/FeaturesUtil: feature: android.hardware.location

2021-04-07 13:23:39.268 16238-16268/? I/FeaturesUtil: feature: android.hardware.vulkan.level

2021-04-07 13:23:39.268 16238-16268/? I/FeaturesUtil: feature: android.hardware.telephony

2021-04-07 13:23:39.268 16238-16268/? I/FeaturesUtil: feature: android.software.file_based_encryption

2021-04-07 13:23:39.269 16238-16268/? I/TestRunner: finished: getFeatures(com.aniu.featuresmock.FeaturesUtilTest)

總結(jié) 不直接使用Mock,而是使用Spy,最大限度保持測試環(huán)境真實可靠,從而保證測試效果 測試代碼不要放在test目錄,要放在androidTest目錄,保證在真實設(shè)備上跑(不然代碼也會報錯)。目錄結(jié)構(gòu)如下:

如何使用Mock修改Android設(shè)備上的features

以上就是如何使用Mock修改Android設(shè)備上的features的詳細(xì)內(nèi)容,更多關(guān)于Mock修改Android features的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
精品精品99| 久久成人国产| 麻豆成人91精品二区三区| 视频一区中文字幕精品 | 国产毛片久久久| 国产剧情在线观看一区| 亚洲91在线| 欧美在线观看天堂一区二区三区| 成人午夜亚洲| 中文字幕在线高清| 激情五月综合| 欧美日韩国产亚洲一区| 国产精品一区二区99| 福利欧美精品在线| 日韩大片在线播放| 亚洲欧洲美洲av| 亚洲va在线| 999在线观看精品免费不卡网站| 美女黄网久久| 日本精品在线播放| 久久中文字幕二区| 综合精品一区| 国产一区二区精品久| 亚洲黄色网址| 亚洲精品自拍| 高清精品久久| 婷婷综合网站| 欧美激情视频一区二区三区免费 | 国产一区二区三区四区大秀 | 日韩精品一二三四| 日韩高清三区| 一级欧洲+日本+国产| 国产一区二区中文| 丁香六月综合| 国产精品免费大片| 色网在线免费观看| 激情欧美丁香| 日韩中文字幕在线一区| 卡一卡二国产精品| 国产主播一区| 日韩一区二区三区精品视频第3页| 国产精品一区二区美女视频免费看| 日韩久久精品| 日韩超碰人人爽人人做人人添| 国产亚洲久久| 国产成人久久精品麻豆二区| 免费污视频在线一区| 色天使综合视频| 国产伦乱精品| 97国产成人高清在线观看| 一级欧美视频| 中文在线不卡| 欧美日韩 国产精品| 久久久久久久欧美精品| 亚洲制服欧美另类| 日本在线视频一区二区| 日韩有吗在线观看| 欧美一区二区三区久久精品| 亚洲风情在线资源| 巨乳诱惑日韩免费av| 久久国产精品美女| 久久97久久97精品免视看秋霞| 亚洲一二av| 尤物在线精品| 欧美激情在线精品一区二区三区| 国产精久久一区二区| 一区免费在线| 婷婷综合福利| 亚洲国产成人二区| 亚洲精品自拍| 999国产精品视频| 久久精品av麻豆的观看方式| 亚洲www啪成人一区二区| 日韩高清不卡在线| 久久理论电影| 欧美日韩中文一区二区| 欧美精品三级在线| 国产一区二区三区久久| 999久久久91| 日本伊人午夜精品| 欧美日韩在线二区| 热久久久久久| 欧洲av一区二区| 国产精品香蕉| 蜜臀91精品一区二区三区| 国产激情综合| 一区二区高清| 亚洲免费成人| 欧美sss在线视频| 国产精品一卡| 97成人在线| 国产一区二区高清| 成人精品亚洲| 日韩在线短视频| 欧美激情另类| 黄色亚洲精品| 中文精品电影| 伊人成人网在线看| 91亚洲成人| 韩国三级一区| 红杏一区二区三区| 天海翼亚洲一区二区三区| 久久久久久久久久久妇女| 国产福利91精品一区二区| 欧美日韩一区自拍| 亚洲毛片在线| 男女性色大片免费观看一区二区 | 国产精品22p| 午夜电影亚洲| 国产亚洲亚洲| 亚洲成人二区| 久久精品91| 久久av一区| 麻豆91精品| 日本久久一区| 欧美永久精品| 国产乱码午夜在线视频| 日韩精品诱惑一区?区三区| 国产一区二区三区四区| 国产精品视频一区二区三区综合 | 麻豆精品蜜桃| 给我免费播放日韩视频| 婷婷五月色综合香五月| 日韩综合一区二区三区| 国产精品成人一区二区不卡| 亚洲ww精品| 欧美一区三区| 久久久久亚洲| 午夜亚洲福利| 欧美日韩一二三四| 黄色在线网站噜噜噜| 久久av中文| 91精品国产自产精品男人的天堂| 日韩亚洲精品在线观看| 欧美在线不卡| 国产精品麻豆成人av电影艾秋| 欧美日本二区| 国产激情精品一区二区三区| 免费在线亚洲欧美| 欧美亚洲国产日韩| 久久不卡国产精品一区二区| 亚洲综合在线电影| 蜜桃av一区二区在线观看| 视频一区国产视频| 日韩美女精品| 国产亚洲精品精品国产亚洲综合| 国产精品宾馆| 在线精品视频一区| 国产在线视频欧美一区| 亚洲在线成人| 欧美男人天堂| 欧美激情三区| 欧美资源在线| 正在播放日韩精品| 91精品国产乱码久久久久久久 | 国产真实久久| 中文字幕日韩欧美精品高清在线| 日本中文字幕一区二区| 亚洲作爱视频| 丝袜av一区| 亚洲精品极品少妇16p| 视频一区中文字幕国产| 国产精品一区二区三区av| 亚洲国产不卡| 日韩在线视频一区二区三区| 国产精品男女| 美女被久久久| 国产伦精品一区二区三区在线播放 | 欧美一级专区| 午夜电影一区| 国产一区二区三区不卡av| 国产精品主播在线观看| 另类欧美日韩国产在线| 国产日产一区| 国产欧美日韩在线一区二区| 九九色在线视频| 岛国精品一区| 国产精品4hu.www| 欧美日本三区| 精品久久精品| 激情久久久久久| 免费看黄色91| 日韩精品一页| 一本大道色婷婷在线| 首页欧美精品中文字幕| 麻豆精品在线观看| 国产视频一区三区| 久久精品伊人| 伊人影院久久| 老鸭窝一区二区久久精品| 欧美日中文字幕| 日本不卡一区二区三区| 日本高清不卡一区二区三区视频| 亚洲久草在线| 日本在线啊啊| 在线国产精品一区| 吉吉日韩欧美| 蜜桃av一区二区三区电影| 日韩在线观看| 日韩av不卡一区二区|