Creating Custom Shadows ——创建自定义shadow
Custom shadows are a Robolectric feature that allows you to make targeted changes in the way Android functions under test. This could be anything from capturing simply that a method was called, to inserting code that interacts with test objects, to doing nothing at all.
Custom shadows allow you to include shadow functionality in only some of your test code, as opposed to adding or modifying a Shadow in Robolectric source. They also allow your shadow to refer to domain specific context, like domain objects in your test classes.
Writing a Custom Shadow
Custom shadows are structured much the same as normal shadow classes. They must include the@Implements(AndroidClassName.class) annotation on the class definition. You can use the normal shadow implementation options, such as shadowing instance methods using @Implementation or shadowing constructors usingpublic void __constructor__(...).
@Implements(Bitmap.class)
public class MyShadowBitmap {
@RealObject private Bitmap realBitmap;
private int bitmapQuality = -1; @Implementation
public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) {
bitmapQuality = quality;
return realBitmap.compress(format, quality, stream);
} public int getQuality() {
return bitmapQuality;
}
}
}
Using a Custom Shadow
Custom Shadows get hooked up to Robolectric using the @Config annotation on the test class or test method, using the shadowsarray attribute. To use the MyShadowBitmap class mentioned in the previous section, you would annotate the test in question with@Config(shadows={MyShadowBitmap.class}), and to include multiple custom shadows:@Config(shadows={MyShadowBitmap.class, MyOtherCustomShadow.class}). This causes Robolectric to recognize and use your custom shadow when executing code against the class you shadowed.
However, the Robolectric.shadowOf() method will not work with custom shadows, as it has to be implemented in Robolectric for each shadow class. You can instead use Robolectric.shadowOf_() and cast the return value to the custom Shadow class you implemented.
Also, if you choose to shadow an Android class that already is shadowed in Robolectric, you will replace the Robolectric shadow. You could try inheriting from the Robolectric shadow if you still need the base shadow functionality.
Creating Custom Shadows ——创建自定义shadow的更多相关文章
- 使用 custom element 创建自定义元素
很早我们就可以在 HTML 文档中写 <custome-element></custom-element> 这样的自定义名称标签.但是浏览器对于不认识的标签一律当成一个普通的行 ...
- Collection View Programming Guide for iOS---(六)---Creating Custom Layouts
Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...
- GHOST CMS - 创建自定义主页 Creating a custom home page
创建自定义主页 Creating a custom home page 为你的网站创建一个自定义的主页是一个让你从人群中脱颖而出的好方法,并把你自己独特的印记存放在你的网上.本教程向您展示了如何在Gh ...
- ArcGIS Engine环境下创建自定义的ArcToolbox Geoprocessing工具
在上一篇日志中介绍了自己通过几何的方法合并断开的线要素的ArcGIS插件式的应用程序.但是后来考虑到插件式的程序的配置和使用比较繁琐,也没有比较好的错误处理机制,于是我就把之前的程序封装成一个类似于A ...
- 【IOS笔记】Creating Custom Content View Controllers
Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...
- 【翻译】在Ext JS和Sencha Touch中创建自定义布局
原文:Creating Custom Layouts in Ext JS and Sencha Touch 布局系统是Sencha框架中最强大和最独特的一部分.布局会处理应用程序中每个组件的大小和位置 ...
- 【UiPath 中文教程】02 - 创建自定义 Activity
在 UiPath Studio 中,活动 (Activity) 是流程自动化的基石,是构成自动化程序的最小模块.它们被包含在一个个 NuGet 包中. UiPath Studio 中有 3 类包: 官 ...
- View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers
Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...
- 在html中创建自定义标签
创建并使用自定义标签 Web Components 标准非常重要的一个特性是,它使开发者能够将HTML页面的功能封装为 custom elements(自定义标签),本篇介绍使用 CustomElem ...
随机推荐
- Uva_10253 Series-Parallel Networks
题目链接 题目大意: 1:一条单独的边是串并联网络 2:G1,G2为串并联网络, 将它们的源点与汇点分别连接起来, 得到的也是串并联网络(并联) 3:G1,G2为串并联网络, 将G1的汇点与G2的源点 ...
- data guard switchover切换异常
data guard switchover切换异常 查看DG数据库备份库发现,switchover_status为SWITCHOVER LATENT SQL> select OPEN_MODE, ...
- bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 613 Solved: 256[Submit][Status] ...
- 基于springMVC+springSecurity3.x+Mybaits3.x的权限系统,,开放源码,支持开源
原文地址:http://blog.csdn.net/mmm333zzz/article/details/16863543/
- USB枚举的详细流程
附一个很好的枚举过程的详细流程: ◆ 用户将一个USB设备插入USB端口,主机为端口供电,设备此时处于上电状态.◆ 主机检测设备.◆ 集线器使用中断通道将事件报告给主机.◆ 主机发送Get_Port_ ...
- servlet单例多线程
Servlet如何处理多个请求访问? Servlet容器默认是采用单实例多线程的方式处理多个请求的: 1.当web服务器启动的时候(或客户端发送请求到服务器时),Servlet就被加载并实例化(只存在 ...
- BZOJ1271: [BeiJingWc2008]秦腾与教学评估
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1271 题解: 这种题真是太神了! 只需要考虑被覆盖的次数的奇偶性,并且保证满足题意的点至多只有 ...
- Light OJ 1031 - Easy Game(区间DP)
题目大意: 给你一个n,代表n个数字,现在有两个选手,选手A,B轮流有有一次机会,每个选手一次可以得到一个或者多个数字,从左侧或者右侧,但是不能同时从两边取数字,当所有的数字被取完,那么游戏结束.然后 ...
- 获取css的属性值
# -*- coding:utf-8 -*- """ 在元素上执行双击操作 """ from selenium import webdriv ...
- UVa 10400 记忆化搜索
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...