getEditsStoreSizeBytes() works also when the localStorage is not initialized (our keys don't exist)

This commit is contained in:
Javier Abadia 2014-01-28 10:54:19 +01:00
parent 1f2c10e0ea
commit 0f35c55f7b
2 changed files with 25 additions and 5 deletions

View File

@ -180,9 +180,11 @@ define(["esri/graphic"], function(Graphic)
getEditsStoreSizeBytes: function() getEditsStoreSizeBytes: function()
{ {
return EDITS_QUEUE_KEY.length + window.localStorage.getItem(EDITS_QUEUE_KEY).length + var editsQueueValue = window.localStorage.getItem(EDITS_QUEUE_KEY);
REDO_STACK_KEY.length + window.localStorage.getItem(REDO_STACK_KEY).length; var redoStackValue = window.localStorage.getItem(REDO_STACK_KEY);
return (editsQueueValue? EDITS_QUEUE_KEY.length + editsQueueValue.length : 0) +
(redoStackValue? REDO_STACK_KEY.length + redoStackValue.length : 0);
}, },
getLocalStorageSizeBytes: function() getLocalStorageSizeBytes: function()

View File

@ -1,6 +1,9 @@
"use strict" "use strict";
var KEY_PREFIX = "__LOCAL_STORAGE_TEST__"; var KEY_PREFIX = "__LOCAL_STORAGE_TEST__";
var EDITS_QUEUE_KEY = "esriEditsQueue";
var REDO_STACK_KEY = "esriRedoStack";
var EXECUTE_LONG_TESTS = true; var EXECUTE_LONG_TESTS = true;
describe("Internal Methods", function() describe("Internal Methods", function()
@ -445,7 +448,7 @@ describe("Public Interface", function()
it("report edit store size", function() it("report edit store size", function()
{ {
usedBytes = g_editsStore.getEditsStoreSizeBytes(); usedBytes = g_editsStore.getEditsStoreSizeBytes();
expect(usedBytes).toBe(705); expect(usedBytes).toBe(692);
}); });
it("report total local storage size", function() it("report total local storage size", function()
@ -454,6 +457,14 @@ describe("Public Interface", function()
expect(usedBytes).not.toBeGreaterThan(totalBytes); expect(usedBytes).not.toBeGreaterThan(totalBytes);
}); });
it("report edit store size when uninitalized", function()
{
window.localStorage.removeItem( EDITS_QUEUE_KEY );
window.localStorage.removeItem( REDO_STACK_KEY );
var usedBytes = g_editsStore.getEditsStoreSizeBytes();
expect(usedBytes).toBe(0);
});
it("exhaust localStorage capacity", function() it("exhaust localStorage capacity", function()
{ {
if( EXECUTE_LONG_TESTS ) if( EXECUTE_LONG_TESTS )
@ -461,6 +472,13 @@ describe("Public Interface", function()
console.log("this will take some time"); console.log("this will take some time");
var sizeBefore = g_editsStore.getLocalStorageSizeBytes(); var sizeBefore = g_editsStore.getLocalStorageSizeBytes();
if( sizeBefore == 0)
{
// if not initialized, create the empty elements
window.localStorage.setItem( EDITS_QUEUE_KEY, "");
window.localStorage.setItem( REDO_STACK_KEY, "");
sizeBefore = g_editsStore.getLocalStorageSizeBytes();
}
// first, fill localStorage up to max capacity // first, fill localStorage up to max capacity
try try