1/* 2 * Copyright 2012 AndroidPlot.com 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.androidplot.xy; 18 19import android.content.Context; 20import android.graphics.*; 21import android.os.Handler; 22import android.util.Log; 23import android.view.View; 24import com.androidplot.Plot; 25import com.androidplot.PlotTest; 26import com.androidplot.mock.MockContext; 27import com.androidplot.mock.MockPaint; 28import com.androidplot.ui.widget.TextLabelWidget; 29import com.androidplot.util.Configurator; 30import com.androidplot.util.FontUtils; 31import com.androidplot.util.PixelUtils; 32import mockit.*; 33import org.junit.After; 34import org.junit.Before; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37 38import java.lang.reflect.Method; 39import java.util.Arrays; 40import java.util.HashMap; 41import java.util.List; 42 43import static junit.framework.Assert.assertEquals; 44 45@UsingMocksAndStubs({Log.class,View.class,Context.class,Handler.class,Paint.class,Color.class, 46 Rect.class, RectF.class,FontUtils.class, PixelUtils.class, Canvas.class}) 47 48public class XYPlotTest { 49 50 XYPlot plot; // testing 51 52 List<Integer> numList1; 53 List<Integer> numList2; 54 SimpleXYSeries series1; 55 56 @Before 57 public void setUp() throws Exception { 58 Mockit.setUpMocks(MockPaint.class,MockContext.class); 59 new MockUp<View>() { 60 @Mock int getWidth() { return 100;} 61 @Mock int getHeight() { return 100;} 62 63 }; 64 65 plot = new XYPlot(null, "test"); 66 numList1 = Arrays.asList(0, 1, 3, 5, 10, 15, 25, 50, 75, 100); // 10 elements 67 numList2 = Arrays.asList(-100, 0, 1, 3, 5, 10, 15, 25, 50, 75, 100, 200); // 12 elements 68 series1 = new SimpleXYSeries(numList1, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, ""); 69 } 70 71 @After 72 public void tearDown() throws Exception { 73 74 } 75 76 @Test 77 public void testOriginFixedMode() throws Exception { 78 plot.addSeries(series1, new LineAndPointFormatter()); 79 plot.centerOnDomainOrigin(5, 2, BoundaryMode.FIXED); 80 plot.calculateMinMaxVals(); 81 82 83 assertEquals(3.0, plot.getCalculatedMinX()); 84 assertEquals(7.0, plot.getCalculatedMaxX()); 85 } 86 87 @Test 88 public void testOriginAutoMode() throws Exception { 89 plot.addSeries(series1, new LineAndPointFormatter()); 90 plot.centerOnDomainOrigin(5); 91 plot.calculateMinMaxVals(); 92 //plot.updateMinMaxVals(); 93 94 assertEquals(10.0, plot.getCalculatedMaxX()); // symmetry is @ 10, not 9 95 assertEquals(0.0, plot.getCalculatedMinX()); 96 97 plot.centerOnRangeOrigin(50); 98 plot.calculateMinMaxVals(); 99 100 assertEquals(100.0, plot.getCalculatedMaxY()); 101 assertEquals(0.0, plot.getCalculatedMinY()); 102 103 } 104 105 @Test 106 public void testOriginGrowMode() throws Exception { 107 plot.addSeries(series1, new LineAndPointFormatter()); 108 plot.centerOnDomainOrigin(5, null, BoundaryMode.GROW); 109 plot.calculateMinMaxVals(); 110 111 assertEquals(0.0, plot.getCalculatedMinX()); 112 assertEquals(10.0, plot.getCalculatedMaxX()); 113 114 // introduce a larger domain set. boundaries should change 115 series1.setModel(numList2, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 116 plot.calculateMinMaxVals(); 117 118 assertEquals(-1.0, plot.getCalculatedMinX()); 119 assertEquals(11.0, plot.getCalculatedMaxX()); 120 121 // revert series model back to the previous set. boundaries should remain the same 122 series1.setModel(numList1, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 123 plot.calculateMinMaxVals(); 124 125 assertEquals(-1.0, plot.getCalculatedMinX()); 126 assertEquals(11.0, plot.getCalculatedMaxX()); 127 } 128 129 @Test 130 public void testOriginShrinkMode() throws Exception { 131 plot.addSeries(series1, new LineAndPointFormatter()); 132 plot.centerOnDomainOrigin(5, null, BoundaryMode.SHRINNK); 133 plot.calculateMinMaxVals(); 134 135 assertEquals(0.0, plot.getCalculatedMinX()); 136 assertEquals(10.0, plot.getCalculatedMaxX()); 137 138 // update with more extreme values...nothing should change in shrink mode: 139 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 140 141 assertEquals(0.0, plot.getCalculatedMinX()); 142 assertEquals(10.0, plot.getCalculatedMaxX()); 143 144 } 145 146 147 // Ifor not sure about filling in test stubs just going to do my own stuff instead. 148 @Test 149 public void testsetDomainBoundaries() throws Exception { 150 plot.addSeries(series1, new LineAndPointFormatter()); 151 plot.calculateMinMaxVals(); 152 153 // default to auto so check them 154 assertEquals(0, plot.getCalculatedMinX()); 155 assertEquals(9, plot.getCalculatedMaxX()); 156 157 plot.setDomainBoundaries(2, BoundaryMode.FIXED, 8, BoundaryMode.FIXED); 158 plot.calculateMinMaxVals(); 159 160 // fixed 161 assertEquals(2, plot.getCalculatedMinX()); 162 assertEquals(8, plot.getCalculatedMaxX()); 163 164 // back to auto 165 plot.setDomainBoundaries(2, BoundaryMode.AUTO, 8, BoundaryMode.AUTO); 166 plot.calculateMinMaxVals(); 167 168 // check again 169 assertEquals(0, plot.getCalculatedMinX()); 170 assertEquals(9, plot.getCalculatedMaxX()); 171 172 // we are not testing MinY well with this dataset. 173 // try grow 174 plot.setDomainBoundaries(2, BoundaryMode.GROW, 8, BoundaryMode.GROW); 175 plot.calculateMinMaxVals(); 176 177 // check inital 178 assertEquals(0, plot.getCalculatedMinX()); 179 assertEquals(9, plot.getCalculatedMaxX()); 180 181 // update with more extreme values... 182 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 183 plot.calculateMinMaxVals(); 184 185 // after growing 186 assertEquals(0, plot.getCalculatedMinX()); 187 assertEquals(11, plot.getCalculatedMaxX()); 188 189 // back to previous 190 series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 191 plot.calculateMinMaxVals(); 192 193 // should not of changed. 194 assertEquals(0, plot.getCalculatedMinX()); 195 assertEquals(11, plot.getCalculatedMaxX()); 196 197 // back to big 198 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 199 200 plot.setDomainBoundaries(2, BoundaryMode.SHRINNK, 8, BoundaryMode.SHRINNK); 201 plot.calculateMinMaxVals(); 202 203 // check inital 204 assertEquals(0, plot.getCalculatedMinX()); 205 assertEquals(11, plot.getCalculatedMaxX()); 206 207 // now small 208 series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 209 plot.calculateMinMaxVals(); 210 211 // after shrinking 212 assertEquals(0, plot.getCalculatedMinX()); 213 assertEquals(9, plot.getCalculatedMaxX()); 214 215 // back to previous 216 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 217 plot.calculateMinMaxVals(); 218 219 // should not of changed. 220 assertEquals(0, plot.getCalculatedMinX()); 221 assertEquals(9, plot.getCalculatedMaxX()); 222 223 // back to auto 224 plot.setDomainBoundaries(2, BoundaryMode.AUTO, 8, BoundaryMode.AUTO); 225 plot.calculateMinMaxVals(); 226 227 // should of changed. 228 assertEquals(0, plot.getCalculatedMinX()); 229 assertEquals(11, plot.getCalculatedMaxX()); 230 } 231 232 @Test 233 public void testsetRangeBoundaries() throws Exception { 234 plot.addSeries(series1, new LineAndPointFormatter()); 235 plot.calculateMinMaxVals(); 236 237 // default to auto so check them 238 assertEquals(0, plot.getCalculatedMinY()); 239 assertEquals(100, plot.getCalculatedMaxY()); 240 241 plot.setRangeBoundaries(5, BoundaryMode.FIXED, 80, BoundaryMode.FIXED); 242 plot.calculateMinMaxVals(); 243 244 // fixed 245 assertEquals(5, plot.getCalculatedMinY()); 246 assertEquals(80, plot.getCalculatedMaxY()); 247 248 // back to auto 249 plot.setRangeBoundaries(2, BoundaryMode.AUTO, 8, BoundaryMode.AUTO); 250 plot.calculateMinMaxVals(); 251 252 // check again 253 assertEquals(0, plot.getCalculatedMinY()); 254 assertEquals(100, plot.getCalculatedMaxY()); 255 256 // try grow 257 plot.setRangeBoundaries(2, BoundaryMode.GROW, 8, BoundaryMode.GROW); 258 plot.calculateMinMaxVals(); 259 260 // check inital 261 assertEquals(0, plot.getCalculatedMinY()); 262 assertEquals(100, plot.getCalculatedMaxY()); 263 264 // update with more extreme values... 265 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 266 plot.calculateMinMaxVals(); 267 268 // after growing 269 assertEquals(-100, plot.getCalculatedMinY()); 270 assertEquals(200, plot.getCalculatedMaxY()); 271 272 // back to previous 273 series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 274 plot.calculateMinMaxVals(); 275 276 // should not of changed. 277 assertEquals(-100, plot.getCalculatedMinY()); 278 assertEquals(200, plot.getCalculatedMaxY()); 279 280 // back to big 281 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 282 283 plot.setRangeBoundaries(2, BoundaryMode.SHRINNK, 8, BoundaryMode.SHRINNK); 284 plot.calculateMinMaxVals(); 285 286 // check inital 287 assertEquals(-100, plot.getCalculatedMinY()); 288 assertEquals(200, plot.getCalculatedMaxY()); 289 290 // now small 291 series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 292 plot.calculateMinMaxVals(); 293 294 // after shrinking 295 assertEquals(0, plot.getCalculatedMinY()); 296 assertEquals(100, plot.getCalculatedMaxY()); 297 298 // back to previous 299 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 300 plot.calculateMinMaxVals(); 301 302 // should not of changed. 303 assertEquals(0, plot.getCalculatedMinY()); 304 assertEquals(100, plot.getCalculatedMaxY()); 305 306 // back to auto 307 plot.setRangeBoundaries(2, BoundaryMode.AUTO, 8, BoundaryMode.AUTO); 308 plot.calculateMinMaxVals(); 309 310 // should of changed. 311 assertEquals(-100, plot.getCalculatedMinY()); 312 assertEquals(200, plot.getCalculatedMaxY()); 313 } 314 315 @Test 316 public void testSetDomainRightMinMax() throws Exception { 317 plot.addSeries(series1, new LineAndPointFormatter()); 318 plot.calculateMinMaxVals(); 319 320 // default to auto so check them 321 assertEquals(0, plot.getCalculatedMinX()); 322 assertEquals(9, plot.getCalculatedMaxX()); 323 324 plot.setDomainRightMax(10); 325 plot.calculateMinMaxVals(); 326 327 // same values. 328 assertEquals(0, plot.getCalculatedMinX()); 329 assertEquals(9, plot.getCalculatedMaxX()); 330 331 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 332 plot.calculateMinMaxVals(); 333 334 // on RightMax 335 assertEquals(0, plot.getCalculatedMinX()); 336 assertEquals(10, plot.getCalculatedMaxX()); 337 338 plot.setDomainRightMax(null); 339 plot.calculateMinMaxVals(); 340 341 // back to full 342 assertEquals(0, plot.getCalculatedMinX()); 343 assertEquals(11, plot.getCalculatedMaxX()); 344 345 // now the RightMin 346 plot.setDomainRightMin(10); 347 plot.calculateMinMaxVals(); 348 349 // still to full 350 assertEquals(0, plot.getCalculatedMinX()); 351 assertEquals(11, plot.getCalculatedMaxX()); 352 353 // small list 354 series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 355 plot.calculateMinMaxVals(); 356 357 // on RightMin 358 assertEquals(0, plot.getCalculatedMinX()); 359 assertEquals(10, plot.getCalculatedMaxX()); 360 361 // now off again 362 plot.setDomainRightMin(null); 363 plot.calculateMinMaxVals(); 364 365 // small values. 366 assertEquals(0, plot.getCalculatedMinX()); 367 assertEquals(9, plot.getCalculatedMaxX()); 368 } 369 370 @Test 371 public void testSetRangeTopBottomMinMax() throws Exception { 372 plot.addSeries(series1, new LineAndPointFormatter()); 373 plot.calculateMinMaxVals(); 374 375 // default to auto so check them 376 assertEquals(0, plot.getCalculatedMinY()); 377 assertEquals(100, plot.getCalculatedMaxY()); 378 379 plot.setRangeTopMax(110); 380 plot.setRangeBottomMin(-50); 381 plot.calculateMinMaxVals(); 382 383 // same values. 384 assertEquals(0, plot.getCalculatedMinY()); 385 assertEquals(100, plot.getCalculatedMaxY()); 386 387 series1.setModel(numList2,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 388 plot.calculateMinMaxVals(); 389 390 // on Limits 391 assertEquals(-50, plot.getCalculatedMinY()); 392 assertEquals(110, plot.getCalculatedMaxY()); 393 394 plot.setRangeTopMax(null); 395 plot.setRangeBottomMin(null); 396 plot.calculateMinMaxVals(); 397 398 // back to full 399 assertEquals(-100, plot.getCalculatedMinY()); 400 assertEquals(200, plot.getCalculatedMaxY()); 401 402 // now the Min 403 plot.setRangeTopMin(150); 404 plot.setRangeBottomMax(-60); 405 plot.calculateMinMaxVals(); 406 407 // still to full 408 assertEquals(-100, plot.getCalculatedMinY()); 409 assertEquals(200, plot.getCalculatedMaxY()); 410 411 // small list 412 series1.setModel(numList1,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); 413 plot.calculateMinMaxVals(); 414 415 // on Limits 416 assertEquals(-60, plot.getCalculatedMinY()); 417 assertEquals(150, plot.getCalculatedMaxY()); 418 419 // now off again 420 plot.setRangeTopMin(null); 421 plot.setRangeBottomMax(null); 422 plot.calculateMinMaxVals(); 423 424 // small values. 425 assertEquals(0, plot.getCalculatedMinY()); 426 assertEquals(100, plot.getCalculatedMaxY()); 427 } 428 429 @Test 430 public void testSetDomainUpperBoundary() throws Exception { 431 432 } 433 434 @Test 435 public void testSetDomainLowerBoundary() throws Exception { 436 437 } 438 439 @Test 440 public void testSetRangeUpperBoundary() throws Exception { 441 442 } 443 444 @Test 445 public void testSetRangeLowerBoundary() throws Exception { 446 447 } 448 449 @Test 450 public void testSetDomainOrigin() throws Exception { 451 452 } 453 454 @Test 455 public void testSetRangeOrigin() throws Exception { 456 457 } 458 459 @Test 460 public void testConfigure() throws Exception { 461 //Context context = Mockit.setUpMock(new MockContext()); 462 Context context = new MockContext.MockContext2(); 463 HashMap<String, String> params = new HashMap<String, String>(); 464 String param1 = "this is a test."; 465 String param2 = Plot.RenderMode.USE_BACKGROUND_THREAD.toString(); 466 String param3 = "#FF0000"; 467 params.put("title", param1); 468 params.put("renderMode", param2); 469 params.put("backgroundPaint.color", param3); 470 params.put("graphWidget.domainLabelPaint.color", param3); 471 472 Configurator.configure(context, plot, params); 473 assertEquals(param1, plot.getTitle()); 474 assertEquals(Plot.RenderMode.USE_BACKGROUND_THREAD, plot.getRenderMode()); 475 assertEquals(Color.parseColor(param3), plot.getBackgroundPaint().getColor()); 476 assertEquals(Color.parseColor(param3), plot.getGraphWidget().getDomainLabelPaint().getColor()); 477 } 478} 479