• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.junit.Assert.assertEquals;
5 
6 import android.app.Application;
7 import android.widget.RadioGroup;
8 import androidx.test.core.app.ApplicationProvider;
9 import androidx.test.ext.junit.runners.AndroidJUnit4;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 
16 @RunWith(AndroidJUnit4.class)
17 public class ShadowRadioGroupTest {
18   private static final int BUTTON_ID = 3245;
19   private Application context;
20   private RadioGroup radioGroup;
21 
22   @Before
setUp()23   public void setUp() throws Exception {
24     context = ApplicationProvider.getApplicationContext();
25     radioGroup = new RadioGroup(context);
26   }
27 
28   @Test
checkedRadioButtonId()29   public void checkedRadioButtonId() throws Exception {
30     assertThat(radioGroup.getCheckedRadioButtonId()).isEqualTo(-1);
31     radioGroup.check(99);
32     assertThat(radioGroup.getCheckedRadioButtonId()).isEqualTo(99);
33   }
34 
35   @Test
check_shouldCallOnCheckedChangeListener()36   public void check_shouldCallOnCheckedChangeListener() throws Exception {
37     TestOnCheckedChangeListener listener = new TestOnCheckedChangeListener();
38     radioGroup.setOnCheckedChangeListener(listener);
39 
40     radioGroup.check(BUTTON_ID);
41 
42     assertEquals(Arrays.asList(BUTTON_ID), listener.onCheckedChangedCheckedIds);
43     assertEquals(Arrays.asList(radioGroup), listener.onCheckedChangedGroups);
44   }
45 
46   @Test
clearCheck_shouldCallOnCheckedChangeListenerTwice()47   public void clearCheck_shouldCallOnCheckedChangeListenerTwice() throws Exception {
48     TestOnCheckedChangeListener listener = new TestOnCheckedChangeListener();
49 
50     radioGroup.check(BUTTON_ID);
51     radioGroup.setOnCheckedChangeListener(listener);
52     radioGroup.clearCheck();
53 
54     assertEquals(Arrays.asList(-1), listener.onCheckedChangedCheckedIds);
55     assertEquals(Arrays.asList(radioGroup), listener.onCheckedChangedGroups);
56   }
57 
58   private static class TestOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
59     public ArrayList<RadioGroup> onCheckedChangedGroups = new ArrayList<>();
60     public ArrayList<Integer> onCheckedChangedCheckedIds = new ArrayList<>();
61 
62     @Override
onCheckedChanged(RadioGroup group, int checkedId)63     public void onCheckedChanged(RadioGroup group, int checkedId) {
64       onCheckedChangedGroups.add(group);
65       onCheckedChangedCheckedIds.add(checkedId);
66     }
67   }
68 }
69