1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dialer.filterednumber;
18
19import android.app.AlertDialog;
20import android.app.DialogFragment;
21import android.content.ContentResolver;
22import android.content.DialogInterface;
23import android.test.ActivityInstrumentationTestCase2;
24
25import com.android.dialer.DialtactsActivity;
26import com.android.dialer.filterednumber.BlockedNumbersMigrator.Listener;
27
28import org.mockito.Mock;
29import org.mockito.MockitoAnnotations;
30
31/**
32 * Instrumentation tests for {@link MigrateBlockedNumbersDialogFragment}. Note for these tests to
33 * work properly, the device's screen must be on.
34 */
35public class MigrateBlockedNumbersDialogFragmentInstrumentationTest extends
36        ActivityInstrumentationTestCase2<DialtactsActivity> {
37
38    private static final String SHOW_TAG = "ShowTag";
39
40    private BlockedNumbersMigrator mBlockedNumbersMigrator;
41    @Mock private Listener mListener;
42    private DialogFragment mMigrateDialogFragment;
43
44    public MigrateBlockedNumbersDialogFragmentInstrumentationTest() {
45        super(DialtactsActivity.class);
46    }
47
48    @Override
49    public void setUp() throws Exception {
50        super.setUp();
51        MockitoAnnotations.initMocks(this);
52        mBlockedNumbersMigrator = new SynchronousBlockedNumbersMigrator(
53                getActivity().getContentResolver());
54        mMigrateDialogFragment = MigrateBlockedNumbersDialogFragment
55                .newInstance(mBlockedNumbersMigrator, mListener);
56        getInstrumentation().runOnMainSync(new Runnable() {
57            @Override
58            public void run() {
59                mMigrateDialogFragment.show(getActivity().getFragmentManager(), SHOW_TAG);
60            }
61        });
62        getInstrumentation().waitForIdleSync();
63    }
64
65    public void testDialogAppears() {
66        assertTrue(mMigrateDialogFragment.getDialog().isShowing());
67    }
68
69    public void testDialogPositiveButtonPress() {
70        getInstrumentation().runOnMainSync(new Runnable() {
71            @Override
72            public void run() {
73                ((AlertDialog) mMigrateDialogFragment.getDialog())
74                        .getButton(DialogInterface.BUTTON_POSITIVE).performClick();
75            }
76        });
77        getInstrumentation().waitForIdleSync();
78        // Dialog was dismissed
79        assertNull(mMigrateDialogFragment.getDialog());
80    }
81
82    private static class SynchronousBlockedNumbersMigrator extends BlockedNumbersMigrator {
83        public SynchronousBlockedNumbersMigrator(ContentResolver contentResolver) {
84            super(contentResolver);
85        }
86
87        @Override
88        public boolean migrate(BlockedNumbersMigrator.Listener listener) {
89            listener.onComplete();
90            return true;
91        }
92    }
93}
94