changeset 99:544d339d2b4c

Added the beginnings of a black-box app that can be used to generate/run functional tests.
author Atul Varma <avarma@mozilla.com>
date Tue, 27 Apr 2010 09:59:15 -0700
parents 97fc889dbed4
children c486d35fad27
files black-box.html index.html js/black-box.js js/modules/app.js js/modules/date-utils.js js/modules/mocks.js js/tests/test-bugzilla.js
diffstat 7 files changed, 124 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/black-box.html	Tue Apr 27 09:59:15 2010 -0700
@@ -0,0 +1,18 @@
+<html>
+<head>
+  <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
+ <title>Bugzilla Dashboard - Black Box</title>
+</head>
+<body style="background: black; color: white;">
+<h1 class="title">Bugzilla Dashboard - Black Box</h1>
+<div style="text-align: center;">
+<iframe src="about:blank" id="dashboard"
+        style="width: 640px; height: 480px; border: none;"></iframe>
+</div>
+</body>
+<!-- Base Scripts -->
+<script src="js/jquery.js"></script>
+<script src="js/require.js"></script>
+<script src="js/modules/mocks.js"></script>
+<script src="js/black-box.js"></script>
+</html>
--- a/index.html	Mon Apr 26 22:16:32 2010 -0700
+++ b/index.html	Tue Apr 27 09:59:15 2010 -0700
@@ -165,13 +165,20 @@
     $("#loading-screen").hide();
     $(".hide-while-loading").show();
 
-    var moduleExports = {};
-    var require = Require.build(Require.modules, moduleExports);
-    require("app/loader").init(moduleExports, {
+    var options = {
       jQuery: jQuery,
       Bugzilla: Bugzilla,
       window: window
-   });
+    };
+
+    if (document.location.search == "?testing=1") {
+      window.parent.onDashboardLoaded(window, options);
+      return;
+    }
+
+    var moduleExports = {};
+    var require = Require.build(Require.modules, moduleExports);
+    require("app/loader").init(moduleExports, options);
   });
 });
 </script>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/js/black-box.js	Tue Apr 27 09:59:15 2010 -0700
@@ -0,0 +1,25 @@
+function onDashboardLoaded(dashboard, options) {
+  var require = Require.build();
+
+  // Needed for Firebug, which won't log iframe errors to the console.
+  $(dashboard).error(
+    function(event) {
+      console.warn("An error occurred in the dashboard iframe.");
+    });
+
+  var moduleExports = {};
+  var dbrequire = dashboard.Require.build(dashboard.Require.modules,
+                                          moduleExports);
+  options.cache = require("mocks/cache").create();
+  options.Bugzilla = require("mocks/bugzilla").create(options.Bugzilla);
+  dbrequire("date-utils").now = function() {
+    return new Date("Tue Apr 27 2010 09:00:00 GMT");
+  };
+  dbrequire("app/loader").init(moduleExports, options);
+}
+
+$(window).ready(
+  function() {
+    var iframe = $("#dashboard").get(0);
+    iframe.src = "index.html?testing=1";
+  });
--- a/js/modules/app.js	Mon Apr 26 22:16:32 2010 -0700
+++ b/js/modules/app.js	Tue Apr 27 09:59:15 2010 -0700
@@ -1,9 +1,14 @@
 Require.modules["app/loader"] = function(exports, require) {
   exports.init = function init(moduleExports, options) {
-    var cache = require("cache/html5").create(
-      "bugzilla-dashboard-cache",
-      options.window.sessionStorage
-    );
+    var cache;
+    if ("cache" in options)
+      cache = options.cache;
+    else 
+      cache = require("cache/html5").create(
+        "bugzilla-dashboard-cache",
+        options.window.sessionStorage
+      );
+
     var bugzilla = require("app/bugzilla-auth").create(options.Bugzilla);
 
     moduleExports.bugzilla = bugzilla;
@@ -615,18 +620,12 @@
       });
   }
 
-  function timeAgo(ms) {
-    var now = new Date();
-    var then = new Date(now - ms);
-    return dateUtils.dateToISO8601(then);
-  }
-
   const MS_PER_HOUR = 1000 * 60 * 60;
   const MS_PER_DAY =  MS_PER_HOUR * 24;
   const MS_PER_WEEK = MS_PER_DAY * 7;
 
   var defaults = {
-    changed_after: timeAgo(MS_PER_WEEK * 14)
+    changed_after: dateUtils.timeAgo(MS_PER_WEEK * 14)
   };
 
   function update(myUsername, isAuthenticated, forceUpdate) {
@@ -665,7 +664,7 @@
 
     report("#fixed-bugs", key, forceUpdate,
            {resolution: ["FIXED"],
-            changed_after: timeAgo(MS_PER_WEEK),
+            changed_after: dateUtils.timeAgo(MS_PER_WEEK),
             email1: myUsername,
             email1_type: "equals",
             email1_assigned_to: 1,
--- a/js/modules/date-utils.js	Mon Apr 26 22:16:32 2010 -0700
+++ b/js/modules/date-utils.js	Tue Apr 27 09:59:15 2010 -0700
@@ -1,4 +1,9 @@
 Require.modules["date-utils"] = function(exports) {
+  // Dynamically replace this when QA testing, for determinism.
+  exports.now = function now() {
+    return new Date();
+  };
+
   // Taken from MDC @ Core_JavaScript_1.5_Reference/Objects/Date.
   exports.dateToISO8601 = function dateToISO8601(d) {
     function pad(n) { return n < 10 ? '0' + n : n; }
@@ -48,7 +53,7 @@
   // long ago the date represents.
   exports.prettyDate = function prettyDate(time, now){
     if (!now)
-      now = (new Date()).getTime();
+      now = exports.now().getTime();
 
     var date = exports.dateFromISO8601(time),
     diff = ((now - date.getTime()) / 1000),
@@ -68,4 +73,9 @@
       day_diff < 7 && day_diff + " days ago" ||
       day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
   };
+
+  exports.timeAgo = function timeAgo(ms) {
+    var then = new Date(exports.now() - ms);
+    return exports.dateToISO8601(then);
+  };
 };
--- a/js/modules/mocks.js	Mon Apr 26 22:16:32 2010 -0700
+++ b/js/modules/mocks.js	Tue Apr 27 09:59:15 2010 -0700
@@ -1,4 +1,49 @@
-Require.modules["mocks"] = function(exports, require) {
+Require.modules["mocks/cache"] = function(exports, require) {
+  function copy(obj) {
+    return JSON.parse(JSON.stringify(obj));
+  }
+
+  function MockCache() {
+    this.cache = {};
+  };
+
+  MockCache.prototype = {
+    get: function get(key) {
+      console.log("cache get", key);
+      if (key in this.cache)
+        return copy(this.cache[key]);
+      return null;
+    },
+    set: function set(key, value) {
+      console.log("cache set", key);
+      this.cache[key] = copy(value);
+    },
+    clear: function clear(key, value) {
+      this.cache = {};
+    }
+  };
+
+  exports.create = function create() {
+    return new MockCache();
+  };
+};
+
+Require.modules["mocks/bugzilla"] = function(exports, require) {
+  exports.create = function create(Bugzilla) {
+    function MockBugzilla() {
+      this.ajax = function ajax(options) {
+        console.log(options);
+        throw new Error("MockBugzilla.ajax() not implemented");
+      };
+    };
+
+    MockBugzilla.prototype = Bugzilla;
+
+    return new MockBugzilla();
+  };
+};
+
+Require.modules["mocks/xhr"] = function(exports, require) {
   function MockXMLHttpRequest(delegate) {
     var self = this;
 
@@ -50,7 +95,7 @@
     };
   }
 
-  exports.xhr = function xhr(delegate) {
+  exports.create = function create(delegate) {
     return new MockXMLHttpRequest(delegate);
   };
 };
--- a/js/tests/test-bugzilla.js	Mon Apr 26 22:16:32 2010 -0700
+++ b/js/tests/test-bugzilla.js	Tue Apr 27 09:59:15 2010 -0700
@@ -12,7 +12,7 @@
 
   expect(expected.length);
 
-  var xhr = require("mocks").xhr(
+  var xhr = require("mocks/xhr").create(
     function xhrDelegate(methodName, args) {
       var jsonableArgs = [];
       args.forEach(
@@ -20,7 +20,6 @@
           if (typeof(arg) != "function")
             jsonableArgs.push(arg);
         });
-      ;
       same([methodName, jsonableArgs], expected.splice(0, 1)[0]);
     });