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.concurrent;
27
28 import java.util.concurrent.atomic.AtomicBoolean;
29 import javafx.concurrent.mocks.EpicFailTask;
30 import javafx.concurrent.mocks.InfiniteTask;
31 import javafx.concurrent.mocks.MythicalEvent;
32 import javafx.concurrent.mocks.SimpleTask;
33 import javafx.event.EventHandler;
34 import org.junit.Test;
35
36 import static org.junit.Assert.*;
37
38 /**
39 * Tests that the event notification machinery on Task works as expected.
40 * Event filters should be called before the onFoo methods, and the onFoo
41 * methods before the event handlers. The protected methods should be called
42 * in any case whether or not the event ends up consumed, and should always
43 * be called last.
44 */
45 public class TaskEventTest {
46
47 /***************************************************************************
48 *
49 * Tests for onScheduled
50 *
51 **************************************************************************/
52
53 @Test public void onScheduledPropertyNameShouldMatchMethodName() {
463 assertTrue(failedCalled.get());
464 }
465
466 /***************************************************************************
467 *
468 * A mythical subclass should be able to set an event handler and
469 * have events fired on the Service work.
470 *
471 **************************************************************************/
472
473 @Test public void eventFiredOnSubclassWorks() {
474 final AtomicBoolean result = new AtomicBoolean(false);
475 MythicalTask task = new MythicalTask();
476 task.setHandler(mythicalEvent -> result.set(true));
477 task.fireEvent(new MythicalEvent());
478 assertTrue(result.get());
479 }
480
481 private static final class MythicalTask extends SimpleTask {
482 public void setHandler(EventHandler<MythicalEvent> h) {
483 super.setEventHandler(MythicalEvent.ANY, h);
484 }
485 }
486 }
|
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 java.util.concurrent.atomic.AtomicBoolean;
29 import javafx.concurrent.Task;
30 import javafx.concurrent.TaskShim;
31 import javafx.concurrent.WorkerStateEvent;
32 import test.javafx.concurrent.mocks.EpicFailTask;
33 import test.javafx.concurrent.mocks.InfiniteTask;
34 import test.javafx.concurrent.mocks.MythicalEvent;
35 import test.javafx.concurrent.mocks.SimpleTask;
36 import javafx.event.EventHandler;
37 import javafx.event.EventType;
38 import org.junit.Test;
39
40 import static org.junit.Assert.*;
41
42 /**
43 * Tests that the event notification machinery on Task works as expected.
44 * Event filters should be called before the onFoo methods, and the onFoo
45 * methods before the event handlers. The protected methods should be called
46 * in any case whether or not the event ends up consumed, and should always
47 * be called last.
48 */
49 public class TaskEventTest {
50
51 /***************************************************************************
52 *
53 * Tests for onScheduled
54 *
55 **************************************************************************/
56
57 @Test public void onScheduledPropertyNameShouldMatchMethodName() {
467 assertTrue(failedCalled.get());
468 }
469
470 /***************************************************************************
471 *
472 * A mythical subclass should be able to set an event handler and
473 * have events fired on the Service work.
474 *
475 **************************************************************************/
476
477 @Test public void eventFiredOnSubclassWorks() {
478 final AtomicBoolean result = new AtomicBoolean(false);
479 MythicalTask task = new MythicalTask();
480 task.setHandler(mythicalEvent -> result.set(true));
481 task.fireEvent(new MythicalEvent());
482 assertTrue(result.get());
483 }
484
485 private static final class MythicalTask extends SimpleTask {
486 public void setHandler(EventHandler<MythicalEvent> h) {
487 TaskShim.setEventHandler(this, MythicalEvent.ANY, h);
488 }
489 }
490 }
|