1 /* 2 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.javafx.scene.control; 27 28 import com.sun.javafx.scene.SceneHelper; 29 import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains; 30 import static org.junit.Assert.assertEquals; 31 import static org.junit.Assert.assertFalse; 32 import static org.junit.Assert.assertNull; 33 import static org.junit.Assert.assertSame; 34 import static org.junit.Assert.assertTrue; 35 import static org.junit.Assert.fail; 36 import javafx.beans.property.IntegerProperty; 37 import javafx.beans.property.ObjectProperty; 38 import javafx.beans.property.SimpleIntegerProperty; 39 import javafx.beans.property.SimpleObjectProperty; 40 import javafx.css.CssMetaData; 41 import javafx.css.StyleableProperty; 42 import javafx.scene.Node; 43 import javafx.scene.Scene; 44 import javafx.scene.input.MouseEvent; 45 import javafx.scene.layout.StackPane; 46 import javafx.scene.layout.VBox; 47 import javafx.stage.Stage; 48 import javafx.util.Callback; 49 50 import org.junit.Before; 51 import org.junit.Ignore; 52 import org.junit.Test; 53 54 import test.com.sun.javafx.pgstub.StubToolkit; 55 import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer; 56 import test.com.sun.javafx.scene.control.infrastructure.MouseEventGenerator; 57 import com.sun.javafx.tk.Toolkit; 58 import javafx.scene.control.Label; 59 import javafx.scene.control.Pagination; 60 61 public class PaginationTest { 62 private Pagination pagination; 63 private Toolkit tk; 64 private Scene scene; 65 private Stage stage; 66 private StackPane root; 67 68 @Before public void setup() { 69 pagination = new Pagination(); 70 tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM) 71 root = new StackPane(); 72 scene = new Scene(root); 73 stage = new Stage(); 74 stage.setScene(scene); 75 } 76 77 /********************************************************************* 78 * Helper methods * 79 ********************************************************************/ 80 private void show() { 81 stage.show(); 82 stage.requestFocus(); 83 } 84 85 /********************************************************************* 86 * Tests for default values * 87 ********************************************************************/ 88 89 @Test public void defaultConstructorShouldSetStyleClassTo_pagination() { 90 assertStyleClassContains(pagination, "pagination"); 91 } 92 93 @Test public void defaultCurrentPageIndex() { 94 assertEquals(pagination.getCurrentPageIndex(), 0); 95 } 96 97 @Test public void defaultPageCount() { 98 assertEquals(pagination.getPageCount(), Pagination.INDETERMINATE); 99 } 100 101 @Test public void defaultPageFactory() { 102 assertNull(pagination.getPageFactory()); 103 } 104 105 @Test public void defaultMaxPageIndicatorCount() { 106 assertEquals(pagination.getMaxPageIndicatorCount(), 10); 107 } 108 109 /********************************************************************* 110 * Tests for property binding * 111 ********************************************************************/ 112 113 @Test public void checkMaxPageIndicatorCountPropertyBind() { 114 IntegerProperty intPr = new SimpleIntegerProperty(200); 115 pagination.maxPageIndicatorCountProperty().bind(intPr); 116 assertEquals("number of visible pages cannot be bound", pagination.maxPageIndicatorCountProperty().getValue(), 200.0, 0.0); 117 intPr.setValue(105); 118 assertEquals("number of visible pages cannot be bound", pagination.maxPageIndicatorCountProperty().getValue(), 105.0, 0.0); 119 } 120 121 @Test(expected = java.lang.UnsupportedOperationException.class) public void checkPageIndexPropertyBind() { 122 IntegerProperty intPr = new SimpleIntegerProperty(10); 123 pagination.currentPageIndexProperty().bind(intPr); 124 } 125 126 @Test public void checkPageFactoryPropertyBind() { 127 Callback callback = arg0 -> null; 128 ObjectProperty objPr = new SimpleObjectProperty(callback); 129 pagination.pageFactoryProperty().bind(objPr); 130 assertSame("page factory cannot be bound", pagination.pageFactoryProperty().getValue(), callback); 131 } 132 133 /********************************************************************* 134 * CSS related Tests * 135 ********************************************************************/ 136 @Test public void whenMaxPageIndicatorCountIsBound_impl_cssSettable_ReturnsFalse() { 137 CssMetaData styleable = ((StyleableProperty)pagination.maxPageIndicatorCountProperty()).getCssMetaData(); 138 assertTrue(styleable.isSettable(pagination)); 139 IntegerProperty intPr = new SimpleIntegerProperty(10); 140 pagination.maxPageIndicatorCountProperty().bind(intPr); 141 assertFalse(styleable.isSettable(pagination)); 142 } 143 144 @Test public void whenMaxPageIndicatorCountIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() { 145 CssMetaData styleable = ((StyleableProperty)pagination.maxPageIndicatorCountProperty()).getCssMetaData(); 146 assertTrue(styleable.isSettable(pagination)); 147 } 148 149 @Test public void canSpecifyMaxPageIndicatorCountViaCSS() { 150 ((StyleableProperty)pagination.maxPageIndicatorCountProperty()).applyStyle(null, 100); 151 assertSame(100, pagination.getMaxPageIndicatorCount()); 152 } 153 154 /******************************************************************** 155 * Miscellaneous Tests * 156 ********************************************************************/ 157 158 @Test public void setCurrentPageIndexAndNavigateWithKeyBoard() { 159 pagination.setPageCount(25); 160 pagination.setPageFactory(pageIndex -> { 161 Node n = createPage(pageIndex); 162 return n; 163 }); 164 root.setPrefSize(400, 400); 165 root.getChildren().add(pagination); 166 show(); 167 168 tk.firePulse(); 169 assertTrue(pagination.isFocused()); 170 171 KeyEventFirer keyboard = new KeyEventFirer(pagination); 172 keyboard.doRightArrowPress(); 173 tk.firePulse(); 174 175 assertEquals(1, pagination.getCurrentPageIndex()); 176 177 keyboard.doRightArrowPress(); 178 tk.firePulse(); 179 180 assertEquals(2, pagination.getCurrentPageIndex()); 181 } 182 183 @Ignore @Test public void setCurrentPageIndexAndNavigateWithMouse() { 184 pagination.setPageCount(25); 185 pagination.setPageFactory(pageIndex -> { 186 Node n = createPage(pageIndex); 187 return n; 188 }); 189 190 root.setPrefSize(400, 400); 191 root.getChildren().add(pagination); 192 show(); 193 194 root.applyCss(); 195 root.layout(); 196 tk.firePulse(); 197 assertTrue(pagination.isFocused()); 198 199 double xval = (pagination.localToScene(pagination.getLayoutBounds())).getMinX(); 200 double yval = (pagination.localToScene(pagination.getLayoutBounds())).getMinY(); 201 202 SceneHelper.processMouseEvent(scene, 203 MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+170, yval+380)); 204 tk.firePulse(); 205 206 assertEquals(3, pagination.getCurrentPageIndex()); 207 } 208 209 @Test public void setCurrentPageIndexAndVerifyCallback() { 210 pagination.setPageCount(25); 211 pagination.setPageFactory(pageIndex -> { 212 Node n = createPage(pageIndex); 213 assertTrue(pageIndex == 0 || pageIndex == 4); 214 return n; 215 }); 216 217 root.setPrefSize(400, 400); 218 root.getChildren().add(pagination); 219 show(); 220 221 pagination.setCurrentPageIndex(4); 222 } 223 224 @Test public void setCountToZero() { 225 pagination.setPageCount(0); 226 227 root.setPrefSize(400, 400); 228 root.getChildren().add(pagination); 229 show(); 230 231 assertEquals(Integer.MAX_VALUE, pagination.getPageCount()); 232 } 233 234 @Test public void setCurrentPageIndexLessThanZero() { 235 pagination.setPageCount(100); 236 root.setPrefSize(400, 400); 237 root.getChildren().add(pagination); 238 show(); 239 240 pagination.setCurrentPageIndex(5); 241 pagination.setCurrentPageIndex(-1); 242 assertEquals(0, pagination.getCurrentPageIndex()); 243 } 244 245 @Test public void setCurrentPageIndexGreaterThanPageCount() { 246 pagination.setPageCount(100); 247 root.setPrefSize(400, 400); 248 root.getChildren().add(pagination); 249 show(); 250 251 pagination.setCurrentPageIndex(5); 252 pagination.setCurrentPageIndex(100); 253 assertEquals(99, pagination.getCurrentPageIndex()); 254 } 255 256 @Test public void setMaxPageIndicatorCountLessThanZero() { 257 pagination.setPageCount(100); 258 root.setPrefSize(400, 400); 259 root.getChildren().add(pagination); 260 show(); 261 262 pagination.setMaxPageIndicatorCount(-1); 263 assertEquals(10, pagination.getMaxPageIndicatorCount()); 264 } 265 266 @Test public void setMaxPageIndicatorCountGreaterThanPageCount() { 267 pagination.setPageCount(100); 268 root.setPrefSize(400, 400); 269 root.getChildren().add(pagination); 270 show(); 271 272 pagination.setMaxPageIndicatorCount(101); 273 assertEquals(10, pagination.getMaxPageIndicatorCount()); 274 } 275 276 @Test public void pageCountIsLessThanMaxPageIndicatorCount_RT21660() { 277 pagination.setPageCount(5); 278 root.setPrefSize(400, 400); 279 root.getChildren().add(pagination); 280 show(); 281 282 pagination.setCurrentPageIndex(4); 283 tk.firePulse(); 284 assertTrue(pagination.isFocused()); 285 286 KeyEventFirer keyboard = new KeyEventFirer(pagination); 287 keyboard.doRightArrowPress(); 288 tk.firePulse(); 289 290 assertEquals(4, pagination.getCurrentPageIndex()); 291 } 292 293 @Test public void divideByZeroErrorWhenSizeIsSmall_RT22687() { 294 pagination.setPageCount(15); 295 root.setMaxSize(100, 200); 296 root.getChildren().add(pagination); 297 show(); 298 299 try { 300 KeyEventFirer keyboard = new KeyEventFirer(pagination); 301 keyboard.doRightArrowPress(); 302 tk.firePulse(); 303 } catch (Exception e) { 304 fail(); 305 } 306 assertEquals(1, pagination.getCurrentPageIndex()); 307 } 308 309 public VBox createPage(int pageIndex) { 310 VBox box = new VBox(5); 311 int page = pageIndex * 10; 312 for (int i = page; i < page + 10; i++) { 313 Label l = new Label("PAGE INDEX " + pageIndex); 314 box.getChildren().add(l); 315 } 316 return box; 317 } 318 }