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 | // Tests common Places telemetry probes by faking the telemetry service. |
michael@0 | 5 | |
michael@0 | 6 | Components.utils.import("resource://gre/modules/PlacesDBUtils.jsm"); |
michael@0 | 7 | |
michael@0 | 8 | let histograms = { |
michael@0 | 9 | PLACES_PAGES_COUNT: function (val) do_check_eq(val, 1), |
michael@0 | 10 | PLACES_BOOKMARKS_COUNT: function (val) do_check_eq(val, 1), |
michael@0 | 11 | PLACES_TAGS_COUNT: function (val) do_check_eq(val, 1), |
michael@0 | 12 | PLACES_FOLDERS_COUNT: function (val) do_check_eq(val, 1), |
michael@0 | 13 | PLACES_KEYWORDS_COUNT: function (val) do_check_eq(val, 1), |
michael@0 | 14 | PLACES_SORTED_BOOKMARKS_PERC: function (val) do_check_eq(val, 100), |
michael@0 | 15 | PLACES_TAGGED_BOOKMARKS_PERC: function (val) do_check_eq(val, 100), |
michael@0 | 16 | PLACES_DATABASE_FILESIZE_MB: function (val) do_check_true(val > 0), |
michael@0 | 17 | // The journal may have been truncated. |
michael@0 | 18 | PLACES_DATABASE_JOURNALSIZE_MB: function (val) do_check_true(val >= 0), |
michael@0 | 19 | PLACES_DATABASE_PAGESIZE_B: function (val) do_check_eq(val, 32768), |
michael@0 | 20 | PLACES_DATABASE_SIZE_PER_PAGE_B: function (val) do_check_true(val > 0), |
michael@0 | 21 | PLACES_EXPIRATION_STEPS_TO_CLEAN2: function (val) do_check_true(val > 1), |
michael@0 | 22 | //PLACES_AUTOCOMPLETE_1ST_RESULT_TIME_MS: function (val) do_check_true(val > 1), |
michael@0 | 23 | PLACES_IDLE_FRECENCY_DECAY_TIME_MS: function (val) do_check_true(val > 0), |
michael@0 | 24 | PLACES_IDLE_MAINTENANCE_TIME_MS: function (val) do_check_true(val > 0), |
michael@0 | 25 | PLACES_ANNOS_BOOKMARKS_COUNT: function (val) do_check_eq(val, 1), |
michael@0 | 26 | PLACES_ANNOS_BOOKMARKS_SIZE_KB: function (val) do_check_eq(val, 1), |
michael@0 | 27 | PLACES_ANNOS_PAGES_COUNT: function (val) do_check_eq(val, 1), |
michael@0 | 28 | PLACES_ANNOS_PAGES_SIZE_KB: function (val) do_check_eq(val, 1), |
michael@0 | 29 | PLACES_FRECENCY_CALC_TIME_MS: function (val) do_check_true(val >= 0), |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | function run_test() |
michael@0 | 33 | { |
michael@0 | 34 | run_next_test(); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | add_task(function test_execute() |
michael@0 | 38 | { |
michael@0 | 39 | // Put some trash in the database. |
michael@0 | 40 | const URI = NetUtil.newURI("http://moz.org/"); |
michael@0 | 41 | |
michael@0 | 42 | let folderId = PlacesUtils.bookmarks.createFolder(PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 43 | "moz test", |
michael@0 | 44 | PlacesUtils.bookmarks.DEFAULT_INDEX); |
michael@0 | 45 | let itemId = PlacesUtils.bookmarks.insertBookmark(folderId, |
michael@0 | 46 | uri, |
michael@0 | 47 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 48 | "moz test"); |
michael@0 | 49 | PlacesUtils.tagging.tagURI(uri, ["tag"]); |
michael@0 | 50 | PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword"); |
michael@0 | 51 | |
michael@0 | 52 | // Set a large annotation. |
michael@0 | 53 | let content = ""; |
michael@0 | 54 | while (content.length < 1024) { |
michael@0 | 55 | content += "0"; |
michael@0 | 56 | } |
michael@0 | 57 | PlacesUtils.annotations.setItemAnnotation(itemId, "test-anno", content, 0, |
michael@0 | 58 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 59 | PlacesUtils.annotations.setPageAnnotation(uri, "test-anno", content, 0, |
michael@0 | 60 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 61 | |
michael@0 | 62 | // Request to gather telemetry data. |
michael@0 | 63 | Cc["@mozilla.org/places/categoriesStarter;1"] |
michael@0 | 64 | .getService(Ci.nsIObserver) |
michael@0 | 65 | .observe(null, "gather-telemetry", null); |
michael@0 | 66 | |
michael@0 | 67 | yield promiseAsyncUpdates(); |
michael@0 | 68 | |
michael@0 | 69 | // Test expiration probes. |
michael@0 | 70 | for (let i = 0; i < 2; i++) { |
michael@0 | 71 | yield promiseAddVisits({ |
michael@0 | 72 | uri: uri("http://" + i + ".moz.org/"), |
michael@0 | 73 | visitDate: Date.now() // [sic] |
michael@0 | 74 | }); |
michael@0 | 75 | } |
michael@0 | 76 | Services.prefs.setIntPref("places.history.expiration.max_pages", 0); |
michael@0 | 77 | let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver); |
michael@0 | 78 | expire.observe(null, "places-debug-start-expiration", 1); |
michael@0 | 79 | expire.observe(null, "places-debug-start-expiration", -1); |
michael@0 | 80 | |
michael@0 | 81 | // Test autocomplete probes. |
michael@0 | 82 | /* |
michael@0 | 83 | // This is useful for manual testing by changing the minimum time for |
michael@0 | 84 | // autocomplete telemetry to 0, but there is no way to artificially delay |
michael@0 | 85 | // autocomplete by more than 50ms in a realiable way. |
michael@0 | 86 | Services.prefs.setIntPref("browser.urlbar.search.sources", 3); |
michael@0 | 87 | Services.prefs.setIntPref("browser.urlbar.default.behavior", 0); |
michael@0 | 88 | function AutoCompleteInput(aSearches) { |
michael@0 | 89 | this.searches = aSearches; |
michael@0 | 90 | } |
michael@0 | 91 | AutoCompleteInput.prototype = { |
michael@0 | 92 | timeout: 10, |
michael@0 | 93 | textValue: "", |
michael@0 | 94 | searchParam: "", |
michael@0 | 95 | popupOpen: false, |
michael@0 | 96 | minResultsForPopup: 0, |
michael@0 | 97 | invalidate: function() {}, |
michael@0 | 98 | disableAutoComplete: false, |
michael@0 | 99 | completeDefaultIndex: false, |
michael@0 | 100 | get popup() { return this; }, |
michael@0 | 101 | onSearchBegin: function() {}, |
michael@0 | 102 | onSearchComplete: function() {}, |
michael@0 | 103 | setSelectedIndex: function() {}, |
michael@0 | 104 | get searchCount() { return this.searches.length; }, |
michael@0 | 105 | getSearchAt: function(aIndex) this.searches[aIndex], |
michael@0 | 106 | QueryInterface: XPCOMUtils.generateQI([ |
michael@0 | 107 | Ci.nsIAutoCompleteInput, |
michael@0 | 108 | Ci.nsIAutoCompletePopup, |
michael@0 | 109 | ]) |
michael@0 | 110 | }; |
michael@0 | 111 | let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
michael@0 | 112 | getService(Ci.nsIAutoCompleteController); |
michael@0 | 113 | controller.input = new AutoCompleteInput(["history"]); |
michael@0 | 114 | controller.startSearch("moz"); |
michael@0 | 115 | */ |
michael@0 | 116 | |
michael@0 | 117 | // Test idle probes. |
michael@0 | 118 | PlacesUtils.history.QueryInterface(Ci.nsIObserver) |
michael@0 | 119 | .observe(null, "idle-daily", null); |
michael@0 | 120 | PlacesDBUtils.maintenanceOnIdle(); |
michael@0 | 121 | |
michael@0 | 122 | yield promiseTopicObserved("places-maintenance-finished"); |
michael@0 | 123 | |
michael@0 | 124 | for (let histogramId in histograms) { |
michael@0 | 125 | do_log_info("checking histogram " + histogramId); |
michael@0 | 126 | let validate = histograms[histogramId]; |
michael@0 | 127 | let snapshot = Services.telemetry.getHistogramById(histogramId).snapshot(); |
michael@0 | 128 | validate(snapshot.sum); |
michael@0 | 129 | do_check_true(snapshot.counts.reduce(function(a, b) a + b) > 0); |
michael@0 | 130 | } |
michael@0 | 131 | }); |
michael@0 | 132 | |
michael@0 | 133 | add_test(function test_healthreport_callback() { |
michael@0 | 134 | PlacesDBUtils.telemetry(null, function onResult(data) { |
michael@0 | 135 | do_check_neq(data, null); |
michael@0 | 136 | |
michael@0 | 137 | do_check_eq(Object.keys(data).length, 2); |
michael@0 | 138 | do_check_eq(data.PLACES_PAGES_COUNT, 1); |
michael@0 | 139 | do_check_eq(data.PLACES_BOOKMARKS_COUNT, 1); |
michael@0 | 140 | |
michael@0 | 141 | run_next_test(); |
michael@0 | 142 | }); |
michael@0 | 143 | }); |
michael@0 | 144 |