1 package autotest.tko;
2 
3 import autotest.common.JsonRpcProxy;
4 import autotest.common.Utils;
5 import autotest.common.CustomHistory.HistoryToken;
6 import autotest.tko.TableView.TableSwitchListener;
7 import autotest.tko.TableView.TableViewConfig;
8 
9 import com.google.gwt.core.client.EntryPoint;
10 import com.google.gwt.core.client.GWT;
11 import com.google.gwt.core.client.JavaScriptObject;
12 import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
13 import com.google.gwt.dom.client.Element;
14 
15 public class EmbeddedTkoClient implements EntryPoint, TableSwitchListener {
16     private String autotestServerUrl;
17     private TestDetailView testDetailView; // we'll use this to generate history tokens
18 
onModuleLoad()19     public void onModuleLoad() {
20         JsonRpcProxy.setDefaultBaseUrl(JsonRpcProxy.TKO_BASE_URL);
21         testDetailView = new TestDetailView();
22         injectNativeMethods();
23     }
24 
25     /**
26      * Creates a global object named Autotest with the following methods.  This objects acts as the
27      * entry point for externally-written native code to create embeddable widgets.
28      * * initialize(autoservServerUrl) -- must call this before anything else, passing in the URL
29      *   to the Autotest server (i.e. "http://myhost").
30      * * createMetricsPlot(parent) -- returns a metrics plot object attached to the given parent
31      *   element.
32      */
injectNativeMethods()33     private native void injectNativeMethods() /*-{
34         var instance = this;
35         $wnd.Autotest = {
36             initialize: function(autotestServerUrl) {
37                 instance.@autotest.tko.EmbeddedTkoClient::initialize(Ljava/lang/String;)(autotestServerUrl);
38             },
39 
40             createMetricsPlot: function(parent) {
41                 return instance.@autotest.tko.EmbeddedTkoClient::createMetricsPlot(Lcom/google/gwt/dom/client/Element;)(parent);
42             }
43         }
44     }-*/;
45 
46     @SuppressWarnings("unused") // called from native
initialize(String autotestServerUrl)47     private void initialize(String autotestServerUrl) {
48         this.autotestServerUrl = autotestServerUrl;
49         JsonRpcProxy proxy = JsonRpcProxy.createProxy(autotestServerUrl + JsonRpcProxy.TKO_BASE_URL,
50                                                       true);
51         JsonRpcProxy.setProxy(JsonRpcProxy.TKO_BASE_URL, proxy);
52     }
53 
54     @SuppressWarnings("unused") // called from native
createMetricsPlot(Element parent)55     private JavaScriptObject createMetricsPlot(Element parent) {
56         UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();
57         if (handler == null) {
58             return doCreateMetricsPlot(parent);
59         }
60 
61         try {
62             return doCreateMetricsPlot(parent);
63         } catch (Throwable throwable) {
64             handler.onUncaughtException(throwable);
65             return null;
66         }
67     }
68 
doCreateMetricsPlot(Element parent)69     private JavaScriptObject doCreateMetricsPlot(Element parent) {
70         if (parent == null) {
71             throw new IllegalArgumentException("parent element cannot be null");
72         }
73         Plot plot = new MetricsPlot();
74         plot.setDrilldownTrigger();
75         plot.setListener(this);
76         parent.appendChild(plot.getElement());
77         return plot.getNativeProxy();
78     }
79 
getSelectTestHistoryToken(int testId)80     public HistoryToken getSelectTestHistoryToken(int testId) {
81         testDetailView.updateObjectId(Integer.toString(testId));
82         return testDetailView.getHistoryArguments();
83     }
84 
onSelectTest(int testId)85     public void onSelectTest(int testId) {
86         String fullUrl = autotestServerUrl + "/new_tko/#" + getSelectTestHistoryToken(testId);
87         Utils.openUrlInNewWindow(fullUrl);
88     }
89 
onSwitchToTable(TableViewConfig config)90     public void onSwitchToTable(TableViewConfig config) {
91         throw new UnsupportedOperationException();
92     }
93 }
94