Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 1158238 - [System] Add import activity
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGrandon committed Apr 27, 2015
1 parent d21634a commit 6288639
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/system/index.html
Expand Up @@ -136,6 +136,7 @@
<script defer src="js/screen_brightness_transition.js"></script>
<script defer src="js/screen_manager.js"></script>
<script defer src="js/activities.js"></script>
<script defer src="js/import.js"></script>
<script defer src="js/usb_storage.js"></script>
<script defer src="js/external_storage_monitor.js"></script>
<script defer src="js/browser.js"></script>
Expand Down
4 changes: 3 additions & 1 deletion apps/system/js/bootstrap.js
@@ -1,7 +1,7 @@
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

/*global ActivityWindowManager, Browser, SecureWindowFactory,
/*global ActivityWindowManager, Browser, SecureWindowFactory, Import,
SecureWindowManager, HomescreenLauncher, HomescreenWindowManager,
FtuLauncher, SourceView, ScreenManager, Places, Activities,
DeveloperHUD, DialerAgent, RemoteDebugger, HomeGesture,
Expand Down Expand Up @@ -245,3 +245,5 @@ window.browser = new Browser();
window.browser.start();
window.browserSettings = new BrowserSettings();
window.browserSettings.start();
window.import = new Import();
window.import.start();
55 changes: 55 additions & 0 deletions apps/system/js/import.js
@@ -0,0 +1,55 @@
'use strict';

(function(exports) {

function Import() {}

/**
* Opens a new private window.
* @param {String} url The url to navigate to
*/
Import.prototype = {

/**
* Starts listening to activities.
*/
start: function() {
window.navigator.mozSetMessageHandler('activity',
this.handleActivity.bind(this));
},

/**
* Handles activity messages.
*/
handleActivity: function(activity) {
var name = activity.source.name;
switch (name) {
case 'import':
this.importApp(activity);
break;
}
},

/**
* Imports an application into the system.
*/
importApp: function(activity) {
var blob = activity.source.data.blob;
navigator.mozApps.mgmt.import(blob)
.then((addon) => {
// Enable the addon by default.
navigator.mozApps.mgmt.setEnabled(addon, true);

activity.postResult({
manifestURL: addon.manifestURL
});
})
.catch((error) => {
activity.postError(error);
});
}
};

exports.Import = Import;

}(window));
10 changes: 10 additions & 0 deletions apps/system/manifest.webapp
Expand Up @@ -76,6 +76,16 @@
"284": "/style/icons/system_284.png"
},
"activities": {
"import": {
"filters": {
"type": "app",
"blob": {
"required": true
}
},
"disposition": "inline",
"returnValue": true
},
"view": {
"filters": {
"type": "url",
Expand Down
Binary file not shown.
78 changes: 78 additions & 0 deletions apps/system/test/marionette/import_app_test.js
@@ -0,0 +1,78 @@
'use strict';
/* global MozActivity */

var assert = require('assert');
var Server = require('../../../../shared/test/integration/server');

marionette('Import App', function() {

var client = marionette.client();

var home, server, system;

suiteSetup(function(done) {
Server.create(__dirname + '/fixtures/', function(err, _server) {
server = _server;
done();
});
});

suiteTeardown(function() {
server.stop();
});

setup(function() {
system = client.loader.getAppClass('system');
home = client.loader.getAppClass('verticalhome');
system.waitForStartup();
home.waitForLaunch();
});

function countApps() {
return client.executeAsyncScript(function() {
navigator.mozApps.mgmt.getAll().onsuccess = function(event) {
marionetteScriptFinished(event.target.result.length);
};
});
}

test('imports an app into the system', function() {
var numApps = countApps();
var appZipUrl = server.url('simple_addon.zip');

// Fire the import activity from the home screen.
client.executeAsyncScript(function(appZipUrl) {

// Execute an XHR to get the addon blob content.
var xhr = new XMLHttpRequest({mozSystem: true});
xhr.open('GET', appZipUrl, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
sendActivity(this.response);
}
};
xhr.onerror = function(e) {
console.log('Error fetching app zip.');
};
xhr.send();

// Send a moz activity to the system app with the blob.
function sendActivity(blob) {
var activity = new MozActivity({
name: 'import',
data: {
type: 'app',
blob: blob
}
});
activity.onsuccess = function() {
marionetteScriptFinished();
};
}
}, [appZipUrl]);

// We've installed an app, so assert that we find the app in mozApps.mgmt.
assert.equal(numApps + 1, countApps());
});
});
1 change: 1 addition & 0 deletions apps/system/test/unit/bootstrap_test.js
Expand Up @@ -42,6 +42,7 @@ requireApp('system/js/global_overlay_window_manager.js');
requireApp('system/js/rocketbar.js');
requireApp('system/js/home_gesture.js');
requireApp('system/js/homescreen_launcher.js');
requireApp('system/js/import.js');
requireApp('system/js/internet_sharing.js');
requireApp('system/js/input_window_manager.js');
requireApp('system/js/layout_manager.js');
Expand Down

0 comments on commit 6288639

Please sign in to comment.