Merge "Add test setup for checks plugin"
diff --git a/gr-checks/gr-checks-chip-view.js b/gr-checks/gr-checks-chip-view.js
index 3ed66e1..e96cdd3 100644
--- a/gr-checks/gr-checks-chip-view.js
+++ b/gr-checks/gr-checks-chip-view.js
@@ -23,6 +23,8 @@
     FAILED: 'failed',
   };
 
+  const CHECKS_POLL_INTERVAL_MS = 60 * 1000;
+
   const Defs = {};
   /**
    * @typedef {{
@@ -81,11 +83,12 @@
       _downgradeFailureToWarning: {
         type: Boolean,
         value: false
-      }
+      },
+      pollChecksInterval: Object
     },
 
     observers: [
-      '_fetchChecks(change, revision, getChecks)',
+      '_pollChecksRegularly(change, revision, getChecks)',
     ],
 
     listeners: {
@@ -115,13 +118,21 @@
       getChecks(change._number, revision._number).then(checks => {
         this.set('_hasChecks', checks.length > 0);
         if (checks.length > 0) {
-          this.set(
-              '_checkStatuses', computeCheckStatuses(checks));
+          this.set('_checkStatuses', computeCheckStatuses(checks));
           this.set('_downgradeFailureToWarning', downgradeFailureToWarning(checks));
         }
       });
     },
 
+    _pollChecksRegularly(change, revision, getChecks) {
+      if (this.pollChecksInterval) {
+        clearInterval(this.pollChecksInterval);
+      }
+      const poll = () => this._fetchChecks(change, revision, getChecks);
+      poll();
+      this.pollChecksInterval = setInterval(poll, CHECKS_POLL_INTERVAL_MS)
+    },
+
     /**
      * @param {!Object} checkStatuses The number of checks in each status.
      * @return {string}
diff --git a/gr-checks/gr-checks-view.html b/gr-checks/gr-checks-view.html
index 49e09c7..67129ee 100644
--- a/gr-checks/gr-checks-view.html
+++ b/gr-checks/gr-checks-view.html
@@ -1,3 +1,4 @@
+<link rel="import" href="gr-checks-status.html">
 <dom-module id="gr-checks-view">
   <template>
     <style>
diff --git a/gr-checks/gr-checks-view.js b/gr-checks/gr-checks-view.js
index 6d54eb2..e709b28 100644
--- a/gr-checks/gr-checks-view.js
+++ b/gr-checks/gr-checks-view.js
@@ -2,6 +2,17 @@
   'use strict';
 
   const Defs = {};
+
+  const Statuses = window.Gerrit.Checks.Statuses;
+  const StatusPriorityOrder = [
+    Statuses.FAILED,
+    Statuses.SCHEDULED,
+    Statuses.RUNNING,
+    Statuses.SUCCESSFUL,
+    Statuses.NOT_STARTED,
+    Statuses.NOT_RELEVANT,
+  ];
+
   /**
    * @typedef {{
    *   _number: number,
@@ -46,6 +57,24 @@
       '_fetchChecks(change, revision, getChecks)',
     ],
 
+
+    _orderChecks(a, b) {
+      if (a.state != b.state) {
+        let indexA = StatusPriorityOrder.indexOf(a.state);
+        let indexB = StatusPriorityOrder.indexOf(b.state);
+        if (indexA != -1 && indexB != -1) {
+          return indexA - indexB;
+        }
+        return indexA == -1 ? 1 : -1;
+      }
+      if (a.state === Statuses.FAILED) {
+        if (a.blocking && b.blocking && a.blocking.length !== b.blocking.length) {
+          return a.blocking.length == 0 ? 1 : -1;
+        }
+      }
+      return a.checker_name.localeCompare(b.checker_name);
+    },
+
     /**
      * @param {!Defs.Change} change
      * @param {!Defs.Revision} revision
@@ -54,6 +83,7 @@
     _fetchChecks(change, revision, getChecks) {
       getChecks(change._number, revision._number).then(checks => {
         if (checks && checks.length) {
+          checks.sort(this._orderChecks);
           this.set('_checks', checks);
           this.set('_status', LoadingStatus.RESULTS);
         } else {