0

I am trying to test a checkbox on a PreferenceScreen.

The PreferenceScreen contains two CheckBoxPreferences, each with unique android:id.

    <CheckBoxPreference
        android:id="@+id/first_checkbox"
        android:key="first_checkbox"
        android:title="First checkbox" />

    <CheckBoxPreference
        android:id="@+id/second_checkbox"
        android:key="second_checkbox"
        android:title="Second checkbox" />

Here are the id values from R.java file:

public static final class id {
    public static final int first_checkbox=0x7f0a0000;
    public static final int second_checkbox=0x7f0a0001;
}

On my test:

    ViewInteraction cbxFirst = onView(withId(R.id.first_checkbox));

I see:

    NoMatchingViewException: No views in  hierarchy found matching: with id:  
    com.test.fragmentpreference:id/first_checkbox

When I try to search by 'android.R.id.checkbox' instead of 'R.id.first_checkbox':

    ViewInteraction cbxFirst = onView(withId(android.R.id.checkbox));

I receive:

    AmbiguousViewMatcherException: 'with id: android:id/checkbox' matches multiple views in the hierarchy.

My question is: How can I test the first CheckBoxPreference using 'first_checkbox' id ?

T. Ellis
  • 1
  • 2
  • How about accessing it with text like `onView(withText("First checkbox"));`? – Kamran Ahmed Oct 22 '16 at 09:25
  • @Kamran Ahmed I am able to access it with 'onView(withText("First checkbox"));' But my goal is to understand how to use unique android:id to access a specific view, because in many cases using 'withText()' is more error-prone when you have many views with slightly different text. – T. Ellis Oct 22 '16 at 09:46
  • I agree, I was just trying to understand more about the problem. In a normal case it should have worked... I am not sure if `CheckBoxPreference` works differently, it should ideally not. – Kamran Ahmed Oct 22 '16 at 09:48

1 Answers1

0

According to https://developer.android.com/reference/android/preference/CheckBoxPreference.html :

"android:id" is not an allowable xml attribute of CheckBoxPreference.

In this case I have mistakenly added the "android:id" to the CheckBoxPreference declaration in preference.xml.

T. Ellis
  • 1
  • 2