This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
/
ThumbsBarTest.java
332 lines (314 loc) · 14.6 KB
/
ThumbsBarTest.java
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.leanback.widget;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.view.View;
import androidx.leanback.R;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class ThumbsBarTest {
private Context mContext;
private ThumbsBar mBar;
/**
* Check ThumbsBar's initialization based on the constructor
*/
@Test
public void checkThumbsBarInitialize() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
assertEquals(mBar.mThumbHeightInPixel, mContext.getResources().getDimensionPixelSize(
R.dimen.lb_playback_transport_thumbs_height));
assertEquals(mBar.mThumbWidthInPixel, mContext.getResources().getDimensionPixelSize(
R.dimen.lb_playback_transport_thumbs_width));
assertEquals(mBar.mHeroThumbHeightInPixel, mContext.getResources().getDimensionPixelSize(
R.dimen.lb_playback_transport_hero_thumbs_height));
assertEquals(mBar.mHeroThumbWidthInPixel, mContext.getResources().getDimensionPixelSize(
R.dimen.lb_playback_transport_hero_thumbs_width));
}
/**
* Check getHeroIndex method when input is an even number
*/
@Test
public void checkGetHeroIndexOnEvenNumber() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int childCountForTest = 4;
// according to the algorithm, hero thumb's index should be childCounts / 2
int expectedHeroIndex = 2;
when(mBar.getChildCount()).thenReturn(childCountForTest);
assertEquals(mBar.getHeroIndex(), expectedHeroIndex);
}
/**
* Check getHeroIndex method when input is an odd number.
*/
@Test
public void checkGetHeroIndexOnOddNumber() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int childCountForTest = 5;
// according to the algorithm, hero thumb's index should be childCounts / 2
int expectedHeroIndex = 2;
when(mBar.getChildCount()).thenReturn(childCountForTest);
assertEquals(mBar.getHeroIndex(), expectedHeroIndex);
}
/**
* Check setThumbSize method.
*/
@Test
public void checkSetThumbSize() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int screenWidthInPixelForTest = 2560;
int screenHeightInPixelForTest = 1600;
int thumbsWidthInPixelForTest = 128;
int thumbsHeightInPixelForTest = 256;
// set screen size explicitly so the thumbs bar will have child view inside of it
mBar.measure(View.MeasureSpec.makeMeasureSpec(screenWidthInPixelForTest,
View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(screenHeightInPixelForTest,
View.MeasureSpec.EXACTLY));
mBar.setThumbSize(thumbsWidthInPixelForTest, thumbsHeightInPixelForTest);
// Verify the behavior of setThumbSize method
assertEquals(mBar.mThumbWidthInPixel, thumbsWidthInPixelForTest);
assertEquals(mBar.mThumbHeightInPixel, thumbsHeightInPixelForTest);
// iterate through all child view to test if its width/ height has been set successfully
for (int i = 0; i < mBar.getChildCount(); i++) {
if (i != mBar.getHeroIndex()) {
assertEquals(mBar.getChildAt(i).getLayoutParams().width,
thumbsWidthInPixelForTest);
assertEquals(mBar.getChildAt(i).getLayoutParams().height,
thumbsHeightInPixelForTest);
} else {
assertEquals(mBar.getChildAt(i).getLayoutParams().width,
mContext.getResources().getDimensionPixelSize(
R.dimen.lb_playback_transport_hero_thumbs_width));
assertEquals(mBar.getChildAt(i).getLayoutParams().height,
mContext.getResources().getDimensionPixelSize(
R.dimen.lb_playback_transport_hero_thumbs_height));
}
}
}
/**
* Check setHeroThumbSize method.
*/
@Test
public void checkSetHeroThumbSize() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int screenWidthInPixelForTest = 2560;
int screenHeightInPixelForTest = 1600;
int HeroThumbsWidthInPixelForTest = 256;
int HeroThumbsHeightInPixelForTest = 512;
// set screen size explicitly so the thumbs bar will have child view inside of it
mBar.measure(View.MeasureSpec.makeMeasureSpec(
screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(
screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
mBar.setHeroThumbSize(HeroThumbsWidthInPixelForTest, HeroThumbsHeightInPixelForTest);
// Verify the behavior of setThumbSize method
assertEquals(mBar.mHeroThumbWidthInPixel, HeroThumbsWidthInPixelForTest);
assertEquals(mBar.mHeroThumbHeightInPixel, HeroThumbsHeightInPixelForTest);
// iterate through all child view to test if its width/ height has been set successfully
for (int i = 0; i < mBar.getChildCount(); i++) {
if (i != mBar.getHeroIndex()) {
assertEquals(mBar.getChildAt(i).getLayoutParams().width,
mContext.getResources().getDimensionPixelSize(
R.dimen.lb_playback_transport_thumbs_width));
assertEquals(mBar.getChildAt(i).getLayoutParams().height,
mContext.getResources().getDimensionPixelSize(
R.dimen.lb_playback_transport_thumbs_height));
} else {
assertEquals(mBar.getChildAt(i).getLayoutParams().width,
HeroThumbsWidthInPixelForTest);
assertEquals(mBar.getChildAt(i).getLayoutParams().height,
HeroThumbsHeightInPixelForTest);
}
}
}
/**
* Check setThumbSpace method.
*/
@Test
public void checkSetThumbSpace() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int thumbSpaceInPixelForTest = 48;
mBar.setThumbSpace(thumbSpaceInPixelForTest);
assertEquals(mBar.mMeasuredMarginInPixel, thumbSpaceInPixelForTest);
verify(mBar).requestLayout();
}
/**
* check calculateNumberOfThumbs method when the result from roundUp function is less than 2
*
* Firstly, to make sure the test cases can run on different devices with different screen
* density (i.e. The return value from roundUp function should be the same no matter what kind
* of device/ emulator is connected),
* the screen width for test is set using dp, the pixel value will be computed by
* multiplying context.getResources().getDisplayMetrics().density
*
* In this test case, the screen width is set to 240 in dp, so the calculation result should
* be 1. According to the algorithm of calculateNumOfThumbs, it should be reassigned to 2 and
* the final result should be 3 after counting the hero thumb.
*/
@LargeTest
@Test
public void checkCalculateNumberOfThumbs1() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int screenWidthInPixelForTest =
(int) (240 * mContext.getResources().getDisplayMetrics().density);
int screenHeightInPixelForTest =
(int) (240 * mContext.getResources().getDisplayMetrics().density);
int expectedChildCounts = 3;
mBar.measure(View.MeasureSpec.makeMeasureSpec(
screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(
screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
assertEquals(mBar.getChildCount(), expectedChildCounts);
}
/**
* check calculateNumberOfThumbs method when the result from roundUp function is an odd number
* and larger than 2.
*
* In this test case, the screen width is set to 680 in dp, so the calculation result should
* be 3. According to the algorithm of calculateNumOfThumbs, it should be incremented by 1, so
* the final result is 5 after counting the hero thumb.
*/
@Test
public void checkCalculateNumberOfThumbs2() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int screenWidthInPixelForTest =
(int) (680 * mContext.getResources().getDisplayMetrics().density);
int screenHeightInPixelForTest =
(int) (680 * mContext.getResources().getDisplayMetrics().density);
int expectedChildCounts = 5;
mBar.measure(View.MeasureSpec.makeMeasureSpec(
screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(
screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
assertEquals(mBar.getChildCount(), expectedChildCounts);
}
/**
* check calculateNumberOfThumbs method when the result from roundUp function is an even number
* and larger than 2
*
* In this test case, the screen width is set to 800 in dp, so the calculation result should
* be 4. Finally the result is expected to be 5 after counting the hero thumb.
*/
@Test
public void checkCalculateNumberOfThumbs3() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int screenWidthInPixelForTest =
(int) (800 * mContext.getResources().getDisplayMetrics().density);
int screenHeightInPixelForTest =
(int) (800 * mContext.getResources().getDisplayMetrics().density);
int expectedChildCounts = 5;
mBar.measure(View.MeasureSpec.makeMeasureSpec(
screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(
screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
assertEquals(mBar.getChildCount(), expectedChildCounts);
}
/**
* check setNumberOfThumbs method
*
* When user calling setNumberOfThumbs(int numOfThumbs) method. The flag mIsUserSets will be
* toggled to true to honor user's choice, and the result of child view's number should
* not be impacted by calculateNumberOfThumbs(int widthInPixel) method.
*
* In this test case, the screen width is set to 960 in dp, the calculation result from
* calculateNumberOfThumbs method should be 5. But after calling setNumberOfThumbs function to
* set thumbs' number to 3, this value should not impact the final result.
*/
@Test
public void checkSetNumberOfThumbs() {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mBar = Mockito.spy(new ThumbsBar(mContext, null));
}
});
int screenWidthInPixelForTest =
(int) (960 * mContext.getResources().getDisplayMetrics().density);
int screenHeightInPixelForTest =
(int) (960 * mContext.getResources().getDisplayMetrics().density);
int numberOfThumbs = 3;
mBar.setNumberOfThumbs(numberOfThumbs);
mBar.measure(View.MeasureSpec.makeMeasureSpec(
screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(
screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
assertEquals(mBar.getChildCount(), numberOfThumbs);
}
}