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 test.javafx.concurrent; 27 28 import javafx.beans.value.ChangeListener; 29 import javafx.beans.value.ObservableValue; 30 import java.util.ArrayList; 31 import java.util.List; 32 import javafx.concurrent.Task; 33 import javafx.concurrent.Worker; 34 35 import test.javafx.concurrent.mocks.SimpleTask; 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import static org.junit.Assert.*; 40 41 /** 42 * A Task which simply completes normally. It never throws an Exception. 43 * It also never increments the progress or touches messages or anything 44 * else. 45 */ 46 public class TaskSimpleTest { 47 private Task<String> task; 48 49 @Before public void setup() { 50 task = new SimpleTask(); 51 52 // Checks that the running property is always correct. 53 task.runningProperty().addListener((o, oldValue, newValue) -> { 54 Worker.State s = task.getState(); 55 if (newValue) { 56 assertTrue(s == Worker.State.SCHEDULED || s == Worker.State.RUNNING); 57 } else { 58 assertTrue(s != Worker.State.SCHEDULED && s != Worker.State.RUNNING); 59 } 60 }); 61 } 62 63 /************************************************************************ 64 * Test the initial values for the Task, 65 ***********************************************************************/ 66 67 @Test public void stateShouldBe_READY_ByDefault() { 68 assertEquals(Task.State.READY, task.getState()); 69 } 70 71 @Test public void workDoneShouldBe_Indeterminate_ByDefault() { 72 assertEquals(-1, task.getWorkDone(), 0); 73 } 74 75 @Test public void totalWorkShouldBe_Indeterminate_ByDefault() { 76 assertEquals(-1, task.getTotalWork(), 0); 77 } 78 79 @Test public void progressShouldBe_Indeterminate_ByDefault() { 80 assertEquals(-1, task.getWorkDone(), 0); 81 } 82 83 @Test public void valueShouldBe_Null_ByDefault() { 84 assertNull(task.getValue()); 85 } 86 87 @Test public void exceptionShouldBe_Null_ByDefault() { 88 assertNull(task.getException()); 89 } 90 91 @Test public void runningShouldBe_False_ByDefault() { 92 assertEquals(false, task.isRunning()); 93 } 94 95 @Test public void messageShouldBe_EmptyString_ByDefault() { 96 assertEquals("", task.getMessage()); 97 } 98 99 @Test public void titleShouldBe_EmptyString_ByDefault() { 100 assertEquals("", task.getTitle()); 101 } 102 103 @Test public void isCancelledShouldBe_False_ByDefault() { 104 assertEquals(false, task.isCancelled()); 105 } 106 107 @Test public void isDoneShouldBe_False_ByDefault() { 108 assertEquals(false, task.isDone()); 109 } 110 111 /************************************************************************ 112 * Run the task and make sure that the states SCHEDULED, RUNNING, and 113 * SUCCEEDED were all encountered in order. Check the condition at the 114 * end of execution. Progress should equal maxProgress (-1 in this case), 115 * percentDone should still be -1. value should be "Sentinel" and 116 * exception should be null, running should be false. 117 ***********************************************************************/ 118 119 @Test public void afterRunningStatesShouldHaveBeen_SCHEDULED_RUNNING_SUCCEEDED() { 120 final List<Worker.State> states = new ArrayList<Worker.State>(); 121 task.stateProperty().addListener((observable, oldValue, newValue) -> { 122 states.add(newValue); 123 }); 124 125 task.run(); 126 127 assertArrayEquals(states.toArray(), new Worker.State[]{ 128 Worker.State.SCHEDULED, 129 Worker.State.RUNNING, 130 Worker.State.SUCCEEDED 131 }); 132 } 133 134 @Test public void afterRunningWorkDoneShouldBe_Indeterminate() { 135 task.run(); 136 assertEquals(-1, task.getWorkDone(), 0); 137 } 138 139 @Test public void afterRunningTotalWorkShouldBe_Indeterminate() { 140 task.run(); 141 assertEquals(-1, task.getTotalWork(), 0); 142 } 143 144 @Test public void afterRunningProgressShouldBe_Indeterminate() { 145 task.run(); 146 assertEquals(-1, task.getWorkDone(), 0); 147 } 148 149 @Test public void afterRunningValueShouldBe_Finished() { 150 task.run(); 151 assertEquals("Sentinel", task.getValue()); 152 } 153 154 @Test public void afterRunningExceptionShouldBe_Null() { 155 task.run(); 156 assertNull(task.getException()); 157 } 158 159 @Test public void afterRunningMessageShouldBe_EmptyString() { 160 task.run(); 161 assertEquals("", task.getMessage()); 162 } 163 164 @Test public void afterRunningTitleShouldBe_EmptyString() { 165 task.run(); 166 assertEquals("", task.getTitle()); 167 } 168 169 @Test public void afterRunning_isCancelled_ShouldBe_False() { 170 task.run(); 171 assertEquals(false, task.isCancelled()); 172 } 173 174 @Test public void afterRunning_isDone_ShouldBe_True() { 175 task.run(); 176 assertEquals(true, task.isDone()); 177 } 178 179 /************************************************************************ 180 * TODO Need to resolve what should happen when get() is called 181 * The following few tests are setup so that we can test that 182 * invoking "get" instead of "run" still results in the state 183 * being set correctly for the Task. In theory, the exception, 184 * value, and other fields should be updated when get is called (?) 185 ***********************************************************************/ 186 187 }