Espresso で アイドリング

espresso

Espresso は以下の非同期処理は考慮してくれるようですが.

- メッセージキューを利用したUIイベント
- デフォルト AsyncTask のスレッドプールを利用したタスク

Square Island: Espresso: Custom Idling Resource

これらに該当しない処理でも, テスト実行を「あるタイミング」まで待たせたいときがありますよね?

カスタムした「IdlingResource」を使ってみましょう.

Espresso Idling Resource

ここで書かれているアプローチとしては2つ.

Counting running jobs: When a job starts, increment a counter. When it finishes, decrement it. The app is idle if the counter is zero. This approach is very simple and accounts for most situations. CountingIdlingResource does exactly this.

Querying state: It might be more reliable to ask a work queue or an HTTP client (or whatever is doing the background work) if it is busy. If the state is exposed, implementing an Idling Resource is trivial.

これらのうち, 汎用的に使えるジョブカウンターを利用した方法を, Googleサンプルを眺めつつ手順整理しておきます.

Android Testing Codelab

1. IdlingResource ユーティリティの作成

アイドリング向けのユーテリティを作成します.
そのまま2つのクラスを利用してもいいかもしれません.

EspressoIdlingResource.java at master · googlecodelabs/android-testing

SimpleCountingIdlingResource.java at master · googlecodelabs/android-testing

2. テスト対象の Activity に記述

テスト対象となる Activity にテスト時に利用するメソッドを記述しておきます.
テスト時にはこのメソッドを利用して, 先ほどのユーティリティを利用登録します.


    @VisibleForTesting
    public IdlingResource getCountingIdlingResource() {
        return EspressoIdlingResource.getIdlingResource();
    }

AddNoteActivity.java at master · googlecodelabs/android-testing

NotesActivity.java at master · googlecodelabs/android-testing

3. 「待たせる」処理の記述

Presenter や View などのテスト対象クラス内にアイドリングの利用タイミングを記述しておきます.


EspressoIdlingResource.increment(); 


EspressoIdlingResource.decrement(); 

カウンターを増減しながらアイドリングを操作します.
カウンターが0になると処理が再開されます.


    @Override
    public void loadNotes(boolean forceUpdate) {
        mNotesView.setProgressIndicator(true);
        if (forceUpdate) {
            mNotesRepository.refreshData();
        }

        // The network request might be handled in a different thread so make sure Espresso knows
        // that the app is busy until the response is handled.
        EspressoIdlingResource.increment(); // App is busy until further notice

        mNotesRepository.getNotes(new NotesRepository.LoadNotesCallback() {
            @Override
            public void onNotesLoaded(List<Note> notes) {
                EspressoIdlingResource.decrement(); // Set app as idle.
                mNotesView.setProgressIndicator(false);
                mNotesView.showNotes(notes);
            }
        });
    }

AddNoteFragment.java at master · googlecodelabs/android-testing

NotesPresenter.java at master · googlecodelabs/android-testing

4. テスト内で登録する

テスト対象Activityに記述したメソッドを利用してテストコードへ登録します
アイドリング処理がテスト時に反映されます.


    @Before
    public void registerIdlingResource() {
        Espresso.registerIdlingResources(
                mAddNoteIntentsTestRule.getActivity().getCountingIdlingResource());
    }

    @After
    public void unregisterIdlingResource() {
        Espresso.unregisterIdlingResources(
                mAddNoteIntentsTestRule.getActivity().getCountingIdlingResource());
    }

AddNoteScreenTest.java at master · googlecodelabs/android-testing

まとめ

テストにかかるコストがただの自己満足で終わらないようにしたいです.

JakeWharton/okhttp-idling-resource: An Espresso IdlingResource for OkHttp.

PSA: Dont Use Espresso Idling Resources like Google does · Philosophical Hacker


意味不明な Gradle の Warning や Error を消して激ビルド時間短縮

なんとなく Gradle コンソールを開けてみると死ぬほど書き出されてるエラーやワーニング.


AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$8) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$9) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$1) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from s

いつから出ていたのかわかりません.

Proguard 周りなのでしょうが, 調べても意味がわかりませんでした.

が,

Large number of errors during Gradle build after upgrading to Android Studio 2.0 - Stack Overflow

意味のない warning や error は, .idea フォルダを消すことで取り除くことができます。このフォルダは Android Studio 1.5 が作成したもので 2.0 にインポートされています。消しても自動的に作成されます。

ideadir

一度削除すると再作成されますが, 意味不明なログが吐かれなくなり, ビルド時間も数分から数十秒に激速となりました。

...

などと喜んでいたが, また出てる.

...

Issue Tacker を見てみる.

Issue 215748 - android - AGPBI reports errors instead of warnings - Android Open Source Project - Issue Tracker - Google Project Hosting

まだ生きてる issue なのですが, 一時しのぎとして以下とか.


# https://code.google.com/p/android/issues/detail?id=215748
-keepattributes Exceptions, Signature, InnerClasses, EnclosingMethod
-dontoptimize

一応出なくなりますけれども,

速くなりますけれども,

「don't optimize」て...

またはこんな一時しのぎ法も.

Issue 222989 - android - Android Studio 2.2 : Build successful with 1944 errors - Android Open Source Project - Issue Tracker - Google Project Hosting

It affects me too it is caused by gradle not android studio (but it tries to update the gradle)
Temporary solution:
I went back to gradle 2.1.3 (that I used earlier):

classpath 'com.android.tools.build:gradle:2.1.3' ->this works

If I am using classpath 'com.android.tools.build:gradle:2.2.0' I am also getting tons of warning message and the output is an unaligned file...

「com.android.tools.build:gradle のバージョンを下げる」という方法ですが, うまくダウングレードできないような...

なんなんすかね, まったく.

issues_-_android_-_android_open_source_project_-_issue_tracker_-_google_project_hosting

Issues - android - Android Open Source Project - Issue Tracker - Google Project Hosting

2016-10-05

待てないので以下でとりあえず対応.
AGPBI 関連の多量のエラーを排除する


いまどきの MVP 実装パターンを眺めるべし「Android Architecture Blueprints」

このような, 質問がありました.

こんにちは。

私はアプリの開発を2年ほどやっているものです。
しかし, いつも似たようなコードの繰り返しばかりで少しも進歩していないように思っています。

小さい会社なので, モバイルアプリ開発者は私だけで, だれも私のコードを見ることがないので, 私のコードの間違いを指摘されることはなく, 会社で開発されている他のコードを見ることもありません。

最新技術を利用しすばらしい実装を行っているオープンソースアプリのコードを勉強したいです。

そのようなアプリをどこで見つけたらよいか教えて下さい。

よろしくお願いいたします。

I would like to study some up-to-date open source apps, preferably with material design, do you have any suggestions? : androiddev

このような環境で日々を過ごし, 似たようなことを考えてる開発者は多いと思います。

以下のサイトはいかがでしょうか. よくある ToDo アプリでのサンプルとなっています.

Android Architecture Blueprints [beta] - A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.

ここでフォーカスされているのは, 構造, 設計, テスト, メンテナンスのしやすさですが, リファレンスとして, または, アプリ開発のスタート地点として利用できます.

MVP (基本的な Model-View-Presenter)

googlesamples/android-architecture at todo-mvp

このサンプルはすべての基本となります. 構造を持つフレームワークを使わないシンプルな Model-View-Presenter パターンの実装例です. ローカル/リモートのデータソースである Repository を手作業で Dependency Injection しています. 非同期処理はコールバックを利用しています.

mvp

MVP + Loader

googlesamples/android-architecture at todo-mvp-loaders

Repository から Loader を使ってデータを取得します.

- コールバックなしで Repository 内のデータを非同期で読み込むことができる.
- データソースを監視しており, Repository の内容が変化すると新しい結果として配送できる.
- 画面回転のあと自動的に直近の Loader を再接続できる.

mvp-loaders

MVP + Loader + ContentProvider

googlesamples/android-architecture at todo-mvp-contentproviders

Repository からのデータ取得に ContentProvider を使います.

- 構造化されたデータへのアクセス操作可能.
- 別プロセスで稼働しているコードからデータへ接続できる標準的なインターフェースとなる.

mvp-contentproviders

MVP + DataBinding

googlesamples/android-architecture at todo-databinding

DataBiding ライブラリを利用して, UI要素にデータとアクションをバインドしています. 厳格には Model-View-ViewModel や Model-View-Presenter パターンではありません. ViewModel と Presenter 両方使用しています.

DataBinding ライブラリは, データとUI要素を連携する重複するコードを削減してくれます.

- レイアウトファイルがUI要素へのバインドに利用されている.
- 同時にイベントもアクションハンドラーと結合されている.
- データの監視が可能で必要であるときには自動で更新するようにセットすることができる.

mvp-databinding

MVP + Clean Architecture

googlesamples/android-architecture at todo-mvp-clean

Clean Architecture に基づいており, Presentation と Repository レイヤーの間に Domain レイヤーが存在して, アプリを3つのレイヤーに分けています.

Domain レイヤーでは, すべてのビジネスロジックを収納しており, Presenter に使われる use-case か interactor と命名されたクラスから始まる. これらの use-case は Presentation レイヤーから作ることができるすべての実装可能なアクションを提供します.

mvp-clean

その他

その他, 最近流行のフレームワークや考え方を考慮しての実装サンプルも続々と作成中のようです.

Architecture Blueprints の非同期処理実装にみる Android SDK の方向性

MVP + Dagger2
MVP + RxJava
MVP + Fragmentなし

非常に柔軟性のある使えるサンプルとなると思われます. ぜひご確認あれ.

Architecture Blueprints の非同期処理実装にみる Android SDK の方向性

Hacker News Radio (翻訳) - Google Play の Android アプリ