1 /*
   2  * Copyright (c) 2012, 2015, 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.scene.web;
  27 
  28 import com.sun.javafx.scene.web.Debugger;
  29 import java.util.ArrayList;
  30 import java.util.Arrays;
  31 import java.util.List;
  32 import javafx.util.Callback;
  33 import static org.junit.Assert.assertEquals;
  34 import static org.junit.Assert.fail;
  35 import org.junit.Test;
  36 import org.junit.After;
  37 
  38 public class DebuggerTest extends TestBase {
  39 
  40     @Test
  41     public void testSimpleMessageExchange() {
  42         submit(() -> {
  43             Debugger debugger = getEngine().impl_getDebugger();
  44 
  45             final List<String> callbackMessages = new ArrayList<String>();
  46             debugger.setMessageCallback(message -> {
  47                 callbackMessages.add(message);
  48                 return null;
  49             });
  50             debugger.setEnabled(true);
  51             debugger.sendMessage(q(
  52                     "{'method':'Debugger.pause','id':16}"));
  53             assertEquals(
  54                     Arrays.asList(q("{'result':{},'id':16}")),
  55                     callbackMessages);
  56         });
  57     }
  58 
  59     @Test
  60     public void testEnabledProperty() {
  61         submit(() -> {
  62             Debugger debugger = getEngine().impl_getDebugger();
  63 
  64             assertEquals(false, debugger.isEnabled());
  65 
  66             debugger.setEnabled(true);
  67             assertEquals(true, debugger.isEnabled());
  68 
  69             debugger.setEnabled(false);
  70             assertEquals(false, debugger.isEnabled());
  71 
  72             debugger.setEnabled(true);
  73             debugger.setEnabled(true);
  74             assertEquals(true, debugger.isEnabled());
  75 
  76             debugger.setEnabled(false);
  77             debugger.setEnabled(false);
  78             assertEquals(false, debugger.isEnabled());
  79         });
  80     }
  81 
  82     @Test
  83     public void testMessageCallbackProperty() {
  84         submit(() -> {
  85             Debugger debugger = getEngine().impl_getDebugger();
  86             Callback<String,Void> callback = new Callback<String,Void>() {
  87                 public Void call(String message) {
  88                     return null;
  89                 }
  90             };
  91 
  92             assertEquals(null, debugger.getMessageCallback());
  93 
  94             debugger.setMessageCallback(callback);
  95             assertEquals(callback, debugger.getMessageCallback());
  96 
  97             debugger.setMessageCallback(null);
  98             assertEquals(null, debugger.getMessageCallback());
  99         });
 100     }
 101 
 102     @Test
 103     public void testSendMessageIllegalStateException() {
 104         submit(() -> {
 105             Debugger debugger = getEngine().impl_getDebugger();
 106             try {
 107                 debugger.sendMessage("foo");
 108                 fail("IllegalStateException expected but not thrown");
 109             } catch (IllegalStateException expected) {}
 110         });
 111     }
 112 
 113     @Test
 114     public void testSendMessageNullPointerException() {
 115         submit(() -> {
 116             Debugger debugger = getEngine().impl_getDebugger();
 117             debugger.setEnabled(true);
 118             try {
 119                 debugger.sendMessage(null);
 120                 fail("NullPointerException expected but not thrown");
 121             } catch (NullPointerException expected) {}
 122         });
 123     }
 124 
 125     @Test
 126     public void testThreadCheck() {
 127         Debugger debugger = getEngine().impl_getDebugger();
 128 
 129         try {
 130             debugger.isEnabled();
 131             fail("IllegalStateException expected but not thrown");
 132         } catch (IllegalStateException expected) {}
 133 
 134         try {
 135             debugger.setEnabled(true);
 136             fail("IllegalStateException expected but not thrown");
 137         } catch (IllegalStateException expected) {}
 138 
 139         try {
 140             debugger.sendMessage("foo");
 141             fail("IllegalStateException expected but not thrown");
 142         } catch (IllegalStateException expected) {}
 143 
 144         try {
 145             debugger.getMessageCallback();
 146             fail("IllegalStateException expected but not thrown");
 147         } catch (IllegalStateException expected) {}
 148 
 149         try {
 150             debugger.setMessageCallback(null);
 151             fail("IllegalStateException expected but not thrown");
 152         } catch (IllegalStateException expected) {}
 153     }
 154 
 155     private static String q(String s) {
 156         return s.replace('\'', '\"');
 157     }
 158 
 159     @After
 160     public void disableDebug() {
 161         submit(() -> {
 162             getEngine().impl_getDebugger().setEnabled(false);
 163         });
 164     }
 165 }