1

エスプレッソでテストしたい設定画面のあるアプリに取り組んでいます。この設定画面で、次のような PreferenceFragment を追加します。

setContentView(R.layout.settings_activity);

...

getFragmentManager().beginTransaction().replace(R.id.content_frame, new V4SettingsFragment()).commit();

設定_アクティビティ

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/toolbar" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@id/content_frame"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:id="@+id/shadow_prelollipop"
        android:background="@drawable/shadow_toolbar" />
</FrameLayout>

この設定画面は普通に使う分には問題ないのですが、エスプレッソテストで設定を確認・変更しようとすると何かがおかしいです。

AndroidJUnit4 テスト

@Rule
public ActivityTestRule<V4SettingsActivity> settingsActivity = new ActivityTestRule<>(V4SettingsActivity.class);

@Test
public void checkFragement(){

    Resources resources = settingsActivity.getActivity().getResources();


    Matcher<View> adapterMatcher = allOf(isDescendantOfA(withId(R.id.content_frame)), withId(android.R.id.list));

    onData(allOf(
            is(instanceOf(Preference.class)),
            withKey(resources.getString(R.string.prefs_category_display_preferredviewer))))
           .inAdapterView(adapterMatcher);
}

これは私がエスプレッソから得るエラーです:

....CustomFailureHandler$NoHierarchyException: android.support.test.espresso.AmbiguousViewMatcherException: Multiple Ambiguous Views found for matcher (is descendant of a: with id: ....:id/content_frame and with id: android:id/list)

さらに調査したところ、アプリを通常に使用する場合と Espresso テストで使用する場合で、設定ページのレイアウトが異なって見えることがわかりました。通常、id content_frame を持つ LinearLayout 内には、1 つの子 (id android:id/list を持つ) しかありません。しかし、Espresso テストを実行すると、同じ LinearLayout ビューに 2 つの子が含まれていることがわかります。

Espresso テスト内で次のコードを実行すると:

ViewGroup viewById = (ViewGroup) settingsActivity.getActivity().findViewById(R.id.content_frame);
    for(int i=0; i<viewById.getChildCount(); i++){
        ViewGroup vgChild = (ViewGroup) viewById.getChildAt(i);
        Log.d(TAG, "switchDirectToHtmlReader() " + vgChild.toString()); // called two times during espresso test
    }

次の出力が得られます。

D: switchDirectToHtmlReader() android.widget.ListView{2854b6d9 V.ED.VC. ......ID 0,0-1080,0 #102000a android:id/list}
D: switchDirectToHtmlReader() android.widget.LinearLayout{37eb976a     V.E..... ........ 0,0-1080,1677}

しかし、エスプレッソ テストではなくアクティビティで同様のコードを実行すると、次の出力が得られます。

D: onCreate() android.widget.ListView{149b8a90 V.ED.VC. ......I. 0,0-0,0 #102000a android:id/list}

そのため、通常アプリを使用すると取得する id android:id/list を持つ 1 つの子だけではなく、エスプレッソ テストを実行すると 2 つの子が取得されます。

私は何を間違っていますか、なぜ階層は通常の使用で階層とそれほど異なって見えるのですか?

4

1 に答える 1