• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.widget.RadioButton;
4 import android.widget.RadioGroup;
5 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 
9 import static org.hamcrest.CoreMatchers.equalTo;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.assertTrue;
13 
14 @RunWith(WithTestDefaultsRunner.class)
15 public class RadioButtonTest {
16     @Test
canBeExplicitlyChecked()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
canBeToggledBetweenCheckedState()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
canBeClickedToToggleCheckedState()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
shouldInformRadioGroupThatItIsChecked()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