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

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: Created 8 years, 5 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..81ce1573495d94998f29af86a8c242cf9d0eec81
--- /dev/null
+++ b/chrome/browser/resources/ntp_search/mock/mock.js
@@ -0,0 +1,187 @@
+// 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.
+
+var __chrome__ = chrome;
+var chrome = (function() {
+
+ var shouldRegisterData = !!window.chrome && !!window.chrome.send;
+
+ var callbackMap = {
+ 'metricsHandler:logEventTime': 1,
+ 'metricsHandler:recordInHistogram': 1,
+ 'getApps': 'ntp.getAppsCallback',
+ 'getRecentlyClosedTabs': 'ntp.setRecentlyClosedTabs',
+ 'getMostVisited': 'ntp.setMostVisitedPages'
+ };
+
+ // TODO include automatically in the recorded data
+ var mockedThumbnailUrls = [
+ "http---www.wikipedia.org-",
+ "http---www.deviantart.com-",
+ "http---wefunkradio.com-",
+ "http---youtube.com-",
+ "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) {
jeremycho_google 2012/08/02 03:00:49 if (index != -1)
pedrosimonetti2 2012/08/03 18:14:12 Done.
+ // Remove an element from a particular index.
+ mockUrl = thumbnailUrlList.splice(index, 0);
jeremycho_google 2012/08/02 03:00:49 This doesn't remove anything. splice(index, 1) in
pedrosimonetti2 2012/08/03 18:14:12 You're right. The bug wasn't visible for me becaus
+ } 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 = {
+
+ mock: function(newDataMap) {
+ if (newDataMap) {
+ dataMap = newDataMap;
+ if (!shouldRegisterData) {
+ mockLoadData();
+ }
+ } else {
+ return recordedDataMap;
+ }
+ },
+
+ send: function() {
+ //console.log(message, arguments.length, arguments);
+ if (!isInitialized) {
+ initialize();
+ }
+
+ var message = arguments[0];
+ var shouldCallChromeSend = false;
+
+ var data;
+ var callback;
+ var callbackNamespace;
+
+ if (callbackMap.hasOwnProperty(message)) {
+
+ callbackNamespace = callbackMap[message];
+
+ if (shouldRegisterData) {
+ if (callbackNamespace !== 1) {
+ interceptCallback(message, callbackNamespace);
+ }
+ } else {
+ if (dataMap.hasOwnProperty(message)) {
+ data = dataMap[message];
+ callback = namespace(callbackNamespace);
+ setTimeout(function() {
+ callback.apply(window, data);
+ }, 0);
+ } else {
+ if (callbackNamespace !== 1) {
+ console.warn('No mock registered for message "%s".', message);
+ }
+ }
+ }
+
+ } 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();
+ }
+
+ return ChromeMock;
+})();
+
jeremycho_google 2012/08/02 03:00:49 Extra newline.
pedrosimonetti2 2012/08/03 18:14:12 Done.

Powered by Google App Engine
This is Rietveld 408576698