blob: 83a1c7e9caa95d0a5b80741cff43d02186fb35d5 [file] [log] [blame]
<!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">
<script>void(0);</script>
<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.change = {};
element.changeNum = '42';
element.patchNum = '2';
element.actions = {
'/': {
method: 'DELETE',
label: 'Delete',
title: 'Delete draft change 42',
enabled: true,
},
};
return element.reload();
});
test('_shouldHideActions', function() {
assert.isTrue(element._shouldHideActions(undefined, true));
assert.isTrue(element._shouldHideActions({base: {}}, false));
assert.isFalse(element._shouldHideActions({base: ['test']}, false));
});
test('hide revision action', function(done) {
flush(function() {
var buttonEl = element.$$('[data-action-key="submit"]');
assert.isOk(buttonEl);
assert.throws(element.setActionHidden.bind(element, 'invalid type'));
element.setActionHidden(element.ActionType.REVISION,
element.RevisionActions.SUBMIT, true);
assert.lengthOf(element._hiddenActions, 1);
element.setActionHidden(element.ActionType.REVISION,
element.RevisionActions.SUBMIT, true);
assert.lengthOf(element._hiddenActions, 1);
flush(function() {
var buttonEl = element.$$('[data-action-key="submit"]');
assert.isNotOk(buttonEl);
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 menu action', function(done) {
flush(function() {
var buttonEl = element.$.moreActions.$$('span[data-id="delete"]');
assert.isOk(buttonEl);
assert.throws(element.setActionHidden.bind(element, 'invalid type'));
element.setActionHidden(element.ActionType.CHANGE,
element.ChangeActions.DELETE, true);
assert.lengthOf(element._hiddenActions, 1);
element.setActionHidden(element.ActionType.CHANGE,
element.ChangeActions.DELETE, true);
assert.lengthOf(element._hiddenActions, 1);
flush(function() {
var buttonEl = element.$.moreActions.$$('span[data-id="delete"]');
assert.isNotOk(buttonEl);
element.setActionHidden(element.ActionType.CHANGE,
element.RevisionActions.DELETE, false);
flush(function() {
var buttonEl = element.$.moreActions.$$('span[data-id="delete"]');
assert.isOk(buttonEl);
done();
});
});
});
});
test('buttons exist', function(done) {
element._loading = false;
flush(function() {
var buttonEls = Polymer.dom(element.root)
.querySelectorAll('gr-button');
var menuItems = element.$.moreActions.items;
assert.equal(buttonEls.length + menuItems.length, 6);
assert.isFalse(element.hidden);
done();
});
});
test('delete buttons have explicit labels', function(done) {
flush(function() {
var deleteItems = element.$.moreActions.items.filter(function(item) {
return item.id === 'delete';
});
assert.equal(deleteItems.length, 2);
assert.notEqual(deleteItems[0].name, deleteItems[1].name);
assert.isTrue(
deleteItems[0].name === 'Delete Revision' ||
deleteItems[0].name === 'Delete Change'
);
assert.isTrue(
deleteItems[1].name === 'Delete Revision' ||
deleteItems[1].name === '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('_actionComparator sort order', function() {
var actions = [
{label: '123', __type: 'change', __key: 'review'},
{label: 'abc', __type: 'revision'},
{label: 'abc', __type: 'change'},
{label: 'def', __type: 'change'},
{label: 'def', __type: 'change', __primary: true},
];
var result = actions.slice();
result.reverse();
result.sort(element._actionComparator);
assert.deepEqual(result, actions);
});
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('chain state', function() {
assert.equal(element._hasKnownChainState, false);
element.hasParent = true;
assert.equal(element._hasKnownChainState, true);
element.hasParent = false;
});
test('_calculateDisabled', function() {
var hasKnownChainState = false;
var action = {__key: 'rebase', enabled: true};
assert.equal(
element._calculateDisabled(action, hasKnownChainState), true);
action.__key = 'delete';
assert.equal(
element._calculateDisabled(action, hasKnownChainState), false);
action.__key = 'rebase';
hasKnownChainState = true;
assert.equal(
element._calculateDisabled(action, hasKnownChainState), false);
action.enabled = false;
assert.equal(
element._calculateDisabled(action, hasKnownChainState), true);
});
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',
};
// rebase on other
element.$.confirmRebase.base = '1234';
element._handleRebaseConfirm();
assert.deepEqual(fireActionStub.lastCall.args,
['/rebase', rebaseAction, true, {base: '1234'}]);
// rebase on parent
element.$.confirmRebase.base = null;
element._handleRebaseConfirm();
assert.deepEqual(fireActionStub.lastCall.args,
['/rebase', rebaseAction, true, {base: null}]);
// rebase on tip
element.$.confirmRebase.base = '';
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) {
element._hasKnownChainState = true;
flush(function() {
var rebaseButton = element.$$('gr-button[data-action-key="rebase"]');
assert.ok(rebaseButton);
MockInteractions.tap(rebaseButton);
flushAsynchronousOperations();
assert.isFalse(element.$.confirmRebase.hidden);
element._handleCherrypickTap();
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() {
element._handleCherrypickTap();
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.
// Add attributes that are used to determine the message.
element.$.confirmCherrypick.commitMessage = 'foo message';
element.$.confirmCherrypick.changeStatus = 'OPEN';
element.$.confirmCherrypick.commitNum = '123';
element._handleCherrypickConfirm();
assert.equal(element.$.confirmCherrypick.$.messageInput.value,
'foo message');
assert.deepEqual(fireActionStub.lastCall.args, [
'/cherrypick', action, true, {
destination: 'master',
message: 'foo message',
},
]);
});
test('branch name cleared when re-open cherrypick', function() {
var emptyBranchName = '';
element.$.confirmCherrypick.branch = 'master';
element._handleCherrypickTap();
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 + '"]'));
});
});
test('_setLoadingOnButtonWithKey top-level', function() {
var key = 'rebase';
var cleanup = element._setLoadingOnButtonWithKey(key);
assert.equal(element._actionLoadingMessage, 'Rebasing...');
var button = element.$$('[data-action-key="' + key + '"]');
assert.isTrue(button.hasAttribute('loading'));
assert.isTrue(button.disabled);
assert.isOk(cleanup);
assert.isFunction(cleanup);
cleanup();
assert.isFalse(button.hasAttribute('loading'));
assert.isFalse(button.disabled);
assert.isNotOk(element._actionLoadingMessage);
});
test('_setLoadingOnButtonWithKey overflow menu', function() {
var key = 'cherrypick';
var cleanup = element._setLoadingOnButtonWithKey(key);
assert.equal(element._actionLoadingMessage, 'Cherry-Picking...');
assert.include(element._disabledMenuActions, 'cherrypick');
assert.isFunction(cleanup);
cleanup();
assert.notOk(element._actionLoadingMessage);
assert.notInclude(element._disabledMenuActions, 'cherrypick');
});
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;
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() {
element._handleDeleteTap();
assert.isFalse(fireActionStub.called);
});
test('shows confirm dialog', function() {
element._handleDeleteTap();
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() {
element._handleDeleteTap();
MockInteractions.tap(
element.$$('#confirmDeleteDialog').$$('gr-button:not([primary])'));
flushAsynchronousOperations();
assert.isTrue(element.$$('#confirmDeleteDialog').hidden);
assert.isFalse(fireActionStub.called);
});
});
suite('quick approve', function() {
setup(function() {
element.change = {
current_revision: 'abc1234',
};
element.change = {
current_revision: 'abc1234',
labels: {
foo: {
values: {
'-1': '',
' 0': '',
'+1': '',
},
},
},
permitted_labels: {
foo: ['-1', ' 0', '+1'],
},
};
flushAsynchronousOperations();
});
test('added when can approve', function() {
var approveButton = element.$$('gr-button[data-action-key=\'review\']');
assert.isNotNull(approveButton);
});
test('is first in list of actions', function() {
var approveButton = element.$$('gr-button');
assert.equal(approveButton.getAttribute('data-label'), 'foo+1');
});
test('not added when already approved', function() {
element.change = {
current_revision: 'abc1234',
labels: {
foo: {
approved: {},
values: {},
},
},
permitted_labels: {
foo: [' 0', '+1'],
},
};
flushAsynchronousOperations();
var approveButton = element.$$('gr-button[data-action-key=\'review\']');
assert.isNull(approveButton);
});
test('not added when label not permitted', function() {
element.change = {
current_revision: 'abc1234',
labels: {
foo: {values: {}},
},
permitted_labels: {
bar: [],
},
};
flushAsynchronousOperations();
var approveButton = element.$$('gr-button[data-action-key=\'review\']');
assert.isNull(approveButton);
});
test('approves when taped', function() {
var fireActionStub = sinon.stub(element, '_fireAction');
MockInteractions.tap(
element.$$('gr-button[data-action-key=\'review\']'));
flushAsynchronousOperations();
assert.isTrue(fireActionStub.called);
assert.isTrue(fireActionStub.calledWith('/review'));
var payload = fireActionStub.lastCall.args[3];
assert.deepEqual(payload.labels, {foo: '+1'});
fireActionStub.restore();
});
test('not added when multiple labels are required', function() {
element.change = {
current_revision: 'abc1234',
labels: {
foo: {values: {}},
bar: {values: {}},
},
permitted_labels: {
foo: [' 0', '+1'],
bar: [' 0', '+1', '+2'],
},
};
flushAsynchronousOperations();
var approveButton = element.$$('gr-button[data-action-key=\'review\']');
assert.isNull(approveButton);
});
test('button label for missing approval', function() {
element.change = {
current_revision: 'abc1234',
labels: {
foo: {
values: {
' 0': '',
'+1': '',
},
},
bar: {approved: {}, values: {}},
},
permitted_labels: {
foo: [' 0', '+1'],
bar: [' 0', '+1', '+2'],
},
};
flushAsynchronousOperations();
var approveButton = element.$$('gr-button[data-action-key=\'review\']');
assert.equal(approveButton.getAttribute('data-label'), 'foo+1');
});
test('no quick approve if score is not maximal for a label', function() {
element.change = {
current_revision: 'abc1234',
labels: {
bar: {
value: 1,
values: {
' 0': '',
'+1': '',
'+2': '',
},
},
},
permitted_labels: {
bar: [' 0', '+1'],
},
};
flushAsynchronousOperations();
var approveButton = element.$$('gr-button[data-action-key=\'review\']');
assert.isNull(approveButton);
});
test('approving label with a non-max score', function() {
element.change = {
current_revision: 'abc1234',
labels: {
bar: {
value: 1,
values: {
' 0': '',
'+1': '',
'+2': '',
},
},
},
permitted_labels: {
bar: [' 0', '+1', '+2'],
},
};
flushAsynchronousOperations();
var approveButton = element.$$('gr-button[data-action-key=\'review\']');
assert.equal(approveButton.getAttribute('data-label'), 'bar+2');
});
});
});
</script>