-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathTitleAndButtonsContainerTest.kt
More file actions
495 lines (419 loc) · 19.1 KB
/
Copy pathTitleAndButtonsContainerTest.kt
File metadata and controls
495 lines (419 loc) · 19.1 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package com.reactnativenavigation.views
import android.app.Activity
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.reactnativenavigation.BaseTest
import com.reactnativenavigation.options.Alignment
import com.reactnativenavigation.options.params.Colour
import com.reactnativenavigation.options.params.NullThemeColour
import com.reactnativenavigation.options.params.ThemeColour
import com.reactnativenavigation.views.stack.topbar.titlebar.ButtonBar
import com.reactnativenavigation.views.stack.topbar.titlebar.DEFAULT_LEFT_MARGIN_PX
import com.reactnativenavigation.views.stack.topbar.titlebar.TitleAndButtonsContainer
import com.reactnativenavigation.views.stack.topbar.titlebar.TitleSubTitleLayout
import org.assertj.core.api.Assertions
import org.assertj.core.api.AssertionsForInterfaceTypes.assertThat
import org.junit.Ignore
import org.junit.Test
import org.mockito.Mockito
import org.mockito.Mockito.times
import org.mockito.kotlin.spy
import org.mockito.kotlin.verify
import kotlin.math.roundToInt
import kotlin.test.assertFalse
private const val UUT_WIDTH = 1000
private const val UUT_HEIGHT = 100
class TitleAndButtonsContainerTest : BaseTest() {
lateinit var uut: TitleAndButtonsContainer
private lateinit var activity: Activity
private lateinit var mockLeftBar: ButtonBar
private lateinit var mockRightBar: ButtonBar
private lateinit var mockComponent: View
override fun beforeEach() {
super.beforeEach()
setup()
}
private fun setup(
mockUUT: Boolean = true,
direction: Int = View.LAYOUT_DIRECTION_LTR,
titleBarWidth: Int = 0,
titleBarHeight: Int = 0,
componentWidth: Int = 0,
componentHeight: Int = 0,
rightBarWidth: Int = 0,
leftBarWidth: Int = 0,
alignment: Alignment = Alignment.Default
) {
activity = newActivity()
val originalUUT = TitleAndButtonsContainer(activity)
uut = if (mockUUT) spy(originalUUT) else originalUUT
mockLeftBar = spy(ButtonBar(activity))
mockRightBar = spy(ButtonBar(activity))
mockComponent = spy(View(activity))
val mockTitleSubtitleLayout = spy(TitleSubTitleLayout(activity))
Mockito.doReturn(rightBarWidth).`when`(mockRightBar).measuredWidth
Mockito.doReturn(leftBarWidth).`when`(mockLeftBar).measuredWidth
if (mockUUT)
Mockito.doReturn(direction).`when`(uut).layoutDirection
Mockito.doReturn(titleBarWidth).`when`(mockTitleSubtitleLayout).measuredWidth
Mockito.doReturn(titleBarHeight).`when`(mockTitleSubtitleLayout).measuredHeight
Mockito.doReturn(componentWidth).`when`(mockComponent).measuredWidth
Mockito.doReturn(componentHeight).`when`(mockComponent).measuredHeight
uut.setTitleBarAlignment(alignment)
if (rightBarWidth > 0 || leftBarWidth > 0)
uut.setButtonBars(mockLeftBar, mockRightBar)
if (componentWidth > 0)
uut.setComponent(mockComponent, alignment)
uut.setTitleSubtitleLayout(mockTitleSubtitleLayout)
activity.window.decorView.layoutDirection = direction
activity.setContentView(FrameLayout(activity).apply {
layoutDirection = direction
addView(uut, ViewGroup.LayoutParams(UUT_WIDTH, UUT_HEIGHT))
})
idleMainLooper()
}
@Test
fun `animateLeftRightButtons - should be false as default`(){
assertFalse(mockLeftBar.shouldAnimate)
assertFalse(mockRightBar.shouldAnimate)
}
@Test
fun `animateLeftRightButtons - should change corresponding button bar`(){
setup(rightBarWidth = 10,leftBarWidth = 20)
uut.animateLeftButtons(true)
verify(mockLeftBar).shouldAnimate=true
uut.animateLeftButtons(false)
verify(mockLeftBar).shouldAnimate=false
uut.animateRightButtons(true)
verify(mockRightBar).shouldAnimate=true
uut.animateRightButtons(false)
verify(mockRightBar).shouldAnimate=false
}
@Test
fun `setComponent - should not change component id`() {
val component = View(activity).apply { id = 19 }
val component2 = View(activity).apply { id = 29 }
uut.setComponent(component)
assertThat(component.id).isEqualTo(19)
uut.setComponent(component2)
assertThat(component.id).isEqualTo(19)
assertThat(component2.id).isEqualTo(29)
uut.clearTitle()
assertThat(component.id).isEqualTo(19)
assertThat(component2.id).isEqualTo(29)
uut.setComponent(component2)
assertThat(component.id).isEqualTo(19)
assertThat(component2.id).isEqualTo(29)
uut.setTitle("title")
assertThat(component.id).isEqualTo(19)
assertThat(component2.id).isEqualTo(29)
}
@Test
fun `onLayout - should set title component center vertically`() {
val componentHeight = (UUT_HEIGHT / 4f).roundToInt()
setup(componentWidth = 100, componentHeight = componentHeight)
val component = uut.getTitleComponent()
assertThat(component.top).isEqualTo((UUT_HEIGHT / 2f - componentHeight / 2f).roundToInt())
assertThat(component.bottom).isEqualTo((UUT_HEIGHT / 2f + componentHeight / 2f).roundToInt())
}
@Test
fun `onLayout - set title text should take all parent height`() {
setup(titleBarWidth = 100, titleBarHeight = UUT_HEIGHT)
val component = uut.getTitleComponent()
assertThat(component.layoutParams.height).isEqualTo(ViewGroup.LayoutParams.MATCH_PARENT)
assertThat(component.top).isEqualTo(0)
assertThat(component.bottom).isEqualTo(UUT_HEIGHT)
}
@Test
fun `onLayout - title should not overlap with toolbar and have to re-layout`() {
val leftBarWidth = (UUT_WIDTH / 4f).roundToInt()
val rightBarWidth = (UUT_WIDTH / 4f).roundToInt()
val titleBarWidth = 3 * (UUT_WIDTH / 4f).roundToInt()
setup(titleBarWidth = titleBarWidth, titleBarHeight = UUT_HEIGHT, leftBarWidth = leftBarWidth, rightBarWidth = rightBarWidth)
val component = uut.getTitleComponent()
assertThat(component.left).isEqualTo(leftBarWidth + DEFAULT_LEFT_MARGIN_PX)
assertThat(component.right).isEqualTo(UUT_WIDTH - rightBarWidth - DEFAULT_LEFT_MARGIN_PX)
}
@Test
fun `onLayout - title component should not overlap with toolbar and have to re-layout`() {
val leftBarWidth = (UUT_WIDTH / 4f).roundToInt()
val rightBarWidth = (UUT_WIDTH / 4f).roundToInt()
val titleBarWidth = 3 * (UUT_WIDTH / 4f).roundToInt()
setup(componentWidth = titleBarWidth, componentHeight = UUT_HEIGHT, leftBarWidth = leftBarWidth, rightBarWidth =
rightBarWidth)
val component = uut.getTitleComponent()
assertThat(component.left).isEqualTo(leftBarWidth + DEFAULT_LEFT_MARGIN_PX)
assertThat(component.right).isEqualTo(UUT_WIDTH - rightBarWidth - DEFAULT_LEFT_MARGIN_PX)
}
@Test
fun `onLayout - title should be aligned left and take needed the width + margins when no buttons`() {
val titleBarWidth = 200
setup(titleBarWidth = titleBarWidth)
val component = uut.getTitleComponent()
assertThat(component.left).isEqualTo(DEFAULT_LEFT_MARGIN_PX)
assertThat(component.right).isEqualTo(titleBarWidth + 2 * DEFAULT_LEFT_MARGIN_PX)
}
@Test
fun `setTitle - RTL - should be aligned right and take available width when no buttons`() {
val titleBarWidth = 200
setup(direction = View.LAYOUT_DIRECTION_RTL, titleBarWidth = titleBarWidth)
val titleComponent = uut.getTitleComponent()
assertThat(titleComponent.right).isEqualTo(UUT_WIDTH - DEFAULT_LEFT_MARGIN_PX)
assertThat(titleComponent.left).isEqualTo(UUT_WIDTH- DEFAULT_LEFT_MARGIN_PX-titleBarWidth-DEFAULT_LEFT_MARGIN_PX )
}
@Test
fun `Title - should place title between the toolbars`() {
val leftBarWidth = 50
val rightBarWidth = 100
val titleBarWidth = 200
setup(leftBarWidth = leftBarWidth, rightBarWidth = rightBarWidth, titleBarWidth = titleBarWidth)
val titleSubTitleLayout = uut.getTitleComponent() as TitleSubTitleLayout
idleMainLooper()
assertThat(titleSubTitleLayout.left).isEqualTo(leftBarWidth + DEFAULT_LEFT_MARGIN_PX)
assertThat(titleSubTitleLayout.right).isEqualTo(leftBarWidth + DEFAULT_LEFT_MARGIN_PX + titleBarWidth + DEFAULT_LEFT_MARGIN_PX)
}
@Test
fun `Title - should place title between the toolbars at center`() {
val leftBarWidth = 50
val rightBarWidth = 100
val titleBarWidth = 200
setup(
leftBarWidth = leftBarWidth,
rightBarWidth = rightBarWidth,
titleBarWidth = titleBarWidth,
alignment = Alignment.Center
)
idleMainLooper()
assertThat(uut.getTitleComponent().left).isEqualTo((UUT_WIDTH / 2f - titleBarWidth / 2f).roundToInt())
assertThat(uut.getTitleComponent().right).isEqualTo((UUT_WIDTH / 2f + titleBarWidth / 2f).roundToInt())
}
@Test
fun `Title - center should not overlap with toolbars and be resized to fit between`() {
val leftBarWidth = (UUT_WIDTH / 4f).roundToInt()
val rightBarWidth = (UUT_WIDTH / 4f).roundToInt()
val titleBarWidth = (0.75 * UUT_WIDTH).roundToInt()
val spaceBetween = UUT_WIDTH - leftBarWidth - rightBarWidth
setup(
leftBarWidth = leftBarWidth,
rightBarWidth = rightBarWidth,
titleBarWidth = titleBarWidth,
alignment = Alignment.Center
)
idleMainLooper()
assertThat(uut.getTitleComponent().left).isEqualTo((UUT_WIDTH / 2f - spaceBetween / 2f).roundToInt())
assertThat(uut.getTitleComponent().right).isEqualTo((UUT_WIDTH / 2f + spaceBetween / 2f).roundToInt())
}
@Test
fun `Title - left should not overlap with toolbars and be resized to fit between`() {
val leftBarWidth = (UUT_WIDTH / 4f).roundToInt()
val rightBarWidth = (UUT_WIDTH / 4f).roundToInt()
val titleBarWidth = (0.75 * UUT_WIDTH).roundToInt()
setup(
leftBarWidth = leftBarWidth,
rightBarWidth = rightBarWidth,
titleBarWidth = titleBarWidth,
alignment = Alignment.Default
)
idleMainLooper()
assertThat(uut.getTitleComponent().left).isEqualTo(leftBarWidth + DEFAULT_LEFT_MARGIN_PX)
assertThat(uut.getTitleComponent().right).isEqualTo(UUT_WIDTH - rightBarWidth - DEFAULT_LEFT_MARGIN_PX)
}
@Test
fun `Component - center should not overlap with toolbars and be resized to fit between`() {
val leftBarWidth = (UUT_WIDTH / 4f).roundToInt()
val rightBarWidth = (UUT_WIDTH / 4f).roundToInt()
val componentWidth = (0.75 * UUT_WIDTH).roundToInt()
val spaceBetween = UUT_WIDTH - leftBarWidth - rightBarWidth
setup(
leftBarWidth = leftBarWidth,
rightBarWidth = rightBarWidth,
componentWidth = componentWidth,
alignment = Alignment.Center
)
idleMainLooper()
assertThat(uut.getTitleComponent().left).isEqualTo((UUT_WIDTH / 2f - spaceBetween / 2f).roundToInt())
assertThat(uut.getTitleComponent().right).isEqualTo((UUT_WIDTH / 2f + spaceBetween / 2f).roundToInt())
}
@Test
fun `Component - left should not overlap with toolbars and be resized to fit between`() {
val leftBarWidth = (UUT_WIDTH / 4f).roundToInt()
val rightBarWidth = (UUT_WIDTH / 4f).roundToInt()
val componentWidth = (0.75 * UUT_WIDTH).roundToInt()
setup(
leftBarWidth = leftBarWidth,
rightBarWidth = rightBarWidth,
componentWidth = componentWidth,
alignment = Alignment.Default
)
idleMainLooper()
assertThat(uut.getTitleComponent().left).isEqualTo(leftBarWidth + DEFAULT_LEFT_MARGIN_PX)
assertThat(uut.getTitleComponent().right).isEqualTo(UUT_WIDTH - rightBarWidth - DEFAULT_LEFT_MARGIN_PX)
}
@Test
fun `Component - title width shrinks by measured right buttons only`() {
val rightButtonsWidth = 48
setup(
rightBarWidth = rightButtonsWidth,
componentWidth = UUT_WIDTH,
alignment = Alignment.Default
)
idleMainLooper()
assertThat(uut.getTitleComponent().left).isEqualTo(DEFAULT_LEFT_MARGIN_PX)
assertThat(uut.getTitleComponent().right).isEqualTo(UUT_WIDTH - rightButtonsWidth - DEFAULT_LEFT_MARGIN_PX)
}
@Test
fun `Component - should place title between the toolbars`() {
val leftBarWidth = 50
val rightBarWidth = 100
val componentWidth = 200
setup(leftBarWidth = leftBarWidth, rightBarWidth = rightBarWidth, titleBarWidth = 0, componentWidth = componentWidth)
val component = uut.getTitleComponent()
idleMainLooper()
assertThat(component.left).isEqualTo(leftBarWidth + DEFAULT_LEFT_MARGIN_PX)
assertThat(component.right).isEqualTo(leftBarWidth + DEFAULT_LEFT_MARGIN_PX + componentWidth + DEFAULT_LEFT_MARGIN_PX)
}
@Test
fun `onLayout - should place title component between the toolbars at center`() {
val componentWidth = 200
setup(leftBarWidth = 50, rightBarWidth = 100, componentWidth = componentWidth,
alignment = Alignment.Center)
val component = uut.getTitleComponent()
idleMainLooper()
assertThat(component.left).isEqualTo((UUT_WIDTH / 2f - componentWidth / 2f).roundToInt())
assertThat(component.right).isEqualTo((UUT_WIDTH / 2f + componentWidth / 2f).roundToInt())
}
@Test
fun `onLayout - should measure and layout children when alignment changes`() {
val titleBarWidth = 200
setup(
titleBarWidth = titleBarWidth,
alignment = Alignment.Center
)
var component = uut.getTitleComponent()
idleMainLooper()
assertThat(component.left).isEqualTo((UUT_WIDTH / 2f - titleBarWidth / 2f).roundToInt())
assertThat(component.right).isEqualTo((UUT_WIDTH / 2f + titleBarWidth / 2f).roundToInt())
setup(
titleBarWidth = titleBarWidth,
alignment = Alignment.Fill
)
component = uut.getTitleComponent()
idleMainLooper()
// Fill alignment must span the full container width between left and right
// button bars (zero-width here). The previous assertion that allowed the
// DEFAULT_LEFT_MARGIN_PX reservation matched left-aligned behaviour, not Fill.
assertThat(component.left).isEqualTo(0)
assertThat(component.right).isEqualTo(UUT_WIDTH)
}
@Test
fun `setComponent - should set dynamic width-height`() {
val component = View(activity).apply { id = 19 }
uut.setComponent(component)
idleMainLooper()
assertThat(component.layoutParams.width).isEqualTo(ViewGroup.LayoutParams.WRAP_CONTENT)
assertThat(component.layoutParams.height).isEqualTo(ViewGroup.LayoutParams.WRAP_CONTENT)
}
@Test
fun setComponent_shouldChangeDifferentComponents() {
setup(mockUUT = false)
val component = View(activity).apply { id = 19 }
val component2 = View(activity).apply { id = 29 }
uut.setComponent(component)
val titleComponent = uut.getTitleComponent()
assertThat(titleComponent).isEqualTo(component)
uut.setComponent(component2, Alignment.Fill)
assertThat(uut.findViewById<View?>(component.id)).isNull()
assertThat(uut.findViewById<View?>(component2.id)).isEqualTo(component2)
}
@Test
fun setComponent_shouldResetTextTitleWhenSettingComponent() {
setup(mockUUT = false)
uut.setTitle("Title")
assertThat(uut.getTitle()).isEqualTo("Title")
val component = View(activity)
uut.setComponent(component)
assertThat(uut.getTitle()).isNullOrEmpty()
}
@Test
fun setComponent_doesNothingIfComponentIsAlreadyAdded() {
setup(componentWidth = 100)
idleMainLooper()
assertThat(uut.getComponent()).isNotNull()
uut.setComponent(mockComponent)
idleMainLooper()
assertThat(uut.getComponent()?.id).isEqualTo(mockComponent.id)
}
@Test
fun setTitle_shouldChangeTheTitle() {
uut.setTitle("Title")
assertThat(uut.getTitle()).isEqualTo("Title")
}
@Test
fun setTitle_shouldResetTitleComponentWhenSettingTextTitle() {
setup(mockUUT = false)
val component = View(activity)
uut.setComponent(component)
assertThat(uut.getTitle()).isNullOrEmpty()
uut.setTitle("Title")
assertThat(uut.getTitle()).isEqualTo("Title")
}
@Test
fun setSubTitle_textShouldBeAlignedAtStartCenterVertical() {
uut.setSubtitle("Subtitle")
val passedView = uut.getTitleSubtitleBar()
assertThat(passedView.visibility).isEqualTo(View.VISIBLE)
assertThat(passedView.getSubTitleTxtView().text).isEqualTo("Subtitle")
assertThat((passedView.getSubTitleTxtView().layoutParams as LinearLayout.LayoutParams).gravity).isEqualTo(Gravity.START or Gravity.CENTER_VERTICAL)
}
@Test
fun setBackgroundColor_changesTitleBarBgColor() {
uut = Mockito.spy(uut)
uut.setBackgroundColor(NullThemeColour())
verify(uut, times(0)).setBackgroundColor(Color.GRAY)
uut.setBackgroundColor(ThemeColour(Colour(Color.GRAY)))
verify(uut, times(1)).setBackgroundColor(Color.GRAY)
}
@Test
fun setTitleFontSize_changesTitleFontSize() {
uut.setTitleFontSize(1f)
Assertions.assertThat(getTitleSubtitleView().getTitleTxtView().textSize).isEqualTo(1f)
}
@Test
fun setSubTitleFontSize_changesTitleFontSize() {
uut.setSubtitleFontSize(1f)
Assertions.assertThat(getTitleSubtitleView().getSubTitleTxtView().textSize).isEqualTo(1f)
}
@Test
fun setTitleColor_changesTitleColor() {
uut.setTitleColor(Color.YELLOW)
assertThat(getTitleSubtitleView().getTitleTxtView().currentTextColor).isEqualTo(Color.YELLOW)
}
@Test
fun setSubTitleColor_changesTitleColor() {
uut.setSubtitleColor(Color.YELLOW)
assertThat(getTitleSubtitleView().getSubTitleTxtView().currentTextColor).isEqualTo(Color.YELLOW)
}
@Test
fun getTitle_returnCurrentTextInTitleTextView() {
assertThat(uut.getTitle()).isEmpty()
uut.setTitle("TiTle")
assertThat(uut.getTitle()).isEqualTo("TiTle")
}
@Test
fun clearCurrentTitle_shouldCleatTextAndRemoveComponent() {
uut.setTitle("Title")
assertThat(uut.getTitle()).isEqualTo("Title")
uut.clearTitle()
assertThat(uut.getTitle()).isNullOrEmpty()
val component = View(activity)
uut.setComponent(component)
assertThat(uut.getTitleComponent()).isEqualTo(component)
uut.clearTitle()
assertThat(uut.getTitleComponent()).isNotEqualTo(component)
}
private fun getTitleSubtitleView() = (uut.getTitleComponent() as TitleSubTitleLayout)
}