blob: cdca3655bf28569672eea5ea56d70b5da23db27c [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-messages-list</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="gr-messages-list.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-messages-list></gr-messages-list>
</template>
</test-fixture>
<script>
var randomMessage = function(opt_params) {
var params = opt_params || {};
var author1 = {
_account_id: 1115495,
name: 'Andrew Bonventre',
email: 'andybons@chromium.org',
};
return {
id: params.id || Math.random().toString(),
date: params.date || '2016-01-12 20:28:33.038000',
message: params.message || Math.random().toString(),
_revision_number: params._revision_number || 1,
author: params.author || author1,
};
};
var randomAutomated = function(opt_params) {
return Object.assign({tag: 'autogenerated:gerrit:replace'},
randomMessage(opt_params));
};
suite('gr-messages-list tests', function() {
var element;
var messages;
var sandbox;
var getMessages = function() {
return Polymer.dom(element.root).querySelectorAll('gr-message');
};
setup(function() {
stub('gr-rest-api-interface', {
getConfig: function() { return Promise.resolve({}); },
getLoggedIn: function() { return Promise.resolve(false); },
});
sandbox = sinon.sandbox.create();
element = fixture('basic');
messages = _.times(3, randomMessage);
element.messages = messages;
flushAsynchronousOperations();
});
teardown(function() {
sandbox.restore();
});
test('show some old messages', function() {
assert.isTrue(element.$.messageControlsContainer.hasAttribute('hidden'));
element.messages = _.times(26, randomMessage);
flushAsynchronousOperations();
assert.isFalse(element.$.messageControlsContainer.hasAttribute('hidden'));
assert.equal(getMessages().length, 20);
assert.equal(element.$.incrementMessagesBtn.innerText,
'Show 5 more');
MockInteractions.tap(element.$.incrementMessagesBtn);
flushAsynchronousOperations();
assert.equal(getMessages().length, 25);
assert.equal(element.$.incrementMessagesBtn.innerText,
'Show 1 more');
MockInteractions.tap(element.$.incrementMessagesBtn);
flushAsynchronousOperations();
assert.isTrue(element.$.messageControlsContainer.hasAttribute('hidden'));
assert.equal(getMessages().length, 26);
});
test('show all old messages', function() {
assert.isTrue(element.$.messageControlsContainer.hasAttribute('hidden'));
element.messages = _.times(26, randomMessage);
flushAsynchronousOperations();
assert.isFalse(element.$.messageControlsContainer.hasAttribute('hidden'));
assert.equal(getMessages().length, 20);
assert.equal(element.$.oldMessagesBtn.innerText, 'Show all 6 messages');
MockInteractions.tap(element.$.oldMessagesBtn);
flushAsynchronousOperations();
assert.equal(getMessages().length, 26);
assert.isTrue(element.$.messageControlsContainer.hasAttribute('hidden'));
});
test('message count respects automated', function() {
element.messages = _.times(10, randomAutomated)
.concat(_.times(11, randomMessage));
flushAsynchronousOperations();
assert.equal(element.$.oldMessagesBtn.innerText, 'Show 1 message');
assert.isFalse(element.$.messageControlsContainer.hasAttribute('hidden'));
MockInteractions.tap(element.$.automatedMessageToggle);
flushAsynchronousOperations();
assert.isTrue(element.$.messageControlsContainer.hasAttribute('hidden'));
});
test('message count still respects non-automated on toggle', function() {
element.messages = _.times(10, randomMessage)
.concat(_.times(11, randomAutomated));
flushAsynchronousOperations();
assert.equal(element.$.oldMessagesBtn.innerText, 'Show 1 message');
assert.isFalse(element.$.messageControlsContainer.hasAttribute('hidden'));
MockInteractions.tap(element.$.automatedMessageToggle);
flushAsynchronousOperations();
assert.equal(element.$.oldMessagesBtn.innerText, 'Show 1 message');
assert.isFalse(element.$.messageControlsContainer.hasAttribute('hidden'));
});
test('show all messages respects expand', function() {
element.messages = _.times(10, randomAutomated)
.concat(_.times(11, randomMessage));
flushAsynchronousOperations();
MockInteractions.tap(element.$$('#collapse-messages')); // Expand all.
flushAsynchronousOperations();
var messages = getMessages();
assert.equal(messages.length, 20);
for (var i = 0; i < messages.length; i++) {
assert.isTrue(messages[i]._expanded);
}
MockInteractions.tap(element.$.oldMessagesBtn);
flushAsynchronousOperations();
messages = getMessages();
assert.equal(messages.length, 21);
for (var i = 0; i < messages.length; i++) {
assert.isTrue(messages[i]._expanded);
}
});
test('show all messages respects collapse', function() {
element.messages = _.times(10, randomAutomated)
.concat(_.times(11, randomMessage));
flushAsynchronousOperations();
MockInteractions.tap(element.$$('#collapse-messages')); // Expand all.
MockInteractions.tap(element.$$('#collapse-messages')); // Collapse all.
flushAsynchronousOperations();
var messages = getMessages();
assert.equal(messages.length, 20);
for (var i = 0; i < messages.length; i++) {
assert.isFalse(messages[i]._expanded);
}
MockInteractions.tap(element.$.oldMessagesBtn);
flushAsynchronousOperations();
messages = getMessages();
assert.equal(messages.length, 21);
for (var i = 0; i < messages.length; i++) {
assert.isFalse(messages[i]._expanded);
}
});
test('expand/collapse all', function() {
var allMessageEls = getMessages();
for (var i = 0; i < allMessageEls.length; i++) {
allMessageEls[i]._expanded = false;
}
MockInteractions.tap(allMessageEls[1]);
assert.isTrue(allMessageEls[1]._expanded);
MockInteractions.tap(element.$$('#collapse-messages'));
allMessageEls = getMessages();
for (var i = 0; i < allMessageEls.length; i++) {
assert.isTrue(allMessageEls[i]._expanded);
}
MockInteractions.tap(element.$$('#collapse-messages'));
allMessageEls = getMessages();
for (var i = 0; i < allMessageEls.length; i++) {
assert.isFalse(allMessageEls[i]._expanded);
}
});
test('expand/collapse from external keypress', function() {
MockInteractions.tap(element.$$('#collapse-messages'));
var allMessageEls = getMessages();
for (var i = 0; i < allMessageEls.length; i++) {
assert.isTrue(allMessageEls[i]._expanded);
}
// Expand/collapse all text also changes.
assert.equal(element.$$('#collapse-messages').textContent.trim(),
'Collapse all');
MockInteractions.tap(element.$$('#collapse-messages'));
allMessageEls = getMessages();
for (var i = 0; i < allMessageEls.length; i++) {
assert.isFalse(allMessageEls[i]._expanded);
}
// Expand/collapse all text also changes.
assert.equal(element.$$('#collapse-messages').textContent.trim(),
'Expand all');
});
test('hide messages does not appear when no automated messages',
function() {
assert.isOk(element.$$('#automatedMessageToggleContainer[hidden]'));
});
test('scroll to message', function() {
var allMessageEls = getMessages();
for (var i = 0; i < allMessageEls.length; i++) {
allMessageEls[i].set('message.expanded', false);
}
var scrollToStub = sandbox.stub(window, 'scrollTo');
var highlightStub = sandbox.stub(element, '_highlightEl');
element.scrollToMessage('invalid');
for (var i = 0; i < allMessageEls.length; i++) {
assert.isFalse(allMessageEls[i]._expanded,
'expected gr-message ' + i + ' to not be expanded');
}
var messageID = messages[1].id;
element.scrollToMessage(messageID);
assert.isTrue(
element.$$('[data-message-id="' + messageID + '"]')._expanded);
assert.isTrue(scrollToStub.calledOnce);
assert.isTrue(highlightStub.calledOnce);
});
test('scroll to message offscreen', function() {
var scrollToStub = sandbox.stub(window, 'scrollTo');
var highlightStub = sandbox.stub(element, '_highlightEl');
element.messages = _.times(25, randomMessage);
flushAsynchronousOperations();
assert.isFalse(scrollToStub.called);
assert.isFalse(highlightStub.called);
var messageID = element.messages[1].id;
element.scrollToMessage(messageID);
assert.isTrue(scrollToStub.calledOnce);
assert.isTrue(highlightStub.calledOnce);
assert.equal(element._visibleMessages.length, 24);
assert.isTrue(
element.$$('[data-message-id="' + messageID + '"]')._expanded);
});
test('messages', function() {
var author = {
_account_id: 42,
name: 'Marvin the Paranoid Android',
email: 'marvin@sirius.org',
};
var comments = {
file1: [
{
message: 'message text',
updated: '2016-09-27 00:18:03.000000000',
in_reply_to: '6505d749_f0bec0aa',
line: 62,
id: '6505d749_10ed44b2',
patch_set: 2,
author: {
email: 'some@email.com',
_account_id: 123,
},
},
{
message: 'message text',
updated: '2016-09-27 00:18:03.000000000',
in_reply_to: 'c5912363_6b820105',
line: 42,
id: '450a935e_0f1c05db',
patch_set: 2,
author: author,
},
{
message: 'message text',
updated: '2016-09-27 00:18:03.000000000',
in_reply_to: '6505d749_f0bec0aa',
line: 62,
id: '6505d749_10ed44b2',
patch_set: 2,
author: author,
},
],
file2: [
{
message: 'message text',
updated: '2016-09-27 00:18:03.000000000',
in_reply_to: 'c5912363_4b7d450a',
line: 132,
id: '450a935e_4f260d25',
patch_set: 2,
author: author,
},
],
};
var messages = [].concat(
randomMessage(),
{
_index: 5,
_revision_number: 4,
message: 'Uploaded patch set 4.',
date: '2016-09-28 13:36:33.000000000',
author: author,
id: '8c19ccc949c6d482b061be6a28e10782abf0e7af',
},
{
_index: 6,
_revision_number: 4,
message: 'Patch Set 4:\n\n(6 comments)',
date: '2016-09-28 13:36:33.000000000',
author: author,
id: 'e7bfdbc842f6b6d8064bc68e0f52b673f40c0ca5',
}
);
element.comments = comments;
element.messages = messages;
var isAuthor = function(author, message) {
return message.author._account_id === author._account_id;
};
var isMarvin = isAuthor.bind(null, author);
flushAsynchronousOperations();
var messageElements = getMessages();
assert.equal(messageElements.length, messages.length);
assert.deepEqual(messageElements[1].message, messages[1]);
assert.deepEqual(messageElements[2].message, messages[2]);
assert.deepEqual(messageElements[1].comments.file1,
comments.file1.filter(isMarvin));
assert.deepEqual(messageElements[1].comments.file2,
comments.file2.filter(isMarvin));
assert.deepEqual(messageElements[2].comments, {});
});
test('messages without author do not throw', function() {
var comments = {
file1: [
{
message: 'message text',
updated: '2016-09-27 00:18:03.000000000',
in_reply_to: '6505d749_f0bec0aa',
line: 62,
id: '6505d749_10ed44b2',
patch_set: 2,
author: {
email: 'some@email.com',
_account_id: 123,
},
},
]};
var messages = [{
_index: 5,
_revision_number: 4,
message: 'Uploaded patch set 4.',
date: '2016-09-28 13:36:33.000000000',
id: '8c19ccc949c6d482b061be6a28e10782abf0e7af',
}];
element.messages = messages;
element.comments = comments;
flushAsynchronousOperations();
var messageEls = getMessages();
assert.equal(messageEls.length, 1);
assert.equal(messageEls[0].message.message, messages[0].message);
});
});
suite('gr-messages-list automate tests', function() {
var element;
var messages;
var getMessages = function() {
return Polymer.dom(element.root).querySelectorAll('gr-message');
};
var getHiddenMessages = function() {
return Polymer.dom(element.root).querySelectorAll('gr-message[hidden]');
};
var randomMessageReviewer = {
reviewer: {},
};
setup(function() {
stub('gr-rest-api-interface', {
getConfig: function() { return Promise.resolve({}); },
getLoggedIn: function() { return Promise.resolve(false); },
});
element = fixture('basic');
messages = _.times(2, randomAutomated);
messages.push(randomMessageReviewer);
element.messages = messages;
flushAsynchronousOperations();
});
test('hide autogenerated button is not hidden', function() {
assert.isNotOk(element.$$('#automatedMessageToggle[hidden]'));
});
test('autogenerated messages are not hidden initially', function() {
var allHiddenMessageEls = getHiddenMessages();
//There are no hidden messages.
assert.isFalse(!!allHiddenMessageEls.length);
});
test('autogenerated messages hidden after hide button tap', function() {
var allHiddenMessageEls = getHiddenMessages();
element._hideAutomated = false;
MockInteractions.tap(element.$.automatedMessageToggle);
flushAsynchronousOperations();
allMessageEls = getMessages();
allHiddenMessageEls = getHiddenMessages();
// Autogenerated messages are now hidden.
assert.equal(allHiddenMessageEls.length, allMessageEls.length);
});
test('autogenerated messages not hidden after show button tap', function() {
var allHiddenMessageEls = getHiddenMessages();
element._hideAutomated = true;
MockInteractions.tap(element.$.automatedMessageToggle);
allHiddenMessageEls = getHiddenMessages();
//Autogenerated messages are now hidden.
assert.isFalse(!!allHiddenMessageEls.length);
});
test('_getDelta', function() {
var messages = [randomMessage()];
assert.equal(element._getDelta([], messages, false), 1);
assert.equal(element._getDelta([], messages, true), 1);
messages = _.times(7, randomMessage);
assert.equal(element._getDelta([], messages, false), 5);
assert.equal(element._getDelta([], messages, true), 5);
messages = _.times(4, randomMessage)
.concat(_.times(2, randomAutomated))
.concat(_.times(3, randomMessage));
var dummyArr = _.times(2, randomMessage);
assert.equal(element._getDelta(dummyArr, messages, false), 5);
assert.equal(element._getDelta(dummyArr, messages, true), 7);
});
test('_getHumanMessages', function() {
assert.equal(
element._getHumanMessages(_.times(5, randomAutomated)).length, 0);
assert.equal(
element._getHumanMessages(_.times(5, randomMessage)).length, 5);
var messages = _.shuffle(_.times(5, randomMessage)
.concat(_.times(5, randomAutomated)));
messages = element._getHumanMessages(messages);
assert.equal(messages.length, 5);
assert.isFalse(element._hasAutomatedMessages(messages));
});
});
</script>