1244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov/*
2244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * Copyright (C) 2015 The Android Open Source Project
3244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov *
4244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
5244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * you may not use this file except in compliance with the License.
6244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * You may obtain a copy of the License at
7244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov *
8244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
9244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov *
10244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
11244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
12244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * See the License for the specific language governing permissions and
14244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * limitations under the License.
15244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov */
16244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovpackage android.support.v7.app;
17244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
1890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikovimport android.content.Context;
19244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.content.DialogInterface;
20244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.graphics.drawable.ColorDrawable;
2190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikovimport android.os.Handler;
2290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikovimport android.os.Message;
23244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.support.annotation.ColorInt;
2490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikovimport android.support.annotation.StringRes;
2580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikovimport android.support.test.InstrumentationRegistry;
26512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banesimport android.support.test.annotation.UiThreadTest;
27244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.support.test.espresso.Espresso;
28244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.support.test.espresso.ViewInteraction;
29244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.support.v7.appcompat.test.R;
30244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.support.v7.testutils.TestUtilsMatchers;
3190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikovimport android.test.suitebuilder.annotation.MediumTest;
32244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.test.suitebuilder.annotation.SmallTest;
3390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikovimport android.text.TextUtils;
34512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banesimport android.util.TypedValue;
358a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikovimport android.view.LayoutInflater;
36244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.view.View;
378a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikovimport android.widget.ArrayAdapter;
38244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.widget.Button;
392d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikovimport android.widget.CheckedTextView;
40244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.widget.ImageView;
412d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikovimport android.widget.ListAdapter;
422d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikovimport android.widget.ListView;
432d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikovimport org.hamcrest.Matcher;
4480e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikovimport org.junit.Before;
4580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikovimport org.junit.Test;
46edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikovimport org.mockito.ArgumentCaptor;
47244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
48244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static android.support.test.espresso.Espresso.onData;
49244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static android.support.test.espresso.Espresso.onView;
50244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static android.support.test.espresso.action.ViewActions.click;
51244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static android.support.test.espresso.assertion.PositionAssertions.isBelow;
52244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
53244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static android.support.test.espresso.assertion.ViewAssertions.matches;
5490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikovimport static android.support.test.espresso.matcher.LayoutMatchers.hasEllipsizedText;
55244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static android.support.test.espresso.matcher.RootMatchers.isDialog;
56244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static android.support.test.espresso.matcher.ViewMatchers.*;
57244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static org.hamcrest.Matchers.instanceOf;
5890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikovimport static org.hamcrest.Matchers.not;
59244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static org.hamcrest.core.AllOf.allOf;
60244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport static org.hamcrest.core.Is.is;
6180e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikovimport static org.junit.Assert.*;
6231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikovimport static org.mockito.Mockito.*;
63244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
642d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov/**
652d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov * Tests in this class make a few assumptions about the underlying implementation of
662d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov * <code>AlertDialog</code>. While the assumptions don't go all the way down to individual
672d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov * <code>R.id</code> references or very specific layout arrangements, internal refactoring
682d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov * of <code>AlertDialog</code> might require corresponding restructuring of the matching
692d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov * tests. Specifically:
702d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov *
712d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov * <ul>
722d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov *     <li>Testing <code>setIcon</code> API assumes that the icon is displayed by a separate
732d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov *     <code>ImageView</code> which is a sibling of a title view.</li>
742d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov *     <li>Testing <code>setMultiChoiceItems</code> API assumes that each item in the list
752d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov *     is rendered by a single <code></code>CheckedTextView</code>.</li>
762d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov *     <li>Testing <code>setSingleChoiceItems</code> API assumes that each item in the list
772d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov *     is rendered by a single <code></code>CheckedTextView</code>.</li>
782d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov * </ul>
792d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov */
8080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikovpublic class AlertDialogTest extends BaseInstrumentationTestCase<AlertDialogTestActivity> {
81244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    private Button mButton;
82244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
83244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    private AlertDialog mAlertDialog;
84244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
85244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public AlertDialogTest() {
86244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        super(AlertDialogTestActivity.class);
87244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
88244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
8980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Before
9080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    public void setUp() {
9180e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final AlertDialogTestActivity activity = mActivityTestRule.getActivity();
92244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        mButton = (Button) activity.findViewById(R.id.test_button);
93244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
94244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
95244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    private void wireBuilder(final AlertDialog.Builder builder) {
96244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        mButton.setOnClickListener(new View.OnClickListener() {
97244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
98244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public void onClick(View v) {
99244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                mAlertDialog = builder.show();
100244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
101244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        });
102244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
103244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
10480e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
105244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
106512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes    @UiThreadTest
107512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes    public void testBuilderTheme() {
108512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes        final Context context = mActivityTestRule.getActivity();
109512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes        final AlertDialog dialog = new AlertDialog.Builder(context, R.style.Theme_TextColors)
110512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes                .setTitle(R.string.alert_dialog_title)
111512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes                .setMessage(R.string.alert_dialog_content)
112512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes                .create();
113512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes
114512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes        final TypedValue tv = new TypedValue();
115512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes        dialog.getContext().getTheme().resolveAttribute(android.R.attr.textColorPrimary, tv, true);
116512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes        assertEquals(0xFF0000FF, tv.data);
117512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes    }
118512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes
119512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes    @Test
120512bb28a39aed8b154b60f3fc1752aa96f7f1125Chris Banes    @SmallTest
121244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testBasicContent() {
12280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
1238a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(context)
124244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
125244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content);
126244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
127244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
128244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
129244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
130244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Test that we're showing a dialog with vertically stacked title and content
1318a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String expectedTitle = context.getString(R.string.alert_dialog_title);
1328a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String expectedMessage = context.getString(R.string.alert_dialog_content);
1338a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedTitle)).inRoot(isDialog()).check(matches(isDisplayed()));
1348a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedMessage)).inRoot(isDialog()).check(matches(isDisplayed()));
1358a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedMessage)).inRoot(isDialog()).check(
1368a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                isBelow(withText(expectedTitle)));
1372d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
13890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        assertNull("No list view", mAlertDialog.getListView());
13990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
14090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        assertEquals("Positive button not shown", View.GONE,
14190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).getVisibility());
14290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        assertEquals("Negative button not shown", View.GONE,
14390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).getVisibility());
14490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        assertEquals("Neutral button not shown", View.GONE,
14590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                mAlertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).getVisibility());
146244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
147244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
1488a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    // Tests for message logic
1498a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
15080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
1518a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    @SmallTest
1528a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    public void testMessageString() {
1538a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String dialogMessage = "Dialog message";
15480e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
1558a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
1568a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setMessage(dialogMessage);
1578a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        wireBuilder(builder);
1588a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
1598a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
1608a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(dialogMessage)).inRoot(isDialog()).check(matches(isDisplayed()));
1618a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
1628a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
16380e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
1648a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    @SmallTest
16580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    public void testMessageStringPostCreation() {
1668a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String dialogInitialMessage = "Initial message";
1678a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String dialogUpdatedMessage = "Updated message";
16880e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
1698a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
1708a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setMessage(dialogInitialMessage);
1718a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        wireBuilder(builder);
1728a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
1738a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Click the button to show the dialog and check that it shows the initial message
1748a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
1758a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(dialogInitialMessage)).inRoot(isDialog()).check(matches(isDisplayed()));
1768a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
1778a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Update the dialog message
17880e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1798a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            @Override
1808a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            public void run() {
1818a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                mAlertDialog.setMessage(dialogUpdatedMessage);
1828a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            }
1838a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        });
1848a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Check that the old message is not showing
1858a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(dialogInitialMessage)).inRoot(isDialog()).check(doesNotExist());
1868a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // and that the new message is showing
1878a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(dialogUpdatedMessage)).inRoot(isDialog()).check(matches(isDisplayed()));
1888a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
1898a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
1908a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    // Tests for custom title logic
1918a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
1928a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    /**
1938a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov     * Helper method to verify that setting custom title hides the default title and shows
1948a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov     * the custom title above the dialog message.
1958a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov     */
1968a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    private void verifyCustomTitle() {
19780e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
1988a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
1998a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Test that we're showing a dialog with vertically stacked custom title and content
2008a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String title = context.getString(R.string.alert_dialog_title);
2018a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String expectedCustomTitle = context.getString(R.string.alert_dialog_custom_title);
2028a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String expectedMessage = context.getString(R.string.alert_dialog_content);
2038a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2048a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Check that the default title is not showing
2058a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(title)).inRoot(isDialog()).check(doesNotExist());
2068a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Check that the custom title is fully displayed with no text eliding and is
2078a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // stacked above the message
2088a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedCustomTitle)).inRoot(isDialog()).check(
2098a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                matches(isCompletelyDisplayed()));
2108a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedCustomTitle)).inRoot(isDialog()).check(
2118a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                matches(not(hasEllipsizedText())));
2128a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedMessage)).inRoot(isDialog()).check(matches(isDisplayed()));
2138a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedMessage)).inRoot(isDialog()).check(
2148a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                isBelow(withText(expectedCustomTitle)));
2158a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
2168a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
21780e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
2188a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    @SmallTest
2198a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    public void testCustomTitle() {
22080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
2218a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final LayoutInflater inflater = LayoutInflater.from(context);
2228a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(context)
2238a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
2248a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
2258a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setCustomTitle(inflater.inflate(R.layout.alert_dialog_custom_title, null, false));
2268a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        wireBuilder(builder);
2278a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2288a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
2298a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2308a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        verifyCustomTitle();
2318a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
2328a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
23380e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
2348a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    @SmallTest
2358a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    public void testCustomTitlePostCreation() {
23680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
2378a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final LayoutInflater inflater = LayoutInflater.from(context);
2388a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final AlertDialog.Builder builder = new AlertDialog.Builder(context)
2398a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
2408a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setMessage(R.string.alert_dialog_content);
2418a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2428a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        mButton.setOnClickListener(new View.OnClickListener() {
2438a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            @Override
2448a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            public void onClick(View v) {
2458a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                mAlertDialog = builder.create();
2468a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2478a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                // Configure custom title
2488a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                mAlertDialog.setCustomTitle(inflater.inflate(
2498a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                        R.layout.alert_dialog_custom_title, null, false));
2508a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2518a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                mAlertDialog.show();
2528a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            }
2538a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        });
2548a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2558a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Click the button to create the dialog, configure custom title and show the dialog
2568a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
2578a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2588a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        verifyCustomTitle();
2598a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
2608a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2618a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    // Tests for custom view logic
2628a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2638a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    /**
2648a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov     * Helper method to verify that setting custom view shows the content of that view.
2658a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov     */
2668a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    private void verifyCustomView() {
26780e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
2688a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2698a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Test that we're showing a dialog with vertically stacked custom title and content
2708a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String expectedCustomText1 = context.getString(R.string.alert_dialog_custom_text1);
2718a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String expectedCustomText2 = context.getString(R.string.alert_dialog_custom_text2);
2728a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2738a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Check that we're showing the content of our custom view
2748a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withId(R.id.alert_dialog_custom_view)).inRoot(isDialog()).check(
2758a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                matches(isCompletelyDisplayed()));
2768a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedCustomText1)).inRoot(isDialog()).check(
2778a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                matches(isCompletelyDisplayed()));
2788a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedCustomText1)).inRoot(isDialog()).check(
2798a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                matches(not(hasEllipsizedText())));
2808a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedCustomText2)).inRoot(isDialog()).check(
2818a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                matches(isCompletelyDisplayed()));
2828a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withText(expectedCustomText2)).inRoot(isDialog()).check(
2838a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                matches(not(hasEllipsizedText())));
2848a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
2858a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
28680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
2878a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    @SmallTest
2888a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    public void testCustomView() {
28980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
2908a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final LayoutInflater inflater = LayoutInflater.from(context);
2918a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(context)
2928a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
2938a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
2948a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setView(inflater.inflate(R.layout.alert_dialog_custom_view, null, false));
2958a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        wireBuilder(builder);
2968a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2978a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
2988a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
2998a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        verifyCustomView();
3008a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
3018a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
30280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
3038a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    @SmallTest
3048a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    public void testCustomViewById() {
30580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
3068a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(context)
3078a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
3088a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
3098a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setView(R.layout.alert_dialog_custom_view);
3108a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        wireBuilder(builder);
3118a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
3128a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
3138a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
3148a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        verifyCustomView();
3158a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
3168a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
31780e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
3188a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    @SmallTest
3198a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    public void testCustomViewPostCreation() {
32080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
3218a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final LayoutInflater inflater = LayoutInflater.from(context);
3228a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final AlertDialog.Builder builder = new AlertDialog.Builder(context)
3238a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
3248a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setMessage(R.string.alert_dialog_content);
3258a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
3268a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        mButton.setOnClickListener(new View.OnClickListener() {
3278a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            @Override
3288a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            public void onClick(View v) {
3298a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                mAlertDialog = builder.create();
3308a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
3318a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                // Configure custom view
3328a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                mAlertDialog.setView(inflater.inflate(
3338a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                        R.layout.alert_dialog_custom_view, null, false));
3348a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
3358a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                mAlertDialog.show();
3368a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov            }
3378a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        });
3388a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
3398a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        // Click the button to create the dialog, configure custom view and show the dialog
3408a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
3418a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
3428a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        verifyCustomView();
3438a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
3448a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
34590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    // Tests for cancel logic
34690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
34780e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
348244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
349244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testCancelCancelableDialog() {
35031c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        DialogInterface.OnCancelListener mockCancelListener =
35131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnCancelListener.class);
35280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
353244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
354244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
355244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setCancelable(true)
35631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                .setOnCancelListener(mockCancelListener);
357244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
358244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
359244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
360244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
361244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Emulate a tap on the device BACK button
362244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        Espresso.pressBack();
363244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
364244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Since our dialog is cancelable, check that the cancel listener has been invoked
36531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verify(mockCancelListener, times(1)).onCancel(mAlertDialog);
366244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
367244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
36880e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
369244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
370244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testCancelNonCancelableDialog() {
37131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        DialogInterface.OnCancelListener mockCancelListener =
37231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnCancelListener.class);
37380e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
374244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
375244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
376244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setCancelable(false)
37731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                .setOnCancelListener(mockCancelListener);
378244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
379244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
380244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
381244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
382244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Emulate a tap on the device BACK button
383244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        Espresso.pressBack();
384244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
385244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Since our dialog is not cancelable, check that the cancel listener has not been invoked
38631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verify(mockCancelListener, never()).onCancel(mAlertDialog);
387244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
388244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
38990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    // Tests for items content logic (simple, single-choice, multi-choice)
39090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
39131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov    private void verifySimpleItemsContent(String[] expectedContent,
39231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            DialogInterface.OnClickListener onClickListener) {
3932d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final int expectedCount = expectedContent.length;
3942d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
3952d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
3962d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
3972d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final ListView listView = mAlertDialog.getListView();
3982d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        assertNotNull("List view is shown", listView);
3992d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
4002d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final ListAdapter listAdapter = listView.getAdapter();
4012d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        assertEquals("List has " + expectedCount + " entries",
4022d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                expectedCount, listAdapter.getCount());
4032d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        for (int i = 0; i < expectedCount; i++) {
4042d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            assertEquals("List entry #" + i, expectedContent[i], listAdapter.getItem(i));
4052d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        }
4062d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
4072d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // Test that all items are showing
4082d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onView(withText("Dialog title")).inRoot(isDialog()).check(matches(isDisplayed()));
4092d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        for (int i = 0; i < expectedCount; i++) {
4102d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            onData(allOf(is(instanceOf(String.class)), is(expectedContent[i]))).inRoot(isDialog()).
4112d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    check(matches(isDisplayed()));
4122d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        }
4132d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
41431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        // Verify that our click listener hasn't been called yet
41531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verify(onClickListener, never()).onClick(any(DialogInterface.class), any(int.class));
4162d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // Test that a click on an item invokes the registered listener
4172d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        int indexToClick = expectedCount - 2;
4182d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onData(allOf(is(instanceOf(String.class)), is(expectedContent[indexToClick]))).
4192d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                inRoot(isDialog()).perform(click());
42031c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verify(onClickListener, times(1)).onClick(mAlertDialog, indexToClick);
4212d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
4222d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
42380e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
4242d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    @SmallTest
4258a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    public void testCustomAdapter() {
42680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final Context context = mActivityTestRule.getActivity();
4278a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        final String[] content = context.getResources().getStringArray(R.array.alert_dialog_items);
42831c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        final DialogInterface.OnClickListener mockClickListener =
42931c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnClickListener.class);
43080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
4318a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
4328a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov                .setAdapter(
43331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                        new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, content),
43431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                        mockClickListener);
4358a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov        wireBuilder(builder);
4368a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
43731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verifySimpleItemsContent(content, mockClickListener);
4388a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    }
4398a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov
44080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
4418a7d734aa813d9697611038c6ac3e68861898bcdKirill Grouchnikov    @SmallTest
4422d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    public void testSimpleItemsFromRuntimeArray() {
4432d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final String[] content = new String[] { "Alice", "Bob", "Charlie", "Delta" };
44431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        final DialogInterface.OnClickListener mockClickListener =
44531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnClickListener.class);
44680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
4472d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
44831c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                .setItems(content, mockClickListener);
4492d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        wireBuilder(builder);
4502d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
45131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verifySimpleItemsContent(content, mockClickListener);
4522d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
453244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
45480e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
455244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
4562d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    public void testSimpleItemsFromResourcesArray() {
45731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        final DialogInterface.OnClickListener mockClickListener =
45831c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnClickListener.class);
45980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
460244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
46131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                .setItems(R.array.alert_dialog_items, mockClickListener);
462244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
463244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
46480e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        verifySimpleItemsContent(mActivityTestRule.getActivity().getResources().getStringArray(
46531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                R.array.alert_dialog_items), mockClickListener);
4662d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
4672d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
4682d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    /**
4692d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * Helper method to verify the state of the multi-choice items list. It gets the String
4702d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * array of content and verifies that:
4712d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     *
4722d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * 1. The items in the array are rendered as CheckedTextViews inside a ListView
4732d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * 2. Each item in the array is displayed
4742d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * 3. Checked state of each row in the ListView corresponds to the matching entry in the
4752d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     *    passed boolean array
4762d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     */
4772d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    private void verifyMultiChoiceItemsState(String[] expectedContent,
4782d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            boolean[] checkedTracker) {
4792d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final int expectedCount = expectedContent.length;
4802d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
4812d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final ListView listView = mAlertDialog.getListView();
4822d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        assertNotNull("List view is shown", listView);
4832d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
4842d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final ListAdapter listAdapter = listView.getAdapter();
4852d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        assertEquals("List has " + expectedCount + " entries",
4862d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                expectedCount, listAdapter.getCount());
4872d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        for (int i = 0; i < expectedCount; i++) {
4882d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            assertEquals("List entry #" + i, expectedContent[i], listAdapter.getItem(i));
4892d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        }
4902d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
4912d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        for (int i = 0; i < expectedCount; i++) {
4922d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            Matcher checkedStateMatcher = checkedTracker[i] ? TestUtilsMatchers.isCheckedTextView() :
4932d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    TestUtilsMatchers.isNonCheckedTextView();
4942d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            // Check that the corresponding row is rendered as CheckedTextView with expected
4952d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            // checked state.
4962d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            onData(allOf(is(instanceOf(String.class)), is(expectedContent[i]))).inRoot(isDialog()).
4972d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    check(matches(allOf(
4982d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            isDisplayed(),
4992d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            isAssignableFrom(CheckedTextView.class),
5002d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            isDescendantOfA(isAssignableFrom(ListView.class)),
5012d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            checkedStateMatcher)));
5022d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        }
5032d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
5042d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
5052d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    private void verifyMultiChoiceItemsContent(String[] expectedContent,
5062d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            final boolean[] checkedTracker) {
5072d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final int expectedCount = expectedContent.length;
5082d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
509244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
510244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
5112d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final ListView listView = mAlertDialog.getListView();
5122d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        assertNotNull("List view is shown", listView);
5132d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
5142d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final ListAdapter listAdapter = listView.getAdapter();
5152d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        assertEquals("List has " + expectedCount + " entries",
5162d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                expectedCount, listAdapter.getCount());
5172d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        for (int i = 0; i < expectedCount; i++) {
5182d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            assertEquals("List entry #" + i, expectedContent[i], listAdapter.getItem(i));
5192d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        }
5202d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
521244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Test that all items are showing
522244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withText("Dialog title")).inRoot(isDialog()).check(matches(isDisplayed()));
5232d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifyMultiChoiceItemsState(expectedContent, checkedTracker);
5242d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
5252d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // We're going to click item #1 and test that the click listener has been invoked to
5262d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // update the original state array
5272d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        boolean[] expectedAfterClick1 = checkedTracker.clone();
5282d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        expectedAfterClick1[1] = !expectedAfterClick1[1];
5292d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onData(allOf(is(instanceOf(String.class)), is(expectedContent[1]))).
5302d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                inRoot(isDialog()).perform(click());
5312d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifyMultiChoiceItemsState(expectedContent, expectedAfterClick1);
5322d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
5332d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // Now click item #1 again and test that the click listener has been invoked to update the
5342d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // original state array again
5352d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        expectedAfterClick1[1] = !expectedAfterClick1[1];
5362d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onData(allOf(is(instanceOf(String.class)), is(expectedContent[1]))).
5372d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                inRoot(isDialog()).perform(click());
5382d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifyMultiChoiceItemsState(expectedContent, expectedAfterClick1);
5392d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
5402d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // Now we're going to click the last item and test that the click listener has been invoked
5412d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // to update the original state array
5422d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        boolean[] expectedAfterClickLast = checkedTracker.clone();
5432d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        expectedAfterClickLast[expectedCount - 1] = !expectedAfterClickLast[expectedCount - 1];
5442d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onData(allOf(is(instanceOf(String.class)), is(expectedContent[expectedCount - 1]))).
5452d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                inRoot(isDialog()).perform(click());
5462d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifyMultiChoiceItemsState(expectedContent, expectedAfterClickLast);
5472d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
548244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
54980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
5502d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    @SmallTest
5512d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    public void testMultiChoiceItemsFromRuntimeArray() {
5522d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final String[] content = new String[] { "Alice", "Bob", "Charlie", "Delta" };
5532d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final boolean[] checkedTracker = new boolean[] { false, true, false, false };
55480e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
5552d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
5562d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                .setMultiChoiceItems(
5572d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                        content, checkedTracker,
5582d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                        new DialogInterface.OnMultiChoiceClickListener() {
5592d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            @Override
5602d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            public void onClick(DialogInterface dialog, int which,
5612d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                                    boolean isChecked) {
5622d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                                checkedTracker[which] = isChecked;
5632d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            }
5642d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                        });
5652d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        wireBuilder(builder);
5662d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
5672d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // Pass the same boolean[] array as used for initialization since our click listener
5682d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // will be updating its content.
5692d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifyMultiChoiceItemsContent(content, checkedTracker);
5702d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
5712d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
57280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
5732d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    @SmallTest
5742d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    public void testMultiChoiceItemsFromResourcesArray() {
5752d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final boolean[] checkedTracker = new boolean[] { true, false, true, false };
57680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
5772d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
5782d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                .setMultiChoiceItems(R.array.alert_dialog_items, checkedTracker,
5792d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                        new DialogInterface.OnMultiChoiceClickListener() {
5802d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            @Override
5812d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            public void onClick(DialogInterface dialog, int which,
5822d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                                    boolean isChecked) {
5832d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                                checkedTracker[which] = isChecked;
5842d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            }
5852d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                        });
5862d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        wireBuilder(builder);
5872d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
5882d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifyMultiChoiceItemsContent(
58980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov                mActivityTestRule.getActivity().getResources().getStringArray(
59080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov                        R.array.alert_dialog_items),
5912d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                checkedTracker);
5922d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
5932d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
5942d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    /**
5952d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * Helper method to verify the state of the single-choice items list. It gets the String
5962d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * array of content and verifies that:
5972d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     *
5982d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * 1. The items in the array are rendered as CheckedTextViews inside a ListView
5992d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * 2. Each item in the array is displayed
6002d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * 3. Only one row in the ListView is checked, and that corresponds to the passed
6012d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     *    integer index.
6022d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     */
6032d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    private void verifySingleChoiceItemsState(String[] expectedContent,
6042d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            int currentlyExpectedSelectionIndex) {
6052d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final int expectedCount = expectedContent.length;
6062d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6072d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final ListView listView = mAlertDialog.getListView();
6082d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        assertNotNull("List view is shown", listView);
6092d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6102d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final ListAdapter listAdapter = listView.getAdapter();
6112d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        assertEquals("List has " + expectedCount + " entries",
6122d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                expectedCount, listAdapter.getCount());
6132d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        for (int i = 0; i < expectedCount; i++) {
6142d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            assertEquals("List entry #" + i, expectedContent[i], listAdapter.getItem(i));
6152d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        }
6162d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6172d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        for (int i = 0; i < expectedCount; i++) {
6182d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            Matcher checkedStateMatcher = (i == currentlyExpectedSelectionIndex) ?
6192d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    TestUtilsMatchers.isCheckedTextView() :
6202d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    TestUtilsMatchers.isNonCheckedTextView();
6212d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            // Check that the corresponding row is rendered as CheckedTextView with expected
6222d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            // checked state.
6232d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            onData(allOf(is(instanceOf(String.class)), is(expectedContent[i]))).inRoot(isDialog()).
6242d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    check(matches(allOf(
6252d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            isDisplayed(),
6262d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            isAssignableFrom(CheckedTextView.class),
6272d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            isDescendantOfA(isAssignableFrom(ListView.class)),
6282d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                            checkedStateMatcher)));
6292d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        }
6302d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
6312d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6322d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    private void verifySingleChoiceItemsContent(String[] expectedContent,
63331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            int initialSelectionIndex, DialogInterface.OnClickListener onClickListener) {
6342d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final int expectedCount = expectedContent.length;
6352d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        int currentlyExpectedSelectionIndex = initialSelectionIndex;
6362d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6372d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
6382d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6392d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // Test that all items are showing
6402d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onView(withText("Dialog title")).inRoot(isDialog()).check(matches(isDisplayed()));
6412d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifySingleChoiceItemsState(expectedContent, currentlyExpectedSelectionIndex);
6422d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6432d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // We're going to click the first unselected item and test that the click listener has
6442d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // been invoked.
6452d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        currentlyExpectedSelectionIndex = (currentlyExpectedSelectionIndex == 0) ? 1 : 0;
6462d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onData(allOf(is(instanceOf(String.class)),
6472d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                is(expectedContent[currentlyExpectedSelectionIndex]))).
6482d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    inRoot(isDialog()).perform(click());
64931c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verify(onClickListener, times(1)).onClick(mAlertDialog, currentlyExpectedSelectionIndex);
6502d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifySingleChoiceItemsState(expectedContent, currentlyExpectedSelectionIndex);
6512d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6522d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // Now click the same item again and test that the selection has not changed
6532d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onData(allOf(is(instanceOf(String.class)),
6542d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                is(expectedContent[currentlyExpectedSelectionIndex]))).
6552d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                inRoot(isDialog()).perform(click());
65631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verify(onClickListener, times(2)).onClick(mAlertDialog, currentlyExpectedSelectionIndex);
6572d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifySingleChoiceItemsState(expectedContent, currentlyExpectedSelectionIndex);
6582d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
6592d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // Now we're going to click the last item and test that the click listener has been invoked
6602d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        // to update the original state array
6612d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        currentlyExpectedSelectionIndex = expectedCount - 1;
6622d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        onData(allOf(is(instanceOf(String.class)),
6632d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                is(expectedContent[currentlyExpectedSelectionIndex]))).
6642d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                inRoot(isDialog()).perform(click());
66531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verify(onClickListener, times(1)).onClick(mAlertDialog, currentlyExpectedSelectionIndex);
6662d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        verifySingleChoiceItemsState(expectedContent, currentlyExpectedSelectionIndex);
6672d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
6682d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
66980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
6702d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    @SmallTest
6712d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    public void testSingleChoiceItemsFromRuntimeArray() {
6722d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        final String[] content = new String[] { "Alice", "Bob", "Charlie", "Delta" };
67331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        final DialogInterface.OnClickListener mockClickListener =
67431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnClickListener.class);
67580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
6762d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
67731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                .setSingleChoiceItems(content, 2, mockClickListener);
6782d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        wireBuilder(builder);
6792d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
68031c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verifySingleChoiceItemsContent(content, 2, mockClickListener);
6812d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
6822d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
68380e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
6842d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    @SmallTest
6852d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    public void testSingleChoiceItemsFromResourcesArray() {
68631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        final DialogInterface.OnClickListener mockClickListener =
68731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnClickListener.class);
68880e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
6892d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
69031c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                .setSingleChoiceItems(R.array.alert_dialog_items, 1, mockClickListener);
6912d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        wireBuilder(builder);
6922d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
69331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verifySingleChoiceItemsContent(new String[] { "Albania", "Belize", "Chad", "Djibouti" }, 1,
69431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mockClickListener);
695244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
696244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
69790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    // Tests for icon logic
69890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
69980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
700244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
701244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testIconResource() {
70280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
703244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
704244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
705244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setIcon(R.drawable.test_drawable_red);
706244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
707244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
708244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
709244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
710244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
711244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Find the title icon as a visible view that is the sibling of our title
712244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        ViewInteraction titleIconInteraction = onView(allOf(
713244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isAssignableFrom(ImageView.class),
714244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isDisplayed(),
715244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                hasSibling(withText("Dialog title"))));
716244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // And check that it's the expected red color
717244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        titleIconInteraction.check(matches(TestUtilsMatchers.drawable(0xFFFF6030)));
718244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
719244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
72080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
721244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
722244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testIconResourceChangeAfterInitialSetup() throws Throwable {
72380e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
724244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
725244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
726244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setIcon(R.drawable.test_drawable_red);
727244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
728244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
729244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
730244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
731244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
732244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Emulate background loading of the new icon
733244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        Thread.sleep(1000);
734244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
735244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Change the icon
73680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
737244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
738244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public void run() {
739244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                mAlertDialog.setIcon(R.drawable.test_drawable_green);
740244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
741244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        });
742244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
743244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Find the title icon as a visible view that is the sibling of our title
744244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        ViewInteraction titleIconInteraction = onView(allOf(
745244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isAssignableFrom(ImageView.class),
746244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isDisplayed(),
747244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                hasSibling(withText("Dialog title"))));
748244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // And check that it's the expected (newly set) green color
749244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        titleIconInteraction.check(matches(TestUtilsMatchers.drawable(0xFF50E080)));
750244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
751244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
75280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
753244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
754244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testIconResourceChangeWithNoInitialSetup() throws Throwable {
75580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
756244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
757244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content);
758244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
759244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
760244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
761244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
762244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
763244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Emulate background loading of the new icon
764244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        Thread.sleep(1000);
765244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
766244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Change the icon
76780e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
768244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
769244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public void run() {
770244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                mAlertDialog.setIcon(R.drawable.test_drawable_green);
771244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
772244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        });
773244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
774244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Find the title icon as a visible view that is the sibling of our title
775244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        ViewInteraction titleIconInteraction = onView(allOf(
776244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isAssignableFrom(ImageView.class),
777244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isDisplayed(),
778244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                hasSibling(withText("Dialog title"))));
779244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // And check that it's the expected (newly set) green color
780244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        titleIconInteraction.check(matches(TestUtilsMatchers.drawable(0xFF50E080)));
781244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
782244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
78380e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
784244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
785244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testIconResourceRemoveAfterInitialSetup() throws Throwable {
78680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
787244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
788244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
789244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setIcon(R.drawable.test_drawable_red);
790244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
791244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
792244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
793244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
794244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
795244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Emulate background resetting of the icon
796244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        Thread.sleep(1000);
797244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
798244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Change the icon
79980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
800244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
801244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public void run() {
802244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                mAlertDialog.setIcon(0);
803244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
804244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        });
805244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
806244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Find the title icon as a visible view that is the sibling of our title
807244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        ViewInteraction titleIconInteraction = onView(allOf(
808244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isAssignableFrom(ImageView.class),
809244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isDisplayed(),
810244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                hasSibling(withText("Dialog title"))));
811244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // And check that we couldn't find the title icon (since it's expected to be GONE)
812244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        titleIconInteraction.check(doesNotExist());
813244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
814244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
81580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
816244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
817244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testIconDrawable() {
81880e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
819244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
820244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
821244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setIcon(new TestDrawable(0xFF807060, 40, 40));
822244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
823244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
824244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
825244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
826244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
827244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Find the title icon as a visible view that is the sibling of our title
828244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        ViewInteraction titleIconInteraction = onView(allOf(
829244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isAssignableFrom(ImageView.class),
830244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isDisplayed(),
831244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                hasSibling(withText("Dialog title"))));
832244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // And check that it's the expected red color
833244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        titleIconInteraction.check(matches(TestUtilsMatchers.drawable(0xFF807060)));
834244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
835244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
83680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
837244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
838244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testIconResourceDrawableAfterInitialSetup() throws Throwable {
83980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
840244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
841244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
842244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setIcon(new TestDrawable(0xFF807060, 40, 40));
843244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
844244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
845244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
846244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
847244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
848244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Emulate background loading of the new icon
849244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        Thread.sleep(1000);
850244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
851244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Change the icon
85280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
853244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
854244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public void run() {
855244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                mAlertDialog.setIcon(new TestDrawable(0xFF503090, 40, 40));
856244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
857244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        });
858244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
859244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Find the title icon as a visible view that is the sibling of our title
860244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        ViewInteraction titleIconInteraction = onView(allOf(
861244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isAssignableFrom(ImageView.class),
862244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isDisplayed(),
863244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                hasSibling(withText("Dialog title"))));
864244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // And check that it's the expected (newly set) green color
865244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        titleIconInteraction.check(matches(TestUtilsMatchers.drawable(0xFF503090)));
866244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
867244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
86880e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
869244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
870244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testIconDrawableChangeWithNoInitialSetup() throws Throwable {
87180e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
872244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
873244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content);
874244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
875244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
876244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
877244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
878244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
879244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Emulate background loading of the new icon
880244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        Thread.sleep(1000);
881244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
882244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Change the icon
88380e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
884244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
885244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public void run() {
886244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                mAlertDialog.setIcon(new TestDrawable(0xFF503090, 40, 40));
887244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
888244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        });
889244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
890244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Find the title icon as a visible view that is the sibling of our title
891244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        ViewInteraction titleIconInteraction = onView(allOf(
892244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isAssignableFrom(ImageView.class),
893244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isDisplayed(),
894244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                hasSibling(withText("Dialog title"))));
895244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // And check that it's the expected (newly set) green color
896244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        titleIconInteraction.check(matches(TestUtilsMatchers.drawable(0xFF503090)));
897244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
898244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
89980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
900244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    @SmallTest
901244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public void testIconDrawableRemoveAfterInitialSetup() throws Throwable {
90280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
903244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
904244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setMessage(R.string.alert_dialog_content)
905244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                .setIcon(new TestDrawable(0xFF807060, 40, 40));
906244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
907244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        wireBuilder(builder);
908244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
909244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
910244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
911244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Emulate background resetting of the icon
912244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        Thread.sleep(1000);
913244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
914244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Change the icon
91580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
916244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
917244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public void run() {
918244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                mAlertDialog.setIcon(null);
919244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
920244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        });
921244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
922244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // Find the title icon as a visible view that is the sibling of our title
923244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        ViewInteraction titleIconInteraction = onView(allOf(
924244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isAssignableFrom(ImageView.class),
925244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                isDisplayed(),
926244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                hasSibling(withText("Dialog title"))));
927244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        // And check that we couldn't find the title icon (since it's expected to be GONE)
928244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        titleIconInteraction.check(doesNotExist());
929244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
930244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
93190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    // Tests for buttons logic
93290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
93390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    /**
93490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * Helper method to verify visibility and text content of dialog buttons. Gets expected texts
93590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * for three buttons (positive, negative and neutral) and for each button verifies that:
93690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     *
93790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * If the text is null or empty, that the button is GONE
93890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * If the text is not empty, that the button is VISIBLE and shows the corresponding text
93990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     */
94090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    private void verifyButtonContent(String expectedPositiveButtonText,
94190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            String expectedNegativeButtonText, String expectedNeutralButtonText) {
94290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        assertTrue("Dialog is showing", mAlertDialog.isShowing());
94390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
94490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        final Button positiveButton = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
94590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        final Button negativeButton = mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
94690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        final Button neutralButton = mAlertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
94790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
94890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (TextUtils.isEmpty(expectedPositiveButtonText)) {
94990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Positive button not shown", View.GONE, positiveButton.getVisibility());
95090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        } else {
95190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Positive button shown", View.VISIBLE, positiveButton.getVisibility());
95290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Positive button text", expectedPositiveButtonText,
95390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    positiveButton.getText());
95490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
95590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
95690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (TextUtils.isEmpty(expectedNegativeButtonText)) {
95790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Negative button not shown", View.GONE, negativeButton.getVisibility());
95890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        } else {
95990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Negative button shown", View.VISIBLE, negativeButton.getVisibility());
96090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Negative button text", expectedNegativeButtonText,
96190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    negativeButton.getText());
96290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
96390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
96490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (TextUtils.isEmpty(expectedNeutralButtonText)) {
96590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Neutral button not shown", View.GONE, neutralButton.getVisibility());
96690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        } else {
96790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Neutral button shown", View.VISIBLE, neutralButton.getVisibility());
96890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            assertEquals("Neutral button text", expectedNeutralButtonText,
96990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    neutralButton.getText());
97090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
97190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    }
97290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
97390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    /**
97490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * Helper method to verify dialog state after a button has been clicked.
97590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     */
97631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov    private void verifyPostButtonClickState(int whichButtonClicked,
977edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov            DialogInterface.OnDismissListener onDismissListener,
978edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov            Handler messageHandler) {
979edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov        // Verify that a Message with expected 'what' field has been posted on our mock handler
980edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov        ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
981edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov        verify(messageHandler, times(1)).sendMessageDelayed(
982edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov                messageArgumentCaptor.capture(), anyInt());
983edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov        assertEquals("Button clicked", whichButtonClicked, messageArgumentCaptor.getValue().what);
984edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov        // Verify that the dialog is no longer showing
98590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        assertFalse("Dialog is not showing", mAlertDialog.isShowing());
98631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        if (onDismissListener != null) {
987edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov            // And that our mock listener has been called when the dialog was dismissed
98831c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            verify(onDismissListener, times(1)).onDismiss(mAlertDialog);
98931c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        }
99031c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov    }
99131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov
99231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov    /**
99331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov     * Helper method to verify dialog state after a button has been clicked.
99431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov     */
99531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov    private void verifyPostButtonClickState(int whichButtonClicked,
99631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            DialogInterface.OnClickListener onClickListener,
99731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            DialogInterface.OnDismissListener onDismissListener) {
99831c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        if (onClickListener != null) {
99931c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            verify(onClickListener, times(1)).onClick(mAlertDialog, whichButtonClicked);
100031c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        }
100131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        assertFalse("Dialog is not showing", mAlertDialog.isShowing());
100231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        if (onDismissListener != null) {
100331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            verify(onDismissListener, times(1)).onDismiss(mAlertDialog);
100431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        }
100590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    }
100690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
100790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    /**
100890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * Helper method to verify button-related logic for setXXXButton on AlertDialog.Builder
100990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * that gets CharSequence parameter. This method configures the dialog buttons based
101090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * on the passed texts (some of which may be null or empty, in which case the corresponding
101190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * button is not configured), tests the buttons visibility and texts, simulates a click
101290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * on the specified button and then tests the post-click dialog state.
101390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     */
101490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    private void verifyDialogButtons(String positiveButtonText, String negativeButtonText,
101590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            String neutralButtonText, int whichButtonToClick) {
101680e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
101790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                .setTitle(R.string.alert_dialog_title);
101890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Configure buttons with non-empty texts
101931c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        DialogInterface.OnClickListener mockClickListener =
102031c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnClickListener.class);
102190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (!TextUtils.isEmpty(positiveButtonText)) {
102231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            builder.setPositiveButton(positiveButtonText, mockClickListener);
102390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
102490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (!TextUtils.isEmpty(negativeButtonText)) {
102531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            builder.setNegativeButton(negativeButtonText, mockClickListener);
102690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
102790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (!TextUtils.isEmpty(neutralButtonText)) {
102831c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            builder.setNeutralButton(neutralButtonText, mockClickListener);
102990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
103090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Set a dismiss listener to verify that the dialog is dismissed on clicking any button
103131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        DialogInterface.OnDismissListener mockDismissListener =
103231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnDismissListener.class);
103331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        builder.setOnDismissListener(mockDismissListener);
103490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
103590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Wire the builder to the button click and click that button to show the dialog
103690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        wireBuilder(builder);
103790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
103890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
103990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Check that the dialog is showing the configured buttons
104090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyButtonContent(positiveButtonText, negativeButtonText, neutralButtonText);
104190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
104290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Click the specified button and verify the post-click state
104390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        String textOfButtonToClick = null;
104490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        switch (whichButtonToClick) {
104590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_POSITIVE:
104690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = positiveButtonText;
104790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
104890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_NEGATIVE:
104990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = negativeButtonText;
105090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
105190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_NEUTRAL:
105290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = neutralButtonText;
105390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
105490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
105590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(textOfButtonToClick)).inRoot(isDialog()).perform(click());
105631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verifyPostButtonClickState(whichButtonToClick, mockClickListener, mockDismissListener);
105790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    }
105890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
105990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    /**
106090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * Helper method to verify button-related logic for setXXXButton on AlertDialog.Builder
106190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * that gets string resource ID parameter. This method configures the dialog buttons based
106290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * on the passed texts (some of which may be null or empty, in which case the corresponding
106390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * button is not configured), tests the buttons visibility and texts, simulates a click
106490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * on the specified button and then tests the post-click dialog state.
106590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     */
106690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    private void verifyDialogButtons(@StringRes int positiveButtonTextResId,
106790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            @StringRes int negativeButtonTextResId,
106890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            @StringRes int neutralButtonTextResId, int whichButtonToClick) {
106980e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        Context context = mActivityTestRule.getActivity();
107090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        String positiveButtonText = null;
107190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        String negativeButtonText = null;
107290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        String neutralButtonText = null;
107390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
107490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        final AlertDialog.Builder builder = new AlertDialog.Builder(context)
107590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                .setTitle(R.string.alert_dialog_title);
107631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        DialogInterface.OnClickListener mockClickListener =
107731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnClickListener.class);
107890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Configure buttons with non-zero text resource IDs
107990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (positiveButtonTextResId != 0) {
108090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            positiveButtonText = context.getString(positiveButtonTextResId);
108131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            builder.setPositiveButton(positiveButtonTextResId, mockClickListener);
108290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
108390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (negativeButtonTextResId != 0) {
108490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            negativeButtonText = context.getString(negativeButtonTextResId);
108531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            builder.setNegativeButton(negativeButtonTextResId, mockClickListener);
108690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
108790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        if (neutralButtonTextResId != 0) {
108890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            neutralButtonText = context.getString(neutralButtonTextResId);
108931c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov            builder.setNeutralButton(neutralButtonTextResId, mockClickListener);
109090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
109190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Set a dismiss listener to verify that the dialog is dismissed on clicking any button
109231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        DialogInterface.OnDismissListener mockDismissListener =
109331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnDismissListener.class);
109431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        builder.setOnDismissListener(mockDismissListener);
109590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
109690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Wire the builder to the button click and click that button to show the dialog
109790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        wireBuilder(builder);
109890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
109990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
110090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Check that the dialog is showing the configured buttons
110190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyButtonContent(positiveButtonText, negativeButtonText, neutralButtonText);
110290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
110390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Click the specified button and verify the post-click state
110490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        String textOfButtonToClick = null;
110590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        switch (whichButtonToClick) {
110690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_POSITIVE:
110790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = positiveButtonText;
110890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
110990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_NEGATIVE:
111090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = negativeButtonText;
111190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
111290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_NEUTRAL:
111390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = neutralButtonText;
111490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
111590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
111690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(textOfButtonToClick)).inRoot(isDialog()).perform(click());
111731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verifyPostButtonClickState(whichButtonToClick, mockClickListener, mockDismissListener);
111890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    }
111990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
112090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    /**
112190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * Helper method to verify button-related logic for setButton on AlertDialog after the
112290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * dialog has been create()'d. This method configures the dialog buttons based
112390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * on the passed texts (some of which may be null or empty, in which case the corresponding
112490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * button is not configured), tests the buttons visibility and texts, simulates a click
112590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * on the specified button and then tests the post-click dialog state.
112690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     */
112790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    private void verifyDialogButtonsPostCreation(final String positiveButtonText,
112890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            final String negativeButtonText, final String neutralButtonText,
112990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            int whichButtonToClick) {
113080e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
113190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                .setTitle(R.string.alert_dialog_title);
113290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Set a dismiss listener to verify that the dialog is dismissed on clicking any button
113331c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        DialogInterface.OnDismissListener mockDismissListener =
113431c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnDismissListener.class);
113531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        builder.setOnDismissListener(mockDismissListener);
113631c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov
113731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        final DialogInterface.OnClickListener mockClickListener =
113831c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnClickListener.class);
113990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
114090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        mButton.setOnClickListener(new View.OnClickListener() {
114190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            @Override
114290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            public void onClick(View v) {
114390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                mAlertDialog = builder.create();
114490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                // Configure buttons with non-empty texts
114590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                if (!TextUtils.isEmpty(positiveButtonText)) {
114690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    mAlertDialog.setButton(DialogInterface.BUTTON_POSITIVE, positiveButtonText,
114731c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                            mockClickListener);
114890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                }
114990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                if (!TextUtils.isEmpty(negativeButtonText)) {
115090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    mAlertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, negativeButtonText,
115131c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                            mockClickListener);
115290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                }
115390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                if (!TextUtils.isEmpty(neutralButtonText)) {
115490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    mAlertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, neutralButtonText,
115531c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                            mockClickListener);
115690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                }
115790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
115890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                mAlertDialog.show();
115990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            }
116090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        });
116190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
116290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Click the button to create the dialog, configure the buttons and show the dialog
116390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
116490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
116590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Check that the dialog is showing the configured buttons
116690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyButtonContent(positiveButtonText, negativeButtonText, neutralButtonText);
116790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
116890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Click the specified button and verify the post-click state
116990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        String textOfButtonToClick = null;
117090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        switch (whichButtonToClick) {
117190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_POSITIVE:
117290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = positiveButtonText;
117390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
117490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_NEGATIVE:
117590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = negativeButtonText;
117690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
117790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_NEUTRAL:
117890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = neutralButtonText;
117990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
118090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
118190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(textOfButtonToClick)).inRoot(isDialog()).perform(click());
118231c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        verifyPostButtonClickState(whichButtonToClick, mockClickListener, null);
118390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    }
118490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
118590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    /**
118690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * Helper method to verify button-related logic for setButton on AlertDialog after the
118790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * dialog has been create()'d. This method configures the dialog buttons based
118890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * on the passed texts (some of which may be null or empty, in which case the corresponding
118990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * button is not configured), tests the buttons visibility and texts, simulates a click
119090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     * on the specified button and then tests the post-click dialog state.
119190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov     */
119290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    private void verifyDialogButtonsPostCreationMessage(final String positiveButtonText,
119390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            final String negativeButtonText, final String neutralButtonText,
119490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            int whichButtonToClick) {
119580e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        final AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
119690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                .setTitle(R.string.alert_dialog_title);
119790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Set a dismiss listener to verify that the dialog is dismissed on clicking any button
119831c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        DialogInterface.OnDismissListener mockDismissListener =
119931c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov                mock(DialogInterface.OnDismissListener.class);
120031c1690d6ed7ebec75bd9f6c0c74ac29528bd2f8Kirill Grouchnikov        builder.setOnDismissListener(mockDismissListener);
120190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
1202edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov        final Handler mockMessageHandler = mock(Handler.class);
120390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        mButton.setOnClickListener(new View.OnClickListener() {
120490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            @Override
120590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            public void onClick(View v) {
120690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                mAlertDialog = builder.create();
120790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                // Configure buttons with non-empty texts
120890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                if (!TextUtils.isEmpty(positiveButtonText)) {
120990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    mAlertDialog.setButton(DialogInterface.BUTTON_POSITIVE, positiveButtonText,
1210edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov                            Message.obtain(mockMessageHandler, DialogInterface.BUTTON_POSITIVE));
121190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                }
121290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                if (!TextUtils.isEmpty(negativeButtonText)) {
121390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    mAlertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, negativeButtonText,
1214edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov                            Message.obtain(mockMessageHandler, DialogInterface.BUTTON_NEGATIVE));
121590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                }
121690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                if (!TextUtils.isEmpty(neutralButtonText)) {
121790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                    mAlertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, neutralButtonText,
1218edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov                            Message.obtain(mockMessageHandler, DialogInterface.BUTTON_NEUTRAL));
121990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                }
122090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
122190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                mAlertDialog.show();
122290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            }
122390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        });
122490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
122590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Click the button to create the dialog, configure the buttons and show the dialog
122690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
122790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
122890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Check that the dialog is showing the configured buttons
122990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyButtonContent(positiveButtonText, negativeButtonText, neutralButtonText);
123090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
123190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Click the specified button and verify the post-click state
123290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        String textOfButtonToClick = null;
123390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        switch (whichButtonToClick) {
123490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_POSITIVE:
123590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = positiveButtonText;
123690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
123790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_NEGATIVE:
123890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = negativeButtonText;
123990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
124090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov            case DialogInterface.BUTTON_NEUTRAL:
124190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                textOfButtonToClick = neutralButtonText;
124290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                break;
124390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        }
124490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(textOfButtonToClick)).inRoot(isDialog()).perform(click());
1245edfcdcf21924eae3c7c2999ec22c0d9dc84a6cddKirill Grouchnikov        verifyPostButtonClickState(whichButtonToClick, mockDismissListener, mockMessageHandler);
124690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    }
124790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
124880e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
124990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    @SmallTest
125090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    public void testButtonVisibility() {
125190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        final String positiveButtonText = "Positive button";
125290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        final String negativeButtonText = "Negative button";
125390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        final String neutralButtonText = "Neutral button";
125480e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov        AlertDialog.Builder builder = new AlertDialog.Builder(mActivityTestRule.getActivity())
125590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                .setTitle(R.string.alert_dialog_title)
125690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                .setPositiveButton(positiveButtonText, null)
125790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                .setNegativeButton(negativeButtonText, null)
125890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                .setNeutralButton(neutralButtonText, null);
125990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        wireBuilder(builder);
126090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
126190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withId(R.id.test_button)).perform(click());
126290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
126390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Positive button should be fully displayed with no text eliding
126490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(positiveButtonText)).inRoot(isDialog()).check(
126590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                matches(isCompletelyDisplayed()));
126690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(positiveButtonText)).inRoot(isDialog()).check(
126790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                matches(not(hasEllipsizedText())));
126890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
126990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Negative button should be fully displayed with no text eliding
127090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(negativeButtonText)).inRoot(isDialog()).check(
127190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                matches(isCompletelyDisplayed()));
127290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(negativeButtonText)).inRoot(isDialog()).check(
127390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                matches(not(hasEllipsizedText())));
127490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
127590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Neutral button should be fully displayed with no text eliding
127690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(neutralButtonText)).inRoot(isDialog()).check(
127790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                matches(isCompletelyDisplayed()));
127890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        onView(withText(neutralButtonText)).inRoot(isDialog()).check(
127990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                matches(not(hasEllipsizedText())));
128090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    }
128190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
128280e9962bebc88b43b59807f36b4e43fa2fc91c8aKirill Grouchnikov    @Test
128390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    @MediumTest
128490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    public void testButtons() {
128590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Positive-only button
128690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtons("Positive", null, null, AlertDialog.BUTTON_POSITIVE);
128790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtons(R.string.alert_dialog_positive_button, 0, 0,
128890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                AlertDialog.BUTTON_POSITIVE);
128990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtonsPostCreation("Post positive", null, null, AlertDialog.BUTTON_POSITIVE);
129090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtonsPostCreationMessage("Message positive", null, null,
129190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                AlertDialog.BUTTON_POSITIVE);
129290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
129390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Negative-only button
129490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtons(null, "Negative", null, AlertDialog.BUTTON_NEGATIVE);
129590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtons(0, R.string.alert_dialog_negative_button, 0,
129690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                AlertDialog.BUTTON_NEGATIVE);
129790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtonsPostCreation(null, "Post negative", null, AlertDialog.BUTTON_NEGATIVE);
129890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtonsPostCreationMessage(null, "Message negative", null,
129990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                AlertDialog.BUTTON_NEGATIVE);
130090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
130190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Neutral-only button
130290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtons(null, null, "Neutral", AlertDialog.BUTTON_NEUTRAL);
130390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtons(0, 0, R.string.alert_dialog_neutral_button, AlertDialog.BUTTON_NEUTRAL);
130490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtonsPostCreation(null, null, "Post neutral", AlertDialog.BUTTON_NEUTRAL);
130590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtonsPostCreationMessage(null, null, "Message neutral",
130690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                AlertDialog.BUTTON_NEUTRAL);
130790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
130890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Show positive and negative, click positive
130990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtons(R.string.alert_dialog_positive_button,
131090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                R.string.alert_dialog_negative_button, 0, AlertDialog.BUTTON_POSITIVE);
131190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
131290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Show positive and neutral, click neutral
131390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtons("Positive", null, "Neutral", AlertDialog.BUTTON_NEUTRAL);
131490079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
131590079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Show negative and neutral, click negative
131690079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtonsPostCreationMessage(null, "Message negative",
131790079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                "Message neutral", AlertDialog.BUTTON_NEGATIVE);
131890079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
131990079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        // Show all, click positive
132090079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov        verifyDialogButtonsPostCreation("Post positive", "Post negative", "Post neutral",
132190079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov                AlertDialog.BUTTON_POSITIVE);
132290079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov    }
132390079ce0ebfcdd61c65d84ce7b3e4521fe755616Kirill Grouchnikov
1324244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    private static class TestDrawable extends ColorDrawable {
1325244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        private int mWidth;
1326244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        private int mHeight;
1327244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
1328244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        public TestDrawable(@ColorInt int color, int width, int height) {
1329244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            super(color);
1330244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            mWidth = width;
1331244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            mHeight = height;
1332244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        }
1333244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
1334244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        @Override
1335244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        public int getIntrinsicWidth() {
1336244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            return mWidth;
1337244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        }
1338244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
1339244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        @Override
1340244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        public int getIntrinsicHeight() {
1341244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            return mHeight;
1342244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        }
1343244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
1344244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov}