Fix a race condition in new embedded spreadsheets. Also render a nicer page when
no results exist.
The problem occurred when the parent window finished loading and sends the
postMessage before the embedded frame finishes setting up its listener. The
message is then lost, and the frame remains blank. This way passes the job down
as part of the URL, so it's always available in the embedded page.

Signed-off-by: James Ren <[email protected]>

--- autotest/frontend/client/src/autotest/afe/JobDetailView.java        
2010-05-27 14:44:25.000000000 -0700
+++ autotest/frontend/client/src/autotest/afe/JobDetailView.java        
2010-06-01 11:55:52.000000000 -0700
@@ -50,8 +50,6 @@
     public static final int HOSTS_PER_PAGE = 30;
     public static final String RESULTS_MAX_WIDTH = "700px";
     public static final String RESULTS_MAX_HEIGHT = "500px";
-    public static final String EMBEDDED_SPREADSHEET_URL =
-            "/embedded_spreadsheet/EmbeddedSpreadsheetClient.html";
 
     public interface JobDetailListener {
         public void onHostSelected(String hostname);
@@ -68,7 +66,7 @@
     protected Button abortButton = new Button("Abort job");
     protected Button cloneButton = new Button("Clone job");
     protected Button recurringButton = new Button("Create recurring job");
-    protected Frame tkoResultsFrame = new Frame(EMBEDDED_SPREADSHEET_URL);
+    protected Frame tkoResultsFrame = new Frame();
 
     protected JobDetailListener listener;
     private SelectionManager selectionManager;
@@ -103,7 +101,7 @@
 
     @Override
     protected void fetchData() {
-        pointToResults(NO_URL, NO_URL, NO_URL, NO_URL);
+        pointToResults(NO_URL, NO_URL, NO_URL, NO_URL, NO_URL);
         JSONObject params = new JSONObject();
         params.put("id", new JSONNumber(jobId));
         rpcProxy.rpcCall("get_jobs_summary", params, new JsonRpcCallback() {
@@ -150,7 +148,8 @@
 
                 String jobTag = AfeUtils.getJobTag(jobObject);
                 pointToResults(getResultsURL(jobId), getLogsURL(jobTag),
-                               getOldResultsUrl(jobId), getTriageUrl(jobId));
+                               getOldResultsUrl(jobId), getTriageUrl(jobId),
+                               getEmbeddedUrl(jobId));
 
                 String jobTitle = "Job: " + name + " (" + jobTag + ")";
                 displayObjectData(jobTitle);
@@ -360,6 +359,10 @@
                "+AND+status+%253C%253E+%2527GOOD%2527&show_invalid=false";
     }
 
+    private String getEmbeddedUrl(int jobId) {
+        return 
"/embedded_spreadsheet/EmbeddedSpreadsheetClient.html?afe_job_id=" + jobId;
+    }
+
     /**
      * Get the path for a job's raw result files.
      * @param jobLogsId id-owner, e.g. "172-showard"
@@ -369,7 +372,8 @@
     }
 
     protected void pointToResults(String resultsUrl, String logsUrl,
-                                  String oldResultsUrl, String triageUrl) {
+                                  String oldResultsUrl, String triageUrl,
+                                  String embeddedUrl) {
         getElementById("results_link").setAttribute("href", resultsUrl);
         getElementById("old_results_link").setAttribute("href", oldResultsUrl);
         getElementById("raw_results_link").setAttribute("href", logsUrl);
@@ -377,13 +381,14 @@
 
         tkoResultsFrame.setSize(RESULTS_MAX_WIDTH, RESULTS_MAX_HEIGHT);
         if (!resultsUrl.equals(NO_URL)) {
-            updateResultsFrame(tkoResultsFrame.getElement(),
-                    String.valueOf(jobId), Utils.getBaseUrl());
+            updateResultsFrame(tkoResultsFrame.getElement(), embeddedUrl);
         }
     }
 
-    private native void updateResultsFrame(Element frame, String jobId, String 
baseUrl) /*-{
-        frame.contentWindow.postMessage(jobId, baseUrl);
+    private native void updateResultsFrame(Element frame, String embeddedUrl) 
/*-{
+        // Use location.replace() here so that the frame's URL changes don't 
show up in the browser
+        // window's history
+        frame.contentWindow.location.replace(embeddedUrl);
     }-*/;
 
     @Override
--- autotest/frontend/client/src/autotest/tko/EmbeddedSpreadsheetClient.java    
2010-05-27 14:44:25.000000000 -0700
+++ autotest/frontend/client/src/autotest/tko/EmbeddedSpreadsheetClient.java    
2010-06-01 11:55:52.000000000 -0700
@@ -1,12 +1,17 @@
 package autotest.tko;
 
+import autotest.common.JsonRpcCallback;
 import autotest.common.JsonRpcProxy;
 import autotest.common.spreadsheet.Spreadsheet;
 
 import com.google.gwt.core.client.EntryPoint;
+import com.google.gwt.dom.client.Element;
 import com.google.gwt.json.client.JSONNumber;
 import com.google.gwt.json.client.JSONObject;
+import com.google.gwt.json.client.JSONValue;
 import com.google.gwt.user.client.Command;
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.Label;
 import com.google.gwt.user.client.ui.RootPanel;
 
 import java.util.Collections;
@@ -25,7 +30,9 @@
 
     public static final String ROW_HEADER = "hostname";
     public static final String COLUMN_HEADER = "test_name";
+    private static final String NO_RESULTS = "There are no results for this 
query (yet?)";
 
+    private Label noResults = new Label(NO_RESULTS);
     private Spreadsheet spreadsheet = new Spreadsheet();
     private SpreadsheetDataProcessor spreadsheetProcessor =
             new SpreadsheetDataProcessor(spreadsheet);
@@ -33,8 +40,50 @@
     @Override
     public void onModuleLoad() {
         JsonRpcProxy.setDefaultBaseUrl(JsonRpcProxy.TKO_BASE_URL);
-        setupListener();
+        checkAndRender(Window.Location.getParameter("afe_job_id"));
+    }
+
+    private void checkAndRender(String afeJobIdStr) {
+        final JSONObject condition = getFilterCondition(afeJobIdStr);
+        if (condition == null) {
+            showNoResults();
+        }
+
+        JsonRpcProxy.getProxy().rpcCall("get_num_test_views", condition, new 
JsonRpcCallback() {
+            @Override
+            public void onSuccess(JSONValue result) {
+                if (result.isNumber().doubleValue() != 0) {
+                    renderSpreadsheet(condition);
+                } else {
+                    showNoResults();
+                }
+            }
+        });
+    }
 
+    private JSONObject getFilterCondition(String afeJobIdStr) {
+        if (afeJobIdStr == null) {
+            return null;
+        }
+
+        int afeJobId;
+        try {
+            afeJobId = Integer.parseInt(afeJobIdStr);
+        } catch (NumberFormatException e) {
+            return null;
+        }
+
+        JSONObject condition = new JSONObject();
+        condition.put("afe_job_id", new JSONNumber(afeJobId));
+        return condition;
+    }
+
+    private void showNoResults() {
+        RootPanel.get().add(noResults);
+        notifyParent(noResults.getElement());
+    }
+
+    private void renderSpreadsheet(JSONObject condition) {
         
spreadsheetProcessor.setDataSource(TestGroupDataSource.getStatusCountDataSource());
         spreadsheetProcessor.setHeaders(
                 Collections.singletonList(new SimpleHeaderField(ROW_HEADER)),
@@ -42,35 +91,15 @@
                 new JSONObject());
 
         RootPanel.get().add(spreadsheet);
-    }
-
-    private native void setupListener() /*-{
-        var instance = this;
-        $wnd.onGotJobId = function(event) {
-            var jobId = parseInt(event.data);
-            
[email protected]::createSpreadsheet(I)(jobId);
-        }
-
-        $wnd.addEventListener("message", $wnd.onGotJobId, false);
-    }-*/;
-
-    @SuppressWarnings("unused") // called from native
-    private void createSpreadsheet(int afeJobId) {
-        spreadsheet.clear();
-        final JSONObject condition = getFilterCondition(afeJobId);
         spreadsheetProcessor.refresh(condition, new Command() {
             public void execute() {
-                condition.put("extra_info", null);
-                notifyParent(spreadsheet.getElement().getClientWidth(),
-                        spreadsheet.getElement().getClientHeight());
+                notifyParent(spreadsheet.getElement());
             }
         });
     }
 
-    private JSONObject getFilterCondition(int afeJobId) {
-        JSONObject condition = new JSONObject();
-        condition.put("afe_job_id", new JSONNumber(afeJobId));
-        return condition;
+    private void notifyParent(Element elem) {
+        notifyParent(elem.getClientWidth(), elem.getClientHeight());
     }
 
     private native void notifyParent(int width, int height) /*-{
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to