From 596010fc99edb4c2373fc7ebe055300f626366c9 Mon Sep 17 00:00:00 2001 From: Michal Novotny Date: Wed, 11 Jul 2012 11:18:13 +0200 Subject: [PATCH] Bug 737615 - Remove use of synchronous cache API from unit tests - browser tests, r=bsmith --- .../content/test/browser_sanitizeDialog.js | 21 ++++-- .../tests/unit/test_clearHistory_shutdown.js | 67 +++++++++++-------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/browser/base/content/test/browser_sanitizeDialog.js b/browser/base/content/test/browser_sanitizeDialog.js index b0a312c37d5..5463d4d47cd 100644 --- a/browser/base/content/test/browser_sanitizeDialog.js +++ b/browser/base/content/test/browser_sanitizeDialog.js @@ -465,12 +465,6 @@ var gAllTests = [ var cs = Components.classes["@mozilla.org/network/cache-service;1"] .getService(Components.interfaces.nsICacheService); var session = cs.createSession(URL + "/manifest", nsICache.STORE_OFFLINE, nsICache.STREAM_BASED); - var cacheEntry = session.openCacheEntry(URL, nsICache.ACCESS_READ_WRITE, false); - var stream = cacheEntry.openOutputStream(0); - var content = "content"; - stream.write(content, content.length); - stream.close(); - cacheEntry.close(); // Open the dialog let wh = new WindowHelper(); @@ -506,7 +500,20 @@ var gAllTests = [ cs.visitEntries(visitor); is(size, 0, "offline application cache entries evicted"); }; - wh.open(); + + var cacheListener = { + onCacheEntryAvailable: function (entry, access, status) { + is(status, Cr.NS_OK); + var stream = entry.openOutputStream(0); + var content = "content"; + stream.write(content, content.length); + stream.close(); + entry.close(); + wh.open(); + } + }; + + session.asyncOpenCacheEntry(URL, nsICache.ACCESS_READ_WRITE, cacheListener); }, function () { // Test for offline apps permission deletion diff --git a/browser/components/places/tests/unit/test_clearHistory_shutdown.js b/browser/components/places/tests/unit/test_clearHistory_shutdown.js index 3d3fc5266d1..507090cda09 100644 --- a/browser/components/places/tests/unit/test_clearHistory_shutdown.js +++ b/browser/components/places/tests/unit/test_clearHistory_shutdown.js @@ -66,9 +66,7 @@ let notificationsObserver = { } // Check cache. - do_check_false(cacheExists(URL)); - - do_test_finished(); + checkCache(URL); } } @@ -105,7 +103,10 @@ function run_test() { }); print("Add cache."); storeCache(URL, "testData"); +} +function run_test_continue() +{ print("Simulate and wait shutdown."); getDistinctNotifications().forEach( function (topic) @@ -128,38 +129,46 @@ function storeCache(aURL, aContent) { getService(Ci.nsICacheService); let session = cache.createSession("FTP", Ci.nsICache.STORE_ANYWHERE, Ci.nsICache.STREAM_BASED); - let cacheEntry = - session.openCacheEntry(aURL, Ci.nsICache.ACCESS_READ_WRITE, false); - cacheEntry.setMetaDataElement("servertype", "0"); - var oStream = cacheEntry.openOutputStream(0); + var storeCacheListener = { + onCacheEntryAvailable: function (entry, access, status) { + do_check_eq(status, Cr.NS_OK); - var written = oStream.write(aContent, aContent.length); - if (written != aContent.length) { - do_throw("oStream.write has not written all data!\n" + - " Expected: " + written + "\n" + - " Actual: " + aContent.length + "\n"); - } - oStream.close(); - cacheEntry.close(); + entry.setMetaDataElement("servertype", "0"); + var os = entry.openOutputStream(0); + + var written = os.write(aContent, aContent.length); + if (written != aContent.length) { + do_throw("os.write has not written all data!\n" + + " Expected: " + written + "\n" + + " Actual: " + aContent.length + "\n"); + } + os.close(); + entry.close(); + do_execute_soon(run_test_continue); + } + }; + + session.asyncOpenCacheEntry(aURL, + Ci.nsICache.ACCESS_READ_WRITE, + storeCacheListener); } -function cacheExists(aURL) { + +function checkCache(aURL) { let cache = Cc["@mozilla.org/network/cache-service;1"]. getService(Ci.nsICacheService); let session = cache.createSession("FTP", Ci.nsICache.STORE_ANYWHERE, Ci.nsICache.STREAM_BASED); - try { - let cacheEntry = - session.openCacheEntry(aURL, Ci.nsICache.ACCESS_READ, true); - } catch (e) { - if (e.result == Cr.NS_ERROR_CACHE_KEY_NOT_FOUND || - e.result == Cr.NS_ERROR_FAILURE) - return false; - - // Throw the textual error description. - do_throw(e); - } - cacheEntry.close(); - return true; + + var checkCacheListener = { + onCacheEntryAvailable: function (entry, access, status) { + do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND); + do_test_finished(); + } + }; + + session.asyncOpenCacheEntry(aURL, + Ci.nsICache.ACCESS_READ, + checkCacheListener); }