• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.fakes;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.robolectric.Shadows.shadowOf;
5 
6 import android.graphics.drawable.Drawable;
7 import android.view.MenuItem;
8 import android.view.View;
9 import androidx.test.core.app.ApplicationProvider;
10 import androidx.test.ext.junit.runners.AndroidJUnit4;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.robolectric.R;
15 
16 @RunWith(AndroidJUnit4.class)
17 public class RoboMenuItemTest {
18   private MenuItem item;
19   private TestOnActionExpandListener listener;
20 
21   @Before
setUp()22   public void setUp() throws Exception {
23     item = new RoboMenuItem(ApplicationProvider.getApplicationContext());
24     listener =  new TestOnActionExpandListener();
25     item.setOnActionExpandListener(listener);
26   }
27 
28   @Test
shouldCheckTheMenuItem()29   public void shouldCheckTheMenuItem() throws Exception {
30     assertThat(item.isChecked()).isFalse();
31     item.setChecked(true);
32     assertThat(item.isChecked()).isTrue();
33   }
34 
35   @Test
shouldAllowSettingCheckable()36   public void shouldAllowSettingCheckable() throws Exception {
37     assertThat(item.isCheckable()).isFalse();
38     item.setCheckable(true);
39     assertThat(item.isCheckable()).isTrue();
40   }
41 
42   @Test
shouldAllowSettingVisible()43   public void shouldAllowSettingVisible() throws Exception {
44     assertThat(item.isVisible()).isTrue();
45     item.setVisible(false);
46     assertThat(item.isVisible()).isFalse();
47   }
48 
49   @Test
expandActionView_shouldReturnFalseIfActionViewIsNull()50   public void expandActionView_shouldReturnFalseIfActionViewIsNull() throws Exception {
51     item.setActionView(null);
52     assertThat(item.expandActionView()).isFalse();
53   }
54 
55   @Test
expandActionView_shouldSetExpandedTrue()56   public void expandActionView_shouldSetExpandedTrue() throws Exception {
57     item.setActionView(new View(ApplicationProvider.getApplicationContext()));
58     assertThat(item.expandActionView()).isTrue();
59     assertThat(item.isActionViewExpanded()).isTrue();
60   }
61 
62   @Test
expandActionView_shouldInvokeListener()63   public void expandActionView_shouldInvokeListener() throws Exception {
64     item.setActionView(new View(ApplicationProvider.getApplicationContext()));
65     item.expandActionView();
66     assertThat(listener.expanded).isTrue();
67   }
68 
69   @Test
collapseActionView_shouldReturnFalseIfActionViewIsNull()70   public void collapseActionView_shouldReturnFalseIfActionViewIsNull() throws Exception {
71     item.setActionView(null);
72     assertThat(item.collapseActionView()).isFalse();
73   }
74 
75   @Test
collapseActionView_shouldSetExpandedFalse()76   public void collapseActionView_shouldSetExpandedFalse() throws Exception {
77     item.setActionView(new View(ApplicationProvider.getApplicationContext()));
78     item.expandActionView();
79     assertThat(item.collapseActionView()).isTrue();
80     assertThat(item.isActionViewExpanded()).isFalse();
81   }
82 
83   @Test
collapseActionView_shouldInvokeListener()84   public void collapseActionView_shouldInvokeListener() throws Exception {
85     item.setActionView(new View(ApplicationProvider.getApplicationContext()));
86     listener.expanded = true;
87     item.collapseActionView();
88     assertThat(listener.expanded).isFalse();
89   }
90 
91   @Test
methodsShouldReturnThis()92   public void methodsShouldReturnThis() throws Exception {
93     item = item.setEnabled(true);
94     assertThat(item).isNotNull();
95     item = item.setOnMenuItemClickListener(null);
96     assertThat(item).isNotNull();
97     item = item.setActionProvider(null);
98     assertThat(item).isNotNull();
99     item = item.setActionView(0);
100     assertThat(item).isNotNull();
101     item = item.setActionView(null);
102     assertThat(item).isNotNull();
103     item = item.setAlphabeticShortcut('a');
104     assertThat(item).isNotNull();
105     item = item.setCheckable(false);
106     assertThat(item).isNotNull();
107     item = item.setChecked(true);
108     assertThat(item).isNotNull();
109     item = item.setIcon(null);
110     assertThat(item).isNotNull();
111     item = item.setIcon(0);
112     assertThat(item).isNotNull();
113     item = item.setIntent(null);
114     assertThat(item).isNotNull();
115     item = item.setNumericShortcut('6');
116     assertThat(item).isNotNull();
117     item = item.setOnActionExpandListener(null);
118     assertThat(item).isNotNull();
119     item = item.setShortcut('6', 'z');
120     assertThat(item).isNotNull();
121     item = item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
122     assertThat(item).isNotNull();
123     item = item.setTitleCondensed("condensed");
124     assertThat(item).isNotNull();
125     item = item.setVisible(true);
126     assertThat(item).isNotNull();
127     item = item.setTitle("title");
128     assertThat(item).isNotNull();
129     item = item.setTitle(0);
130     assertThat(item).isNotNull();
131   }
132 
133   @Test
setIcon_shouldNullifyOnZero()134   public void setIcon_shouldNullifyOnZero() throws Exception {
135     assertThat(item.getIcon()).isNull();
136     item.setIcon(R.drawable.an_image);
137     assertThat(shadowOf(item.getIcon()).getCreatedFromResId()).isEqualTo(R.drawable.an_image);
138     item.setIcon(0);
139     assertThat(item.getIcon()).isNull();
140   }
141 
142   @Test
getIcon_shouldReturnDrawableFromSetIconDrawable()143   public void getIcon_shouldReturnDrawableFromSetIconDrawable() throws Exception {
144     Drawable testDrawable =
145         ApplicationProvider.getApplicationContext().getResources().getDrawable(R.drawable.an_image);
146     assertThat(testDrawable).isNotNull();
147     assertThat(item.getIcon()).isNull();
148     item.setIcon(testDrawable);
149     assertThat(item.getIcon()).isSameAs(testDrawable);
150   }
151 
152   @Test
getIcon_shouldReturnDrawableFromSetIconResourceId()153   public void getIcon_shouldReturnDrawableFromSetIconResourceId() throws Exception {
154     assertThat(item.getIcon()).isNull();
155     item.setIcon(R.drawable.an_other_image);
156     assertThat(shadowOf(item.getIcon()).getCreatedFromResId()).isEqualTo(R.drawable.an_other_image);
157   }
158 
159   @Test
setOnActionExpandListener_shouldReturnMenuItem()160   public void setOnActionExpandListener_shouldReturnMenuItem() throws Exception {
161     assertThat(item.setOnActionExpandListener(listener)).isSameAs(item);
162   }
163 
164   static class TestOnActionExpandListener implements MenuItem.OnActionExpandListener {
165     private boolean expanded = false;
166 
167     @Override
onMenuItemActionExpand(MenuItem menuItem)168     public boolean onMenuItemActionExpand(MenuItem menuItem) {
169       expanded = true;
170       return true;
171     }
172 
173     @Override
onMenuItemActionCollapse(MenuItem menuItem)174     public boolean onMenuItemActionCollapse(MenuItem menuItem) {
175       expanded = false;
176       return true;
177     }
178   }
179 }
180