2121
2222import com .androidplot .test .*;
2323import com .androidplot .ui .*;
24+ import com .androidplot .util .DisplayDimensions ;
2425
2526import org .junit .*;
2627import org .mockito .*;
@@ -67,7 +68,7 @@ public class XYGraphWidgetTest extends AndroidplotTest {
6768
6869
6970 @ Before
70- public void setUp () throws Exception {
71+ public void setUp () {
7172 size = spy (new Size (100 , SizeMode .ABSOLUTE , 100 , SizeMode .ABSOLUTE ));
7273 xyPlot = spy (new XYPlot (getContext (), "XYPlot" ));
7374 when (xyPlot .getRegistry ()).thenReturn (seriesRegistry );
@@ -83,21 +84,16 @@ public void setUp() throws Exception {
8384 graphWidget .setLabelRect (new RectF (0 , 0 , 100 , 100 ));
8485 }
8586
86- @ After
87- public void tearDown () throws Exception {
88-
89- }
90-
9187 @ Test
92- public void testProcessAttrs () throws Exception {
88+ public void processAttrs_withDefaults_disablesGridClipping () {
9389 XYGraphWidget graphWidget = spy (new XYGraphWidget (layoutManager , xyPlot , size ));
9490
9591 graphWidget .processAttrs (typedArray );
9692 verify (graphWidget , times (1 )).setGridClippingEnabled (false );
9793 }
9894
9995 @ Test
100- public void testDoOnDraw_drawGridOnTopFalse () throws Exception {
96+ public void doOnDraw_drawGridOnTopFalse_drawsGridAfterData () throws Exception {
10197 XYGraphWidget graphWidget = spy (new XYGraphWidget (layoutManager , xyPlot , size ));
10298 graphWidget .setDrawGridOnTop (false );
10399 doNothing ().when (graphWidget ).drawGrid (canvas );
@@ -113,7 +109,7 @@ public void testDoOnDraw_drawGridOnTopFalse() throws Exception {
113109 }
114110
115111 @ Test
116- public void testDoOnDraw_drawGridOnTopTrue () throws Exception {
112+ public void doOnDraw_drawGridOnTopTrue_drawsGridBeforeData () throws Exception {
117113 XYGraphWidget graphWidget = spy (new XYGraphWidget (layoutManager , xyPlot , size ));
118114 graphWidget .setDrawGridOnTop (true );
119115 doNothing ().when (graphWidget ).drawGrid (canvas );
@@ -129,7 +125,7 @@ public void testDoOnDraw_drawGridOnTopTrue() throws Exception {
129125 }
130126
131127 @ Test
132- public void testDrawMarkers () throws Exception {
128+ public void drawMarkers_drawsLineForEachMarker () {
133129 xyPlot .addMarker (new XValueMarker (1 , "x" ));
134130 xyPlot .addMarker (new YValueMarker (-1 , "y" ));
135131
@@ -140,42 +136,22 @@ public void testDrawMarkers() throws Exception {
140136 .drawLine (anyFloat (), anyFloat (), anyFloat (), anyFloat (), any (Paint .class ));
141137 }
142138
143- protected void runDrawGridTest () {
144- doNothing ().when (graphWidget ).
145- drawDomainLine (any (Canvas .class ), anyFloat (), any (Number .class ), any (Paint .class ), anyBoolean ());
146-
147- doNothing ().when (graphWidget ).
148- drawRangeLine (any (Canvas .class ), anyFloat (), any (Number .class ), any (Paint .class ), anyBoolean ());
149-
150- xyPlot .setRangeBoundaries (0 , 100 , BoundaryMode .FIXED );
151- xyPlot .setDomainBoundaries (0 , 100 , BoundaryMode .FIXED );
152-
153- graphWidget .drawGrid (canvas );
154-
155- // expecting a 100x100 grid to be drawn:
156- verify (graphWidget , times (100 ))
157- .drawDomainLine (eq (canvas ), anyFloat (), anyFloat (), any (Paint .class ), eq (false ));
158-
159- verify (graphWidget , times (100 ))
160- .drawRangeLine (eq (canvas ), anyFloat (), anyFloat (), any (Paint .class ), eq (false ));
161- }
162-
163139 @ Test
164- public void testDrawGrid_nullOrigin () throws Exception {
140+ public void drawGrid_nullOrigin_drawsGrid () {
165141 when (xyPlot .getDomainOrigin ()).thenReturn (null );
166142 when (xyPlot .getRangeOrigin ()).thenReturn (null );
167143 runDrawGridTest ();
168144 }
169145
170146 @ Test
171- public void testDrawGrid_zeroOrigin () throws Exception {
147+ public void drawGrid_zeroOrigin_drawsGrid () {
172148 when (xyPlot .getDomainOrigin ()).thenReturn (0 );
173149 when (xyPlot .getRangeOrigin ()).thenReturn (0 );
174150 runDrawGridTest ();
175151 }
176152
177153 @ Test
178- public void testDrawGrid_centeredOrigin () throws Exception {
154+ public void drawGrid_centeredOrigin_drawsGrid () {
179155
180156 // set origin to midpoint so we exercise
181157 // code to draw lines on both sides of the origin:
@@ -185,7 +161,7 @@ public void testDrawGrid_centeredOrigin() throws Exception {
185161 }
186162
187163 @ Test
188- public void testDrawCursors_ifCursorPaintAndPositionAreSet () throws Exception {
164+ public void drawCursors_withCursorPaintAndPosition_drawsCursorLines () {
189165 final Paint domainCursorPaint = new Paint ();
190166 graphWidget .setDomainCursorPaint (domainCursorPaint );
191167
@@ -201,7 +177,7 @@ public void testDrawCursors_ifCursorPaintAndPositionAreSet() throws Exception {
201177 }
202178
203179 @ Test
204- public void testDrawCursors_ifCursorPaintAndPositionAreNotSet () throws Exception {
180+ public void testDrawCursors_noCursorPaintOrPosition_drawsNoCursorLines () {
205181 graphWidget .setDomainCursorPaint (null );
206182 graphWidget .setRangeCursorPaint (null );
207183
@@ -212,7 +188,7 @@ public void testDrawCursors_ifCursorPaintAndPositionAreNotSet() throws Exception
212188 }
213189
214190 @ Test
215- public void testDrawCursorLabel () throws Exception {
191+ public void drawCursorLabel_drawsText () {
216192 graphWidget .setDomainCursorPosition (0f );
217193 graphWidget .setRangeCursorPosition (0f );
218194 XYGraphWidget .CursorLabelFormatter clf = mock (XYGraphWidget .CursorLabelFormatter .class );
@@ -224,7 +200,7 @@ public void testDrawCursorLabel() throws Exception {
224200 }
225201
226202 @ Test
227- public void testSetLineLabelEdges () throws Exception {
203+ public void setLineLabelEdges_setsEdges () {
228204
229205 graphWidget .setLineLabelEdges (XYGraphWidget .Edge .LEFT , XYGraphWidget .Edge .BOTTOM );
230206 assertTrue (graphWidget .isLineLabelEnabled (XYGraphWidget .Edge .LEFT ));
@@ -238,7 +214,7 @@ public void testSetLineLabelEdges() throws Exception {
238214 }
239215
240216 @ Test
241- public void testSetLineLabelEdges_bitfield () throws Exception {
217+ public void setLineLabelEdges_bitfield_setsEdges () {
242218
243219 graphWidget .setLineLabelEdges (
244220 XYGraphWidget .Edge .TOP .getValue () | XYGraphWidget .Edge .RIGHT .getValue ());
@@ -250,7 +226,7 @@ public void testSetLineLabelEdges_bitfield() throws Exception {
250226 }
251227
252228 @ Test
253- public void testScreenToSeries () throws Exception {
229+ public void screenToSeries_returnsSeriesCoords () {
254230 when (xyPlot .getBounds ()).thenReturn (new RectRegion (-100 , 100 , -100 , 100 ));
255231
256232 XYCoords coords = graphWidget .screenToSeries (new PointF (0 , 0 ));
@@ -267,7 +243,7 @@ public void testScreenToSeries() throws Exception {
267243 }
268244
269245 @ Test
270- public void testSeriesToScreen () throws Exception {
246+ public void seriesToScreen_returnsScreenPoint () {
271247 when (xyPlot .getBounds ()).thenReturn (new RectRegion (-100 , 100 , -100 , 100 ));
272248
273249 PointF point = graphWidget .seriesToScreen (new XYCoords (-100 , 100 ));
@@ -284,7 +260,7 @@ public void testSeriesToScreen() throws Exception {
284260 }
285261
286262 @ Test
287- public void testScreenToSeriesX () throws Exception {
263+ public void screenToSeriesX_returnsSeriesValue () {
288264 when (xyPlot .getBounds ()).thenReturn (new RectRegion (-100 , 100 , -100 , 100 ));
289265
290266 assertEquals (-100 , graphWidget .screenToSeriesX (new PointF (0 , 0 )).intValue ());
@@ -293,7 +269,7 @@ public void testScreenToSeriesX() throws Exception {
293269 }
294270
295271 @ Test
296- public void testScreenToSeriesY () throws Exception {
272+ public void screenToSeriesY_returnsSeriesValue () {
297273 when (xyPlot .getBounds ()).thenReturn (new RectRegion (-100 , 100 , -100 , 100 ));
298274
299275 assertEquals (100 , graphWidget .screenToSeriesY (new PointF (0 , 0 )).intValue ());
@@ -302,7 +278,7 @@ public void testScreenToSeriesY() throws Exception {
302278 }
303279
304280 @ Test
305- public void testSeriesToScreenX () throws Exception {
281+ public void seriesToScreenX_returnsScreenValue () {
306282 when (xyPlot .getBounds ()).thenReturn (new RectRegion (-100 , 100 , -100 , 100 ));
307283
308284 assertEquals (0f , graphWidget .seriesToScreenX (-100 ));
@@ -311,11 +287,45 @@ public void testSeriesToScreenX() throws Exception {
311287 }
312288
313289 @ Test
314- public void testSeriesToScreenY () throws Exception {
290+ public void seriesToScreenY_returnsScreenValue () {
315291 when (xyPlot .getBounds ()).thenReturn (new RectRegion (-100 , 100 , -100 , 100 ));
316292
317293 assertEquals (100f , graphWidget .seriesToScreenY (100 ));
318294 assertEquals (0f , graphWidget .seriesToScreenY (-100 ));
319295 assertEquals (50f , graphWidget .seriesToScreenY (0 ));
320296 }
297+
298+ @ Test
299+ public void setGridInsets_updatesGridRect () {
300+ graphWidget .setGridInsets (new Insets (0 , 0 , 0 , 0 ));
301+ final RectF oldRect = graphWidget .getGridRect ();
302+
303+ graphWidget .setGridInsets (new Insets (2 , 2 , 2 , 2 ));
304+ final RectF newRect = graphWidget .getGridRect ();
305+
306+ assertEquals (oldRect .left + 2 , newRect .left );
307+ assertEquals (oldRect .top + 2 , newRect .top );
308+ assertEquals (oldRect .right - 2 , newRect .right );
309+ assertEquals (oldRect .bottom -2 , newRect .bottom );
310+ }
311+
312+ private void runDrawGridTest () {
313+ doNothing ().when (graphWidget ).
314+ drawDomainLine (any (Canvas .class ), anyFloat (), any (Number .class ), any (Paint .class ), anyBoolean ());
315+
316+ doNothing ().when (graphWidget ).
317+ drawRangeLine (any (Canvas .class ), anyFloat (), any (Number .class ), any (Paint .class ), anyBoolean ());
318+
319+ xyPlot .setRangeBoundaries (0 , 100 , BoundaryMode .FIXED );
320+ xyPlot .setDomainBoundaries (0 , 100 , BoundaryMode .FIXED );
321+
322+ graphWidget .drawGrid (canvas );
323+
324+ // expecting a 100x100 grid to be drawn:
325+ verify (graphWidget , times (100 ))
326+ .drawDomainLine (eq (canvas ), anyFloat (), anyFloat (), any (Paint .class ), eq (false ));
327+
328+ verify (graphWidget , times (100 ))
329+ .drawRangeLine (eq (canvas ), anyFloat (), anyFloat (), any (Paint .class ), eq (false ));
330+ }
321331}
0 commit comments