Chromium Code Reviews| Index: chrome/test/data/chromeos/file_manager/share_dialog_mock/main.js |
| diff --git a/chrome/test/data/chromeos/file_manager/share_dialog_mock/main.js b/chrome/test/data/chromeos/file_manager/share_dialog_mock/main.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..315261af44eae998114ec3d45a3e5434e93b76a5 |
| --- /dev/null |
| +++ b/chrome/test/data/chromeos/file_manager/share_dialog_mock/main.js |
| @@ -0,0 +1,151 @@ |
| +// Namespace for the share_dialog mock. |
| +var share_dialog = {}; |
|
yoshiki
2013/08/06 01:17:01
Name of namespace should be camelCase.
mtomasz
2013/08/06 02:02:35
Done.
|
| + |
| +/** |
| + * Origin of Files.app. |
| + * @type {string} |
| + * @const |
| + */ |
| +share_dialog.EMBEDDER_ORIGIN = |
| + 'chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj'; |
| + |
| +/** |
| + * Target width of the sharing window in pixels. |
| + * @type {number} |
| + * @const |
| + */ |
| +share_dialog.TARGET_WIDTH = 350; |
| + |
| +/** |
| + * Target height of the sharing window in pixels. |
| + * @type {number} |
| + * @const |
| + */ |
| +share_dialog.TARGET_HEIGHT = 250; |
| + |
| +/** |
| + * Target window of Files.app. Used to communicate over messages. Filled out |
| + * once the first message from the embedder arrives. |
| + * @type {Window} |
| + */ |
| +share_dialog.embedderTarget = null; |
| + |
| +/** |
| + * List of pending messages enqueued to be sent before establishing the target. |
| + * @type {Array.<Object>} |
| + */ |
| +share_dialog.pendingMessages = []; |
| + |
| +/** |
| + * Sends a message to the embedder. If the embedder target is not available, |
| + * then enqueues them. Such enqueued messages will be sent as soon as the target |
| + * is available. |
| + * |
| + * @param {string} type Message identifier |
| + * @param {Object=} opt_args Arguments for the message. |
| + * @private |
| + */ |
| +share_dialog.sendMessage_ = function(type, opt_args) { |
| + if (!share_dialog.embedderTarget) { |
| + share_dialog.pendingMessages.push({type: type, args: opt_args}); |
| + return; |
| + } |
| + |
| + var data = {}; |
| + data.type = type; |
| + if (opt_args) |
| + data.args = args; |
|
yoshiki
2013/08/06 01:17:01
opt_args?
mtomasz
2013/08/06 02:02:35
Oops! Done.
|
| + |
| + share_dialog.embedderTarget.postMessage(JSON.stringify(data), |
| + share_dialog.EMBEDDER_ORIGIN); |
|
yoshiki
2013/08/06 01:17:01
nit: indentation
mtomasz
2013/08/06 02:02:35
Done.
|
| +}; |
| + |
| +/** |
| + * Handles a request from the embedder to make the body visible. |
| + * @private |
| + */ |
| +share_dialog.onMakeBodyVisible_ = function() { |
| + document.body.style.display = ''; |
| +}; |
| + |
| +/** |
| + * Handles an event from the embedder than preparation to show the contents |
| + * is done. |
| + * @private |
| + */ |
| +share_dialog.onPrepareComplete_ = function() { |
| + share_dialog.resize(); |
| +}; |
| + |
| +/** |
| + * Handles an event from the embedder than preparation resize the window is |
| + * done. |
| + * @private |
| + */ |
| +share_dialog.onResizeComplete_ = function() { |
| + var container = document.querySelector('#container'); |
| + container.style.width = share_dialog.TARGET_WIDTH + 'px'; |
| + container.style.height = share_dialog.TARGET_HEIGHT + 'px'; |
| +}; |
| + |
| +/** |
| + * Prepares the embedder to make the contents visible. |
| + */ |
| +share_dialog.prepareForVisible = function() { |
| + share_dialog.sendMessage_('prepareForVisible'); |
| +}; |
| + |
| +/** |
| + * Resizes the embedder to the content window dimensions. |
| + */ |
| +share_dialog.resize = function() { |
| + share_dialog.sendMessage_('prepareForResize'); |
| +}; |
| + |
| +/** |
| + * Handles messages sent by the embedder. If it is the first message, then |
| + * the target is established and all enqueued messages to be sent to the |
| + * embedder are sent before handling the message from the embedder. |
| + * |
| + * @param {Event} message Message event. |
| + * @private |
| + */ |
| +share_dialog.onMessage_ = function(message) { |
| + if (message.origin != share_dialog.EMBEDDER_ORIGIN) |
| + return; |
| + |
| + if (!share_dialog.embedderTarget) { |
| + share_dialog.embedderTarget = message.source; |
| + for (var i = 0; i < share_dialog.pendingMessages.length; i++) { |
| + share_dialog.sendMessage_(share_dialog.pendingMessages[i].type, |
| + share_dialog.pendingMessages[i].args); |
|
yoshiki
2013/08/06 01:17:01
nit: indentation is off.
mtomasz
2013/08/06 02:02:35
Done.
|
| + } |
| + share_dialog.pendingMessages = []; |
| + } |
| + |
| + var packet = JSON.parse(message.data) |
| + var type = packet.type; |
| + var args = packet.args; |
| + |
| + switch (type) { |
| + case 'makeBodyVisible': |
| + share_dialog.onMakeBodyVisible_(args); |
| + break; |
| + case 'prepareComplete': |
| + share_dialog.onPrepareComplete_(args); |
| + break; |
| + case 'resizeComplete': |
| + share_dialog.onResizeComplete_(args); |
| + break; |
| + } |
| +}; |
| + |
| +/** |
| + * Initializes the mocked share dialog. |
| + */ |
| +share_dialog.initialize = function() { |
| + window.addEventListener('message', share_dialog.onMessage_); |
| + share_dialog.prepareForVisible(); |
| +}; |
| + |
| +window.addEventListener('load', share_dialog.initialize); |