summaryrefslogtreecommitdiff
path: root/AndroidPlot-Core/src/test/java/com/androidplot/xy/XYPlotTest.java
blob: 04d5945e14a6c95758562f24d5bbabf659ac08ed (plain)
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
/*
 * Copyright 2012 AndroidPlot.com
 *
 *    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 com.androidplot.xy;

import android.content.Context;
import android.graphics.*;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import com.androidplot.Plot;
import com.androidplot.PlotTest;
import com.androidplot.mock.MockContext;
import com.androidplot.mock.MockPaint;
import com.androidplot.ui.widget.TextLabelWidget;
import com.androidplot.util.Configurator;
import com.androidplot.util.FontUtils;
import com.androidplot.util.PixelUtils;
import mockit.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static junit.framework.Assert.assertEquals;

@UsingMocksAndStubs({Log.class,View.class,Context.class,Handler.class,Paint.class,Color.class,
        Rect.class, RectF.class,FontUtils.class, PixelUtils.class, Canvas.class})

public class XYPlotTest {

    XYPlot plot;  // testing
    
    List<Integer> numList1;
    List<Integer> numList2;
    SimpleXYSeries series1;

    @Before
    public void setUp() throws Exception {
        Mockit.setUpMocks(MockPaint.class,MockContext.class);
        new MockUp<View>() {
            @Mock int getWidth() { return 100;}
            @Mock int getHeight() { return 100;}

        };

        plot = new XYPlot(null, "test");
        numList1 = Arrays.asList(0, 1, 3, 5, 10, 15, 25, 50, 75, 100); // 10 elements
        numList2 = Arrays.asList(-100, 0, 1, 3, 5, 10, 15, 25, 50, 75, 100, 200); // 12 elements
        series1 = new SimpleXYSeries(numList1, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "");
    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void testOriginFixedMode() throws Exception {
        plot.addSeries(series1, new LineAndPointFormatter());
        plot.centerOnDomainOrigin(5, 2, BoundaryMode.FIXED);
        plot.calculateMinMaxVals();


        assertEquals(3.0, plot.getCalculatedMinX());
        assertEquals(7.0, plot.getCalculatedMaxX());
    }

    @Test
    public void testOriginAutoMode() throws Exception {
        plot.addSeries(series1, new LineAndPointFormatter());
        plot.centerOnDomainOrigin(5);
        plot.calculateMinMaxVals();
        //plot.updateMinMaxVals();

        assertEquals(10.0, plot.getCalculatedMaxX()); // symmetry is @ 10, not 9
        assertEquals(0.0, plot.getCalculatedMinX());

        plot.centerOnRangeOrigin(50);
        plot.calculateMinMaxVals();

        assertEquals(100.0, plot.getCalculatedMaxY());
        assertEquals(0.0, plot.getCalculatedMinY());

    }

    @Test
    public void testOriginGrowMode() throws Exception {
        plot.addSeries(series1, new LineAndPointFormatter());
        plot.centerOnDomainOrigin(5, null, BoundaryMode.GROW);
        plot.calculateMinMaxVals();

        assertEquals(0.0, plot.getCalculatedMinX());
        assertEquals(10.0, plot.getCalculatedMaxX());

        // introduce a larger domain set.  boundaries should change
        series1.setModel(numList2, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();

        assertEquals(-1.0, plot.getCalculatedMinX());
        assertEquals(11.0, plot.getCalculatedMaxX());

        // revert series model back to the previous set.  boundaries should remain the same
        series1.setModel(numList1, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();

        assertEquals(-1.0, plot.getCalculatedMinX());
        assertEquals(11.0, plot.getCalculatedMaxX());
    }

    @Test
    public void testOriginShrinkMode() throws Exception {
        plot.addSeries(series1, new LineAndPointFormatter());
        plot.centerOnDomainOrigin(5, null, BoundaryMode.SHRINNK);
        plot.calculateMinMaxVals();

        assertEquals(0.0, plot.getCalculatedMinX());
        assertEquals(10.0, plot.getCalculatedMaxX());

        // update with more extreme values...nothing should change in shrink mode:
        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);

        assertEquals(0.0, plot.getCalculatedMinX());
        assertEquals(10.0, plot.getCalculatedMaxX());
                
    }


 // Ifor not sure about filling in test stubs just going to do my own stuff instead.
    @Test
    public void testsetDomainBoundaries() throws Exception {
        plot.addSeries(series1, new LineAndPointFormatter());
        plot.calculateMinMaxVals();

        // default to auto so check them
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(9, plot.getCalculatedMaxX());

        plot.setDomainBoundaries(2, BoundaryMode.FIXED, 8, BoundaryMode.FIXED);
        plot.calculateMinMaxVals();

        // fixed
        assertEquals(2, plot.getCalculatedMinX());
        assertEquals(8, plot.getCalculatedMaxX());

        // back to auto
        plot.setDomainBoundaries(2, BoundaryMode.AUTO, 8, BoundaryMode.AUTO);
        plot.calculateMinMaxVals();

        // check again
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(9, plot.getCalculatedMaxX());
        
        // we are not testing MinY well with this dataset.
        // try grow
        plot.setDomainBoundaries(2, BoundaryMode.GROW, 8, BoundaryMode.GROW);
        plot.calculateMinMaxVals();

        // check inital
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(9, plot.getCalculatedMaxX());
        
        // update with more extreme values...
        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();

        // after growing
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(11, plot.getCalculatedMaxX());

        // back to previous
        series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();
        
        // should not of changed.
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(11, plot.getCalculatedMaxX());

        // back to big
        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        
        plot.setDomainBoundaries(2, BoundaryMode.SHRINNK, 8, BoundaryMode.SHRINNK);
        plot.calculateMinMaxVals();

        // check inital
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(11, plot.getCalculatedMaxX());
        
        // now small
        series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();

        // after shrinking
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(9, plot.getCalculatedMaxX());

        // back to previous
        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();
        
        // should not of changed.
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(9, plot.getCalculatedMaxX());

        // back to auto
        plot.setDomainBoundaries(2, BoundaryMode.AUTO, 8, BoundaryMode.AUTO);
        plot.calculateMinMaxVals();
        
        // should of changed.
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(11, plot.getCalculatedMaxX());
    }
    
    @Test
    public void testsetRangeBoundaries() throws Exception {
        plot.addSeries(series1, new LineAndPointFormatter());
        plot.calculateMinMaxVals();

        // default to auto so check them
        assertEquals(0, plot.getCalculatedMinY());
        assertEquals(100, plot.getCalculatedMaxY());

        plot.setRangeBoundaries(5, BoundaryMode.FIXED, 80, BoundaryMode.FIXED);
        plot.calculateMinMaxVals();

        // fixed
        assertEquals(5, plot.getCalculatedMinY());
        assertEquals(80, plot.getCalculatedMaxY());

        // back to auto
        plot.setRangeBoundaries(2, BoundaryMode.AUTO, 8, BoundaryMode.AUTO);
        plot.calculateMinMaxVals();

        // check again
        assertEquals(0, plot.getCalculatedMinY());
        assertEquals(100, plot.getCalculatedMaxY());
        
        // try grow
        plot.setRangeBoundaries(2, BoundaryMode.GROW, 8, BoundaryMode.GROW);
        plot.calculateMinMaxVals();

        // check inital
        assertEquals(0, plot.getCalculatedMinY());
        assertEquals(100, plot.getCalculatedMaxY());
        
        // update with more extreme values...
        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();

        // after growing
        assertEquals(-100, plot.getCalculatedMinY());
        assertEquals(200, plot.getCalculatedMaxY());

        // back to previous
        series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();
        
        // should not of changed.
        assertEquals(-100, plot.getCalculatedMinY());
        assertEquals(200, plot.getCalculatedMaxY());

        // back to big
        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        
        plot.setRangeBoundaries(2, BoundaryMode.SHRINNK, 8, BoundaryMode.SHRINNK);
        plot.calculateMinMaxVals();

        // check inital
        assertEquals(-100, plot.getCalculatedMinY());
        assertEquals(200, plot.getCalculatedMaxY());
        
        // now small
        series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();

        // after shrinking
        assertEquals(0, plot.getCalculatedMinY());
        assertEquals(100, plot.getCalculatedMaxY());

        // back to previous
        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();
        
        // should not of changed.
        assertEquals(0, plot.getCalculatedMinY());
        assertEquals(100, plot.getCalculatedMaxY());

        // back to auto
        plot.setRangeBoundaries(2, BoundaryMode.AUTO, 8, BoundaryMode.AUTO);
        plot.calculateMinMaxVals();
        
        // should of changed.
        assertEquals(-100, plot.getCalculatedMinY());
        assertEquals(200, plot.getCalculatedMaxY());
    }
    
    @Test
    public void testSetDomainRightMinMax() throws Exception {
        plot.addSeries(series1, new LineAndPointFormatter());
        plot.calculateMinMaxVals();

        // default to auto so check them
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(9, plot.getCalculatedMaxX());
        
        plot.setDomainRightMax(10);
        plot.calculateMinMaxVals();

        // same values.
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(9, plot.getCalculatedMaxX());

        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();

        // on RightMax
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(10, plot.getCalculatedMaxX());

        plot.setDomainRightMax(null);
        plot.calculateMinMaxVals();

        // back to full
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(11, plot.getCalculatedMaxX());
        
        // now the RightMin
        plot.setDomainRightMin(10);
        plot.calculateMinMaxVals();

        // still to full
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(11, plot.getCalculatedMaxX());

        // small list
        series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();
        
        // on RightMin
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(10, plot.getCalculatedMaxX());

        // now off again
        plot.setDomainRightMin(null);
        plot.calculateMinMaxVals();

        // small values.
        assertEquals(0, plot.getCalculatedMinX());
        assertEquals(9, plot.getCalculatedMaxX());
    }
    
    @Test
    public void testSetRangeTopBottomMinMax() throws Exception {
        plot.addSeries(series1, new LineAndPointFormatter());
        plot.calculateMinMaxVals();

        // default to auto so check them
        assertEquals(0, plot.getCalculatedMinY());
        assertEquals(100, plot.getCalculatedMaxY());
        
        plot.setRangeTopMax(110);
        plot.setRangeBottomMin(-50);
        plot.calculateMinMaxVals();

        // same values.
        assertEquals(0, plot.getCalculatedMinY());
        assertEquals(100, plot.getCalculatedMaxY());

        series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();

        // on Limits
        assertEquals(-50, plot.getCalculatedMinY());
        assertEquals(110, plot.getCalculatedMaxY());

        plot.setRangeTopMax(null);
        plot.setRangeBottomMin(null);
        plot.calculateMinMaxVals();

        // back to full
        assertEquals(-100, plot.getCalculatedMinY());
        assertEquals(200, plot.getCalculatedMaxY());
        
        // now the Min
        plot.setRangeTopMin(150);
        plot.setRangeBottomMax(-60);
        plot.calculateMinMaxVals();

        // still to full
        assertEquals(-100, plot.getCalculatedMinY());
        assertEquals(200, plot.getCalculatedMaxY());

        // small list
        series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
        plot.calculateMinMaxVals();
        
        // on Limits
        assertEquals(-60, plot.getCalculatedMinY());
        assertEquals(150, plot.getCalculatedMaxY());

        // now off again
        plot.setRangeTopMin(null);
        plot.setRangeBottomMax(null);
        plot.calculateMinMaxVals();

        // small values.
        assertEquals(0, plot.getCalculatedMinY());
        assertEquals(100, plot.getCalculatedMaxY());
    }
    
    @Test
    public void testSetDomainUpperBoundary() throws Exception {

    }

    @Test
    public void testSetDomainLowerBoundary() throws Exception {

    }

    @Test
    public void testSetRangeUpperBoundary() throws Exception {

    }

    @Test
    public void testSetRangeLowerBoundary() throws Exception {

    }

    @Test
    public void testSetDomainOrigin() throws Exception {

    }

    @Test
    public void testSetRangeOrigin() throws Exception {

    }

    @Test
    public void testConfigure() throws Exception {
        //Context context = Mockit.setUpMock(new MockContext());
        Context context = new MockContext.MockContext2();
        HashMap<String, String> params = new HashMap<String, String>();
        String param1 = "this is a test.";
        String param2 = Plot.RenderMode.USE_BACKGROUND_THREAD.toString();
        String param3 = "#FF0000";
        params.put("title", param1);
        params.put("renderMode", param2);
        params.put("backgroundPaint.color", param3);
        params.put("graphWidget.domainLabelPaint.color", param3);

        Configurator.configure(context, plot, params);
        assertEquals(param1, plot.getTitle());
        assertEquals(Plot.RenderMode.USE_BACKGROUND_THREAD, plot.getRenderMode());
        assertEquals(Color.parseColor(param3), plot.getBackgroundPaint().getColor());
        assertEquals(Color.parseColor(param3), plot.getGraphWidget().getDomainLabelPaint().getColor());
    }
}