1 /*
   2  * Copyright (c) 2010, 2013, 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.concurrent;
  27 
  28 import javafx.concurrent.Task;
  29 import javafx.concurrent.TaskShim;
  30 import test.javafx.concurrent.mocks.ProgressingTask;
  31 import org.junit.Before;
  32 import org.junit.Test;
  33 
  34 import static org.junit.Assert.*;
  35 
  36 /**
  37  * A Task which always completes normally, but which also progresses from
  38  * 0 to 20 (inclusive). Initially the indeterminate state is used for
  39  * progress and maxProgress, but during execution the progress and
  40  * maxProgress are updated.
  41  */
  42 public class TaskProgressTest {
  43     private Task task;
  44 
  45     @Before public void setup() {
  46         task = new ProgressingTask();
  47     }
  48 
  49     /************************************************************************
  50      * Run the task and check that the final progress and maxProgress and
  51      * percentDone have correct values.
  52      ***********************************************************************/
  53 
  54     @Test public void afterRunningWorkDoneShouldBe_20() {
  55         task.run();
  56         assertEquals(20, task.getWorkDone(), 0);
  57     }
  58 
  59     @Test public void afterRunningTotalWorkShouldBe_20() {
  60         task.run();
  61         assertEquals(20, task.getTotalWork(), 0);
  62     }
  63 
  64     @Test public void afterRunningProgressShouldBe_1() {
  65         task.run();
  66         assertEquals(1, task.getProgress(), 0);
  67     }
  68 
  69     /************************************************************************
  70      * Test the updateProgress method on Task, that various inputs lead to
  71      * expected outputs according to the specification
  72      ***********************************************************************/
  73 
  74     @Test public void updateProgress_Long_0_100() {
  75         TaskShim.updateProgress(task, 0, 100);
  76         assertEquals(0, task.getProgress(), 0);
  77         assertEquals(0, task.getWorkDone(), 0);
  78         assertEquals(100, task.getTotalWork(), 0);
  79     }
  80 
  81     @Test public void updateProgress_Long_n1_100() {
  82         TaskShim.updateProgress(task, -1, 100);
  83         assertEquals(-1, task.getProgress(), 0);
  84         assertEquals(-1, task.getWorkDone(), 0);
  85         assertEquals(100, task.getTotalWork(), 0);
  86     }
  87 
  88     @Test public void updateProgress_Long_n10_100() {
  89         TaskShim.updateProgress(task, -10, 100);
  90         assertEquals(-1, task.getProgress(), 0);
  91         assertEquals(-1, task.getWorkDone(), 0);
  92         assertEquals(100, task.getTotalWork(), 0);
  93     }
  94 
  95     @Test public void updateProgress_Long_MIN_VALUE_100() {
  96         TaskShim.updateProgress(task, Long.MIN_VALUE, 100);
  97         assertEquals(-1, task.getProgress(), 0);
  98         assertEquals(-1, task.getWorkDone(), 0);
  99         assertEquals(100, task.getTotalWork(), 0);
 100     }
 101 
 102     @Test public void updateProgress_Long_10_100() {
 103         TaskShim.updateProgress(task, 10, 100);
 104         assertEquals(.1, task.getProgress(), 0);
 105         assertEquals(10, task.getWorkDone(), 0);
 106         assertEquals(100, task.getTotalWork(), 0);
 107     }
 108 
 109     @Test public void updateProgress_Long_100_100() {
 110         TaskShim.updateProgress(task, 100, 100);
 111         assertEquals(1, task.getProgress(), 0);
 112         assertEquals(100, task.getWorkDone(), 0);
 113         assertEquals(100, task.getTotalWork(), 0);
 114     }
 115 
 116     @Test public void updateProgress_Long_110_100() {
 117         TaskShim.updateProgress(task, 110, 100);
 118         assertEquals(1, task.getProgress(), 0);
 119         assertEquals(100, task.getWorkDone(), 0);
 120         assertEquals(100, task.getTotalWork(), 0);
 121     }
 122 
 123     @Test public void updateProgress_Long_MAX_VALUE_100() {
 124         TaskShim.updateProgress(task, Long.MAX_VALUE, 100);
 125         assertEquals(1, task.getProgress(), 0);
 126         assertEquals(100, task.getWorkDone(), 0);
 127         assertEquals(100, task.getTotalWork(), 0);
 128     }
 129 
 130     @Test public void updateProgress_Long_0_n1() {
 131         TaskShim.updateProgress(task, 0, -1);
 132         assertEquals(-1, task.getProgress(), 0);
 133         assertEquals(-1, task.getWorkDone(), 0);
 134         assertEquals(-1, task.getTotalWork(), 0);
 135     }
 136 
 137     @Test public void updateProgress_Long_0_n10() {
 138         TaskShim.updateProgress(task, 0, -10);
 139         assertEquals(-1, task.getProgress(), 0);
 140         assertEquals(-1, task.getWorkDone(), 0);
 141         assertEquals(-1, task.getTotalWork(), 0);
 142     }
 143 
 144     @Test public void updateProgress_Long_0_MIN_VALUE() {
 145         TaskShim.updateProgress(task, 0, Long.MIN_VALUE);
 146         assertEquals(-1, task.getProgress(), 0);
 147         assertEquals(-1, task.getWorkDone(), 0);
 148         assertEquals(-1, task.getTotalWork(), 0);
 149     }
 150 
 151     @Test public void updateProgress_Long_100_10() {
 152         TaskShim.updateProgress(task, 100, 10);
 153         assertEquals(1, task.getProgress(), 0);
 154         assertEquals(10, task.getWorkDone(), 0);
 155         assertEquals(10, task.getTotalWork(), 0);
 156     }
 157 
 158     @Test public void updateProgress_Long_100_MAX_VALUE() {
 159         TaskShim.updateProgress(task, 100, Long.MAX_VALUE);
 160         assertEquals(100.0 / Long.MAX_VALUE, task.getProgress(), 0);
 161         assertEquals(100, task.getWorkDone(), 0);
 162         assertEquals(Long.MAX_VALUE, task.getTotalWork(), 0);
 163     }
 164 
 165     /* Now test the Double variants (Infinity, NaN)*/
 166     @Test public void updateProgress_Double_Infinity_100() {
 167         TaskShim.updateProgress(task, Double.POSITIVE_INFINITY, 100);
 168         assertEquals(-1, task.getProgress(), 0);
 169         assertEquals(-1, task.getWorkDone(), 0);
 170         assertEquals(100, task.getTotalWork(), 0);
 171     }
 172 
 173     @Test public void updateProgress_Double_NInfinity_100() {
 174         TaskShim.updateProgress(task, Double.NEGATIVE_INFINITY, 100);
 175         assertEquals(-1, task.getProgress(), 0);
 176         assertEquals(-1, task.getWorkDone(), 0);
 177         assertEquals(100, task.getTotalWork(), 0);
 178     }
 179 
 180     @Test public void updateProgress_Double_NaN_100() {
 181         TaskShim.updateProgress(task, Double.NaN, 100);
 182         assertEquals(-1, task.getProgress(), 0);
 183         assertEquals(-1, task.getWorkDone(), 0);
 184         assertEquals(100, task.getTotalWork(), 0);
 185     }
 186 
 187     @Test public void updateProgress_Double_0_Infinity() {
 188         TaskShim.updateProgress(task, 0, Double.POSITIVE_INFINITY);
 189         assertEquals(-1, task.getProgress(), 0);
 190         assertEquals(-1, task.getWorkDone(), 0);
 191         assertEquals(-1, task.getTotalWork(), 0);
 192     }
 193 
 194     @Test public void updateProgress_Double_0_NInfinity() {
 195         TaskShim.updateProgress(task, 0, Double.NEGATIVE_INFINITY);
 196         assertEquals(-1, task.getProgress(), 0);
 197         assertEquals(-1, task.getWorkDone(), 0);
 198         assertEquals(-1, task.getTotalWork(), 0);
 199     }
 200 
 201     @Test public void updateProgress_Double_0_NaN() {
 202         TaskShim.updateProgress(task, 0, Double.NaN);
 203         assertEquals(-1, task.getProgress(), 0);
 204         assertEquals(-1, task.getWorkDone(), 0);
 205         assertEquals(-1, task.getTotalWork(), 0);
 206     }
 207 
 208     @Test public void updateProgress_Double_Infinity_Infinity() {
 209         TaskShim.updateProgress(task, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
 210         assertEquals(-1, task.getProgress(), 0);
 211         assertEquals(-1, task.getWorkDone(), 0);
 212         assertEquals(-1, task.getTotalWork(), 0);
 213     }
 214 
 215     @Test public void updateProgress_Double_NInfinity_Infinity() {
 216         TaskShim.updateProgress(task, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
 217         assertEquals(-1, task.getProgress(), 0);
 218         assertEquals(-1, task.getWorkDone(), 0);
 219         assertEquals(-1, task.getTotalWork(), 0);
 220     }
 221 
 222     @Test public void updateProgress_Double_Infinity_NInfinity() {
 223         TaskShim.updateProgress(task, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
 224         assertEquals(-1, task.getProgress(), 0);
 225         assertEquals(-1, task.getWorkDone(), 0);
 226         assertEquals(-1, task.getTotalWork(), 0);
 227     }
 228 
 229     @Test public void updateProgress_Double_NInfinity_NInfinity() {
 230         TaskShim.updateProgress(task, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
 231         assertEquals(-1, task.getProgress(), 0);
 232         assertEquals(-1, task.getWorkDone(), 0);
 233         assertEquals(-1, task.getTotalWork(), 0);
 234     }
 235 
 236     @Test public void updateProgress_Double_Infinity_NaN() {
 237         TaskShim.updateProgress(task, Double.POSITIVE_INFINITY, Double.NaN);
 238         assertEquals(-1, task.getProgress(), 0);
 239         assertEquals(-1, task.getWorkDone(), 0);
 240         assertEquals(-1, task.getTotalWork(), 0);
 241     }
 242 
 243     @Test public void updateProgress_Double_NInfinity_NaN() {
 244         TaskShim.updateProgress(task, Double.NEGATIVE_INFINITY, Double.NaN);
 245         assertEquals(-1, task.getProgress(), 0);
 246         assertEquals(-1, task.getWorkDone(), 0);
 247         assertEquals(-1, task.getTotalWork(), 0);
 248     }
 249 
 250     @Test public void updateProgress_Double_NaN_Infinity() {
 251         TaskShim.updateProgress(task, Double.NaN, Double.POSITIVE_INFINITY);
 252         assertEquals(-1, task.getProgress(), 0);
 253         assertEquals(-1, task.getWorkDone(), 0);
 254         assertEquals(-1, task.getTotalWork(), 0);
 255     }
 256 
 257     @Test public void updateProgress_Double_NaN_NInfinity() {
 258         TaskShim.updateProgress(task, Double.NaN, Double.NEGATIVE_INFINITY);
 259         assertEquals(-1, task.getProgress(), 0);
 260         assertEquals(-1, task.getWorkDone(), 0);
 261         assertEquals(-1, task.getTotalWork(), 0);
 262     }
 263 
 264     @Test public void updateProgress_Double_NaN_NaN() {
 265         TaskShim.updateProgress(task, Double.NaN, Double.NaN);
 266         assertEquals(-1, task.getProgress(), 0);
 267         assertEquals(-1, task.getWorkDone(), 0);
 268         assertEquals(-1, task.getTotalWork(), 0);
 269     }
 270 }