This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1158238 - [System] Add import activity
- Loading branch information
1 parent
d21634a
commit 6288639
Showing
7 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters