1package com.xtremelabs.robolectric.shadows;
2
3import android.widget.RadioButton;
4import android.widget.RadioGroup;
5import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6import org.junit.Test;
7import org.junit.runner.RunWith;
8
9import static org.hamcrest.CoreMatchers.equalTo;
10import static org.junit.Assert.assertFalse;
11import static org.junit.Assert.assertThat;
12import static org.junit.Assert.assertTrue;
13
14@RunWith(WithTestDefaultsRunner.class)
15public class RadioButtonTest {
16    @Test
17    public void canBeExplicitlyChecked() throws Exception {
18        RadioButton radioButton = new RadioButton(null);
19        assertFalse(radioButton.isChecked());
20
21        radioButton.setChecked(true);
22        assertTrue(radioButton.isChecked());
23
24        radioButton.setChecked(false);
25        assertFalse(radioButton.isChecked());
26    }
27
28    @Test
29    public void canBeToggledBetweenCheckedState() throws Exception {
30        RadioButton radioButton = new RadioButton(null);
31        assertFalse(radioButton.isChecked());
32
33        radioButton.toggle();
34        assertTrue(radioButton.isChecked());
35
36        radioButton.toggle();
37        assertFalse(radioButton.isChecked());
38    }
39
40    @Test
41    public void canBeClickedToToggleCheckedState() throws Exception {
42        RadioButton radioButton = new RadioButton(null);
43        assertFalse(radioButton.isChecked());
44
45        radioButton.performClick();
46        assertTrue(radioButton.isChecked());
47
48        radioButton.performClick();
49        assertFalse(radioButton.isChecked());
50    }
51
52    @Test
53    public void shouldInformRadioGroupThatItIsChecked() throws Exception {
54        RadioButton radioButton1 = new RadioButton(null);
55        radioButton1.setId(99);
56        RadioButton radioButton2 = new RadioButton(null);
57        radioButton2.setId(100);
58
59        RadioGroup radioGroup = new RadioGroup(null);
60        radioGroup.addView(radioButton1);
61        radioGroup.addView(radioButton2);
62
63        radioButton1.setChecked(true);
64        assertThat(radioGroup.getCheckedRadioButtonId(), equalTo(radioButton1.getId()));
65
66        radioButton2.setChecked(true);
67        assertThat(radioGroup.getCheckedRadioButtonId(), equalTo(radioButton2.getId()));
68    }
69}
70