1 /*
   2  * Copyright (c) 2011, 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.com.sun.javafx.event;
  27 
  28 import com.sun.javafx.event.EventDispatchTree;
  29 import com.sun.javafx.event.EventDispatchTreeImpl;
  30 import javafx.event.EventDispatcher;
  31 import org.junit.Assert;
  32 import org.junit.Test;
  33 
  34 public final class EventDispatchTreeTest {
  35     @Test
  36     public void resetTest() {
  37         EventDispatchTree eventDispatchTree = new EventDispatchTreeImpl();
  38 
  39         final EventDispatcher dispatcherA = new LabeledEventDispatcher("A");
  40         final EventDispatcher dispatcherB = new LabeledEventDispatcher("B");
  41         final EventDispatcher dispatcherC = new LabeledEventDispatcher("C");
  42 
  43         eventDispatchTree = eventDispatchTree.append(dispatcherA);
  44         ((EventDispatchTreeImpl) eventDispatchTree).reset();
  45 
  46         eventDispatchTree = eventDispatchTree.append(dispatcherB)
  47                                              .append(dispatcherC);
  48 
  49         Assert.assertEquals("(B->(C))", eventDispatchTree.toString());
  50     }
  51 
  52     @Test
  53     public void mergeTreeTest() {
  54         EventDispatchTree eventDispatchTree =
  55                 new EventDispatchTreeImpl();
  56 
  57         final EventDispatcher[] dispatchers =
  58                 new EventDispatcher[12];
  59         for (int i = 0; i < dispatchers.length; ++i) {
  60             dispatchers[i] = new LabeledEventDispatcher(Integer.toString(i));
  61         }
  62         
  63         eventDispatchTree =
  64                 eventDispatchTree.mergeTree(
  65                     eventDispatchTree.createTree()
  66                                      .append(dispatchers[0])
  67                                      .append(dispatchers[1])
  68                                      .append(dispatchers[2])
  69                                      .append(dispatchers[3]));
  70         
  71         eventDispatchTree =
  72                 eventDispatchTree.mergeTree(
  73                     eventDispatchTree.createTree()
  74                                      .append(dispatchers[4])
  75                                      .append(dispatchers[6])
  76                                      .prepend(dispatchers[2])
  77                                      .prepend(dispatchers[1])
  78                                      .prepend(dispatchers[0]));
  79 
  80         eventDispatchTree =
  81                 eventDispatchTree.mergeTree(
  82                     eventDispatchTree.createTree()
  83                                      .prepend(dispatchers[7])
  84                                      .prepend(dispatchers[5])
  85                                      .prepend(dispatchers[4])
  86                                      .prepend(dispatchers[2])
  87                                      .prepend(dispatchers[1])
  88                                      .prepend(dispatchers[0]));
  89 
  90         eventDispatchTree =
  91                 eventDispatchTree.mergeTree(
  92                     eventDispatchTree.createTree()
  93                                      .prepend(dispatchers[2])
  94                                      .prepend(dispatchers[1])
  95                                      .prepend(dispatchers[0])
  96                                      .append(dispatchers[4])
  97                                      .append(dispatchers[5])
  98                                      .append(dispatchers[8]));
  99 
 100 
 101         eventDispatchTree = eventDispatchTree.prepend(dispatchers[9]);
 102         eventDispatchTree = eventDispatchTree.append(dispatchers[10]);
 103 
 104 
 105         eventDispatchTree =
 106                 eventDispatchTree.mergeTree(
 107                     eventDispatchTree.createTree()
 108                                      .append(dispatchers[0])
 109                                      .append(dispatchers[1])
 110                                      .append(dispatchers[2])
 111                                      .append(dispatchers[4])
 112                                      .append(dispatchers[6])
 113                                      .append(dispatchers[10])
 114                                      .append(dispatchers[11]));
 115         eventDispatchTree =
 116                 eventDispatchTree.mergeTree(
 117                     eventDispatchTree.createTree()
 118                                      .append(dispatchers[9])
 119                                      .append(dispatchers[0])
 120                                      .append(dispatchers[1])
 121                                      .append(dispatchers[2])
 122                                      .append(dispatchers[4])
 123                                      .append(dispatchers[6])
 124                                      .append(dispatchers[10])
 125                                      .append(dispatchers[11]));
 126 
 127         //             /|
 128         //            9 0
 129         //           /  |
 130         //          0   1
 131         //         /    |
 132         //        1     2
 133         //       /      |
 134         //      2       4
 135         //     / \      |
 136         //    3   4     6
 137         //   /   / \    |
 138         // 10   6   5   10
 139         //     /   / \  |
 140         //   10   7   8 11
 141         //    |   |   |
 142         //   11  10   10
 143 
 144         Assert.assertEquals(
 145                 "(9->(0->(1->(2->(3->(10),4->(6->(10->(11)),5->"
 146                     + "(7->(10),8->(10))))))),"
 147                     + "0->(1->(2->(4->(6->(10->(11)))))))",
 148                 eventDispatchTree.toString());
 149     }
 150 
 151     @Test
 152     public void dispatchEventTest() {
 153         final EventCountingDispatcher[] dispatchers =
 154                 new EventCountingDispatcher[8];
 155         for (int i = 0; i < dispatchers.length; ++i) {
 156             dispatchers[i] = new EventCountingDispatcher(Integer.toString(i));
 157         }
 158 
 159         EventDispatchTree eventDispatchTree =
 160                 new EventDispatchTreeImpl();
 161 
 162         eventDispatchTree = eventDispatchTree.append(dispatchers[1]);
 163 
 164         eventDispatchTree =
 165                 eventDispatchTree.mergeTree(
 166                     eventDispatchTree.createTree().append(dispatchers[2])
 167                                                   .append(dispatchers[4]));
 168 
 169         eventDispatchTree =
 170                 eventDispatchTree.mergeTree(
 171                     eventDispatchTree.createTree().append(dispatchers[2])
 172                                                   .append(dispatchers[5])
 173                                                   .append(dispatchers[7]));
 174 
 175         eventDispatchTree =
 176                 eventDispatchTree.mergeTree(
 177                     eventDispatchTree.createTree().append(dispatchers[3])
 178                                                   .append(dispatchers[6]));
 179 
 180         eventDispatchTree = eventDispatchTree.prepend(dispatchers[0]);
 181 
 182         //   0
 183         //  /|\
 184         // 1 2 3
 185         //  / \ \
 186         // 4   5 6
 187         //     |
 188         //     7
 189 
 190         eventDispatchTree.dispatchEvent(new EmptyEvent());
 191         verifyCapturingEventCounters(dispatchers, 1, 1, 1, 1, 1, 1, 1, 1);
 192         verifyBubblingEventCounters(dispatchers, 1, 1, 1, 1, 1, 1, 1, 1);
 193 
 194         dispatchers[0].setConsumeCapturingEvent(true);
 195         eventDispatchTree.dispatchEvent(new EmptyEvent());
 196         verifyCapturingEventCounters(dispatchers, 2, 1, 1, 1, 1, 1, 1, 1);
 197         verifyBubblingEventCounters(dispatchers, 1, 1, 1, 1, 1, 1, 1, 1);
 198 
 199         dispatchers[0].setConsumeCapturingEvent(false);
 200         dispatchers[2].setConsumeCapturingEvent(true);
 201         eventDispatchTree.dispatchEvent(new EmptyEvent());
 202         verifyCapturingEventCounters(dispatchers, 3, 2, 2, 2, 1, 1, 2, 1);
 203         verifyBubblingEventCounters(dispatchers, 2, 2, 1, 2, 1, 1, 2, 1);
 204 
 205         dispatchers[2].setConsumeCapturingEvent(false);
 206         dispatchers[7].setConsumeBubblingEvent(true);
 207         eventDispatchTree.dispatchEvent(new EmptyEvent());
 208         verifyCapturingEventCounters(dispatchers, 4, 3, 3, 3, 2, 2, 3, 2);
 209         verifyBubblingEventCounters(dispatchers, 3, 3, 2, 3, 2, 1, 3, 2);
 210 
 211         dispatchers[4].setConsumeBubblingEvent(true);
 212         dispatchers[7].setConsumeBubblingEvent(true);
 213         eventDispatchTree.dispatchEvent(new EmptyEvent());
 214         verifyCapturingEventCounters(dispatchers, 5, 4, 4, 4, 3, 3, 4, 3);
 215         verifyBubblingEventCounters(dispatchers, 4, 4, 2, 4, 3, 1, 4, 3);
 216     }
 217 
 218     private static void verifyCapturingEventCounters(
 219             final EventCountingDispatcher[] dispatchers,
 220             final int... counters) {
 221         for (int i = 0; i < dispatchers.length; ++i) {
 222             Assert.assertEquals(
 223                     counters[i],
 224                     dispatchers[i].getCapturingEventCount());
 225         }
 226     }
 227 
 228     private static void verifyBubblingEventCounters(
 229             final EventCountingDispatcher[] dispatchers,
 230             final int... counters) {
 231         for (int i = 0; i < dispatchers.length; ++i) {
 232             Assert.assertEquals(
 233                     counters[i],
 234                     dispatchers[i].getBubblingEventCount());
 235         }
 236     }
 237 }