From 0f35c55f7b7b103a818a16a639df32a8e270a8e6 Mon Sep 17 00:00:00 2001 From: Javier Abadia Date: Tue, 28 Jan 2014 10:54:19 +0100 Subject: [PATCH] getEditsStoreSizeBytes() works also when the localStorage is not initialized (our keys don't exist) --- lib/edit/editsStore.js | 6 ++++-- test/spec/editsStoreSpec.js | 24 +++++++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/edit/editsStore.js b/lib/edit/editsStore.js index 15ab9ff..da70681 100644 --- a/lib/edit/editsStore.js +++ b/lib/edit/editsStore.js @@ -180,9 +180,11 @@ define(["esri/graphic"], function(Graphic) getEditsStoreSizeBytes: function() { - return EDITS_QUEUE_KEY.length + window.localStorage.getItem(EDITS_QUEUE_KEY).length + - REDO_STACK_KEY.length + window.localStorage.getItem(REDO_STACK_KEY).length; + var editsQueueValue = window.localStorage.getItem(EDITS_QUEUE_KEY); + 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() diff --git a/test/spec/editsStoreSpec.js b/test/spec/editsStoreSpec.js index 2f99c8f..f74f40f 100644 --- a/test/spec/editsStoreSpec.js +++ b/test/spec/editsStoreSpec.js @@ -1,6 +1,9 @@ -"use strict" +"use strict"; var KEY_PREFIX = "__LOCAL_STORAGE_TEST__"; +var EDITS_QUEUE_KEY = "esriEditsQueue"; +var REDO_STACK_KEY = "esriRedoStack"; + var EXECUTE_LONG_TESTS = true; describe("Internal Methods", function() @@ -445,7 +448,7 @@ describe("Public Interface", function() it("report edit store size", function() { usedBytes = g_editsStore.getEditsStoreSizeBytes(); - expect(usedBytes).toBe(705); + expect(usedBytes).toBe(692); }); it("report total local storage size", function() @@ -454,6 +457,14 @@ describe("Public Interface", function() 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() { if( EXECUTE_LONG_TESTS ) @@ -461,6 +472,13 @@ describe("Public Interface", function() console.log("this will take some time"); 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 try @@ -505,6 +523,6 @@ describe("Reset store", function() { it("reset the store", function() { - g_editsStore.resetEditsQueue(); + g_editsStore.resetEditsQueue(); }) });