When diffs encounter loading errors (such as 404s or 500s in the diff request), the diff would formerly bubble up an event that showed an error message that blocks the entire page. This works well in the diff view because the singular diff is the main content of the page. However, when expanding many files inline in the change view, if only some of the diffs encounter loading errors, the error message need not obstruct the rest of the page, which may contain diffs that load fine. With this change, inline diffs show their own descriptive loading errors, and won't hide the rest of the content. The structure of the diff loading request is refactored so that errors can be caught and handled cleanly in the promise chain and the overall async-loop that renders many diffs in order will not be halted when one diff fails. Bug: Issue 9590 Change-Id: I9d984d39dcec90119d05acd94afad938e5fb7c1e
293 lines
10 KiB
HTML
293 lines
10 KiB
HTML
<!DOCTYPE html>
|
|
<!--
|
|
@license
|
|
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-diff-cursor</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="../../../test/common-test-setup.html"/>
|
|
<script src="../../../scripts/util.js"></script>
|
|
|
|
<link rel="import" href="../gr-diff/gr-diff.html">
|
|
<link rel="import" href="./gr-diff-cursor.html">
|
|
<link rel="import" href="../../shared/gr-rest-api-interface/mock-diff-response_test.html">
|
|
|
|
<script>void(0);</script>
|
|
|
|
<test-fixture id="basic">
|
|
<template>
|
|
<mock-diff-response></mock-diff-response>
|
|
<gr-diff></gr-diff>
|
|
<gr-diff-cursor></gr-diff-cursor>
|
|
</template>
|
|
</test-fixture>
|
|
|
|
<script>
|
|
suite('gr-diff-cursor tests', () => {
|
|
let sandbox;
|
|
let cursorElement;
|
|
let diffElement;
|
|
let mockDiffResponse;
|
|
|
|
setup(done => {
|
|
sandbox = sinon.sandbox.create();
|
|
|
|
stub('gr-rest-api-interface', {
|
|
getLoggedIn() { return Promise.resolve(false); },
|
|
getDiff() {
|
|
return Promise.resolve(mockDiffResponse.diffResponse);
|
|
},
|
|
});
|
|
|
|
const fixtureElems = fixture('basic');
|
|
mockDiffResponse = fixtureElems[0];
|
|
diffElement = fixtureElems[1];
|
|
cursorElement = fixtureElems[2];
|
|
|
|
// Register the diff with the cursor.
|
|
cursorElement.push('diffs', diffElement);
|
|
|
|
diffElement.patchRange = {basePatchNum: 1, patchNum: 2};
|
|
diffElement.comments = {left: [], right: []};
|
|
diffElement.$.restAPI.getDiffPreferences().then(prefs => {
|
|
diffElement.prefs = prefs;
|
|
});
|
|
|
|
const setupDone = () => {
|
|
cursorElement._updateStops();
|
|
cursorElement.moveToFirstChunk();
|
|
diffElement.removeEventListener('render', setupDone);
|
|
done();
|
|
};
|
|
diffElement.addEventListener('render', setupDone);
|
|
|
|
diffElement.reload();
|
|
});
|
|
|
|
teardown(() => sandbox.restore());
|
|
|
|
test('diff cursor functionality (side-by-side)', () => {
|
|
// The cursor has been initialized to the first delta.
|
|
assert.isOk(cursorElement.diffRow);
|
|
|
|
const firstDeltaRow = diffElement.$$('.section.delta .diff-row');
|
|
assert.equal(cursorElement.diffRow, firstDeltaRow);
|
|
|
|
cursorElement.moveDown();
|
|
|
|
assert.notEqual(cursorElement.diffRow, firstDeltaRow);
|
|
assert.equal(cursorElement.diffRow, firstDeltaRow.nextSibling);
|
|
|
|
cursorElement.moveUp();
|
|
|
|
assert.notEqual(cursorElement.diffRow, firstDeltaRow.nextSibling);
|
|
assert.equal(cursorElement.diffRow, firstDeltaRow);
|
|
});
|
|
|
|
test('cursor scroll behavior', () => {
|
|
cursorElement._handleDiffRenderStart();
|
|
assert.equal(cursorElement._scrollBehavior, 'keep-visible');
|
|
assert.isTrue(cursorElement._focusOnMove);
|
|
|
|
cursorElement._handleWindowScroll();
|
|
assert.equal(cursorElement._scrollBehavior, 'never');
|
|
assert.isFalse(cursorElement._focusOnMove);
|
|
|
|
cursorElement.handleDiffUpdate();
|
|
assert.equal(cursorElement._scrollBehavior, 'keep-visible');
|
|
assert.isTrue(cursorElement._focusOnMove);
|
|
});
|
|
|
|
suite('unified diff', () => {
|
|
setup(done => {
|
|
// We must allow the diff to re-render after setting the viewMode.
|
|
const renderHandler = function() {
|
|
diffElement.removeEventListener('render', renderHandler);
|
|
cursorElement.reInitCursor();
|
|
done();
|
|
};
|
|
diffElement.addEventListener('render', renderHandler);
|
|
diffElement.viewMode = 'UNIFIED_DIFF';
|
|
});
|
|
|
|
test('diff cursor functionality (unified)', () => {
|
|
// The cursor has been initialized to the first delta.
|
|
assert.isOk(cursorElement.diffRow);
|
|
|
|
let firstDeltaRow = diffElement.$$('.section.delta .diff-row');
|
|
assert.equal(cursorElement.diffRow, firstDeltaRow);
|
|
|
|
firstDeltaRow = diffElement.$$('.section.delta .diff-row');
|
|
assert.equal(cursorElement.diffRow, firstDeltaRow);
|
|
|
|
cursorElement.moveDown();
|
|
|
|
assert.notEqual(cursorElement.diffRow, firstDeltaRow);
|
|
assert.equal(cursorElement.diffRow, firstDeltaRow.nextSibling);
|
|
|
|
cursorElement.moveUp();
|
|
|
|
assert.notEqual(cursorElement.diffRow, firstDeltaRow.nextSibling);
|
|
assert.equal(cursorElement.diffRow, firstDeltaRow);
|
|
});
|
|
});
|
|
|
|
test('cursor side functionality', () => {
|
|
// The side only applies to side-by-side mode, which should be the default
|
|
// mode.
|
|
assert.equal(diffElement.viewMode, 'SIDE_BY_SIDE');
|
|
|
|
const firstDeltaSection = diffElement.$$('.section.delta');
|
|
const firstDeltaRow = firstDeltaSection.querySelector('.diff-row');
|
|
|
|
// Because the first delta in this diff is on the right, it should be set
|
|
// to the right side.
|
|
assert.equal(cursorElement.side, 'right');
|
|
assert.equal(cursorElement.diffRow, firstDeltaRow);
|
|
const firstIndex = cursorElement.$.cursorManager.index;
|
|
|
|
// Move the side to the left. Because this delta only has a right side, we
|
|
// should be moved up to the previous line where there is content on the
|
|
// right. The previous row is part of the previous section.
|
|
cursorElement.moveLeft();
|
|
|
|
assert.equal(cursorElement.side, 'left');
|
|
assert.notEqual(cursorElement.diffRow, firstDeltaRow);
|
|
assert.equal(cursorElement.$.cursorManager.index, firstIndex - 1);
|
|
assert.equal(cursorElement.diffRow.parentElement,
|
|
firstDeltaSection.previousSibling);
|
|
|
|
// If we move down, we should skip everything in the first delta because
|
|
// we are on the left side and the first delta has no content on the left.
|
|
cursorElement.moveDown();
|
|
|
|
assert.equal(cursorElement.side, 'left');
|
|
assert.notEqual(cursorElement.diffRow, firstDeltaRow);
|
|
assert.isTrue(cursorElement.$.cursorManager.index > firstIndex);
|
|
assert.equal(cursorElement.diffRow.parentElement,
|
|
firstDeltaSection.nextSibling);
|
|
});
|
|
|
|
test('chunk skip functionality', () => {
|
|
const chunks = Polymer.dom(diffElement.root).querySelectorAll(
|
|
'.section.delta');
|
|
const indexOfChunk = function(chunk) {
|
|
return Array.prototype.indexOf.call(chunks, chunk);
|
|
};
|
|
|
|
// We should be initialized to the first chunk. Since this chunk only has
|
|
// content on the right side, our side should be right.
|
|
let currentIndex = indexOfChunk(cursorElement.diffRow.parentElement);
|
|
assert.equal(currentIndex, 0);
|
|
assert.equal(cursorElement.side, 'right');
|
|
|
|
// Move to the next chunk.
|
|
cursorElement.moveToNextChunk();
|
|
|
|
// Since this chunk only has content on the left side. we should have been
|
|
// automatically mvoed over.
|
|
const previousIndex = currentIndex;
|
|
currentIndex = indexOfChunk(cursorElement.diffRow.parentElement);
|
|
assert.equal(currentIndex, previousIndex + 1);
|
|
assert.equal(cursorElement.side, 'left');
|
|
});
|
|
|
|
test('initialLineNumber disabled', done => {
|
|
const moveToNumStub = sandbox.stub(cursorElement, 'moveToLineNumber');
|
|
const moveToChunkStub = sandbox.stub(cursorElement, 'moveToFirstChunk');
|
|
|
|
function renderHandler() {
|
|
diffElement.removeEventListener('render', renderHandler);
|
|
assert.isFalse(moveToNumStub.called);
|
|
assert.isTrue(moveToChunkStub.called);
|
|
done();
|
|
}
|
|
diffElement.addEventListener('render', renderHandler);
|
|
diffElement.reload();
|
|
});
|
|
|
|
test('initialLineNumber enabled', done => {
|
|
const moveToNumStub = sandbox.stub(cursorElement, 'moveToLineNumber');
|
|
const moveToChunkStub = sandbox.stub(cursorElement, 'moveToFirstChunk');
|
|
|
|
function renderHandler() {
|
|
diffElement.removeEventListener('render', renderHandler);
|
|
assert.isFalse(moveToChunkStub.called);
|
|
assert.isTrue(moveToNumStub.called);
|
|
assert.equal(moveToNumStub.lastCall.args[0], 10);
|
|
assert.equal(moveToNumStub.lastCall.args[1], 'right');
|
|
done();
|
|
}
|
|
diffElement.addEventListener('render', renderHandler);
|
|
|
|
cursorElement.initialLineNumber = 10;
|
|
cursorElement.side = 'right';
|
|
|
|
diffElement.reload();
|
|
});
|
|
|
|
test('getAddress', () => {
|
|
// It should initialize to the first chunk: line 5 of the revision.
|
|
assert.deepEqual(cursorElement.getAddress(),
|
|
{leftSide: false, number: 5});
|
|
|
|
// Revision line 4 is up.
|
|
cursorElement.moveUp();
|
|
assert.deepEqual(cursorElement.getAddress(),
|
|
{leftSide: false, number: 4});
|
|
|
|
// Base line 4 is left.
|
|
cursorElement.moveLeft();
|
|
assert.deepEqual(cursorElement.getAddress(), {leftSide: true, number: 4});
|
|
|
|
// Moving to the next chunk takes it back to the start.
|
|
cursorElement.moveToNextChunk();
|
|
assert.deepEqual(cursorElement.getAddress(),
|
|
{leftSide: false, number: 5});
|
|
|
|
// The following chunk is a removal starting on line 10 of the base.
|
|
cursorElement.moveToNextChunk();
|
|
assert.deepEqual(cursorElement.getAddress(),
|
|
{leftSide: true, number: 10});
|
|
|
|
// Should be null if there is no selection.
|
|
cursorElement.$.cursorManager.unsetCursor();
|
|
assert.isNotOk(cursorElement.getAddress());
|
|
});
|
|
|
|
test('_findRowByNumberAndFile', () => {
|
|
// Get the first ab row after the first chunk.
|
|
const row = Polymer.dom(diffElement.root).querySelectorAll('tr')[8];
|
|
|
|
// It should be line 8 on the right, but line 5 on the left.
|
|
assert.equal(cursorElement._findRowByNumberAndFile(8, 'right'), row);
|
|
assert.equal(cursorElement._findRowByNumberAndFile(5, 'left'), row);
|
|
});
|
|
|
|
test('expand context updates stops', done => {
|
|
sandbox.spy(cursorElement, 'handleDiffUpdate');
|
|
MockInteractions.tap(diffElement.$$('.showContext'));
|
|
flush(() => {
|
|
assert.isTrue(cursorElement.handleDiffUpdate.called);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
</script>
|