1 /*
   2  * Copyright (c) 2011, 2016, 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.webkit.drt;
  27 
  28 import com.sun.webkit.LoadListenerClient;
  29 import com.sun.webkit.UIClient;
  30 import com.sun.webkit.WebPage;
  31 import com.sun.webkit.graphics.WCImage;
  32 import com.sun.webkit.graphics.WCRectangle;
  33 
  34 import java.util.ArrayList;
  35 import java.util.Iterator;
  36 import java.util.List;
  37 
  38 /**
  39  * {@link UIClient} implementation for DRT tests.
  40  */
  41 final class UIClientImpl implements UIClient {
  42 
  43     private WebPage webPage;
  44     private final List<UIClient> clients = new ArrayList<UIClient>();
  45 
  46     private WCRectangle bounds = new WCRectangle(0, 0, 800, 600);
  47 
  48     UIClientImpl() {
  49     }
  50 
  51     void setWebPage(WebPage webPage) {
  52         this.webPage = webPage;
  53     }
  54 
  55     /**
  56      * {@inheritDoc}
  57      */
  58     @Override
  59     public WebPage createPage(boolean menu, boolean status, boolean toolbar,
  60             boolean resizable)
  61     {
  62         UIClientImpl client = new UIClientImpl();
  63         final WebPage page = new WebPage(null, client, null, null, null, false);
  64         client.setWebPage(page);
  65 
  66         page.setBounds(0, 0, 800, 600);
  67 //        webPage.setUsePageCache(true);
  68 
  69         page.addLoadListenerClient(new LoadListenerClient() {
  70             @Override
  71             public void dispatchLoadEvent(long frame, int state, String url, String contentType, double progress, int errorCode) {
  72                 if (state == DOCUMENT_AVAILABLE) {
  73                     DumpRenderTree.drt.dumpUnloadListeners(page, frame);
  74                 }
  75             }
  76             @Override
  77             public void dispatchResourceLoadEvent(long frame, int state, String url, String contentType, double progress, int errorCode) {
  78             }
  79         });
  80 
  81         // This call is needed to add the main frame to WebPage.frames list.
  82         // TODO: investigate why it's not added automatically (via WebPage.fwkFrameCreated) and fix.
  83         page.getMainFrame();
  84 
  85         clients.add(client);
  86         return client.webPage;
  87     }
  88 
  89     /**
  90      * {@inheritDoc}
  91      */
  92     @Override
  93     public void closePage() {
  94         Iterator<UIClient> it = clients.iterator();
  95         while (it.hasNext()) {
  96             it.next().closePage();
  97             it.remove();
  98         }
  99         if (webPage.getMainFrame() != 0) {
 100             webPage.dispose();
 101         }
 102     }
 103 
 104     /**
 105      * {@inheritDoc}
 106      */
 107     @Override
 108     public void showView() {
 109         // look, I'm showing!
 110     }
 111 
 112     /**
 113      * {@inheritDoc}
 114      */
 115     @Override
 116     public WCRectangle getViewBounds() {
 117         return bounds;
 118     }
 119 
 120     /**
 121      * {@inheritDoc}
 122      */
 123     @Override
 124     public void setViewBounds(WCRectangle bounds) {
 125         this.bounds = bounds;
 126     }
 127 
 128     /**
 129      * {@inheritDoc}
 130      */
 131     @Override
 132     public void setStatusbarText(String text) {
 133     }
 134 
 135     /**
 136      * {@inheritDoc}
 137      */
 138     @Override
 139     public void alert(String text) {
 140         DumpRenderTree.out.printf("ALERT: %s\n", text);
 141     }
 142 
 143     /**
 144      * {@inheritDoc}
 145      */
 146     @Override
 147     public boolean confirm(String text) {
 148         DumpRenderTree.out.printf("CONFIRM: %s\n", text);
 149         return false;
 150     }
 151 
 152     /**
 153      * {@inheritDoc}
 154      */
 155     @Override
 156     public String prompt(String text, String defaultValue) {
 157         DumpRenderTree.out.printf("PROMPT: %s, default text: %s\n", text, defaultValue);
 158         return defaultValue;
 159     }
 160 
 161     /**
 162      * {@inheritDoc}
 163      */
 164     @Override
 165     public String[] chooseFile(String initialFileName, boolean multiple, String mimeFilters) {
 166         throw new UnsupportedOperationException("Not supported yet");
 167     }
 168 
 169     /**
 170      * {@inheritDoc}
 171      */
 172     @Override
 173     public void print() {
 174         throw new UnsupportedOperationException("Not supported yet");
 175     }
 176 
 177     /**
 178      * {@inheritDoc}
 179      */
 180     @Override
 181     public void startDrag(WCImage frame, int imageOffsetX, int imageOffsetY,
 182             int eventPosX, int eventPosY, String[] mimeTypes, Object[] values)
 183     {
 184         throw new UnsupportedOperationException("Not supported yet");
 185     }
 186 
 187     /**
 188      * {@inheritDoc}
 189      */
 190     @Override
 191     public void confirmStartDrag() {
 192         throw new UnsupportedOperationException("Not supported yet.");
 193     }
 194 
 195     /**
 196      * {@inheritDoc}
 197      */
 198     @Override
 199     public boolean isDragConfirmed() {
 200         return false;
 201     }
 202 }