(hoofbeats) Takes Tizen MediaContent API into use.
authorSalvatore Iovene <salvatore.iovene@intel.com>
Tue, 22 Jan 2013 09:24:19 +0000 (11:24 +0200)
committerSalvatore Iovene <salvatore.iovene@intel.com>
Tue, 22 Jan 2013 09:24:19 +0000 (11:24 +0200)
examples/hoofbeats/javascripts/library.js
examples/hoofbeats/tests/unit/library.js

index 594dfd9..82a6986 100644 (file)
@@ -2,34 +2,74 @@ $(function() {
     var library = function() {
         // For readability:
         this.initialized = false;
-        this.root = undefined;
+        this.fetchCount = 100;
+        this.fetchOffset = 0;
+        this.mediaItems = [];
 
         this.initialize = function() {
             if (this.initialized)
                 return;
 
-            window.requestFileSystem =
-                window.requestFileSystem ||
-                window.webkitRequestFileSystem;
-
-            if (window.requestFileSystem === undefined) {
-                throw Error("Your browser doesn't support file system " +
-                            "operations");
+            if (window.tizen === undefined) {
+                throw Error("You need the Tizen web API  to run Hoofbeats.");
             }
+
+            this.audioTypeFilter = new tizen.AttributeFilter(
+                "type", "EXACTLY", "AUDIO");
+            this.videoTypeFilter = new tizen.AttributeFilter(
+                "type", "EXACTLY", "VIDEO");
+            this.typeFilter = new tizen.CompositeFilter(
+                "UNION", [this.audioTypeFilter, this.videoTypeFilter]);
+
+            this.sortMode = new tizen.SortMode("trackNumber", "ASC");
+            this.mediaSource = tizen.mediacontent.getLocalMediaSource();
         };
 
         this.scan = function() {
             this.initialize()
-            if (this.root === undefined) {
-                throw Error("Root directory is not defined");
+
+            this.mediaItems = [];
+            this.fetchOffset = 0;
+
+            this.mediaSource.findItems(
+                this.findItemsCB.bind(this),
+                this.errorCB.bind(this),
+                null,
+                this.typeFilter,
+                this.sortMode,
+                this.fetchCount,
+                this.fetchOffset);
+        };
+
+        this.errorCB = function(error) {
+            console.log("Error: " + error.name);
+            throw new Error(error.name);
+        };
+
+        this.findItemsCB = function(items) {
+            var self = this;
+
+            items.forEach(function(item, index, items) {
+                self.mediaItems.push(item);
+                console.log("Item added to the library: " + item.title);
+            });
+
+            if (items.length == this.fetchCount) {
+                // There *might* be more items.
+                this.fetchOffset += this.fetchCount;
+                this.mediaSource.findItems(
+                    this.findItemsCB.bind(this),
+                    this.errorCB.bind(this),
+                    null,
+                    this.typeFilter,
+                    this.sortMode,
+                    this.fetchCount,
+                    this.fetchOffset);
             }
         };
     };
 
     library.prototype = {
-        set root(value) { this._root = value; },
-        get root() { return this._root; },
-
         set initialized(value) {this._initialized = value; },
         get initialized() { return this._initialized; }
     };
index f3fa884..3e931db 100644 (file)
@@ -1,37 +1,25 @@
 $(function() {
     module("hoofbeats-library", {
         setup: function() {
-            window._requestFileSystem = window.requestFileSystem;
-            window._webkitRequestFileSystem = window.webkitRequestFileSystem;
+            window._tizen = window.tizen;
         },
         teardown: function() {
-            window.requestFileSystem = window._requestFileSystem;
-            window.webkitRequestFileSystem = window.webkitRequestFileSystem;
+            window.tizen = window._tizen;
         }
     });
 
-    test("set root", function() {
-        s = "/path/to/media/";
-        lib = new HoofbeatsLibrary();
-        lib.root = s;
-        equal(lib.root, s, "root is correctly set");
-    });
-
-    test("scan when root is not defined", function() {
+    test("scan when browser doesn't support Tizen web api",
+    function() {
         lib = new HoofbeatsLibrary();
+        window.tizen = undefined;
         raises(function() {
             lib.scan();
         }, Error, "scan throws Error");
     });
 
-    test("scan when browser doesn't support file system operations",
-    function() {
+    test("successful scan", function() {
         lib = new HoofbeatsLibrary();
-        lib.root = "/";
-        window.requestFileSystem = undefined;
-        window.webkitRequestFileSystem = undefined;
-        raises(function() {
-            lib.scan();
-        }, Error, "scan throws Error");
-    })
+        lib.scan();
+        ok(lib.mediaItems.length > 0, "there are items in the library");
+    });
 });