Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | let gTestTab; |
michael@0 | 7 | let gContentAPI; |
michael@0 | 8 | let gContentWindow; |
michael@0 | 9 | |
michael@0 | 10 | Components.utils.import("resource:///modules/UITour.jsm"); |
michael@0 | 11 | Components.utils.import("resource://gre/modules/UITelemetry.jsm"); |
michael@0 | 12 | Components.utils.import("resource:///modules/BrowserUITelemetry.jsm"); |
michael@0 | 13 | |
michael@0 | 14 | function test() { |
michael@0 | 15 | UITelemetry._enabled = true; |
michael@0 | 16 | |
michael@0 | 17 | registerCleanupFunction(function() { |
michael@0 | 18 | Services.prefs.clearUserPref("browser.uitour.seenPageIDs"); |
michael@0 | 19 | resetSeenPageIDsLazyGetter(); |
michael@0 | 20 | UITelemetry._enabled = undefined; |
michael@0 | 21 | BrowserUITelemetry.setBucket(null); |
michael@0 | 22 | delete window.UITelemetry; |
michael@0 | 23 | delete window.BrowserUITelemetry; |
michael@0 | 24 | }); |
michael@0 | 25 | UITourTest(); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | function resetSeenPageIDsLazyGetter() { |
michael@0 | 29 | delete UITour.seenPageIDs; |
michael@0 | 30 | // This should be kept in sync with how UITour.init() sets this. |
michael@0 | 31 | Object.defineProperty(UITour, "seenPageIDs", { |
michael@0 | 32 | get: UITour.restoreSeenPageIDs.bind(UITour), |
michael@0 | 33 | configurable: true, |
michael@0 | 34 | }); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | function checkExpectedSeenPageIDs(expected) { |
michael@0 | 38 | is(UITour.seenPageIDs.size, expected.length, "Should be " + expected.length + " total seen page IDs"); |
michael@0 | 39 | |
michael@0 | 40 | for (let id of expected) |
michael@0 | 41 | ok(UITour.seenPageIDs.has(id), "Should have seen '" + id + "' page ID"); |
michael@0 | 42 | |
michael@0 | 43 | let prefData = Services.prefs.getCharPref("browser.uitour.seenPageIDs"); |
michael@0 | 44 | prefData = new Map(JSON.parse(prefData)); |
michael@0 | 45 | |
michael@0 | 46 | is(prefData.size, expected.length, "Should be " + expected.length + " total seen page IDs persisted"); |
michael@0 | 47 | |
michael@0 | 48 | for (let id of expected) |
michael@0 | 49 | ok(prefData.has(id), "Should have seen '" + id + "' page ID persisted"); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | let tests = [ |
michael@0 | 53 | function test_seenPageIDs_restore(done) { |
michael@0 | 54 | info("Setting up seenPageIDs to be restored from pref"); |
michael@0 | 55 | let data = JSON.stringify([ |
michael@0 | 56 | ["savedID1", { lastSeen: Date.now() }], |
michael@0 | 57 | ["savedID2", { lastSeen: Date.now() }], |
michael@0 | 58 | // 9 weeks ago, should auto expire. |
michael@0 | 59 | ["savedID3", { lastSeen: Date.now() - 9 * 7 * 24 * 60 * 60 * 1000 }], |
michael@0 | 60 | ]); |
michael@0 | 61 | Services.prefs.setCharPref("browser.uitour.seenPageIDs", |
michael@0 | 62 | data); |
michael@0 | 63 | |
michael@0 | 64 | resetSeenPageIDsLazyGetter(); |
michael@0 | 65 | checkExpectedSeenPageIDs(["savedID1", "savedID2"]); |
michael@0 | 66 | |
michael@0 | 67 | done(); |
michael@0 | 68 | }, |
michael@0 | 69 | function test_seenPageIDs_set_1(done) { |
michael@0 | 70 | gContentAPI.registerPageID("testpage1"); |
michael@0 | 71 | |
michael@0 | 72 | checkExpectedSeenPageIDs(["savedID1", "savedID2", "testpage1"]); |
michael@0 | 73 | |
michael@0 | 74 | const PREFIX = BrowserUITelemetry.BUCKET_PREFIX; |
michael@0 | 75 | const SEP = BrowserUITelemetry.BUCKET_SEPARATOR; |
michael@0 | 76 | |
michael@0 | 77 | let bucket = PREFIX + "UITour" + SEP + "testpage1"; |
michael@0 | 78 | is(BrowserUITelemetry.currentBucket, bucket, "Bucket should have correct name"); |
michael@0 | 79 | |
michael@0 | 80 | gBrowser.selectedTab = gBrowser.addTab("about:blank"); |
michael@0 | 81 | bucket = PREFIX + "UITour" + SEP + "testpage1" + SEP + "inactive" + SEP + "1m"; |
michael@0 | 82 | is(BrowserUITelemetry.currentBucket, bucket, |
michael@0 | 83 | "After switching tabs, bucket should be expiring"); |
michael@0 | 84 | |
michael@0 | 85 | gBrowser.removeTab(gBrowser.selectedTab); |
michael@0 | 86 | gBrowser.selectedTab = gTestTab; |
michael@0 | 87 | BrowserUITelemetry.setBucket(null); |
michael@0 | 88 | done(); |
michael@0 | 89 | }, |
michael@0 | 90 | function test_seenPageIDs_set_2(done) { |
michael@0 | 91 | gContentAPI.registerPageID("testpage2"); |
michael@0 | 92 | |
michael@0 | 93 | checkExpectedSeenPageIDs(["savedID1", "savedID2", "testpage1", "testpage2"]); |
michael@0 | 94 | |
michael@0 | 95 | const PREFIX = BrowserUITelemetry.BUCKET_PREFIX; |
michael@0 | 96 | const SEP = BrowserUITelemetry.BUCKET_SEPARATOR; |
michael@0 | 97 | |
michael@0 | 98 | let bucket = PREFIX + "UITour" + SEP + "testpage2"; |
michael@0 | 99 | is(BrowserUITelemetry.currentBucket, bucket, "Bucket should have correct name"); |
michael@0 | 100 | |
michael@0 | 101 | gBrowser.removeTab(gTestTab); |
michael@0 | 102 | gTestTab = null; |
michael@0 | 103 | bucket = PREFIX + "UITour" + SEP + "testpage2" + SEP + "closed" + SEP + "1m"; |
michael@0 | 104 | is(BrowserUITelemetry.currentBucket, bucket, |
michael@0 | 105 | "After closing tab, bucket should be expiring"); |
michael@0 | 106 | |
michael@0 | 107 | BrowserUITelemetry.setBucket(null); |
michael@0 | 108 | done(); |
michael@0 | 109 | }, |
michael@0 | 110 | ]; |