Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7196)

Unified Diff: chrome/browser/resources/ntp_search/mock/mock.js

Issue 10829131: Refactoring NTP5: new implementation of TilePage and MostVisitedPage (which now inherits from Thumb… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Implemented changes suggested by Jeremy Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/ntp_search/mock/mock.js
diff --git a/chrome/browser/resources/ntp_search/mock/mock.js b/chrome/browser/resources/ntp_search/mock/mock.js
new file mode 100644
index 0000000000000000000000000000000000000000..db09b3fde1397ee65cfd64d28739efe7952c851e
--- /dev/null
+++ b/chrome/browser/resources/ntp_search/mock/mock.js
@@ -0,0 +1,190 @@
+// 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.
+
+// TODO(pedrosimonetti): document how to generate the data pseudo-automatically.
+!/CrOs/.test(navigator.userAgent) && (function() {
+
+ var __chrome__ = chrome;
+ var shouldRegisterData = !!window.chrome && !!window.chrome.send;
+
+ var NO_CALLBACK = 1;
+
+ // Only messages registered in the callback map will be intercepted.
+ var callbackMap = {
+ 'metricsHandler:logEventTime': NO_CALLBACK,
+ 'metricsHandler:recordInHistogram': NO_CALLBACK,
+ 'getApps': 'ntp.getAppsCallback',
+ 'getRecentlyClosedTabs': 'ntp.setRecentlyClosedTabs',
+ 'getMostVisited': 'ntp.setMostVisitedPages'
+ };
+
+ // TODO(pedrosimonetti): include automatically in the recorded data
+ // TODO(pedrosimonetti): document the special char replacement
+ var mockedThumbnailUrls = [
+ "http---www.wikipedia.org-",
+ "http---www.deviantart.com-",
+ "http---wefunkradio.com-",
+ "http---youtube.com-",
jeremycho_google 2012/08/03 20:51:24 Add www.youtube.com, etc. for the mock recently cl
pedrosimonetti2 2012/08/03 22:02:39 The logic behind mocked thumbnails is not that sim
+ "http---amazon.com-",
+ "http---nytimes.com-",
+ "http---news.ycombinator.com-",
+ "http---cnn.com-",
+ "http---ebay.com-",
+ "http---www.google.com-chrome-intl-en-welcome.html",
+ "https---chrome.google.com-webstore-hl-en"
+ ];
+
+
+ //----------------------------------------------------------------------------
+ // Internals
+ //----------------------------------------------------------------------------
+
+ var dataMap = {};
+ var recordedDataMap = {};
+ var isInitialized = false;
+ var thumbnailUrlList = [];
+
+ function initialize() {
+ if (shouldRegisterData || !namespace('ntp')) {
+ return;
+ }
+ isInitialized = true;
+ namespace('ntp.getThumbnailUrl', mockGetThumbnailUrl);
+ }
+
+ function namespace(str, data) {
+ var ns = str.split('.'), name, object = window;
+ for (var i = 0, l = ns.length; i < l; i++) {
+ name = ns[i];
+ if (data && i == (l - 1)) {
+ object = object[name] = data;
+ } else {
+ object = object[name];
+ }
+ }
+ return object == window ? null : object;
+ }
+
+ function copyArray(arr) {
+ return Array.prototype.slice.call(arr, 0);
+ }
+
+ function interceptCallback(message, callbackNamespace) {
+ var original = namespace(callbackNamespace);
+ namespace(callbackNamespace, function() {
+ recordedDataMap[message] = copyArray(arguments);
+ var result = original.apply(window, arguments);
+ namespace(callbackNamespace, original);
+ return result;
+ });
+ }
+
+ function interceptLoadData() {
+ window.addEventListener('load', function() {
+ recordedDataMap['__loadTimeData__'] = loadTimeData.data_;
+ });
+ }
+
+ function mockGetThumbnailUrl(url) {
+ url = url.replace(/[\:\/\?\=]/g, '-');
+
+ if (thumbnailUrlList.length == 0) {
+ thumbnailUrlList = copyArray(mockedThumbnailUrls);
+ }
+ var mockUrl;
+ var index = thumbnailUrlList.indexOf(url);
+ if (index != -1) {
+ // Remove an element from a particular index.
+ mockUrl = thumbnailUrlList.splice(index, 1);
+ } else {
+ // Remove the first element.
+ mockUrl = thumbnailUrlList.shift();
+ }
+
+ mockUrl = 'mock/images/thumb/' + mockUrl + '.png';
+ return mockUrl;
+ }
+
+ function mockLoadData() {
+ if (loadTimeData) {
+ loadTimeData.data = dataMap['__loadTimeData__'];
+ }
+ }
+
+
+ //----------------------------------------------------------------------------
+ // ChromeMock implementation
+ //----------------------------------------------------------------------------
+
+ ChromeMock = {
+
jeremycho_google 2012/08/03 20:51:24 delete newline
pedrosimonetti2 2012/08/03 22:02:39 Done.
+ mock: function(newDataMap) {
+ if (newDataMap) {
+ dataMap = newDataMap;
+ if (!shouldRegisterData) {
+ mockLoadData();
+ }
+ } else {
+ return recordedDataMap;
+ }
+ },
+
+ send: function() {
+ if (!isInitialized) {
+ initialize();
+ }
+
+ var message = arguments[0];
+ var shouldCallChromeSend = false;
+
+ var data;
+ var callback;
+ var callbackNamespace;
+
+ if (callbackMap.hasOwnProperty(message)) {
+
jeremycho_google 2012/08/03 20:51:24 delete newline
pedrosimonetti2 2012/08/03 22:02:39 Done.
+ callbackNamespace = callbackMap[message];
+
+ if (shouldRegisterData) {
+ if (callbackNamespace !== NO_CALLBACK) {
+ interceptCallback(message, callbackNamespace);
+ }
+ } else {
+ if (dataMap.hasOwnProperty(message)) {
+ data = dataMap[message];
+ callback = namespace(callbackNamespace);
+ setTimeout(function() {
+ callback.apply(window, data);
+ }, 0);
+ } else {
+ if (callbackNamespace !== NO_CALLBACK) {
+ console.warn('No mock registered for message "%s".', message);
+ }
+ }
+ }
+
jeremycho_google 2012/08/03 20:51:24 delete newline
pedrosimonetti2 2012/08/03 22:02:39 Done.
+ } else {
+ shouldCallChromeSend = true;
+ console.warn('No callback data registered for message "%s".', message);
+ }
+
+ shouldCallChromeSend = shouldCallChromeSend || shouldRegisterData;
+ if (shouldCallChromeSend) {
+ if (__chrome__ && __chrome__.send) {
+ __chrome__.send(message);
+ }
+ }
+ },
+ };
+
+ //----------------------------------------------------------------------------
+ // ChromeMock initialization
+ //----------------------------------------------------------------------------
+
+ if (shouldRegisterData) {
+ interceptLoadData();
+ }
+
+ window.chrome = ChromeMock;
+})();

Powered by Google App Engine
This is Rietveld 408576698