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