df4c27342962c3f93a19eb762cb2c22584b186c6Dav GlassYUI.add('cache-offline-tests', function(Y) {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass // Set up the page
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var ASSERT = Y.Assert,
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ARRAYASSERT = Y.ArrayAssert,
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass tearDown = function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.flush();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass };
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var testClass = new Y.Test.Case({
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass name: "Class Tests",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass tearDown : tearDown,
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testDefaults: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isInstanceOf(Y.Cache, this.cache, "Expected instance of Y.Cache.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(null, this.cache.get("max"), "Expected default max of null.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ARRAYASSERT.isEmpty(this.cache.get("entries"), "Expected empty array.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testDestructor: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.destroy();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ARRAYASSERT.isEmpty(this.cache.get("entries"), "Expected empty array.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var testBasic = new Y.Test.Case({
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass name: "Basic Tests",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass tearDown : tearDown,
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testMaxDefault: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(null, this.cache.get("max"), "Expected max to be null.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entries.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isNotNull(this.cache.retrieve(1), "Expected null cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testMaxConfig: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline({max:2});
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isNull(null, this.cache.get("max"), "Expected max to be null.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(2, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testMaxSet: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.set("max", 1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isNull(null, this.cache.get("max"), "Expected max to be null.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(3, "c");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testMaxSetNull: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.set("max", null);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isNull(null, this.cache.get("max"), "Expected max to be null.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(4, "d");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testMaxSetNegative: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline({max:-2});
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isNull(null, this.cache.get("max"), "Expected max to be null.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(4, "d");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testRetrieve: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline(),
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add("b", "c");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var cachedentry = this.cache.retrieve(1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("a", cachedentry.response, "Expected first cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isInstanceOf(Date, cachedentry.cached, "Expected first cached Date.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isInstanceOf(Date, cachedentry.expires, "Expected first expires Date.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass cachedentry = this.cache.retrieve("b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("c", cachedentry.response, "Expected second cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isInstanceOf(Date, cachedentry.cached, "Expected second cached Date.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isInstanceOf(Date, cachedentry.expires, "Expected second expires Date.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testNoExpires: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline({expires:0}),
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add("b", "c");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var cachedentry = this.cache.retrieve(1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("a", cachedentry.response, "Expected cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isInstanceOf(Date, cachedentry.cached, "Expected cached Date.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isNull(cachedentry.expires, "Expected null expires.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testExpiresNumber: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline({expires:3000}),
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var cachedentry = this.cache.retrieve(1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("a", cachedentry.response, "Expected cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isInstanceOf(Date, cachedentry.expires, "Expected cached Date.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.set("expires", 1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.wait(function(){
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass cachedentry = this.cache.retrieve(1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isNull(cachedentry, "Expected expired data.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }, 50);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testExpiresDate: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline({expires:new Date(new Date().getTime() + 86400000)}),
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var cachedentry = this.cache.retrieve(1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("a", cachedentry.response, "Expected cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isInstanceOf(Date, cachedentry.expires, "Expected cached Date.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.flush();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.set("expires", new Date(new Date().getTime() - 86400000));
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass cachedentry = this.cache.retrieve(1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.isNull(cachedentry, "Expected expired data.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testNoMatch: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add("a", "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var cachedentry = this.cache.retrieve("c");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(null, cachedentry, "Expected no match.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testFlush: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(2, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.flush();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(0, this.cache.get("size"), "Expected empty this.cache.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testFlushAll: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(2, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var cache = new Y.CacheOffline({sandbox:"another"});
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass cache.add(2, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass if(window.localStorage) {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(4, localStorage.length, "Expected 4 items.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.CacheOffline.flushAll();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass if(window.localStorage) {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(0, localStorage.length, "Expected empty localStorage.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var testEvents = new Y.Test.Case({
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass name: "Event Tests",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass tearDown : tearDown,
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testAdd: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var mock = new Y.Mock();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.expect(mock, {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass method: "handleAdd",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass args: [Y.Mock.Value(function(e){
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, e.entry.request);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("a", e.entry.response);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass })]
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.on("add", mock.handleAdd);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.verify(mock);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testFlush: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var mock = new Y.Mock();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.expect(mock, {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass method: "handleFlush",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass args: [Y.Mock.Value.Object]
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.on("flush", mock.handleFlush);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.flush();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.verify(mock);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testRequest: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var mock = new Y.Mock();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.expect(mock, {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass method: "handleRequest",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass args: [Y.Mock.Value(function(e){
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(2, e.request);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass })]
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.on("request", mock.handleRequest);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.retrieve(2);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.verify(mock);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testRetrieveSuccess: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var mock = new Y.Mock();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.expect(mock, {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass method: "handleRetrieve",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass args: [Y.Mock.Value(function(e){
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, e.entry.request);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("a", e.entry.response);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass })]
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.on("retrieve", mock.handleRetrieve);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.retrieve(1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.verify(mock);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testRetrieveFailure: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var mock = new Y.Mock();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.expect(mock, {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass method: "handleRetrieve",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass args: [Y.Mock.Value.Any],
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass callCount: 0
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.on("retrieve", mock.handleRetrieve);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.retrieve(2);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Mock.verify(mock);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testCancelAdd: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.on("add", function(e) {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass e.preventDefault();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }, this, true);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass // Test the cancel
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(0, this.cache.get("size"), "Expected 0 entries.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testCancelFlush: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.on("flush", function(e) {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass e.preventDefault();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }, this, true);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.flush();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass // Test the cancel
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var testEntryManagement = new Y.Test.Case({
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass name: "Entry Management Tests",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass tearDown : tearDown,
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testNonUniqueKeys: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline({uniqueKeys:false}); // not supported
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(2, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "c");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(2, this.cache.get("size"), "Expected 2 entries.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testUniqueKeys: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline({uniqueKeys:true});
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(2, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "c");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(2, this.cache.get("size"), "Expected 2 entries.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }/*,
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass NOT SUPPORTED IN CACHEOFFLINE
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testFreshness: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(1, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(2, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(3, "c");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.retrieve(1);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(3, this.cache.get("size"), "Expected 3 entries.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(1, this.cache.get("entries")[2].request, "Expected entry to be refreshed.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }*/
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var testBoundaryValues = new Y.Test.Case({
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass name: "Invalid Value Tests",
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass tearDown : tearDown,
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testUndefinedRequest: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(undefined, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(undefined, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("b", this.cache.retrieve().response, "Expected cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testNullRequest: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(null, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(null, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("b", this.cache.retrieve(null).response, "Expected cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testNaNRequest: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(NaN, "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add(NaN, "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame(0, this.cache.get("size"), "Expected 0 entries.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass },
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass testEmptyStringRequest: function() {
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache = new Y.CacheOffline();
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add("", "a");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass this.cache.add("", "b");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass ASSERT.areSame("b", this.cache.retrieve("").response, "Expected cached response.");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass }
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass });
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass var suite = new Y.Test.Suite({name:"CacheOffline Test Suite"});
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass suite.add(testClass);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass suite.add(testBasic);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass suite.add(testEvents);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass suite.add(testEntryManagement);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass suite.add(testBoundaryValues);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Test.Runner.setName("CacheOffline Test Runner");
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass Y.Test.Runner.add(suite);
df4c27342962c3f93a19eb762cb2c22584b186c6Dav Glass});