-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathButtonPresenterTest.java
More file actions
261 lines (226 loc) · 11 KB
/
ButtonPresenterTest.java
File metadata and controls
261 lines (226 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.reactnativenavigation.utils;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.appcompat.widget.ActionMenuView;
import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.fakes.IconResolverFake;
import com.reactnativenavigation.mocks.ImageLoaderMock;
import com.reactnativenavigation.options.ButtonOptions;
import com.reactnativenavigation.options.IconBackgroundOptions;
import com.reactnativenavigation.options.params.Bool;
import com.reactnativenavigation.options.params.Colour;
import com.reactnativenavigation.options.params.Number;
import com.reactnativenavigation.options.params.ThemeColour;
import com.reactnativenavigation.options.params.Text;
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.ButtonController;
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.ButtonPresenter;
import com.reactnativenavigation.views.stack.topbar.titlebar.ButtonBar;
import com.reactnativenavigation.views.stack.topbar.titlebar.IconBackgroundDrawable;
import com.reactnativenavigation.views.stack.topbar.titlebar.TitleBarButtonCreator;
import org.junit.Ignore;
import org.junit.Test;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadows.ShadowLooper;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@LooperMode(LooperMode.Mode.PAUSED)
public class ButtonPresenterTest extends BaseTest {
private static final String BTN_TEXT = "button1";
private ButtonBar titleBar;
private ButtonPresenter uut;
private ButtonController buttonController;
private ButtonOptions button;
private Activity activity;
@Override
public void beforeEach() {
super.beforeEach();
activity = newActivity();
titleBar = new ButtonBar(activity);
activity.setContentView(titleBar);
button = createButton();
ImageLoader imageLoaderMock;
imageLoaderMock = ImageLoaderMock.mock();
initUUt(imageLoaderMock);
}
private void initUUt(ImageLoader imageLoaderMock) {
IconResolverFake iconResolver = new IconResolverFake(activity, imageLoaderMock);
uut = new ButtonPresenter(activity, button, iconResolver);
buttonController = new ButtonController(
activity,
uut,
button,
mock(TitleBarButtonCreator.class),
mock(ButtonController.OnClickListener.class)
);
}
@Test
public void applyOptions_buttonIsAddedToMenu() {
addButtonAndApplyOptions();
assertThat(findButtonView().getText().toString()).isEqualTo(BTN_TEXT);
}
@Test
public void applyOptions_appliesColorOnButtonTextView() {
button.color = new ThemeColour(new Colour(Color.RED), new Colour(Color.RED));
addButtonAndApplyOptions();
assertThat(findButtonView().getCurrentTextColor()).isEqualTo(Color.RED);
}
@Test
public void applyOptions_appliesColorOnButtonTextViewOnDarkMode() {
mockConfiguration.uiMode = Configuration.UI_MODE_NIGHT_NO;
button.color = new ThemeColour(new Colour(Color.RED), new Colour(Color.BLACK));
MenuItem menuItem = addButtonAndApplyOptions();
assertThat(findButtonView().getCurrentTextColor()).isEqualTo(Color.RED);
mockConfiguration.uiMode = Configuration.UI_MODE_NIGHT_YES;
uut.applyOptions(titleBar, menuItem, buttonController::getView);
assertThat(findButtonView().getCurrentTextColor()).isEqualTo(Color.BLACK);
}
@Test
public void apply_disabledColor() {
button.enabled = new Bool(false);
addButtonAndApplyOptions();
assertThat(findButtonView().getCurrentTextColor()).isEqualTo(ButtonPresenter.DISABLED_COLOR);
}
@Test
public void applyColor_shouldChangeColor() {
MenuItem menuItem = addMenuButton();
uut.applyOptions(titleBar, menuItem, buttonController::getView);
ThemeColour color = new ThemeColour(new Colour(Color.RED), new Colour(Color.RED));
uut.applyColor(titleBar, menuItem, color);
assertThat(findButtonView().getCurrentTextColor()).isEqualTo(Color.RED);
}
@Test
public void applyBackgroundColor_shouldChangeBackgroundColor() {
IconBackgroundDrawable mockD = mock(IconBackgroundDrawable.class);
initUUt(ImageLoaderMock.mock(mockD));
button.enabled = new Bool(true);
button.icon = new Text("icon");
button.color = new ThemeColour(new Colour(Color.RED), new Colour(Color.RED));
IconBackgroundOptions iconBackground = new IconBackgroundOptions();
iconBackground.color = new ThemeColour(new Colour(Color.GREEN),new Colour(Color.GREEN));
button.iconBackground = iconBackground;
MenuItem menuItem = spy(addMenuButton());
uut.applyOptions(titleBar, menuItem, buttonController::getView);
assertThat(((IconBackgroundDrawable)menuItem.getIcon()).getBackgroundColor()).isEqualTo(Color.GREEN);
uut.applyBackgroundColor(titleBar, menuItem, new ThemeColour(new Colour(Color.BLACK),new Colour(Color.BLACK)));
assertThat(((IconBackgroundDrawable)menuItem.getIcon()).getBackgroundColor()).isEqualTo(Color.BLACK);
}
@Test
public void applyOptions_shouldChangeIconColorTint() {
IconBackgroundDrawable mockD = mock(IconBackgroundDrawable.class);
initUUt(ImageLoaderMock.mock(mockD));
button.enabled = new Bool(true);
button.icon = new Text("icon");
button.color = new ThemeColour(new Colour(Color.RED), new Colour(Color.RED));
MenuItem menuItem = spy(addMenuButton());
uut.applyOptions(titleBar, menuItem, buttonController::getView);
Drawable icon = menuItem.getIcon();
assertThat(icon).isNotNull();
verify(icon).setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN));
}
@Test
public void applyOptions_shouldChangeIconDisabledColorTint() {
IconBackgroundDrawable mockD = mock(IconBackgroundDrawable.class);
initUUt(ImageLoaderMock.mock(mockD));
button.enabled = new Bool(false);
button.icon = new Text("icon");
button.color = new ThemeColour(new Colour(Color.RED), new Colour(Color.RED));
button.disabledColor = new ThemeColour(new Colour(Color.YELLOW), new Colour(Color.YELLOW));
MenuItem menuItem = spy(addMenuButton());
uut.applyOptions(titleBar, menuItem, buttonController::getView);
Drawable icon = menuItem.getIcon();
assertThat(icon).isNotNull();
verify(icon).setColorFilter(new PorterDuffColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN));
}
@Test
public void applyOptions_shouldChangeIconColorBackground() {
IconBackgroundDrawable mockD = mock(IconBackgroundDrawable.class);
initUUt(ImageLoaderMock.mock(mockD));
button.enabled = new Bool(true);
button.icon = new Text("icon");
button.color = new ThemeColour(new Colour(Color.RED), new Colour(Color.RED));
IconBackgroundOptions iconBackground = new IconBackgroundOptions();
iconBackground.color = new ThemeColour(new Colour(Color.GREEN),new Colour(Color.GREEN));
button.iconBackground = iconBackground;
MenuItem menuItem = spy(addMenuButton());
uut.applyOptions(titleBar, menuItem, buttonController::getView);
Drawable icon = menuItem.getIcon();
assertThat(icon).isNotNull();
assertThat(icon).isInstanceOf(IconBackgroundDrawable.class);
IconBackgroundDrawable modifed = (IconBackgroundDrawable) icon;
verify(modifed.getWrappedDrawable()).setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN));
assertThat(modifed.getBackgroundColor()).isEqualTo(Color.GREEN);
}
@Test
public void applyOptions_shouldChangeIconDisabledColorBackground() {
IconBackgroundDrawable mockD = mock(IconBackgroundDrawable.class);
initUUt(ImageLoaderMock.mock(mockD));
button.enabled = new Bool(false);
button.icon = new Text("icon");
button.color = new ThemeColour(new Colour(Color.RED), new Colour(Color.RED));
button.disabledColor = new ThemeColour(new Colour(Color.YELLOW), new Colour(Color.YELLOW));
IconBackgroundOptions iconBackground = new IconBackgroundOptions();
iconBackground.color = new ThemeColour( new Colour(Color.GREEN), new Colour(Color.GREEN));
iconBackground.disabledColor = new ThemeColour(new Colour(Color.CYAN),new Colour(Color.CYAN));
button.iconBackground = iconBackground;
MenuItem menuItem = spy(addMenuButton());
uut.applyOptions(titleBar, menuItem, buttonController::getView);
Drawable icon = menuItem.getIcon();
assertThat(icon).isNotNull();
assertThat(icon).isInstanceOf(IconBackgroundDrawable.class);
IconBackgroundDrawable modifed = (IconBackgroundDrawable) icon;
verify(modifed.getWrappedDrawable()).setColorFilter(new PorterDuffColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN));
assertThat(modifed.getBackgroundColor()).isEqualTo(Color.CYAN);
}
@Test
public void applyColor_shouldChangeDisabledColor() {
button.enabled = new Bool(false);
MenuItem menuItem = addMenuButton();
uut.applyOptions(titleBar, menuItem, buttonController::getView);
ThemeColour disabledColor = new ThemeColour(new Colour(Color.BLUE), new Colour(Color.BLUE));
uut.applyDisabledColor(titleBar, menuItem, disabledColor);
assertThat(findButtonView().getCurrentTextColor()).isEqualTo(Color.BLUE);
}
private MenuItem addButtonAndApplyOptions() {
MenuItem menuItem = addMenuButton();
uut.applyOptions(titleBar, menuItem, buttonController::getView);
return menuItem;
}
private MenuItem addMenuButton() {
return titleBar.addButton(Menu.NONE,
1,
0,
SpannableString.valueOf(button.text.get("text")));
}
@Test
public void apply_allCaps() {
button.allCaps = new Bool(false);
addButtonAndApplyOptions();
assertThat(findButtonView().isAllCaps()).isEqualTo(false);
}
private TextView findButtonView() {
ShadowLooper.idleMainLooper();
return (TextView) ViewUtils.findChildrenByClass(
requireNonNull(ViewUtils.findChildByClass(titleBar, ActionMenuView.class)),
TextView.class,
child -> true
).get(0);
}
private ButtonOptions createButton() {
ButtonOptions b = new ButtonOptions();
b.id = "btn1";
b.text = new Text(BTN_TEXT);
b.showAsAction = new Number(MenuItem.SHOW_AS_ACTION_ALWAYS);
return b;
}
}