493 lines
16 KiB
HTML
493 lines
16 KiB
HTML
<!DOCTYPE html>
|
|
<!--
|
|
Copyright (C) 2016 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-change-actions</title>
|
|
|
|
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
|
|
<script src="../../../bower_components/web-component-tester/browser.js"></script>
|
|
<script src="../../../scripts/util.js"></script>
|
|
|
|
<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
|
|
<link rel="import" href="gr-change-actions.html">
|
|
|
|
<test-fixture id="basic">
|
|
<template>
|
|
<gr-change-actions></gr-change-actions>
|
|
</template>
|
|
</test-fixture>
|
|
|
|
<script>
|
|
suite('gr-change-actions tests', function() {
|
|
var element;
|
|
setup(function() {
|
|
stub('gr-rest-api-interface', {
|
|
getChangeRevisionActions: function() {
|
|
return Promise.resolve({
|
|
'/': {
|
|
method: 'DELETE',
|
|
label: 'Delete',
|
|
title: 'Delete draft revision 2',
|
|
enabled: true,
|
|
},
|
|
cherrypick: {
|
|
method: 'POST',
|
|
label: 'Cherry Pick',
|
|
title: 'Cherry pick change to a different branch',
|
|
enabled: true,
|
|
},
|
|
rebase: {
|
|
method: 'POST',
|
|
label: 'Rebase',
|
|
title: 'Rebase onto tip of branch or parent change',
|
|
enabled: true,
|
|
},
|
|
submit: {
|
|
method: 'POST',
|
|
label: 'Submit',
|
|
title: 'Submit patch set 2 into master',
|
|
enabled: true,
|
|
},
|
|
});
|
|
},
|
|
send: function(method, url, payload) {
|
|
if (method !== 'POST') { return Promise.reject('bad method'); }
|
|
|
|
if (url === '/changes/42/revisions/2/submit') {
|
|
return Promise.resolve({
|
|
ok: true,
|
|
text: function() { return Promise.resolve(')]}\'\n{}'); },
|
|
});
|
|
} else if (url === '/changes/42/revisions/2/rebase') {
|
|
return Promise.resolve({
|
|
ok: true,
|
|
text: function() { return Promise.resolve(')]}\'\n{}'); },
|
|
});
|
|
}
|
|
|
|
return Promise.reject('bad url');
|
|
},
|
|
});
|
|
|
|
element = fixture('basic');
|
|
element.changeNum = '42';
|
|
element.patchNum = '2';
|
|
element.actions = {
|
|
'/': {
|
|
method: 'DELETE',
|
|
label: 'Delete',
|
|
title: 'Delete draft change 42',
|
|
enabled: true
|
|
},
|
|
};
|
|
return element.reload();
|
|
});
|
|
|
|
test('hide revision action', function(done) {
|
|
flush(function() {
|
|
var buttonEl = element.$$('[data-action-key="submit"]');
|
|
assert.isOk(buttonEl);
|
|
assert.isFalse(buttonEl.hasAttribute('hidden'));
|
|
assert.throws(element.setActionHidden.bind(element, 'invalid type'));
|
|
element.setActionHidden(element.ActionType.REVISION,
|
|
element.RevisionActions.SUBMIT, true);
|
|
assert.lengthOf(element._hiddenRevisionActions, 1);
|
|
element.setActionHidden(element.ActionType.REVISION,
|
|
element.RevisionActions.SUBMIT, true);
|
|
assert.lengthOf(element._hiddenRevisionActions, 1);
|
|
flush(function() {
|
|
var buttonEl = element.$$('[data-action-key="submit"]');
|
|
assert.isOk(buttonEl);
|
|
assert.isTrue(buttonEl.hasAttribute('hidden'));
|
|
|
|
element.setActionHidden(element.ActionType.REVISION,
|
|
element.RevisionActions.SUBMIT, false);
|
|
flush(function() {
|
|
var buttonEl = element.$$('[data-action-key="submit"]');
|
|
assert.isOk(buttonEl);
|
|
assert.isFalse(buttonEl.hasAttribute('hidden'));
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
test('hide change action', function(done) {
|
|
flush(function() {
|
|
var buttonEl = element.$$('[data-action-key="/"]');
|
|
assert.isOk(buttonEl);
|
|
assert.isFalse(buttonEl.hasAttribute('hidden'));
|
|
assert.throws(element.setActionHidden.bind(element, 'invalid type'));
|
|
element.setActionHidden(element.ActionType.CHANGE,
|
|
element.ChangeActions.DELETE, true);
|
|
assert.lengthOf(element._hiddenChangeActions, 1);
|
|
element.setActionHidden(element.ActionType.CHANGE,
|
|
element.ChangeActions.DELETE, true);
|
|
assert.lengthOf(element._hiddenChangeActions, 1);
|
|
flush(function() {
|
|
var buttonEl = element.$$('[data-action-key="/"]');
|
|
assert.isOk(buttonEl);
|
|
assert.isTrue(buttonEl.hasAttribute('hidden'));
|
|
|
|
element.setActionHidden(element.ActionType.CHANGE,
|
|
element.RevisionActions.DELETE, false);
|
|
flush(function() {
|
|
var buttonEl = element.$$('[data-action-key="/"]');
|
|
assert.isOk(buttonEl);
|
|
assert.isFalse(buttonEl.hasAttribute('hidden'));
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
test('buttons show', function(done) {
|
|
flush(function() {
|
|
var buttonEls = Polymer.dom(element.root).querySelectorAll('gr-button');
|
|
assert.equal(buttonEls.length, 5);
|
|
assert.isFalse(element.hidden);
|
|
done();
|
|
});
|
|
});
|
|
|
|
test('delete buttons have explicit labels', function(done) {
|
|
flush(function() {
|
|
var buttonEls =
|
|
Polymer.dom(element.root).querySelectorAll('[data-action-key="/"]');
|
|
assert.equal(buttonEls.length, 2);
|
|
assert.notEqual(buttonEls[0].getAttribute('data-label'),
|
|
buttonEls[1].getAttribute['data-label']);
|
|
assert.isTrue(
|
|
buttonEls[0].getAttribute('data-label') === 'Delete Revision' ||
|
|
buttonEls[0].getAttribute('data-label') === 'Delete Change'
|
|
);
|
|
assert.isTrue(
|
|
buttonEls[1].getAttribute('data-label') === 'Delete Revision' ||
|
|
buttonEls[1].getAttribute('data-label') === 'Delete Change'
|
|
);
|
|
done();
|
|
});
|
|
});
|
|
|
|
test('get revision object from change', function() {
|
|
var revObj = {_number: 2, foo: 'bar'};
|
|
var change = {
|
|
revisions: {
|
|
rev1: {_number: 1},
|
|
rev2: revObj,
|
|
},
|
|
};
|
|
assert.deepEqual(element._getRevision(change, '2'), revObj);
|
|
});
|
|
|
|
test('submit change', function(done) {
|
|
element.change = {
|
|
revisions: {
|
|
rev1: {_number: 1},
|
|
rev2: {_number: 2},
|
|
},
|
|
};
|
|
element.patchNum = '2';
|
|
|
|
flush(function() {
|
|
var submitButton = element.$$('gr-button[data-action-key="submit"]');
|
|
assert.ok(submitButton);
|
|
MockInteractions.tap(submitButton);
|
|
|
|
// Upon success it should fire the reload-change event.
|
|
element.addEventListener('reload-change', function(e) {
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
|
|
test('submit change with plugin hook', function(done) {
|
|
var canSubmitStub = sinon.stub(element, '_canSubmitChange',
|
|
function() { return false; });
|
|
var fireActionStub = sinon.stub(element, '_fireAction');
|
|
flush(function() {
|
|
var submitButton = element.$$('gr-button[data-action-key="submit"]');
|
|
assert.ok(submitButton);
|
|
MockInteractions.tap(submitButton);
|
|
assert.equal(fireActionStub.callCount, 0);
|
|
|
|
canSubmitStub.restore();
|
|
fireActionStub.restore();
|
|
done();
|
|
});
|
|
});
|
|
|
|
test('rebase change', function(done) {
|
|
var fireActionStub = sinon.stub(element, '_fireAction');
|
|
flush(function() {
|
|
var rebaseButton = element.$$('gr-button[data-action-key="rebase"]');
|
|
MockInteractions.tap(rebaseButton);
|
|
var rebaseAction = {
|
|
__key: 'rebase',
|
|
__type: 'revision',
|
|
__primary: false,
|
|
enabled: true,
|
|
label: 'Rebase',
|
|
method: 'POST',
|
|
title: 'Rebase onto tip of branch or parent change',
|
|
};
|
|
element.$.confirmRebase.base = '1234';
|
|
element._handleRebaseConfirm();
|
|
assert.deepEqual(fireActionStub.lastCall.args,
|
|
['/rebase', rebaseAction, true, {base: '1234'}]);
|
|
|
|
element.$.confirmRebase.base = '';
|
|
element._handleRebaseConfirm();
|
|
assert.deepEqual(fireActionStub.lastCall.args,
|
|
['/rebase', rebaseAction, true, {}]);
|
|
|
|
element.$.confirmRebase.base = 'does not matter';
|
|
element.$.confirmRebase.clearParent = true;
|
|
element._handleRebaseConfirm();
|
|
assert.deepEqual(fireActionStub.lastCall.args,
|
|
['/rebase', rebaseAction, true, {base: ''}]);
|
|
|
|
fireActionStub.restore();
|
|
done();
|
|
});
|
|
});
|
|
|
|
test('two dialogs are not shown at the same time', function(done) {
|
|
flush(function() {
|
|
var rebaseButton = element.$$('gr-button[data-action-key="rebase"]');
|
|
assert.ok(rebaseButton);
|
|
MockInteractions.tap(rebaseButton);
|
|
flushAsynchronousOperations();
|
|
assert.isFalse(element.$.confirmRebase.hidden);
|
|
|
|
var cherryPickButton =
|
|
element.$$('gr-button[data-action-key="cherrypick"]');
|
|
assert.ok(cherryPickButton);
|
|
MockInteractions.tap(cherryPickButton);
|
|
flushAsynchronousOperations();
|
|
assert.isTrue(element.$.confirmRebase.hidden);
|
|
assert.isFalse(element.$.confirmCherrypick.hidden);
|
|
done();
|
|
});
|
|
});
|
|
|
|
suite('cherry-pick', function() {
|
|
var fireActionStub;
|
|
var alertStub;
|
|
|
|
setup(function() {
|
|
fireActionStub = sinon.stub(element, '_fireAction');
|
|
alertStub = sinon.stub(window, 'alert');
|
|
});
|
|
|
|
teardown(function() {
|
|
alertStub.restore();
|
|
fireActionStub.restore();
|
|
});
|
|
|
|
test('works', function() {
|
|
var cherryPickButton =
|
|
element.$$('gr-button[data-action-key="cherrypick"]');
|
|
MockInteractions.tap(cherryPickButton);
|
|
var action = {
|
|
__key: 'cherrypick',
|
|
__type: 'revision',
|
|
__primary: false,
|
|
enabled: true,
|
|
label: 'Cherry Pick',
|
|
method: 'POST',
|
|
title: 'Cherry pick change to a different branch',
|
|
};
|
|
|
|
element._handleCherrypickConfirm();
|
|
assert.equal(fireActionStub.callCount, 0);
|
|
|
|
element.$.confirmCherrypick.branch = 'master';
|
|
element._handleCherrypickConfirm();
|
|
assert.equal(fireActionStub.callCount, 0); // Still needs a message.
|
|
|
|
element.$.confirmCherrypick.message = 'foo message';
|
|
element._handleCherrypickConfirm();
|
|
|
|
assert.deepEqual(fireActionStub.lastCall.args, [
|
|
'/cherrypick', action, true, {
|
|
destination: 'master',
|
|
message: 'foo message',
|
|
}
|
|
]);
|
|
});
|
|
|
|
test('branch name cleared when re-open cherrypick', function() {
|
|
var cherryPickButton =
|
|
element.$$('gr-button[data-action-key="cherrypick"]');
|
|
var emptyBranchName = '';
|
|
element.$.confirmCherrypick.branch = 'master';
|
|
|
|
MockInteractions.tap(cherryPickButton);
|
|
assert.equal(element.$.confirmCherrypick.branch, emptyBranchName);
|
|
});
|
|
});
|
|
|
|
test('custom actions', function(done) {
|
|
// Add a button with the same key as a server-based one to ensure
|
|
// collisions are taken care of.
|
|
var key = element.addActionButton(element.ActionType.REVISION, 'Bork!');
|
|
element.addEventListener(key + '-tap', function(e) {
|
|
assert.equal(e.detail.node.getAttribute('data-action-key'), key);
|
|
element.removeActionButton(key);
|
|
flush(function() {
|
|
assert.notOk(element.$$('[data-action-key="' + key + '"]'));
|
|
done();
|
|
});
|
|
});
|
|
flush(function() {
|
|
MockInteractions.tap(element.$$('[data-action-key="' + key + '"]'));
|
|
});
|
|
});
|
|
|
|
suite('revert change', function() {
|
|
var alertStub;
|
|
var fireActionStub;
|
|
|
|
setup(function() {
|
|
fireActionStub = sinon.stub(element, '_fireAction');
|
|
alertStub = sinon.stub(window, 'alert');
|
|
element.actions = {
|
|
revert: {
|
|
method: 'POST',
|
|
label: 'Revert',
|
|
title: 'Revert the change',
|
|
enabled: true
|
|
}
|
|
};
|
|
return element.reload();
|
|
});
|
|
|
|
teardown(function() {
|
|
alertStub.restore();
|
|
fireActionStub.restore();
|
|
});
|
|
|
|
test('revert change with plugin hook', function(done) {
|
|
element.change = {
|
|
current_revision: 'abc1234',
|
|
};
|
|
var newRevertMsg = 'Modified revert msg';
|
|
var modifyRevertMsgStub = sinon.stub(element, '_modifyRevertMsg',
|
|
function() { return newRevertMsg; });
|
|
var populateRevertMsgStub = sinon.stub(
|
|
element.$.confirmRevertDialog, 'populateRevertMessage',
|
|
function() { return 'original msg'; });
|
|
flush(function() {
|
|
var revertButton = element.$$('gr-button[data-action-key="revert"]');
|
|
MockInteractions.tap(revertButton);
|
|
|
|
assert.equal(element.$.confirmRevertDialog.message, newRevertMsg);
|
|
|
|
populateRevertMsgStub.restore();
|
|
modifyRevertMsgStub.restore();
|
|
done();
|
|
});
|
|
});
|
|
|
|
test('works', function() {
|
|
element.change = {
|
|
current_revision: 'abc1234',
|
|
};
|
|
var populateRevertMsgStub = sinon.stub(
|
|
element.$.confirmRevertDialog, 'populateRevertMessage',
|
|
function() { return 'original msg'; });
|
|
var revertButton = element.$$('gr-button[data-action-key="revert"]');
|
|
MockInteractions.tap(revertButton);
|
|
|
|
element.$.confirmRevertDialog.message = 'foo message';
|
|
element._handleRevertDialogConfirm();
|
|
assert.notOk(alertStub.called);
|
|
|
|
var action = {
|
|
__key: 'revert',
|
|
__type: 'change',
|
|
__primary: false,
|
|
enabled: true,
|
|
label: 'Revert',
|
|
method: 'POST',
|
|
title: 'Revert the change',
|
|
};
|
|
assert.deepEqual(fireActionStub.lastCall.args, [
|
|
'/revert', action, false, {
|
|
message: 'foo message',
|
|
}]);
|
|
populateRevertMsgStub.restore();
|
|
});
|
|
});
|
|
|
|
suite('delete change', function() {
|
|
var fireActionStub;
|
|
var deleteAction;
|
|
|
|
var tapDeleteAction = function() {
|
|
var deleteButton = element.$$('gr-button[data-action-key=\'/\']');
|
|
MockInteractions.tap(deleteButton);
|
|
flushAsynchronousOperations();
|
|
};
|
|
|
|
setup(function() {
|
|
fireActionStub = sinon.stub(element, '_fireAction');
|
|
element.change = {
|
|
current_revision: 'abc1234',
|
|
};
|
|
deleteAction = {
|
|
method: 'DELETE',
|
|
label: 'Delete Change',
|
|
title: 'Delete change X_X',
|
|
enabled: true,
|
|
};
|
|
element.actions = {
|
|
'/': deleteAction,
|
|
};
|
|
});
|
|
|
|
teardown(function() {
|
|
fireActionStub.restore();
|
|
});
|
|
|
|
test('does not delete on action', function() {
|
|
tapDeleteAction();
|
|
assert.isFalse(fireActionStub.called);
|
|
});
|
|
|
|
test('shows confirm dialog', function() {
|
|
tapDeleteAction();
|
|
assert.isFalse(element.$$('#confirmDeleteDialog').hidden);
|
|
MockInteractions.tap(
|
|
element.$$('#confirmDeleteDialog').$$('gr-button[primary]'));
|
|
flushAsynchronousOperations();
|
|
assert.isTrue(fireActionStub.calledWith('/', deleteAction, false));
|
|
});
|
|
|
|
test('hides delete confirm on cancel', function() {
|
|
tapDeleteAction();
|
|
MockInteractions.tap(
|
|
element.$$('#confirmDeleteDialog').$$('gr-button:not([primary])'));
|
|
flushAsynchronousOperations();
|
|
assert.isTrue(element.$$('#confirmDeleteDialog').hidden);
|
|
assert.isFalse(fireActionStub.called);
|
|
});
|
|
});
|
|
});
|
|
</script>
|