michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Tests common Places telemetry probes by faking the telemetry service. michael@0: michael@0: Components.utils.import("resource://gre/modules/PlacesDBUtils.jsm"); michael@0: michael@0: let histograms = { michael@0: PLACES_PAGES_COUNT: function (val) do_check_eq(val, 1), michael@0: PLACES_BOOKMARKS_COUNT: function (val) do_check_eq(val, 1), michael@0: PLACES_TAGS_COUNT: function (val) do_check_eq(val, 1), michael@0: PLACES_FOLDERS_COUNT: function (val) do_check_eq(val, 1), michael@0: PLACES_KEYWORDS_COUNT: function (val) do_check_eq(val, 1), michael@0: PLACES_SORTED_BOOKMARKS_PERC: function (val) do_check_eq(val, 100), michael@0: PLACES_TAGGED_BOOKMARKS_PERC: function (val) do_check_eq(val, 100), michael@0: PLACES_DATABASE_FILESIZE_MB: function (val) do_check_true(val > 0), michael@0: // The journal may have been truncated. michael@0: PLACES_DATABASE_JOURNALSIZE_MB: function (val) do_check_true(val >= 0), michael@0: PLACES_DATABASE_PAGESIZE_B: function (val) do_check_eq(val, 32768), michael@0: PLACES_DATABASE_SIZE_PER_PAGE_B: function (val) do_check_true(val > 0), michael@0: PLACES_EXPIRATION_STEPS_TO_CLEAN2: function (val) do_check_true(val > 1), michael@0: //PLACES_AUTOCOMPLETE_1ST_RESULT_TIME_MS: function (val) do_check_true(val > 1), michael@0: PLACES_IDLE_FRECENCY_DECAY_TIME_MS: function (val) do_check_true(val > 0), michael@0: PLACES_IDLE_MAINTENANCE_TIME_MS: function (val) do_check_true(val > 0), michael@0: PLACES_ANNOS_BOOKMARKS_COUNT: function (val) do_check_eq(val, 1), michael@0: PLACES_ANNOS_BOOKMARKS_SIZE_KB: function (val) do_check_eq(val, 1), michael@0: PLACES_ANNOS_PAGES_COUNT: function (val) do_check_eq(val, 1), michael@0: PLACES_ANNOS_PAGES_SIZE_KB: function (val) do_check_eq(val, 1), michael@0: PLACES_FRECENCY_CALC_TIME_MS: function (val) do_check_true(val >= 0), michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_execute() michael@0: { michael@0: // Put some trash in the database. michael@0: const URI = NetUtil.newURI("http://moz.org/"); michael@0: michael@0: let folderId = PlacesUtils.bookmarks.createFolder(PlacesUtils.unfiledBookmarksFolderId, michael@0: "moz test", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: let itemId = PlacesUtils.bookmarks.insertBookmark(folderId, michael@0: uri, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "moz test"); michael@0: PlacesUtils.tagging.tagURI(uri, ["tag"]); michael@0: PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword"); michael@0: michael@0: // Set a large annotation. michael@0: let content = ""; michael@0: while (content.length < 1024) { michael@0: content += "0"; michael@0: } michael@0: PlacesUtils.annotations.setItemAnnotation(itemId, "test-anno", content, 0, michael@0: PlacesUtils.annotations.EXPIRE_NEVER); michael@0: PlacesUtils.annotations.setPageAnnotation(uri, "test-anno", content, 0, michael@0: PlacesUtils.annotations.EXPIRE_NEVER); michael@0: michael@0: // Request to gather telemetry data. michael@0: Cc["@mozilla.org/places/categoriesStarter;1"] michael@0: .getService(Ci.nsIObserver) michael@0: .observe(null, "gather-telemetry", null); michael@0: michael@0: yield promiseAsyncUpdates(); michael@0: michael@0: // Test expiration probes. michael@0: for (let i = 0; i < 2; i++) { michael@0: yield promiseAddVisits({ michael@0: uri: uri("http://" + i + ".moz.org/"), michael@0: visitDate: Date.now() // [sic] michael@0: }); michael@0: } michael@0: Services.prefs.setIntPref("places.history.expiration.max_pages", 0); michael@0: let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver); michael@0: expire.observe(null, "places-debug-start-expiration", 1); michael@0: expire.observe(null, "places-debug-start-expiration", -1); michael@0: michael@0: // Test autocomplete probes. michael@0: /* michael@0: // This is useful for manual testing by changing the minimum time for michael@0: // autocomplete telemetry to 0, but there is no way to artificially delay michael@0: // autocomplete by more than 50ms in a realiable way. michael@0: Services.prefs.setIntPref("browser.urlbar.search.sources", 3); michael@0: Services.prefs.setIntPref("browser.urlbar.default.behavior", 0); michael@0: function AutoCompleteInput(aSearches) { michael@0: this.searches = aSearches; michael@0: } michael@0: AutoCompleteInput.prototype = { michael@0: timeout: 10, michael@0: textValue: "", michael@0: searchParam: "", michael@0: popupOpen: false, michael@0: minResultsForPopup: 0, michael@0: invalidate: function() {}, michael@0: disableAutoComplete: false, michael@0: completeDefaultIndex: false, michael@0: get popup() { return this; }, michael@0: onSearchBegin: function() {}, michael@0: onSearchComplete: function() {}, michael@0: setSelectedIndex: function() {}, michael@0: get searchCount() { return this.searches.length; }, michael@0: getSearchAt: function(aIndex) this.searches[aIndex], michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIAutoCompleteInput, michael@0: Ci.nsIAutoCompletePopup, michael@0: ]) michael@0: }; michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: controller.input = new AutoCompleteInput(["history"]); michael@0: controller.startSearch("moz"); michael@0: */ michael@0: michael@0: // Test idle probes. michael@0: PlacesUtils.history.QueryInterface(Ci.nsIObserver) michael@0: .observe(null, "idle-daily", null); michael@0: PlacesDBUtils.maintenanceOnIdle(); michael@0: michael@0: yield promiseTopicObserved("places-maintenance-finished"); michael@0: michael@0: for (let histogramId in histograms) { michael@0: do_log_info("checking histogram " + histogramId); michael@0: let validate = histograms[histogramId]; michael@0: let snapshot = Services.telemetry.getHistogramById(histogramId).snapshot(); michael@0: validate(snapshot.sum); michael@0: do_check_true(snapshot.counts.reduce(function(a, b) a + b) > 0); michael@0: } michael@0: }); michael@0: michael@0: add_test(function test_healthreport_callback() { michael@0: PlacesDBUtils.telemetry(null, function onResult(data) { michael@0: do_check_neq(data, null); michael@0: michael@0: do_check_eq(Object.keys(data).length, 2); michael@0: do_check_eq(data.PLACES_PAGES_COUNT, 1); michael@0: do_check_eq(data.PLACES_BOOKMARKS_COUNT, 1); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: