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 com.sun.javafx.event;
  27 
  28 import javafx.event.EventDispatchChain;
  29 import org.junit.Assert;
  30 import org.junit.Test;
  31 
  32 import com.sun.javafx.event.StubBasicEventDispatcher.ConsumeEvent;
  33 
  34 public final class CompositeEventDispatcherTest {
  35     @Test
  36     public void eventDispatchTest() {
  37         final TestCompositeEventDispatcher compositeDispatcher =
  38                 new TestCompositeEventDispatcher();
  39 
  40         final EventCountingDispatcher terminalDispatcher =
  41                 new EventCountingDispatcher();
  42         final EventDispatchChain eventDispatchChain =
  43                 StubEventDispatchChain.EMPTY_CHAIN
  44                                       .append(compositeDispatcher)
  45                                       .append(terminalDispatcher);
  46 
  47         Assert.assertNotNull(eventDispatchChain.dispatchEvent(
  48                                                     new EmptyEvent()));
  49         verifyEventCounters(compositeDispatcher, 1, 1, 1, 1, 1, 1);
  50         Assert.assertEquals(1, terminalDispatcher.getCapturingEventCount());
  51 
  52         compositeDispatcher.getFirstChildDispatcher().setConsumeNextEvent(
  53                 ConsumeEvent.CAPTURING);
  54         Assert.assertNull(eventDispatchChain.dispatchEvent(new EmptyEvent()));
  55         verifyEventCounters(compositeDispatcher, 2, 1, 1, 1, 1, 1);
  56         Assert.assertEquals(1, terminalDispatcher.getCapturingEventCount());
  57         
  58         compositeDispatcher.getSecondChildDispatcher().setConsumeNextEvent(
  59                 ConsumeEvent.CAPTURING);
  60         Assert.assertNull(eventDispatchChain.dispatchEvent(new EmptyEvent()));
  61         verifyEventCounters(compositeDispatcher, 3, 2, 1, 1, 1, 1);
  62         Assert.assertEquals(1, terminalDispatcher.getCapturingEventCount());
  63 
  64         compositeDispatcher.getThirdChildDispatcher().setConsumeNextEvent(
  65                 ConsumeEvent.CAPTURING);
  66         Assert.assertNull(eventDispatchChain.dispatchEvent(new EmptyEvent()));
  67         verifyEventCounters(compositeDispatcher, 4, 3, 2, 1, 1, 1);
  68         Assert.assertEquals(1, terminalDispatcher.getCapturingEventCount());
  69 
  70         compositeDispatcher.getThirdChildDispatcher().setConsumeNextEvent(
  71                 ConsumeEvent.BUBBLING);
  72         Assert.assertNull(eventDispatchChain.dispatchEvent(new EmptyEvent()));
  73         verifyEventCounters(compositeDispatcher, 5, 4, 3, 1, 1, 2);
  74         Assert.assertEquals(2, terminalDispatcher.getCapturingEventCount());
  75 
  76         compositeDispatcher.getSecondChildDispatcher().setConsumeNextEvent(
  77                 ConsumeEvent.BUBBLING);
  78         Assert.assertNull(eventDispatchChain.dispatchEvent(new EmptyEvent()));
  79         verifyEventCounters(compositeDispatcher, 6, 5, 4, 1, 2, 3);
  80         Assert.assertEquals(3, terminalDispatcher.getCapturingEventCount());
  81 
  82         compositeDispatcher.getFirstChildDispatcher().setConsumeNextEvent(
  83                 ConsumeEvent.BUBBLING);
  84         Assert.assertNull(eventDispatchChain.dispatchEvent(new EmptyEvent()));
  85         verifyEventCounters(compositeDispatcher, 7, 6, 5, 2, 3, 4);
  86         Assert.assertEquals(4, terminalDispatcher.getCapturingEventCount());
  87     }
  88 
  89     private void verifyEventCounters(
  90             final TestCompositeEventDispatcher compositeDispatcher,
  91             final int expectedChild1CapturingEventCount,
  92             final int expectedChild2CapturingEventCount,
  93             final int expectedChild3CapturingEventCount,
  94             final int expectedChild1BubblingEventCount,
  95             final int expectedChild2BubblingEventCount,
  96             final int expectedChild3BubblingEventCount) {
  97         Assert.assertEquals(expectedChild1CapturingEventCount,
  98                             compositeDispatcher.getFirstChildDispatcher()
  99                                                .getCapturingEventCount());
 100         Assert.assertEquals(expectedChild2CapturingEventCount,
 101                             compositeDispatcher.getSecondChildDispatcher()
 102                                                .getCapturingEventCount());
 103         Assert.assertEquals(expectedChild3CapturingEventCount,
 104                             compositeDispatcher.getThirdChildDispatcher()
 105                                                .getCapturingEventCount());
 106         Assert.assertEquals(expectedChild1BubblingEventCount,
 107                             compositeDispatcher.getFirstChildDispatcher()
 108                                                .getBubblingEventCount());
 109         Assert.assertEquals(expectedChild2BubblingEventCount,
 110                             compositeDispatcher.getSecondChildDispatcher()
 111                                                .getBubblingEventCount());
 112         Assert.assertEquals(expectedChild3BubblingEventCount,
 113                             compositeDispatcher.getThirdChildDispatcher()
 114                                                .getBubblingEventCount());
 115     }
 116 }