当一些东西被监视时,Mockito会创建一个代理实例.现在,有没有办法将在该代理实例上执行的setter转发到它后面的真实实例?
理由:我有一个我完全没有控制的对象实例,即Android活动.我可以给我的应用程序的大部分地区代理的版本和运行正常的是,但因为我需要在活动的创建阶段很早就创建间谍/代理,目前尚不能完全实例化,例如基地上下文没附着.这发生在代理实例上,当然不被活动实例本身使用(它通过引用自身Activity.this).最终的结果是,这会导致各种崩溃,因为资源解析是通过这个基础上下文发生的,因此内部Fragment机制会抛出NPE等等.
这是一些代码:
public class CustomAndroidJUnitRunner extends AndroidJUnitRunner {
@Override
public Activity newActivity(ClassLoader cl, String className, Intent intent)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Activity activity = super.newActivity(cl, className, intent);
return maybeStubSomeDelegate(activity);
}
private Activity maybeStubSomeDelegate(Activity activity) {
if (!(activity instanceof SomeDelegate)) {
return activity;
}
Activity spiedActivity = spy(activity);
doReturn(SomeDelegateMock.getInstance())
.when((SomeDelegate) spiedActivity)
.getDelegate();
return spiedActivity;
}
}
Run Code Online (Sandbox Code Playgroud)
我很无能为力 - 任何想法?
InstrumentationTestCase2为了纯粹的测试目的,我尝试在派生的测试用例中将文件写入外部SD卡.当在被测应用程序android.permission.WRITE_EXTERNAL_STORAGE的AndroidManifest.xml文件中配置时,这很有效,但如果此设置仅出现在AndroidManifest.xml测试项目的文件中,则不起作用.
当然,我不想将此权限添加到主清单中,因为我在功能测试期间只需要此功能.我怎样才能做到这一点?
我的应用程序在a上进行向下滑动时重新加载数据SwipeRefreshLayout.现在我尝试使用Android Test Kit/Espresso进行测试,如下所示:
onView(withId(R.id.my_refresh_layout)).perform(swipeDown());
Run Code Online (Sandbox Code Playgroud)
不幸的是,这失败了
android.support.test.espresso.PerformException: Error performing 'fast swipe'
on view 'with id: my.app.package:id/my_refresh_layout'.
...
Caused by: java.lang.RuntimeException: Action will not be performed because the target view
does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
Target view: "SwipeRefreshLayout{id=2131689751, res-name=my_refresh_layout, visibility=VISIBLE,
width=480, height=672, has-focus=false, has-focusable=true, has-window-focus=true,
is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false,
is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0,
child-count=2}"
Run Code Online (Sandbox Code Playgroud)
当然,布局是可见的,手动滑动有效,但我不确定我做错了什么?布局横跨整个屏幕,因此Espresso 应该可以对其进行一些操作.
我需要使用Selenium测试JS地理定位功能,我正在使用chromedriver在最新的Chrome上运行测试.
现在问题是Chrome在测试期间提示我启用Geolocation,而且我不知道如何在运行时点击那个小条,所以我拼命寻找一种方法来启动chromedriver和chrome以及一些选项或触发器默认启用此功能.然而,我在这里找到的只是如何完全禁用地理位置.
我该如何解决这个问题?
我的目标是从Android 4.0使用REST Web服务HttpsURLConnection.除非我尝试POST某种方法,否则这种方法很好 这是相关的代码部分:
connection.setDoOutput(true);
connection.setChunkedStreamingMode(0);
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializeObjectToStream(out, object);
byte[] array = out.toByteArray();
connection.getOutputStream().write(array, 0, array.length);
Run Code Online (Sandbox Code Playgroud)
这会引发以下异常:
java.net.HttpRetryException: Cannot retry streamed HTTP body
Run Code Online (Sandbox Code Playgroud)
从调试中我意识到我得到的输出流connection.getOuputStream()是类型的,ChunkedOutputStream并且从挖掘Androids源代码中我认为如果需要重试请求(无论出于何种原因),它会引发上述异常,因为它会发现它是不是用RetryableOutputStream它想在那里.
现在的问题是:如何使我的HttpsURLConnection返回这样的RetryableOutputStream,或者更确切地说,如何正确地阻止分块请求编码?我以为我已经这样做了setChunkedStreamingMode(0),但显然事实并非如此......
[编辑]
不,执行java.net.HTTPUrlConnection忽略0或更低的流模式:
public void setChunkedStreamingMode(int chunkLength) {
[...]
if (chunkLength <= 0) {
this.chunkLength = HttpEngine.DEFAULT_CHUNK_LENGTH;
} else {
this.chunkLength = chunkLength;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在重新设计我现在的应用程序,大致跟随Chris Bane的优秀介绍.但有一件事让我震惊:
如果我将新的v7-appcompat设置Toolbar为动作栏替换setSupportActionBar()并从中派生我的主题Theme.AppCompat.Light.NoActionBar,那么上下文操作栏(CAB)仍会弹出并按下我的内容(包括工具栏):

现在,我可以尝试使CAB覆盖我的工具栏,但我认为后退按钮的UI(基本上是指标和位置)和CAB的文本也与工具栏不匹配(如图所示),所以我想知道工具栏实现中是否存在"CAB"模式,以便工具栏也可以接管这个角色?
我想写一个Espresso matcher来验证`ImageView'是否有特定的位图设置.由于应用程序通过Glide进行图像加载,我认为在实际比较预期位图和实际位图之前,我必须在测试方面做同样的事情以考虑裁剪/居中.
这是我到目前为止提出的:
BitmapRequestBuilder<Uri, Bitmap> bitmapRequest = Glide.with(imageView.getContext())
.load(Uri.parse("file:///android_asset/" + mPath))
.asBitmap();
switch (imageView.getScaleType()) {
case CENTER_CROP:
bitmapRequest.centerCrop();
break;
case FIT_CENTER:
case FIT_START:
case FIT_END:
bitmapRequest.fitCenter();
break;
default:
// no scaling applied to the ImageView under test
}
AtomicReference<Bitmap> bmRef = new AtomicReference<>();
bitmapRequest.into(new SimpleTarget<Bitmap>(
imageView.getMeasuredWidth(),
imageView.getMeasuredHeight()
) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
bmRef.set(resource);
}
});
// ???
try {
Bitmap expected = bmRef.get();
return expected.sameAs(bitmap);
} catch (Exception e) {
throw new IllegalStateException("could …Run Code Online (Sandbox Code Playgroud) 我知道Android的Ant工具带有内置的Emma代码覆盖,但Emma的报告看起来越来越过时,无法轻松集成到其他报告甚至Sonar中,主要是因为Emma缺乏详细的XML报告格式.我想要实现的是
我还有其他选择吗?
编辑: 关于sonar-devel的这篇文章可能总结了Android代码覆盖支持的当前状态以及Sonar支持.叹
在棒棒糖之前,生活很简单.你有一个main thread- 有时也被称为UI thread- 所有GUI的东西都已完成(你在任何情况下都可以避免长时间运行以避免任何类型的打嗝),并且你在background threads那里完成了这个长期运行的东西.
现在在Lollipop及更高版本的Android,IIRC这个术语UI thread似乎指向用户新的RenderThread,一个线程,例如用于动画涟漪,活动之间的英雄元素或任何其他类型的动画,需要发生在主要线程处理输入事件或正在忙于在后台为您创建新的东西.
对于Android Studio 1.3,所有三种线程类型现在都有自己的注释,以表示特定的代码应该在这个特定的线程上运行.对我来说,现在的问题是:作为应用程序开发人员,我是否应该关注运行任何内容UIThread,即在我的应用程序中RenderThread使用@UIThread?