首页
About
友情链接
Search
1
V2ray配置文件翻译
1,017 阅读
2
draw.io(diagrams.net) - 免费开源全平台绘图软件
851 阅读
3
Android 11 中的软件包可见性
695 阅读
4
Windows常用软件
688 阅读
5
Android Studio单元测试的编码过程
475 阅读
Uncategorized
Software
Android
Linux
Communist Party of China
Docker
Windows
登录
Search
Leon
累计撰写
26
篇文章
累计收到
20
条评论
首页
栏目
Uncategorized
Software
Android
Linux
Communist Party of China
Docker
Windows
页面
About
友情链接
搜索到
2
篇与
Android
的结果
2021-09-01
Android 11 中的软件包可见性
官方文档:Android 11 中的软件包可见性 这个特性直接导致了如下错误:如果不在content provider的client的AndroidManifest.xml声明标签: <queries> <package android:name="com.fxkxb.mycontentprovider" /> <provider android:authorities="com.fxkxb.mycontentprovider.provider"/> </queries> <uses-permission android:name="com.fxkxb.mycontentprovider"/>就会导致Failed to find provider info for错误。
2021年09月01日
695 阅读
1 评论
1 点赞
2021-08-13
Android Studio单元测试的编码过程
Android Studio单元测试的编码过程前言本次单元测试过程中遇到了很多很多很多很多问题,技术更新迭代速度实在是太快了,网上搜到的很多东西都过时了,这里找到了一个五年前印度小哥的教学录屏[1]......本来英语不好的我听着蹩脚的印度英语,实在是折磨QAQ....不过总体上按照他的步骤是可行的,期间遇到的各种问题也用过搜索解决了,最终磕磕绊绊完成了简单的单元测试编写。于是有了这篇技术总结。设置测试环境在测试开始前,首先添加 AndroidX Test API[2],以及为项目配置 Android 测试依赖项。最终的在应用的顶级 build.gradle 文件中,依赖项如下:dependencies { androidTestImplementation "org.mockito:mockito-core:2.18.0" androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test:rules:1.4.0' androidTestImplementation 'org.hamcrest:hamcrest-library:1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' }同时确保 defaultConfig { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" }使用Android Studio生成测试类鼠标标记要生成的类,按Alt+Enter,选择第一个。在创建测试类窗口中,测试库我们选择JUnit4。类名可以自定义,建议不要改。父类不选,目标包就是项目包名,勾选setUp和tearDown。最后在要测试的方法前打勾。点击OK。随即弹出窗口提示我们选择测试类类型,我们选Android的测试类。由此我们得到了一个测试类,按Shift+Ctrl+T可在测试类与Activity间切换,这里创建rule并实例化。 @Rule public ActivityTestRule<FirstActivity> mainActivityActivityTestRule = new ActivityTestRule<FirstActivity>(FirstActivity.class); private FirstActivity firstActivity = null; @Before public void setUp() throws Exception { firstActivity = mainActivityActivityTestRule.getActivity(); }这样我们就可以通过实例化的rule来获取页面元素。a Simple Example: 测试文本是否为空。UT代码:TextView t = firstActivity.findViewById(R.id.textView10); assertNull(t.getText()); 2. 点击测试方法左边的绿三角即可开始测试。 ![image-20210813060836192.png][4] 以我的程序为例,来测试LED小灯的点亮功能。@Test public void light() { firstActivity.power = true; TextView[] leds = {firstActivity.findViewById(R.id.textView4), firstActivity.findViewById(R.id.textView6), firstActivity.findViewById(R.id.textView5), firstActivity.findViewById(R.id.textView7)}; int[] ledStatus = {1, 1, 0, 0}; final Drawable.ConstantState drawable = leds[0].getBackground().getConstantState(); final Drawable.ConstantState drawable2 = leds[1].getBackground().getConstantState(); firstActivity.light(ledStatus,leds); final Drawable.ConstantState drawable1 = leds[0].getBackground().getConstantState(); final Drawable.ConstantState drawable3 = leds[1].getBackground().getConstantState(); assertEquals(drawable,drawable1); assertNotEquals(drawable2,drawable3); } 我的Activity部分。 public void light(int[] b, TextView[] t) { for (int i = 0; i < 4; i++) { switch (i) { case 0: if (b[i]==1) { if (power) { t[i].setBackgroundResource(R.drawable.lightred); } } else if (b[i]==-1){ break; }else { t[i].setBackgroundResource(R.drawable.light); } ; break; case 1: if (b[i]==1) { if (power) { t[i].setBackgroundResource(R.drawable.lightgreen); } } else if (b[i]==-1){ break; } else { t[i].setBackgroundResource(R.drawable.light); } ; break; case 2: if (b[i]==1) { if (power) { t[i].setBackgroundResource(R.drawable.lightblue); } } else if (b[i]==-1){ break; } else { t[i].setBackgroundResource(R.drawable.light); } ; break; case 3: if (b[i]==1) { if (power) { t[i].setBackgroundResource(R.drawable.lightup); } } else if (b[i]==-1){ break; } else { t[i].setBackgroundResource(R.drawable.light); } ; break; default: break; } } }点击测试类左边的双绿三角可开始测试类中全部测试方法的测试。 我的测试结果:参考文献Android app development for beginners - 26 - Android - Unit test for Activity - Activity Test Rule: https://www.youtube.com/watch?v=_TR6QcRozAgSet up project for AndroidX Test: https://developer.android.com/training/testing/set-up-projectBuild instrumented unit tests: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests#javaAndroid单元测试(二):Mockito框架的使用: https://blog.csdn.net/qq_17766199/article/details/78450007Q&A on stackoverflow: https://stackoverflow.com/questions/13621407/getting-the-current-background-id-of-a-viewAndroid单元测试-对Activity的测试: https://blog.csdn.net/Double2hao/article/details/77160950
2021年08月13日
475 阅读
1 评论
0 点赞