-
Notifications
You must be signed in to change notification settings - Fork 47
/
TestUtil.kt
473 lines (392 loc) · 15.5 KB
/
TestUtil.kt
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
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package org.jetbrains.letsPlot.core.plot.builder.tooltip
import org.assertj.core.api.Condition
import org.jetbrains.letsPlot.commons.geometry.DoubleRectangle
import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.commons.intern.function.Functions.identity
import org.jetbrains.letsPlot.commons.values.FontFace
import org.jetbrains.letsPlot.commons.values.FontFamily
import org.jetbrains.letsPlot.core.plot.base.Aes
import org.jetbrains.letsPlot.core.plot.base.GeomKind
import org.jetbrains.letsPlot.core.plot.base.layout.TextJustification
import org.jetbrains.letsPlot.core.plot.base.layout.Thickness
import org.jetbrains.letsPlot.core.plot.base.render.linetype.NamedLineType
import org.jetbrains.letsPlot.core.plot.base.theme.AxisTheme
import org.jetbrains.letsPlot.core.plot.base.theme.ThemeTextStyle
import org.jetbrains.letsPlot.core.plot.base.tooltip.*
import org.jetbrains.letsPlot.core.plot.base.tooltip.GeomTargetCollector.TooltipParams
import org.jetbrains.letsPlot.core.plot.base.tooltip.GeomTargetLocator.*
import org.jetbrains.letsPlot.core.plot.builder.presentation.Defaults
import org.jetbrains.letsPlot.core.plot.builder.presentation.Defaults.Common.Tooltip.AXIS_TOOLTIP_COLOR
import org.jetbrains.letsPlot.core.plot.builder.presentation.Defaults.Common.Tooltip.LIGHT_TEXT_COLOR
import org.jetbrains.letsPlot.core.plot.builder.tooltip.MappedDataAccessMock.Companion.variable
import org.jetbrains.letsPlot.core.plot.builder.tooltip.MappedDataAccessMock.Mapping
import org.jetbrains.letsPlot.core.plot.builder.tooltip.loc.LayerTargetLocator
import org.jetbrains.letsPlot.core.plot.builder.tooltip.loc.TargetPrototype
import org.jetbrains.letsPlot.core.plot.builder.tooltip.spec.TooltipSpec
import org.mockito.Mockito.mock
import kotlin.test.assertEquals
import kotlin.test.assertTrue
object TestUtil {
val axisTheme = object : AxisTheme {
override val axis: String
get() = TODO("Not yet implemented")
override fun showLine() = TODO("Not yet implemented")
override fun showTickMarks() = TODO("Not yet implemented")
override fun showLabels() = TODO("Not yet implemented")
override fun showTitle() = TODO("Not yet implemented")
override fun showTooltip() = TODO("Not yet implemented")
override fun titleStyle(): ThemeTextStyle = TODO("Not yet implemented")
override fun titleJustification() = TextJustification(0.5, 1.0)
override fun titleMargins() = Thickness()
override fun lineWidth() = TODO("Not yet implemented")
override fun lineColor() = TODO("Not yet implemented")
override fun tickMarkColor() = TODO("Not yet implemented")
override fun lineType() = NamedLineType.SOLID
override fun tickMarkLineType() = NamedLineType.SOLID
override fun labelStyle(): ThemeTextStyle = TODO("Not yet implemented")
override fun rotateLabels() = TODO("Not yet implemented")
override fun labelAngle(): Double = TODO("Not yet implemented")
override fun tickMarkWidth() = TODO("Not yet implemented")
override fun tickMarkLength() = TODO("Not yet implemented")
override fun tickLabelMargins() = Thickness()
override fun tooltipFill() = AXIS_TOOLTIP_COLOR
override fun tooltipColor() = AXIS_TOOLTIP_COLOR
override fun tooltipStrokeWidth() = 1.0
override fun tooltipLineType() = NamedLineType.SOLID
override fun tooltipTextStyle(): ThemeTextStyle = ThemeTextStyle(
// Defaults.FONT_FAMILY_NORMAL,
FontFamily.SERIF,
FontFace.NORMAL,
Defaults.Common.Tooltip.AXIS_TOOLTIP_FONT_SIZE,
LIGHT_TEXT_COLOR
)
}
private const val VARIABLE_NAME = "A"
private const val VARIABLE_VALUE = "value"
private val defaultTooltipParams = TooltipParams(
emptyMap(),
TipLayoutHint.StemLength.NORMAL,
null,
emptyList()
)
internal fun <T> continuous(aes: Aes<T>): Mapping<T> {
return mappedData(aes, true)
}
internal fun <T> discrete(aes: Aes<T>): Mapping<T> {
return mappedData(aes, false)
}
private fun <T> mappedData(aes: Aes<T>, isContinuous: Boolean): Mapping<T> {
return variable().name(VARIABLE_NAME).value(VARIABLE_VALUE).isContinuous(isContinuous).mapping(aes)
}
internal fun assertText(tooltipSpecs: List<TooltipSpec>, vararg expectedTooltipText: String) {
assertText(tooltipSpecs, listOf(*expectedTooltipText))
}
@SafeVarargs
internal fun assertText(tooltipSpecs: List<TooltipSpec>, vararg expectedTooltips: List<String>) {
assertEquals(expectedTooltips.size.toLong(), tooltipSpecs.size.toLong())
var i = 0
val n = tooltipSpecs.size
while (i < n) {
val tooltipText = tooltipSpecs[i].lines.map(TooltipSpec.Line::toString)
assertListsEqual(expectedTooltips[i], tooltipText)
++i
}
}
private fun <T> assertListsEqual(expected: List<T>, actual: List<T>) {
assertEquals(expected.size.toLong(), actual.size.toLong())
var i = 0
val n = expected.size
while (i < n) {
assertEquals(expected[i], actual[i])
++i
}
}
internal fun assertNoTooltips(tooltipSpecs: List<TooltipSpec>) {
assertTrue(tooltipSpecs.isEmpty())
}
private const val OUTSIDE_DELTA = 10.0
private const val PATH_POINTS_COUNT_PER_KEY = 100
internal fun createLocator(
lookupStrategy: LookupStrategy,
lookupSpace: LookupSpace,
vararg list: TargetPrototype
): GeomTargetLocator {
val targetsList = ArrayList<TargetPrototype>()
targetsList.addAll(list)
return createLocator(
lookupSpec = LookupSpec(lookupSpace, lookupStrategy),
contextualMapping = mock(ContextualMapping::class.java),
targetPrototypes = targetsList
)
}
internal fun createLocator(
lookupSpec: LookupSpec,
contextualMapping: ContextualMapping,
targetPrototypes: List<TargetPrototype>,
geomKind: GeomKind = GeomKind.POINT
): GeomTargetLocator {
return LayerTargetLocator(
geomKind,
lookupSpec,
contextualMapping,
targetPrototypes
)
}
fun coord(x: Double, y: Double): DoubleVector {
return DoubleVector(x, y)
}
fun size(width: Double, height: Double): DoubleVector {
return DoubleVector(width, height)
}
fun rect(pos: DoubleVector, dim: DoubleVector): DoubleRectangle {
return DoubleRectangle(pos, dim)
}
internal fun outsideX(rect: DoubleRectangle): DoubleVector {
return rect.center.add(
point(
rect.width / 2 + OUTSIDE_DELTA,
0.0
)
)
}
internal fun outsideX(rect: DoubleRectangle, delta: Double): DoubleVector {
return rect.origin.add(point(rect.right + delta, 0.0))
}
internal fun outsideY(rect: DoubleRectangle): DoubleVector {
return rect.center.add(
point(
0.0,
rect.height / 2 + OUTSIDE_DELTA
)
)
}
internal fun outsideXY(rect: DoubleRectangle): DoubleVector {
return rect.center.add(
point(
rect.width + OUTSIDE_DELTA,
rect.height / 2 + OUTSIDE_DELTA
)
)
}
private fun betweenX(rect1: DoubleRectangle, rect2: DoubleRectangle): Double {
return between(rect1.left, rect2.left)
}
private fun betweenY(rect1: DoubleRectangle, rect2: DoubleRectangle): Double {
return between(rect1.top, rect2.top)
}
internal fun between(rect1: DoubleRectangle, rect2: DoubleRectangle): DoubleVector {
return DoubleVector(
betweenX(rect1, rect2),
betweenY(rect1, rect2)
)
}
internal fun between(v1: Double, v2: Double): Double {
if (v1 > v2) {
throw IllegalArgumentException()
}
return v1 + (v2 - v1)
}
internal fun middle(v1: Double, v2: Double): Double {
val halfLength = (v2 - v1) / 2
return v1 + halfLength
}
internal fun inside(rect: DoubleRectangle): DoubleVector {
return rect.center
}
internal fun offsetX(p: DoubleVector): DoubleVector {
return p.add(
point(
OUTSIDE_DELTA,
0.0
)
)
}
internal fun offsetX(p: DoubleVector, delta: Double): DoubleVector {
return p.add(point(delta, 0.0))
}
internal fun offsetY(p: DoubleVector): DoubleVector {
return p.add(
point(
0.0,
OUTSIDE_DELTA
)
)
}
internal fun offsetXY(p: DoubleVector): DoubleVector {
return p.add(
point(
OUTSIDE_DELTA,
OUTSIDE_DELTA
)
)
}
internal fun <T> first(collection: List<T>): T {
return collection[0]
}
internal fun <T> last(collection: List<T>): T {
return collection[collection.size - 1]
}
internal fun <R, T> map(collection: Collection<T>, mapFunction: (T) -> R): List<R> {
val result = ArrayList<R>()
for (item in collection) {
result.add(mapFunction(item))
}
return result
}
internal fun findTargets(locator: GeomTargetLocator, p: DoubleVector): List<GeomTarget> {
return getGeomTargets(locator.search(p))
}
internal fun assertEncodedObjects(locator: GeomTargetLocator, coord: DoubleVector, vararg key: Any) {
assertObjects(true, locator, coord, *key)
}
internal fun assertObjects(locator: GeomTargetLocator, coord: DoubleVector, vararg key: Any) {
assertObjects(false, locator, coord, *key)
}
private fun assertObjects(encoded: Boolean, locator: GeomTargetLocator, coord: DoubleVector, vararg key: Any) {
val decode = if (encoded) { it -> decodeKey(it) } else identity<Int>()
val located = getGeomTargets(locator.search(coord))
assertEquals(key.size, located.size)
var i = 0
val n = key.size
while (i < n) {
val geomTarget = located[i]
assertEquals(key[i], decode(geomTarget.hitIndex), "Hit index: $i")
i++
}
}
private fun getGeomTargets(result: LookupResult?): List<GeomTarget> {
return result?.targets ?: emptyList()
}
internal fun assertEmpty(locator: GeomTargetLocator, coord: DoubleVector) {
val located = getGeomTargets(locator.search(coord))
assertTrue(located.isEmpty())
}
internal fun rectTarget(key: Any, rect: DoubleRectangle): TargetPrototype {
val rectShape = HitShape.rect(rect)
return TargetPrototype(
rectShape, { key as Int },
defaultTooltipParams, TipLayoutHint.Kind.HORIZONTAL_TOOLTIP
)
}
internal fun pointTarget(key: Any, p: DoubleVector): TargetPrototype {
val pointShape = HitShape.point(p, 0.0)
return TargetPrototype(
pointShape, { key as Int },
defaultTooltipParams, TipLayoutHint.Kind.VERTICAL_TOOLTIP
)
}
internal fun pathTarget(key: Int, points: List<DoubleVector>): TargetPrototype {
val pathShape = HitShape.path(points)
return TargetPrototype(pathShape, { hitIndex ->
encodeIndex(
key,
hitIndex
)
}, defaultTooltipParams, TipLayoutHint.Kind.HORIZONTAL_TOOLTIP)
}
internal fun pathTarget(points: List<DoubleVector>): TargetPrototype {
val pathShape = HitShape.path(points)
return TargetPrototype(pathShape, identity(), defaultTooltipParams, TipLayoutHint.Kind.HORIZONTAL_TOOLTIP)
}
private fun encodeIndex(key: Int, integer: Int?): Int {
return key * PATH_POINTS_COUNT_PER_KEY + integer!!
}
private fun decodeKey(index: Int?): Int {
return index!! / PATH_POINTS_COUNT_PER_KEY
}
private fun decodeIndex(index: Int?): Int {
return index!! % PATH_POINTS_COUNT_PER_KEY
}
internal fun pathTarget(points: List<DoubleVector>, indexMapper: (Int) -> Int): TargetPrototype {
val pathShape = HitShape.path(points)
return TargetPrototype(pathShape, indexMapper, defaultTooltipParams, TipLayoutHint.Kind.HORIZONTAL_TOOLTIP)
}
internal fun polygonTarget(key: Int, points: List<DoubleVector>): TargetPrototype {
val polygonShape = HitShape.polygon(points)
return TargetPrototype(polygonShape, { key }, defaultTooltipParams, TipLayoutHint.Kind.CURSOR_TOOLTIP)
}
fun point(x: Double, y: Double): DoubleVector {
return DoubleVector(x, y)
}
private fun path(vararg points: DoubleVector): MutableList<DoubleVector> {
val pointsList = ArrayList<DoubleVector>()
pointsList.addAll(points)
return pointsList
}
@SafeVarargs
internal fun multipolygon(vararg pointsArray: MutableList<DoubleVector>): List<DoubleVector> {
val pointsList = ArrayList<DoubleVector>()
for (points in pointsArray) {
closePathIfNeeded(points)
pointsList.addAll(points)
}
return pointsList
}
internal fun polygon(vararg points: DoubleVector): MutableList<DoubleVector> {
val pointsList = path(*points)
closePathIfNeeded(pointsList)
return pointsList
}
private fun closePathIfNeeded(points: MutableList<DoubleVector>) {
if (points[0] != points[points.size - 1]) {
points.add(points[0])
}
}
internal fun horizontalPath(y: Double, vararg xList: Double): List<DoubleVector> {
val pathPoints = ArrayList<DoubleVector>()
for (x in xList) {
pathPoints.add(DoubleVector(x, y))
}
return pathPoints
}
internal fun horizontalPathTarget(key: Int, y: Double, xList: DoubleArray): TargetPrototype {
val pathPoints = horizontalPath(y, *xList)
return pathTarget(key, pathPoints)
}
internal fun pathTarget(key: Int, vararg points: PathPoint): TargetPrototype {
return pathTarget(key, points.map { it.coord })
}
internal class PathPoint(val coord: DoubleVector, val hitIndex: Int) {
val x: Double
get() = coord.x
val y: Double
get() = coord.y
}
internal class PathPointsBuilder {
private var defaultX: Double? = null
private var defaultY: Double? = null
private var counter = 0
fun defaultX(x: Double): PathPointsBuilder {
defaultX = x
return this
}
fun defaultY(y: Double): PathPointsBuilder {
defaultY = y
return this
}
fun x(x: Double): PathPoint {
return PathPoint(DoubleVector(x, defaultY!!), counter++)
}
fun y(y: Double): PathPoint {
return PathPoint(DoubleVector(defaultX!!, y), counter++)
}
fun xy(x: Double, y: Double): PathPoint {
return PathPoint(DoubleVector(x, y), counter++)
}
}
internal class HitIndex(private val myExpected: Int) : Condition<GeomTarget>() {
override fun matches(geomTarget: GeomTarget): Boolean {
val hitIndex = geomTarget.hitIndex
return myExpected == decodeIndex(hitIndex)
}
companion object {
fun equalTo(expected: Int): HitIndex {
return HitIndex(expected)
}
}
}
}