我有一个函数,我想测试哪个在okHttp回调中运行.我正在尝试使用Robolectrics测试它,但回调从未执行过.我认为这是因为测试在请求后继续运行而不等待okHttp返回.到目前为止,我已经尝试过:
ShadowLooper.pauseMainLooper();
Robolectric.flushBackgroundScheduler();
ShadowLooper.unPauseMainLooper();
Run Code Online (Sandbox Code Playgroud)
但那没用.有什么建议?
编辑:
这是我的代码示例:
ApiClient.sendSomeDataToServer(data, callback);
Run Code Online (Sandbox Code Playgroud)
其中ApiClient是包含okHttp客户端的帮助程序类.sendSomeDataToServer API调用看起来像这样:
public static void sendSomeDataToServer(MyObject data, Callback callback){
final Request request = new Request.Builder()
.url(API_SOME_URL)
.post(RequestBody.create(JSON, myObject.getAsJson().toString()))
.build();
sHttpClient.newCall(request).enqueue(callback);
}
Run Code Online (Sandbox Code Playgroud)
其中sHttpClient是初始化的OkHttpClient.
我可以通过Thread.sleep(5000)在我的测试代码中强制并提供自定义回调来测试上面的执行.我试图测试的代码是在回调中.有什么建议我可以测试吗?我真的不想更改主代码以适应测试框架 - 应该反过来.
我希望用户将一堆视频/照片导入我的应用程序。这是我之前使用的代码:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setType("image/*,video/*");
activity.startActivityForResult(intent, REQUEST_CODE_PICK_MEDIA);
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,上面只返回来自新 Google 相册应用的照片。如果我仅将数据类型更改为“video/*”,则照片应用会返回视频。这是给 KitKat+ 的
编辑:
我尝试了以下代码 - 它适用于某些画廊,但不适用于大多数画廊,而不是 Google 相册:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
if (AndroidHelper.isKitKatAndAbove()) {
Log.d(TAG, "Pick from gallery (KitKat+)");
String[] mimeTypes = {"image/*", "video/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
activity.startActivityForResult(intent, REQUEST_CODE_PICK_MEDIA);
} else {
Log.d(TAG, "Pick from gallery (Compatibility)");
activity.startActivityForResult(intent, REQUEST_CODE_PICK_MEDIA);
}
Run Code Online (Sandbox Code Playgroud) 这是我的FAB定义:
<android.support.design.widget.FloatingActionButton
android:id="@+id/button_capture_action_show_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:focusable="true"
android:src="@drawable/c"
Run Code Online (Sandbox Code Playgroud)
在棒棒糖前设备上渲染时似乎有一些额外的填充,我似乎无法将其删除.
棒棒糖渲染:
前棒棒糖渲染:
任何建议非常感谢..
我正在尝试获取Snackbar's TextView。这是我正在使用的代码:
Snackbar snackbar = Snackbar.make(mRecyclerView, message, Snackbar.LENGTH_LONG);
View view = snackbar.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
Run Code Online (Sandbox Code Playgroud)
运行上面的总是设置tv为空。我正在使用设计库版本23.0.0,在小吃店的布局资源中,我可以看到TextViewwith id snackbar_text。我错过了什么?
我是 RxJava(特别是 RxJava2)的新手,我在似乎相对简单的操作上遇到了一些麻烦。我需要从数据库中获取一些数据,遍历数据(它表示为一个列表),对每个项目执行操作,将数据包装在另一个对象中并返回。这是我到目前为止:
mDataManager
.getStuffList(id)
.flatMapIterable(listOfStuff -> listOfStuff)
.flatMap(item ->
mDataManager
.performCount(id, item.getTitle())
.doOnNext(item::setCounter)
.takeLast(1)
.map(counter -> item))
.toList()
.toObservable()
.flatMap(listOfStuff -> Observable.just(new StuffWrapper(listOfStuff));
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我的数据管理器调用永远不会完成。这个想法是,每当底层数据发生变化时,UI 也会发生变化。但是,如果没有完成这些调用, toList() 将不会发出该事件。
我有一个测试创建一个活动,试图从数据库中获取一些数据.这与SQLiteException失败
17:40:40.528 [DEBUG] [TestEventLogger] android.database.sqlite.SQLiteException: Cannot open SQLite connection, base error code: 14
17:40:40.528 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection.rethrow(ShadowSQLiteConnection.java:53)
17:40:40.528 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection.access$600(ShadowSQLiteConnection.java:30)
17:40:40.529 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection$Connections.execute(ShadowSQLiteConnection.java:443)
17:40:40.529 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection$Connections.open(ShadowSQLiteConnection.java:345)
17:40:40.529 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection.nativeOpen(ShadowSQLiteConnection.java:58)
17:40:40.529 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnection.nativeOpen(SQLiteConnection.java)
17:40:40.529 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
17:40:40.529 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
17:40:40.529 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
17:40:40.530 [DEBUG] [TestEventLogger] at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
17:40:40.530 [DEBUG] …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中有几个片段的透明视图.因此,我的片段位于透明叠加层下方,但是当打开叠加层时,用户无法访问它们,因为触摸叠加层将会忽略它.当我启用TalkBack时,所有工作都很好
我的问题是,当我启用TalkBack并且我滑动以选择下一个元素时,一旦TalkBack完成了叠加层中的所有内容,它将开始将焦点设置在叠加下面的项目上.有没有办法阻止它这样做?类似于android:clickable="true"或使用onClick侦听器使透明叠加层拦截所有点击的东西?
我有一个情况,一个长时间运行的进程被包装在一个Observable.fromCallable().这个过程是一个OkHttp调用,如果终止,将抛出一个IOException.如果订阅了observable,则将一次性存储在a中CompositeDisposable,并按预期处理异常.但是,CompositeDisposable在某些情况下,我的代码将清除,在OkHttp没有错误处理的情况下触发线程终止,导致应用程序因未处理的异常而崩溃.这是这个问题的简单单元测试示例:
@Test
public void test(){
CompositeDisposable compositeDisposable = new CompositeDisposable();
Observable<Object> o = Observable.fromCallable(new Callable<Object>() {
@Override
public Object call() throws Exception {
System.out.println("sleeping - this sleep will be interrupted when compositeDisposable gets cleared");
Thread.sleep(3000);
return null;
}
});
compositeDisposable.add(o.subscribeOn(new IoScheduler()).subscribe());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
compositeDisposable.clear();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题?