blob: b3ec30f30a49db4f4e819d7ea0f924d1ed6d204c [file] [log] [blame]
<!DOCTYPE html>
<!--
@license
Copyright (C) 2019 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-api-interface</title>
<script src="/test/common-test-setup.js"></script>
<script src="/bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>
<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<script src="/bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-js-api-interface.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-js-api-interface></gr-js-api-interface>
</template>
</test-fixture>
<script>
suite('gr-gerrit tests', () => {
let element;
let plugin;
let sandbox;
let sendStub;
setup(() => {
sandbox = sinon.sandbox.create();
sendStub = sandbox.stub().returns(Promise.resolve({status: 200}));
stub('gr-rest-api-interface', {
getAccount() {
return Promise.resolve({name: 'Judy Hopps'});
},
send(...args) {
return sendStub(...args);
},
});
element = fixture('basic');
Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js');
Gerrit._setPluginsPending([]);
});
teardown(() => {
sandbox.restore();
element._removeEventCallbacks();
plugin = null;
});
test('reuse plugin for install calls', () => {
let otherPlugin;
Gerrit.install(p => { otherPlugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js');
assert.strictEqual(plugin, otherPlugin);
});
test('flushes preinstalls if provided', () => {
assert.doesNotThrow(() => {
Gerrit._flushPreinstalls();
});
window.Gerrit.flushPreinstalls = sandbox.stub();
Gerrit._flushPreinstalls();
assert.isTrue(window.Gerrit.flushPreinstalls.calledOnce);
delete window.Gerrit.flushPreinstalls;
});
test('versioning', () => {
const callback = sandbox.spy();
Gerrit.install(callback, '0.0pre-alpha');
assert(callback.notCalled);
});
test('_setPluginsCount', done => {
stub('gr-reporting', {
pluginsLoaded() {
done();
},
});
Gerrit._setPluginsCount(0);
});
test('_arePluginsLoaded', () => {
assert.isTrue(Gerrit._arePluginsLoaded());
Gerrit._setPluginsCount(1);
assert.isFalse(Gerrit._arePluginsLoaded());
Gerrit._setPluginsCount(0);
assert.isTrue(Gerrit._arePluginsLoaded());
});
test('_pluginInstalled', () => {
const pluginsLoadedStub = sandbox.stub();
stub('gr-reporting', {
pluginsLoaded: (...args) => pluginsLoadedStub(...args),
});
const plugins = [
'http://test.com/plugins/foo/static/test.js',
'http://test.com/plugins/bar/static/test.js',
];
Gerrit._setPluginsPending(plugins);
Gerrit._pluginInstalled(plugins[0]);
Gerrit._pluginInstalled(plugins[1]);
assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo', 'bar']));
});
test('install calls _pluginInstalled', () => {
sandbox.stub(Gerrit, '_pluginInstalled');
Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js');
// testplugin has already been installed once (in setup).
assert.isFalse(Gerrit._pluginInstalled.called);
// testplugin2 plugin has not yet been installed.
Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin2/static/test.js');
assert.isTrue(Gerrit._pluginInstalled.calledOnce);
});
test('plugin install errors mark plugins as loaded', () => {
Gerrit._setPluginsCount(1);
Gerrit.install(() => {}, '0.0pre-alpha');
return Gerrit.awaitPluginsLoaded();
});
test('multiple ui plugins per java plugin', () => {
const file1 = 'http://test.com/plugins/qaz/static/foo.nocache.js';
const file2 = 'http://test.com/plugins/qaz/static/bar.js';
Gerrit._setPluginsPending([file1, file2]);
Gerrit.install(() => {}, '0.1', file1);
Gerrit.install(() => {}, '0.1', file2);
return Gerrit.awaitPluginsLoaded();
});
test('plugin install errors shows toasts', () => {
const alertStub = sandbox.stub();
document.addEventListener('show-alert', alertStub);
Gerrit._setPluginsCount(1);
Gerrit.install(() => {}, '0.0pre-alpha');
return Gerrit.awaitPluginsLoaded().then(() => {
assert.isTrue(alertStub.calledOnce);
});
});
test('Gerrit._isPluginPreloaded', () => {
Gerrit._preloadedPlugins = {baz: ()=>{}};
assert.isFalse(Gerrit._isPluginPreloaded('plugins/foo/bar'));
assert.isFalse(Gerrit._isPluginPreloaded('http://a.com/42'));
assert.isTrue(Gerrit._isPluginPreloaded('preloaded:baz'));
Gerrit._preloadedPlugins = null;
});
test('preloaded plugins are installed', () => {
const installStub = sandbox.stub();
Gerrit._preloadedPlugins = {foo: installStub};
Gerrit._installPreloadedPlugins();
assert.isTrue(installStub.called);
const pluginApi = installStub.lastCall.args[0];
assert.strictEqual(pluginApi.getPluginName(), 'foo');
});
test('installing preloaded plugin', () => {
let plugin;
window.ASSETS_PATH = 'http://blips.com/chitz';
Gerrit.install(p => { plugin = p; }, '0.1', 'preloaded:foo');
assert.strictEqual(plugin.getPluginName(), 'foo');
assert.strictEqual(plugin.url('/some/thing.html'),
'http://blips.com/chitz/plugins/foo/some/thing.html');
delete window.ASSETS_PATH;
});
});
</script>