aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/shadows/ProgressDialogTest.java
blob: f809bae8e2f49e0968b1f2ab93c672a4e5610867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.xtremelabs.robolectric.shadows;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.Callable;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static junit.framework.Assert.assertNull;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(WithTestDefaultsRunner.class)
public class ProgressDialogTest {

    private ProgressDialog dialog;
    private ShadowProgressDialog shadow;

    @Before
    public void setUp() {
        dialog = new ProgressDialog(null);
        shadow = Robolectric.shadowOf(dialog);
    }

    @Test
    public void shouldExtendAlertDialog() {
        assertThat(shadow, instanceOf(ShadowAlertDialog.class));
    }

    @Test
    public void shouldSetMessage() {
        CharSequence message = "This is only a test";

        assertThat(shadow.getMessage(), nullValue());
        dialog.setMessage(message);
        assertThat((CharSequence) shadow.getMessage(), equalTo(message));
    }

    @Test
    public void shouldSetIndeterminate() {
        assertThat(dialog.isIndeterminate(), equalTo(false));

        dialog.setIndeterminate(true);
        assertThat(dialog.isIndeterminate(), equalTo(true));

        dialog.setIndeterminate(false);
        assertThat(dialog.isIndeterminate(), equalTo(false));
    }

    @Test
    public void show_shouldCreateAProgressDialog() {
        Context context = new Activity();
        TestOnCancelListener cancelListener = new TestOnCancelListener();
        ProgressDialog progressDialog = ProgressDialog.show(context, "Title", "Message", true, true, cancelListener);
        ShadowProgressDialog shadowProgressDialog = shadowOf(progressDialog);
        assertThat(shadowProgressDialog.getContext(), is(context));
        assertThat(shadowProgressDialog.getMessage(), equalTo("Message"));
        assertTrue(shadowProgressDialog.isIndeterminate());
        assertTrue(shadowProgressDialog.isCancelable());

        progressDialog.cancel();
        assertThat(cancelListener.onCancelDialogInterface, is((DialogInterface) progressDialog));
    }

    @Test
    public void show_setsLatestAlertDialogAndLatestDialog_3args() throws Exception{
        assertLatestDialogsSet("Title", "Message", false, false, null, new Callable<ProgressDialog>() {
            @Override
            public ProgressDialog call() throws Exception {
                return ProgressDialog.show(Robolectric.application, "Title", "Message");
            }
        }
        );
    }

    @Test
    public void show_setsLatestAlertDialogAndLatestDialog_4args() throws Exception{
        assertLatestDialogsSet("Title", "Message", true, false, null, new Callable<ProgressDialog>() {
            @Override
            public ProgressDialog call() throws Exception {
                return ProgressDialog.show(Robolectric.application, "Title", "Message", true);
            }
        });
    }

    @Test
    public void show_setsLatestAlertDialogAndLatestDialog_5args() throws Exception{
        assertLatestDialogsSet("Title", "Message", true, true, null, new Callable<ProgressDialog>() {
            @Override
            public ProgressDialog call() throws Exception {
                return ProgressDialog.show(Robolectric.application, "Title", "Message", true, true);
            }
        });
    }

    @Test
    public void show_setsLatestAlertDialogAndLatestDialog_6args() throws Exception{
        final DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
            }
        };

        assertLatestDialogsSet("Title", "Message", true, true, cancelListener, new Callable<ProgressDialog>() {
            @Override
            public ProgressDialog call() throws Exception {
                return ProgressDialog.show(Robolectric.application, "Title", "Message", true, true, cancelListener);
            }
        });
    }

    private void assertLatestDialogsSet(CharSequence expectedTitle, CharSequence expectedMessage, boolean expectedIndeterminate,
            boolean expectedCancelable, DialogInterface.OnCancelListener expectedCancelListener,
            Callable<ProgressDialog> callable) throws Exception {
        assertNull(ShadowDialog.getLatestDialog());
        assertNull(ShadowAlertDialog.getLatestAlertDialog());

        dialog = callable.call();

        assertNotNull(dialog);
        assertEquals(dialog, ShadowDialog.getLatestDialog());
        assertEquals(dialog, ShadowAlertDialog.getLatestAlertDialog());

        assertEquals(expectedIndeterminate, dialog.isIndeterminate());
        assertEquals(expectedMessage, shadowOf(dialog).getMessage());
        assertEquals(expectedTitle, shadowOf(dialog).getTitle());
        assertEquals(expectedCancelable, shadowOf(dialog).isCancelable());
        assertEquals(expectedCancelListener, shadowOf(dialog).getOnCancelListener());
    }

    private static class TestOnCancelListener implements DialogInterface.OnCancelListener {
        public DialogInterface onCancelDialogInterface;

        @Override
        public void onCancel(DialogInterface dialogInterface) {
            onCancelDialogInterface = dialogInterface;

        }
    }
}