Blog | Phodal - A Growth Engineerhttp://www.phodal.com/blog/2017-12-16T14:36:53.924689+00:00Blog大前端 Android 开发日记四:Toolbar 问题及测试编写2017-12-14T14:36:13+00:002017-12-16T14:36:53.924689+00:00Phodal Huanghttp://www.phodal.com/blog/author/root/http://www.phodal.com/blog/android-notes-daily-toolbar-problem-write-test/在之前的两天里,已经实现了大部分的功能。但是,仍然遇到了一些 Toolbar 的问题,除了努力地解决这个问题之外,还写了几个简单的测试。
模块化 APK 的 Toolbar 后退按钮
---
由于应用程序采用的是类似于 RePlugin 的插件化机制。因为在使用样式来将 Toolbar 改为白色的时候,在另外一个 APK 里没有对应的资源。于是,便想着将 Toolbar 改成了个组件:
```
<relativelayout android:background="@color/black" android:gravity="center_vertical" android:layout_height="56dp" android:layout_width="match_parent">
<include layout="@layout/back_button"></include>
<textview android:layout_centerhorizontal="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:padding="@dimen/length_16" android:text="@string/something"></textview>
</relativelayout>
```
对应的后退按钮:
```
<?xml version="1.0" encoding="utf-8"?>
<linearlayout android:gravity="center" android:id="@+id/fast_back" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
<imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingbottom="@dimen/length_16" android:paddingend="@dimen/length_24" android:paddingstart="@dimen/length_16" android:paddingtop="@dimen/length_16" android:src="@drawable/back_icon"></imageview>
</linearlayout>
```
这样在另外一个 APK 中,只需要有相应的图片资源即可。在 Code Diff 的时候,被告知这个可以用图片来实现——做的时候,忘记了这个。只能明天再去改吧。
Android 处理 Toolbar 后退
---
对应的处理后退的逻辑,也由:
```
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
```
变成了简单的后退了。
```
@OnClick(R.id.fast_back)
void clickBackText() {
onBackPressed();
}
```
测试
---
完成了对 Toolbar 的控制之后,我便写了一个测试:
```
@Mock
private DetailView detailView;
@Mock
private DetailRepository detailRepository;
private DetailPresenter detailPresenter;
@Test
public void shouldShowDetail() throws Exception {
DetailModel detail = mock(DetailModel.class);
when(detail.getText()).thenReturn(anyListOf(Related.class));
detailPresenter.onLoadDetailSuccess(detail);
verify(detailView).showDetail(any(DetailModel.class));
}
```
在这个测试里,简单的测试了一个回调成功时,会调用显示详情的逻辑。