1 /*
   2  * Copyright (c) 2012, 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.CompositeEventHandler;
  29 import com.sun.javafx.event.CompositeEventHandlerShim;
  30 import test.com.sun.javafx.event.EventCountingHandler;
  31 import test.com.sun.javafx.event.EmptyEvent;
  32 import javafx.event.Event;
  33 import javafx.event.WeakEventHandler;
  34 import javafx.event.WeakEventHandlerUtil;
  35 import org.junit.Assert;
  36 
  37 import org.junit.Before;
  38 import org.junit.Test;
  39 
  40 public class CompositeEventHandlerTest {
  41     private CompositeEventHandler<Event> compositeEventHandler;
  42 
  43     @Before
  44     public void setUp() {
  45         compositeEventHandler = new CompositeEventHandler<Event>();
  46     }
  47 
  48     @Test
  49     public void weakEventHandlerTest() {
  50         final EventCountingHandler<Event> eventCountingHandler =
  51                 new EventCountingHandler<Event>();
  52         final WeakEventHandler<Event> weakEventHandler =
  53                 new WeakEventHandler<Event>(eventCountingHandler);
  54 
  55         compositeEventHandler.addEventHandler(weakEventHandler);
  56         
  57         Assert.assertTrue(
  58             CompositeEventHandlerShim.containsHandler(compositeEventHandler, weakEventHandler));
  59         compositeEventHandler.dispatchCapturingEvent(new EmptyEvent());
  60         Assert.assertEquals(0, eventCountingHandler.getEventCount());
  61         compositeEventHandler.dispatchBubblingEvent(new EmptyEvent());
  62         Assert.assertEquals(1, eventCountingHandler.getEventCount());
  63 
  64         WeakEventHandlerUtil.clear(weakEventHandler);
  65 
  66         Assert.assertFalse(
  67                 CompositeEventHandlerShim.containsHandler(compositeEventHandler, weakEventHandler));
  68         compositeEventHandler.dispatchCapturingEvent(new EmptyEvent());
  69         Assert.assertEquals(1, eventCountingHandler.getEventCount());
  70         compositeEventHandler.dispatchBubblingEvent(new EmptyEvent());
  71         Assert.assertEquals(1, eventCountingHandler.getEventCount());
  72     }
  73 
  74     @Test
  75     public void weakEventFilterTest() {
  76         final EventCountingHandler<Event> eventCountingFilter =
  77                 new EventCountingHandler<Event>();
  78         final WeakEventHandler<Event> weakEventFilter =
  79                 new WeakEventHandler<Event>(eventCountingFilter);
  80 
  81         compositeEventHandler.addEventFilter(weakEventFilter);
  82 
  83         Assert.assertTrue(
  84                 CompositeEventHandlerShim.containsFilter(compositeEventHandler, weakEventFilter));
  85         compositeEventHandler.dispatchCapturingEvent(new EmptyEvent());
  86         Assert.assertEquals(1, eventCountingFilter.getEventCount());
  87         compositeEventHandler.dispatchBubblingEvent(new EmptyEvent());
  88         Assert.assertEquals(1, eventCountingFilter.getEventCount());
  89 
  90         WeakEventHandlerUtil.clear(weakEventFilter);
  91 
  92         Assert.assertFalse(
  93                 CompositeEventHandlerShim.containsFilter(compositeEventHandler, weakEventFilter));
  94         compositeEventHandler.dispatchCapturingEvent(new EmptyEvent());
  95         Assert.assertEquals(1, eventCountingFilter.getEventCount());
  96         compositeEventHandler.dispatchBubblingEvent(new EmptyEvent());
  97         Assert.assertEquals(1, eventCountingFilter.getEventCount());
  98     }
  99 }