GUI - Angular:: Example unit test for custom filter.

Change-Id: I82863bebe2f9e89097f2d2cedcc866cb884e304d
diff --git a/web/gui/src/main/webapp/_sdh/ng-examples/js/ch09-01-time-ago.js b/web/gui/src/main/webapp/_sdh/ng-examples/js/ch09-01-time-ago.js
new file mode 100644
index 0000000..1a8b7f1
--- /dev/null
+++ b/web/gui/src/main/webapp/_sdh/ng-examples/js/ch09-01-time-ago.js
@@ -0,0 +1,27 @@
+// ch09-01-time-ago.js
+
+angular.module('filterApp', [])
+    .filter('timeAgo', [function () {
+        var _m = 1000 * 60,
+            _h = _m * 60,
+            _d = _h * 24,
+            _mon = _d * 30;
+
+        return function (ts, ignoreSecs) {
+            var showSecs = !ignoreSecs,
+                now = new Date().getTime(),
+                diff = now - ts;
+
+            if (diff < _m && showSecs) {
+                return 'seconds ago';
+            } else if (diff < _h) {
+                return 'minutes ago';
+            } else if (diff < _d) {
+                return 'hours ago';
+            } else if (diff < _mon) {
+                return 'days ago';
+            } else {
+                return 'months ago';
+            }
+        }
+    }]);
diff --git a/web/gui/src/test/webapp/_sdh/ng-examples/js/ch09-01-time-agoSpec.js b/web/gui/src/test/webapp/_sdh/ng-examples/js/ch09-01-time-agoSpec.js
new file mode 100644
index 0000000..a518ae2
--- /dev/null
+++ b/web/gui/src/test/webapp/_sdh/ng-examples/js/ch09-01-time-agoSpec.js
@@ -0,0 +1,40 @@
+// ch09-01-time-agoSpec.js
+
+describe('timeAgo Filter', function () {
+    beforeEach(module('filterApp'));
+
+    var filter;
+    beforeEach(inject(function (timeAgoFilter) {
+        filter = timeAgoFilter;
+    }));
+
+    it('should respond based on timestamp', function() {
+        // The presence of new Date().getTime() makes it slightly
+        // hard to unit test deterministically.
+        // Ideally, we would inject a dateProvider into the timeAgo
+        // filter, but we are trying to keep it simple for now.
+        // So, we assume that our tests are fast enough to execute
+        // in mere milliseconds.
+
+        var t = new Date().getTime();
+        t -= 10000;
+        expect(filter(t)).toEqual('seconds ago');
+        expect(filter(t, true)).toEqual('minutes ago');
+
+        var fmin = t - 1000 * 60;
+        expect(filter(fmin)).toEqual('minutes ago');
+        expect(filter(fmin, true)).toEqual('minutes ago');
+
+        var fhour = t - 1000 * 60 * 68;
+        expect(filter(fhour)).toEqual('hours ago');
+        expect(filter(fhour, true)).toEqual('hours ago');
+
+        var fday = t - 1000 * 60 * 60 * 26;
+        expect(filter(fday)).toEqual('days ago');
+        expect(filter(fday, true)).toEqual('days ago');
+
+        var fmon = t - 1000 * 60 * 60 * 24 * 34;
+        expect(filter(fmon)).toEqual('months ago');
+        expect(filter(fmon, true)).toEqual('months ago');
+    });
+});