michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * Test that nsIPrivateBrowsingChannel.isChannelPrivate yields the correct michael@0: * result for various combinations of .setPrivate() and nsILoadContexts michael@0: */ michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: var URIs = [ michael@0: "http://example.org", michael@0: "https://example.org", michael@0: "ftp://example.org" michael@0: ]; michael@0: michael@0: function LoadContext(usePrivateBrowsing) { michael@0: this.usePrivateBrowsing = usePrivateBrowsing; michael@0: } michael@0: LoadContext.prototype = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsILoadContext, Ci.nsIInterfaceRequestor]), michael@0: getInterface: XPCOMUtils.generateQI([Ci.nsILoadContext]) michael@0: }; michael@0: michael@0: function getChannels() { michael@0: for (let u of URIs) { michael@0: yield Services.io.newChannel(u, null, null); michael@0: } michael@0: } michael@0: michael@0: function checkPrivate(channel, shouldBePrivate) { michael@0: do_check_eq(channel.QueryInterface(Ci.nsIPrivateBrowsingChannel).isChannelPrivate, michael@0: shouldBePrivate); michael@0: } michael@0: michael@0: /** michael@0: * Default configuration michael@0: * Default is non-private michael@0: */ michael@0: add_test(function test_plain() { michael@0: for (let c of getChannels()) { michael@0: checkPrivate(c, false); michael@0: } michael@0: run_next_test(); michael@0: }); michael@0: michael@0: /** michael@0: * Explicitly setPrivate(true), no load context michael@0: */ michael@0: add_test(function test_setPrivate_private() { michael@0: for (let c of getChannels()) { michael@0: c.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(true); michael@0: checkPrivate(c, true); michael@0: } michael@0: run_next_test(); michael@0: }); michael@0: michael@0: /** michael@0: * Explicitly setPrivate(false), no load context michael@0: */ michael@0: add_test(function test_setPrivate_regular() { michael@0: for (let c of getChannels()) { michael@0: c.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(false); michael@0: checkPrivate(c, false); michael@0: } michael@0: run_next_test(); michael@0: }); michael@0: michael@0: /** michael@0: * Load context mandates private mode michael@0: */ michael@0: add_test(function test_LoadContextPrivate() { michael@0: let ctx = new LoadContext(true); michael@0: for (let c of getChannels()) { michael@0: c.notificationCallbacks = ctx; michael@0: checkPrivate(c, true); michael@0: } michael@0: run_next_test(); michael@0: }); michael@0: michael@0: /** michael@0: * Load context mandates regular mode michael@0: */ michael@0: add_test(function test_LoadContextRegular() { michael@0: let ctx = new LoadContext(false); michael@0: for (let c of getChannels()) { michael@0: c.notificationCallbacks = ctx; michael@0: checkPrivate(c, false); michael@0: } michael@0: run_next_test(); michael@0: }); michael@0: michael@0: michael@0: // Do not test simultanous uses of .setPrivate and load context. michael@0: // There is little merit in doing so, and combining both will assert in michael@0: // Debug builds anyway. michael@0: michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }