diff options
16 files changed, 217 insertions, 58 deletions
diff --git a/chrome/browser/chromeos/file_manager/file_manager_jstest.cc b/chrome/browser/chromeos/file_manager/file_manager_jstest.cc index 6228148..415edfa 100644 --- a/chrome/browser/chromeos/file_manager/file_manager_jstest.cc +++ b/chrome/browser/chromeos/file_manager/file_manager_jstest.cc @@ -95,6 +95,10 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, AsyncUtilTest) { FILE_PATH_LITERAL("common/js/async_util_unittest.html"))); } +IN_PROC_BROWSER_TEST_F(FileManagerJsTest, MetricsTest) { + RunTest(base::FilePath(FILE_PATH_LITERAL("common/js/metrics_unittest.html"))); +} + IN_PROC_BROWSER_TEST_F(FileManagerJsTest, TaskController) { RunTest(base::FilePath( FILE_PATH_LITERAL("foreground/js/task_controller_unittest.html"))); diff --git a/chrome/browser_tests.isolate b/chrome/browser_tests.isolate index 1a52ae6..18c0745 100644 --- a/chrome/browser_tests.isolate +++ b/chrome/browser_tests.isolate @@ -224,6 +224,7 @@ '../ui/file_manager/image_loader/', '../ui/file_manager/integration_tests/', '../ui/webui/resources/js/', + '../third_party/analytics/', '../chrome/browser/resources/chromeos/wallpaper_manager/', '<(PRODUCT_DIR)/chromevox_test_data/', '<(PRODUCT_DIR)/content_shell.pak', diff --git a/ui/file_manager/file_manager/common/js/metrics.js b/ui/file_manager/file_manager/common/js/metrics.js new file mode 100644 index 0000000..1e86f36 --- /dev/null +++ b/ui/file_manager/file_manager/common/js/metrics.js @@ -0,0 +1,72 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * @fileoverview Utility methods for accessing chrome.metricsPrivate API. + * + * To be included as a first script in main.html + */ + +var metrics = metrics || metricsBase; + +/** + * Analytics tracking ID for Files app. + * @const {string} + */ +metrics.TRACKING_ID = 'UA-38248358-9'; + +/** + * Convert a short metric name to the full format. + * + * @param {string} name Short metric name. + * @return {string} Full metric name. + * @override + * @private + */ +metrics.convertName_ = function(name) { + return 'FileBrowser.' + name; +}; + +/** @private {analytics.Tracker} */ +metrics.tracker_ = null; + +/** @private {boolean} */ +metrics.enabled_ = false; + +/** @return {!analytics.Tracker} */ +metrics.getTracker = function() { + if (!metrics.tracker_) { + metrics.createTracker_(); + } + return /** @type {!analytics.Tracker} */ (metrics.tracker_); +}; + +/** + * Creates a new analytics tracker. + * @private + */ +metrics.createTracker_ = function() { + var analyticsService = analytics.getService('Files app'); + + // Create a tracker, add a filter that only enables analytics when UMA is + // enabled. + metrics.tracker_ = analyticsService.getTracker(metrics.TRACKING_ID); + metrics.tracker_.addFilter(metrics.umaEnabledFilter_); +}; + +/** + * Queries the chrome UMA enabled setting, and filters hits based on that. + * @param {!analytics.Tracker.Hit} hit + * @private + */ +metrics.umaEnabledFilter_ = function(hit) { + chrome.fileManagerPrivate.isUMAEnabled( + /** @param {boolean} enabled */ + function(enabled) { + metrics.enabled_ = enabled; + }); + if (!metrics.enabled_) { + hit.cancel(); + } +}; diff --git a/ui/file_manager/file_manager/foreground/js/metrics_base.js b/ui/file_manager/file_manager/common/js/metrics_base.js index 1acbc4e..1acbc4e 100644 --- a/ui/file_manager/file_manager/foreground/js/metrics_base.js +++ b/ui/file_manager/file_manager/common/js/metrics_base.js diff --git a/ui/file_manager/file_manager/common/js/metrics_unittest.html b/ui/file_manager/file_manager/common/js/metrics_unittest.html new file mode 100644 index 0000000..fd45566 --- /dev/null +++ b/ui/file_manager/file_manager/common/js/metrics_unittest.html @@ -0,0 +1,24 @@ +<!DOCTYPE html> +<!-- Copyright 2015 The Chromium Authors. All rights reserved. + -- Use of this source code is governed by a BSD-style license that can be + -- found in the LICENSE file. + --> + +<html> +<body> + <script src="../../../../../ui/webui/resources/js/cr.js"></script> + <script src="../../../../../ui/webui/resources/js/cr/event_target.js"></script> + <script src="../../../../../ui/webui/resources/js/cr/ui/array_data_model.js"></script> + <script src="../../../../../ui/webui/resources/js/load_time_data.js"></script> + + <script src="../../../../../third_party/analytics/google-analytics-bundle.js"></script> + + <script src="unittest_util.js"></script> + <script src="util.js"></script> + <script src="metrics_base.js"></script> + <script src="metrics.js"></script> + + + <script src="metrics_unittest.js"></script> +</body> +</html> diff --git a/ui/file_manager/file_manager/common/js/metrics_unittest.js b/ui/file_manager/file_manager/common/js/metrics_unittest.js new file mode 100644 index 0000000..36a9807 --- /dev/null +++ b/ui/file_manager/file_manager/common/js/metrics_unittest.js @@ -0,0 +1,84 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Fake APIs +var chrome; + +/** @type {analytics.Tracker.Hit} The last hit received. */ +var lastHit = null; + +/** @type {!analytics.Tracker} */ +var tracker; + +function setUp() { + setupFakeChromeAPIs(); + + tracker = metrics.getTracker(); + + // Make a filter that logs the last received hit. Cancel the hit so that + // running tests doesn't actually send any analytics. + tracker.addFilter( + /** @param {!analytics.Tracker.Hit} hit */ + function(hit) { + // Log the hit. + lastHit = hit; + hit.cancel(); + }); + // Reset the last logged hit. + lastHit = null; +} + +// Verifies that analytics logging occurs when fileManagerPrivate.isUMAEnabled +// returns true. +function testBasicLogging(callback) { + // Simulate UMA enabled, then check that hits are sent. + chrome.fileManagerPrivate.umaEnabled = true; + reportPromise( + tracker.sendAppView('Test').addCallback( + function() { + assertTrue(!!lastHit); + }), + callback); +} + +// Verifies that analytics logging does not occur when +// fileManagerPrivate.isUMAEnabled returns false. +function testUMADisabled(callback) { + // Simulate UMA disabled, and verify that hits aren't sent. + chrome.fileManagerPrivate.umaEnabled = false; + reportPromise( + tracker.sendAppView('Test').addCallback( + function() { + assertTrue(lastHit === null); + }), + callback); +} + +function setupFakeChromeAPIs() { + chrome = { + runtime: { + getManifest: function() { + return { + version: 0.0 + }; + } + }, + storage: { + local: { + // Analytics uses storage to store the enabled/disabled flag. Hard-wire + // the get method to always return true so analytics is jammed on for + // the purposes of testing. + get: function(data, cb) { cb(true); }, + set: function(data, cb) {} + }, + onChanged: { + addListener: function(cb) {} + } + }, + fileManagerPrivate: { + umaEnabled: false, + isUMAEnabled: function(cb) { cb(chrome.fileManagerPrivate.umaEnabled); } + } + }; +} diff --git a/ui/file_manager/file_manager/foreground/js/compiled_resources.gyp b/ui/file_manager/file_manager/foreground/js/compiled_resources.gyp index ad7a621..ce5e345 100644 --- a/ui/file_manager/file_manager/foreground/js/compiled_resources.gyp +++ b/ui/file_manager/file_manager/foreground/js/compiled_resources.gyp @@ -47,6 +47,8 @@ '../../common/js/util.js', '../../common/js/progress_center_common.js', '../../common/js/lru_cache.js', + '../../common/js/metrics_base.js', + '../../common/js/metrics.js', '../../background/js/file_operation_manager.js', '../../background/js/file_operation_util.js', '../../background/js/file_operation_handler.js', @@ -63,8 +65,7 @@ '../../background/js/background_base.js', '../../background/js/background.js', '../../../image_loader/image_loader_client.js', - './metrics_base.js', - './metrics.js', + './metrics_start.js', './ui/combobutton.js', './ui/commandbutton.js', './ui/file_manager_dialog_base.js', diff --git a/ui/file_manager/file_manager/foreground/js/main_scripts.js b/ui/file_manager/file_manager/foreground/js/main_scripts.js index 7292ba4..c18ed46 100644 --- a/ui/file_manager/file_manager/foreground/js/main_scripts.js +++ b/ui/file_manager/file_manager/foreground/js/main_scripts.js @@ -22,8 +22,9 @@ // // metrics_base.js and metrics.js initiates load performance tracking // so we want to parse it as early as possible. -//<include src="metrics_base.js"> -//<include src="metrics.js"> +//<include src="../../common/js/metrics_base.js"> +//<include src="../../common/js/metrics.js"> +//<include src="metrics_start.js"> // //<include src="../../common/js/lru_cache.js"> //<include src="../../../image_loader/image_loader_client.js"> diff --git a/ui/file_manager/file_manager/foreground/js/metrics.js b/ui/file_manager/file_manager/foreground/js/metrics.js deleted file mode 100644 index 001cea9..0000000 --- a/ui/file_manager/file_manager/foreground/js/metrics.js +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -/** - * @fileoverview Utility methods for accessing chrome.metricsPrivate API. - * - * To be included as a first script in main.html - */ - -var metrics = metricsBase; - -metrics.startInterval('Load.Total'); -metrics.startInterval('Load.Script'); - -/** - * A mapping of enum names to valid values. This object is consulted - * any time an enum value is being reported unaccompanied by a list - * of valid values. - * - * <p>Values in this object should correspond exactly with values - * in {@code tools/metrics/histograms/histograms.xml}. - * - * <p>NEVER REMOVE OR REORDER ITEMS IN THIS LIST! - * - * @private {!Object.<string, !Array.<*>|number>} - */ -metrics.validEnumValues_ = { - 'CloudImport.UserAction': [ - 'IMPORT_INITIATED' - ] -}; - -/** - * Convert a short metric name to the full format. - * - * @param {string} name Short metric name. - * @return {string} Full metric name. - * @override - * @private - */ -metrics.convertName_ = function(name) { - return 'FileBrowser.' + name; -}; diff --git a/ui/file_manager/file_manager/foreground/js/metrics_start.js b/ui/file_manager/file_manager/foreground/js/metrics_start.js new file mode 100644 index 0000000..8695c09 --- /dev/null +++ b/ui/file_manager/file_manager/foreground/js/metrics_start.js @@ -0,0 +1,12 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * @fileoverview Metrics calls to start measurement of script loading. Include + * this as the first script in main.html (i.e. after the common scripts that + * define the metrics namespace). + */ + +metrics.startInterval('Load.Total'); +metrics.startInterval('Load.Script'); diff --git a/ui/file_manager/file_manager/main.html b/ui/file_manager/file_manager/main.html index 4f9fd17..7386bfa 100644 --- a/ui/file_manager/file_manager/main.html +++ b/ui/file_manager/file_manager/main.html @@ -47,8 +47,9 @@ <!-- metrics.js initiates load performance tracking so we want to parse it as early as possible --> - <script src="foreground/js/metrics_base.js"></script> - <script src="foreground/js/metrics.js"></script> + <script src="common/js/metrics_base.js"></script> + <script src="common/js/metrics.js"></script> + <script src="foreground/js/metrics_start.js"></script> <!-- Loads the client of the image loader extension --> <script src="common/js/lru_cache.js"></script> diff --git a/ui/file_manager/gallery/gallery.html b/ui/file_manager/gallery/gallery.html index 062f23d..a63759c 100644 --- a/ui/file_manager/gallery/gallery.html +++ b/ui/file_manager/gallery/gallery.html @@ -21,8 +21,9 @@ <!-- This section is used when the file manager is loaded with 'filemgr-ext-path' command-line flag. --> <!-- Keep the list in sync with gallery_scripts.js. --> - <script src="../file_manager/foreground/js/metrics_base.js"></script> - <script src="../file_manager/foreground/js/metrics.js"></script> + <script src="../file_manager/common/js/metrics_base.js"></script> + <script src="../file_manager/common/js/metrics.js"></script> + <script src="../file_manager/foreground/js/metrics_start.js"></script> <!-- Loads the client of the image loader extension --> <script src="../file_manager/common/js/lru_cache.js"></script> diff --git a/ui/file_manager/gallery/js/compiled_resources.gyp b/ui/file_manager/gallery/js/compiled_resources.gyp index 22542de..4b5ca69 100644 --- a/ui/file_manager/gallery/js/compiled_resources.gyp +++ b/ui/file_manager/gallery/js/compiled_resources.gyp @@ -60,8 +60,9 @@ '../../file_manager/common/js/async_util.js', '../../file_manager/common/js/file_type.js', '../../file_manager/common/js/util.js', - '../../file_manager/foreground/js/metrics_base.js', - '../../file_manager/foreground/js/metrics.js', + '../../file_manager/common/js/metrics_base.js', + '../../file_manager/common/js/metrics.js', + '../../file_manager/foreground/js/metrics_start.js', '../../file_manager/foreground/js/metadata/metadata_cache.js', '../../file_manager/foreground/js/metadata/exif_constants.js', '../../file_manager/foreground/js/mouse_inactivity_watcher.js', diff --git a/ui/file_manager/gallery/js/gallery_scripts.js b/ui/file_manager/gallery/js/gallery_scripts.js index 5e19ca9..914e86e 100644 --- a/ui/file_manager/gallery/js/gallery_scripts.js +++ b/ui/file_manager/gallery/js/gallery_scripts.js @@ -8,8 +8,9 @@ // included file but that's all right since any javascript file should start // with a copyright comment anyway. -//<include src="../../file_manager/foreground/js/metrics_base.js"> -//<include src="../../file_manager/foreground/js/metrics.js"> +//<include src="../../file_manager/common/js/metrics_base.js"> +//<include src="../../file_manager/common/js/metrics.js"> +//<include src="../../file_manager/foreground/js/metrics_start.js"> //<include src="../../file_manager/common/js/lru_cache.js"> //<include src="../../image_loader/image_loader_client.js"> diff --git a/ui/file_manager/video_player/js/compiled_resources.gyp b/ui/file_manager/video_player/js/compiled_resources.gyp index 5bc15cf..867e3c5 100644 --- a/ui/file_manager/video_player/js/compiled_resources.gyp +++ b/ui/file_manager/video_player/js/compiled_resources.gyp @@ -45,7 +45,7 @@ 'depends': [ '../../../../third_party/jstemplate/compiled_resources.gyp:jstemplate', 'error_util.js', - '../../file_manager/foreground/js/metrics_base.js', + '../../file_manager/common/js/metrics_base.js', 'video_player_metrics.js', '../../../webui/resources/js/cr.js', '../../../webui/resources/js/load_time_data.js', diff --git a/ui/file_manager/video_player/js/video_player_scripts.js b/ui/file_manager/video_player/js/video_player_scripts.js index 448f420..e32510c 100644 --- a/ui/file_manager/video_player/js/video_player_scripts.js +++ b/ui/file_manager/video_player/js/video_player_scripts.js @@ -10,7 +10,7 @@ //<include src="error_util.js"> -//<include src="../../file_manager/foreground/js/metrics_base.js"> +//<include src="../../file_manager/common/js/metrics_base.js"> //<include src="video_player_metrics.js"> //<include src="../../../webui/resources/js/cr.js"> |