1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.app.Activity;
4 import android.content.Context;
5 import android.preference.DialogPreference;
6 import android.util.AttributeSet;
7 import com.xtremelabs.robolectric.Robolectric;
8 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
9 import com.xtremelabs.robolectric.tester.android.util.TestAttributeSet;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.junit.runner.RunWith;
13 
14 import java.util.HashMap;
15 
16 import static org.hamcrest.CoreMatchers.equalTo;
17 import static org.hamcrest.CoreMatchers.sameInstance;
18 import static org.junit.Assert.assertThat;
19 
20 @RunWith(WithTestDefaultsRunner.class)
21 public class DialogPreferenceTest {
22 
23     private static final String TEST_DIALOG_MESSAGE = "This is only a test";
24 
25     private DialogPreference preference;
26     private ShadowDialogPreference shadow;
27 
28     private Context context;
29     private TestAttributeSet attrs;
30 
31     @Before
setup()32     public void setup() {
33         HashMap<String, String> hash = new HashMap<String, String>();
34         hash.put("dialogMessage", TEST_DIALOG_MESSAGE);
35         context = new Activity();
36         attrs = new TestAttributeSet(hash);
37         preference = new TestDialogPreference(context, attrs);
38         shadow = Robolectric.shadowOf(preference);
39     }
40 
41     @Test
testConstructors()42     public void testConstructors() {
43         int defStyle = 7;
44 
45         preference = new TestDialogPreference(context, attrs, defStyle);
46         shadow = Robolectric.shadowOf(preference);
47         assertThat(shadow.getContext(), sameInstance(context));
48         assertThat(shadow.getAttrs(), sameInstance((AttributeSet) attrs));
49         assertThat(shadow.getDefStyle(), equalTo(defStyle));
50 
51         preference = new TestDialogPreference(context, attrs);
52         shadow = Robolectric.shadowOf(preference);
53         assertThat(shadow.getContext(), sameInstance(context));
54         assertThat(shadow.getAttrs(), sameInstance((AttributeSet) attrs));
55         assertThat(shadow.getDefStyle(), equalTo(0));
56     }
57 
58     @Test
testGetDialogMessage()59     public void testGetDialogMessage() {
60         assertThat((String) preference.getDialogMessage(), equalTo(TEST_DIALOG_MESSAGE));
61     }
62 
63     protected static class TestDialogPreference extends DialogPreference {
64 
TestDialogPreference(Context context, AttributeSet attrs, int defStyle)65         public TestDialogPreference(Context context, AttributeSet attrs, int defStyle) {
66             super(context, attrs, defStyle);
67         }
68 
TestDialogPreference(Context context, AttributeSet attrs)69         public TestDialogPreference(Context context, AttributeSet attrs) {
70             super(context, attrs);
71         }
72     }
73 }
74