1 /*
   2  * Copyright (c) 2010, 2015, 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 static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains;
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.assertFalse;
  31 import static org.junit.Assert.assertNull;
  32 import static org.junit.Assert.assertSame;
  33 import static org.junit.Assert.assertTrue;
  34 import static org.junit.Assert.fail;
  35 import javafx.beans.property.IntegerProperty;
  36 import javafx.beans.property.ObjectProperty;
  37 import javafx.beans.property.SimpleIntegerProperty;
  38 import javafx.beans.property.SimpleObjectProperty;
  39 import javafx.css.CssMetaData;
  40 import javafx.css.StyleableProperty;
  41 import javafx.scene.Node;
  42 import javafx.scene.Scene;
  43 import javafx.scene.input.MouseEvent;
  44 import javafx.scene.layout.StackPane;
  45 import javafx.scene.layout.VBox;
  46 import javafx.stage.Stage;
  47 import javafx.util.Callback;
  48 
  49 import org.junit.Before;
  50 import org.junit.Ignore;
  51 import org.junit.Test;
  52 
  53 import test.com.sun.javafx.pgstub.StubToolkit;
  54 import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
  55 import test.com.sun.javafx.scene.control.infrastructure.MouseEventGenerator;
  56 import com.sun.javafx.tk.Toolkit;
  57 import javafx.scene.control.Label;
  58 import javafx.scene.control.Pagination;
  59 
  60 public class PaginationTest {
  61     private Pagination pagination;
  62     private Toolkit tk;
  63     private Scene scene;
  64     private Stage stage;
  65     private StackPane root;
  66 
  67     @Before public void setup() {
  68         pagination = new Pagination();
  69         tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM)
  70         root = new StackPane();
  71         scene = new Scene(root);
  72         stage = new Stage();
  73         stage.setScene(scene);
  74     }
  75 
  76     /*********************************************************************
  77      * Helper methods                                                    *
  78      ********************************************************************/
  79     private void show() {
  80         stage.show();
  81         stage.requestFocus();
  82     }
  83 
  84     /*********************************************************************
  85      * Tests for default values                                         *
  86      ********************************************************************/
  87 
  88     @Test public void defaultConstructorShouldSetStyleClassTo_pagination() {
  89         assertStyleClassContains(pagination, "pagination");
  90     }
  91 
  92     @Test public void defaultCurrentPageIndex() {
  93         assertEquals(pagination.getCurrentPageIndex(), 0);
  94     }
  95 
  96     @Test public void defaultPageCount() {
  97         assertEquals(pagination.getPageCount(), Pagination.INDETERMINATE);
  98     }
  99 
 100     @Test public void defaultPageFactory() {
 101         assertNull(pagination.getPageFactory());
 102     }
 103 
 104     @Test public void defaultMaxPageIndicatorCount() {
 105         assertEquals(pagination.getMaxPageIndicatorCount(), 10);
 106     }
 107 
 108     /*********************************************************************
 109      * Tests for property binding                                        *
 110      ********************************************************************/
 111 
 112     @Test public void checkMaxPageIndicatorCountPropertyBind() {
 113         IntegerProperty intPr = new SimpleIntegerProperty(200);
 114         pagination.maxPageIndicatorCountProperty().bind(intPr);
 115         assertEquals("number of visible pages cannot be bound", pagination.maxPageIndicatorCountProperty().getValue(), 200.0, 0.0);
 116         intPr.setValue(105);
 117         assertEquals("number of visible pages cannot be bound", pagination.maxPageIndicatorCountProperty().getValue(), 105.0, 0.0);
 118     }
 119 
 120     @Test(expected = java.lang.UnsupportedOperationException.class) public void checkPageIndexPropertyBind() {
 121         IntegerProperty intPr = new SimpleIntegerProperty(10);
 122         pagination.currentPageIndexProperty().bind(intPr);
 123     }
 124 
 125     @Test public void checkPageFactoryPropertyBind() {
 126         Callback callback = arg0 -> null;
 127         ObjectProperty objPr = new SimpleObjectProperty(callback);
 128         pagination.pageFactoryProperty().bind(objPr);
 129         assertSame("page factory cannot be bound", pagination.pageFactoryProperty().getValue(), callback);
 130     }
 131 
 132     /*********************************************************************
 133      * CSS related Tests                                                 *
 134      ********************************************************************/
 135     @Test public void whenMaxPageIndicatorCountIsBound_impl_cssSettable_ReturnsFalse() {
 136         CssMetaData styleable = ((StyleableProperty)pagination.maxPageIndicatorCountProperty()).getCssMetaData();
 137         assertTrue(styleable.isSettable(pagination));
 138         IntegerProperty intPr = new SimpleIntegerProperty(10);
 139         pagination.maxPageIndicatorCountProperty().bind(intPr);
 140         assertFalse(styleable.isSettable(pagination));
 141     }
 142 
 143     @Test public void whenMaxPageIndicatorCountIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() {
 144         CssMetaData styleable = ((StyleableProperty)pagination.maxPageIndicatorCountProperty()).getCssMetaData();
 145         assertTrue(styleable.isSettable(pagination));
 146     }
 147 
 148     @Test public void canSpecifyMaxPageIndicatorCountViaCSS() {
 149         ((StyleableProperty)pagination.maxPageIndicatorCountProperty()).applyStyle(null, 100);
 150         assertSame(100, pagination.getMaxPageIndicatorCount());
 151     }
 152 
 153     /********************************************************************
 154      * Miscellaneous Tests                                              *
 155      ********************************************************************/
 156 
 157     @Test public void setCurrentPageIndexAndNavigateWithKeyBoard() {
 158         pagination.setPageCount(25);
 159         pagination.setPageFactory(pageIndex -> {
 160             Node n = createPage(pageIndex);
 161             return n;
 162         });
 163         root.setPrefSize(400, 400);
 164         root.getChildren().add(pagination);
 165         show();
 166 
 167         tk.firePulse();
 168         assertTrue(pagination.isFocused());
 169 
 170         KeyEventFirer keyboard = new KeyEventFirer(pagination);
 171         keyboard.doRightArrowPress();
 172         tk.firePulse();
 173 
 174         assertEquals(1, pagination.getCurrentPageIndex());
 175 
 176         keyboard.doRightArrowPress();
 177         tk.firePulse();
 178 
 179         assertEquals(2, pagination.getCurrentPageIndex());
 180     }
 181 
 182     @Ignore @Test public void setCurrentPageIndexAndNavigateWithMouse() {
 183         pagination.setPageCount(25);
 184         pagination.setPageFactory(pageIndex -> {
 185             Node n = createPage(pageIndex);
 186             return n;
 187         });
 188 
 189         root.setPrefSize(400, 400);
 190         root.getChildren().add(pagination);
 191         show();
 192 
 193         root.applyCss();
 194         root.layout();
 195         tk.firePulse();
 196         assertTrue(pagination.isFocused());
 197 
 198         double xval = (pagination.localToScene(pagination.getLayoutBounds())).getMinX();
 199         double yval = (pagination.localToScene(pagination.getLayoutBounds())).getMinY();
 200 
 201         scene.impl_processMouseEvent(
 202             MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+170, yval+380));
 203         tk.firePulse();
 204 
 205         assertEquals(3, pagination.getCurrentPageIndex());
 206     }
 207 
 208     @Test public void setCurrentPageIndexAndVerifyCallback() {
 209         pagination.setPageCount(25);
 210         pagination.setPageFactory(pageIndex -> {
 211             Node n = createPage(pageIndex);
 212             assertTrue(pageIndex == 0 || pageIndex == 4);
 213             return n;
 214         });
 215 
 216         root.setPrefSize(400, 400);
 217         root.getChildren().add(pagination);
 218         show();
 219 
 220         pagination.setCurrentPageIndex(4);
 221     }
 222 
 223     @Test public void setCountToZero() {
 224         pagination.setPageCount(0);
 225 
 226         root.setPrefSize(400, 400);
 227         root.getChildren().add(pagination);
 228         show();
 229 
 230         assertEquals(Integer.MAX_VALUE, pagination.getPageCount());
 231     }
 232 
 233     @Test public void setCurrentPageIndexLessThanZero() {
 234         pagination.setPageCount(100);
 235         root.setPrefSize(400, 400);
 236         root.getChildren().add(pagination);
 237         show();
 238 
 239         pagination.setCurrentPageIndex(5);
 240         pagination.setCurrentPageIndex(-1);
 241         assertEquals(0, pagination.getCurrentPageIndex());
 242     }
 243 
 244     @Test public void setCurrentPageIndexGreaterThanPageCount() {
 245         pagination.setPageCount(100);
 246         root.setPrefSize(400, 400);
 247         root.getChildren().add(pagination);
 248         show();
 249 
 250         pagination.setCurrentPageIndex(5);
 251         pagination.setCurrentPageIndex(100);
 252         assertEquals(99, pagination.getCurrentPageIndex());
 253     }
 254 
 255     @Test public void setMaxPageIndicatorCountLessThanZero() {
 256         pagination.setPageCount(100);
 257         root.setPrefSize(400, 400);
 258         root.getChildren().add(pagination);
 259         show();
 260 
 261         pagination.setMaxPageIndicatorCount(-1);
 262         assertEquals(10, pagination.getMaxPageIndicatorCount());
 263     }
 264 
 265     @Test public void setMaxPageIndicatorCountGreaterThanPageCount() {
 266         pagination.setPageCount(100);
 267         root.setPrefSize(400, 400);
 268         root.getChildren().add(pagination);
 269         show();
 270 
 271         pagination.setMaxPageIndicatorCount(101);
 272         assertEquals(10, pagination.getMaxPageIndicatorCount());
 273     }
 274 
 275     @Test public void pageCountIsLessThanMaxPageIndicatorCount_RT21660() {
 276         pagination.setPageCount(5);
 277         root.setPrefSize(400, 400);
 278         root.getChildren().add(pagination);
 279         show();
 280 
 281         pagination.setCurrentPageIndex(4);
 282         tk.firePulse();
 283         assertTrue(pagination.isFocused());
 284 
 285         KeyEventFirer keyboard = new KeyEventFirer(pagination);
 286         keyboard.doRightArrowPress();
 287         tk.firePulse();
 288 
 289         assertEquals(4, pagination.getCurrentPageIndex());
 290     }
 291 
 292     @Test public void divideByZeroErrorWhenSizeIsSmall_RT22687() {
 293         pagination.setPageCount(15);
 294         root.setMaxSize(100, 200);
 295         root.getChildren().add(pagination);
 296         show();
 297 
 298         try {
 299             KeyEventFirer keyboard = new KeyEventFirer(pagination);
 300             keyboard.doRightArrowPress();
 301             tk.firePulse();
 302         } catch (Exception e) {
 303             fail();
 304         }
 305         assertEquals(1, pagination.getCurrentPageIndex());
 306     }
 307 
 308     public VBox createPage(int pageIndex) {
 309         VBox box = new VBox(5);
 310         int page = pageIndex * 10;
 311         for (int i = page; i < page + 10; i++) {
 312             Label l = new Label("PAGE INDEX " + pageIndex);
 313             box.getChildren().add(l);
 314         }
 315         return box;
 316     }
 317 }