added a method to sort the json annotations. changed the tests accordingly. popcorn-port
authorhamidouk
Thu, 20 Oct 2011 15:50:04 +0200
branchpopcorn-port
changeset 103 2dfd89e91c3a
parent 102 67e8179afde5
child 104 d571e9020092
added a method to sort the json annotations. changed the tests accordingly.
src/js/data.js
src/js/utils.js
unittests/tests/JSONSerializer.js
--- a/src/js/data.js	Thu Oct 20 10:23:54 2011 +0200
+++ b/src/js/data.js	Thu Oct 20 15:50:04 2011 +0200
@@ -58,7 +58,13 @@
   /* a wrapper to get a closure because we lose this in callbacks */
   var wrapper = function(obj) {
     return function(data) {    
-      obj._data = data;            
+      obj._data = data;
+      // sort the data too
+      obj._data["annotations"].sort(function(a, b) 
+          { var a_begin = +a.begin;
+            var b_begin = +b.begin;
+            return a_begin - b_begin;
+          });
       callback(data);
     }
   };
--- a/src/js/utils.js	Thu Oct 20 10:23:54 2011 +0200
+++ b/src/js/utils.js	Thu Oct 20 15:50:04 2011 +0200
@@ -24,7 +24,7 @@
 }
 
 /* convert a time to a percentage in the media */
-IriSP.timeToPourcent = function(time,timetotal){
+IriSP.timeToPourcent = function(time, timetotal){
 	return (parseInt(Math.round(time/timetotal*100)));
 };
 
--- a/unittests/tests/JSONSerializer.js	Thu Oct 20 10:23:54 2011 +0200
+++ b/unittests/tests/JSONSerializer.js	Thu Oct 20 15:50:04 2011 +0200
@@ -20,7 +20,17 @@
         this.requests.push(request);
       };
       
-      var response_array = [{ media: 12, content: "Hey there" }];
+      var response_array = { media: 12, content: "Hey there", 
+                             annotations: [{"begin": "32", "end" : 64}, {"begin": "08", "end" : 27},{"begin": "02", "end" : 61}]  };
+
+      /* sorted array is our comparision array */
+      var sorted_array = IriSP.jQuery.extend({}, response_array);
+      sorted_array.annotations.sort(function(a, b) 
+          { var a_begin = +a.begin;
+            var b_begin = +b.begin;
+            return a_begin - b_begin;
+          });
+          
       var response_string = JSON.stringify(response_array);
   
       var spy_callback = this.spy();
@@ -35,6 +45,18 @@
       ok(spy_callback.calledOnce, "callback called");
       ok(spy_callback.calledWith(response_array), "callback called with correct value");
       deepEqual(ser._data, response_array, "the internal variable is initialized to the correct value");
+      
+      var order_preserved = true;
+      
+      var i = 0;
+      for(i = 0; i < ser._data.length - 1; i++) {
+        if (ser._data.annotations[i].begin > ser._data.annotations[i+1].begin) {
+            order_preserved = false;
+            break;
+        }
+      }
+      
+      ok(order_preserved, "the annotation sub-array is sorted by begin time");
     });
 
     test("currentMedia should return the current media", function() {
@@ -43,5 +65,5 @@
       ser._data = {}
       ser._data.medias = [0];
       equal(ser.currentMedia(), 0, "currentMedia() returns the correct value");
-    });
+    });    
 };
\ No newline at end of file