cacheoffline.html revision 303f7fa80c85cc2236cb9138d002e0da34431d5e
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CacheOffline Tests</title>
<script type="text/javascript" src="/build/yui/yui.js"></script>
<script type="text/javascript" src="/build/cache/cache-debug.js"></script>
</head>
<body class="yui3-skin-sam">
<h1>Cache Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
<script type="text/javascript">
(function() {
YUI({
base: "/build/",
//filter: "debug",
logInclude:{"TestRunner":true},
useConsole: true
}).use("console", "test", "dump", "cache-offline", function(Y) {
// Set up the page
var ASSERT = Y.Assert,
ARRAYASSERT = Y.ArrayAssert,
BTNRUN = Y.get("#btnRun");
BTNRUN.set("disabled", false);
Y.on("click", function(e){
Y.Test.Runner.run();
}, BTNRUN);
var myConsole = new Y.Console().render(),
tearDown = function() {
this.cache.flush();
};
var testClass = new Y.Test.Case({
name: "Class Tests",
tearDown : tearDown,
testDefaults: function() {
this.cache = new Y.CacheOffline();
ASSERT.isInstanceOf(Y.Cache, this.cache, "Expected instance of Y.Cache.");
ASSERT.areSame(null, this.cache.get("max"), "Expected default max of null.");
ARRAYASSERT.isEmpty(this.cache.get("entries"), "Expected empty array.");
},
testDestructor: function() {
this.cache = new Y.CacheOffline();
this.cache.destroy();
ASSERT.isNull(this.cache.get("entries"), "Expected null array.");
}
});
var testBasic = new Y.Test.Case({
name: "Basic Tests",
tearDown : tearDown,
testMaxDefault: function() {
this.cache = new Y.CacheOffline();
ASSERT.areSame(null, this.cache.get("max"), "Expected max to be null.");
this.cache.add(1, "a");
ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entries.");
ASSERT.isNotNull(this.cache.retrieve(1), "Expected null cached response.");
},
testMaxConfig: function() {
this.cache = new Y.CacheOffline({max:2});
ASSERT.isNull(null, this.cache.get("max"), "Expected max to be null.");
this.cache.add(2, "b");
ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
},
testMaxSet: function() {
this.cache = new Y.CacheOffline();
this.cache.set("max", 1);
ASSERT.isNull(null, this.cache.get("max"), "Expected max to be null.");
this.cache.add(3, "c");
ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
},
testMaxSetNull: function() {
this.cache = new Y.CacheOffline();
this.cache.set("max", null);
ASSERT.isNull(null, this.cache.get("max"), "Expected max to be null.");
this.cache.add(4, "d");
ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
},
testMaxSetNegative: function() {
this.cache = new Y.CacheOffline({max:-2});
ASSERT.isNull(null, this.cache.get("max"), "Expected max to be null.");
this.cache.add(4, "d");
ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
},
testRetrieve: function() {
this.cache = new Y.CacheOffline();
this.cache.add(1, "a");
this.cache.add("b", "c");
var cachedresponse = this.cache.retrieve(1).response;
ASSERT.areSame("a", cachedresponse, "Expected first cached response.");
cachedresponse = this.cache.retrieve("b").response;
ASSERT.areSame("c", cachedresponse, "Expected second cached response.");
},
testNoMatch: function() {
this.cache = new Y.CacheOffline();
this.cache.add("a", "b");
var cachedentry = this.cache.retrieve("c");
ASSERT.areSame(null, cachedentry, "Expected no match.");
},
testFlush: function() {
this.cache = new Y.CacheOffline();
this.cache.add(1, "a");
this.cache.add(2, "b");
this.cache.flush();
ASSERT.areSame(0, this.cache.get("size"), "Expected empty this.cache.");
}
});
var testEvents = new Y.Test.Case({
name: "Event Tests",
tearDown : tearDown,
testAdd: function() {
this.cache = new Y.CacheOffline();
this.cache.on("add", function(e) {
this.resume(function() {
ASSERT.areSame(1, e.entry.request);
ASSERT.areSame("a", e.entry.response);
});
}, this, true);
this.cache.add(1, "a");
this.wait();
},
testFlush: function() {
this.cache = new Y.CacheOffline();
this.cache.on("flush", function(e) {
this.cache.detach("flush");
this.resume(function() {
// This function intentionally left blank
});
}, this, true);
this.cache.add(1, "a");
this.cache.flush()
this.wait();
},
testRequest: function() {
this.cache = new Y.CacheOffline();
this.cache.on("request", function(e) {
this.resume(function() {
ASSERT.areSame(2, e.request);
});
}, this, true);
this.cache.add(1, "a");
this.cache.retrieve(2)
this.wait();
},
testRetrieveSuccess: function() {
this.cache = new Y.CacheOffline();
this.cache.on("retrieve", function(e) {
this.resume(function() {
ASSERT.areSame(1, e.entry.request);
ASSERT.areSame("a", e.entry.response);
});
}, this, true);
this.cache.add(1, "a");
this.cache.retrieve(1)
this.wait();
},
testRetrieveFailure: function() {
this.cache = new Y.CacheOffline();
this.cache.on("retrieve", function(e) {
this.resume(function() {
ASSERT.fail();
});
}, this, true);
this.cache.add(1, "a");
this.cache.retrieve(2)
this.wait(function() {
// This function intentionally left blank
}, 0);
},
testCancelAdd: function() {
this.cache = new Y.CacheOffline();
this.cache.on("add", function(e) {
e.preventDefault();
}, this, true);
this.cache.add(1, "a");
// Test the cancel
ASSERT.areSame(0, this.cache.get("size"), "Expected 0 entries.");
},
testCancelFlush: function() {
this.cache = new Y.CacheOffline();
this.cache.on("flush", function(e) {
e.preventDefault();
}, this, true);
this.cache.add(1, "a");
this.cache.flush();
// Test the cancel
ASSERT.areSame(1, this.cache.get("size"), "Expected 1 entry.");
}
});
var testEntryManagement = new Y.Test.Case({
name: "Entry Management Tests",
tearDown : tearDown,
testNonUniqueKeys: function() {
this.cache = new Y.CacheOffline({uniqueKeys:false}); // not supported
this.cache.add(1, "a");
this.cache.add(2, "b");
this.cache.add(1, "c");
ASSERT.areSame(2, this.cache.get("size"), "Expected 2 entries.");
},
testUniqueKeys: function() {
this.cache = new Y.CacheOffline({uniqueKeys:true});
this.cache.add(1, "a");
this.cache.add(2, "b");
this.cache.add(1, "c");
ASSERT.areSame(2, this.cache.get("size"), "Expected 2 entries.");
}/*,
NOT SUPPORTED IN CACHEOFFLINE
testFreshness: function() {
this.cache = new Y.CacheOffline();
this.cache.add(1, "a");
this.cache.add(2, "b");
this.cache.add(3, "c");
this.cache.retrieve(1);
ASSERT.areSame(3, this.cache.get("size"), "Expected 3 entries.");
ASSERT.areSame(1, this.cache.get("entries")[2].request, "Expected entry to be refreshed.");
}*/
});
var testBoundaryValues = new Y.Test.Case({
name: "Invalid Value Tests",
tearDown : tearDown,
testUndefinedRequest: function() {
this.cache = new Y.CacheOffline();
this.cache.add(undefined, "a");
this.cache.add(undefined, "b");
ASSERT.areSame("b", this.cache.retrieve().response, "Expected cached response.");
},
testNullRequest: function() {
this.cache = new Y.CacheOffline();
this.cache.add(null, "a");
this.cache.add(null, "b");
ASSERT.areSame("b", this.cache.retrieve(null).response, "Expected cached response.");
},
testNaNRequest: function() {
this.cache = new Y.CacheOffline();
this.cache.add(NaN, "a");
this.cache.add(NaN, "b");
ASSERT.areSame(0, this.cache.get("size"), "Expected 0 entries.");
},
testEmptyStringRequest: function() {
this.cache = new Y.CacheOffline();
this.cache.add("", "a");
this.cache.add("", "b");
ASSERT.areSame("b", this.cache.retrieve("").response, "Expected cached response.");
}
});
var suite = new Y.Test.Suite({name:"CacheOffline Test Suite"});
suite.add(testClass);
suite.add(testBasic);
suite.add(testEvents);
suite.add(testEntryManagement);
suite.add(testBoundaryValues);
Y.Test.Runner.setName("CacheOffline Test Runner");
Y.Test.Runner.add(suite);
if(window.localStorage) {
window.localStorage.clear();
}
Y.Test.Runner.run();
});
})();
</script>
</body>
</html>