Fix issue where patch range was not being preserved on nav in diff view

Change-Id: I28d92486714448161f8bf3a9c37c7f4a6eb58a93
This commit is contained in:
Andrew Bonventre 2016-01-08 10:13:54 -05:00 committed by Dave Borowitz
parent d88017fb38
commit 0a250522d5
2 changed files with 97 additions and 36 deletions

View File

@ -108,7 +108,7 @@ limitations under the License.
<iron-dropdown id="dropdown" vertical-align="top" vertical-offset="25">
<div class="dropdown-content">
<template is="dom-repeat" items="[[_fileList]]" as="path">
<a href$="[[_computeFileURL(_changeNum, _patchRange.patchNum, path)]]"
<a href$="[[_computeDiffURL(_changeNum, _patchRange, path)]]"
selected$="[[_computeFileSelected(path, _path)]]"
data-key-nav$="[[_computeKeyNav(path, _path, _fileList)]]"
on-tap="_handleFileTap">[[path]]</a>
@ -204,13 +204,9 @@ limitations under the License.
page.show(this._computeChangePath(this._changeNum));
return;
}
page.show(this._diffURL(this._changeNum,
this._patchRange.patchNum,
fileList[idx]));
},
_diffURL: function(changeNum, patchNum, path) {
return '/c/' + changeNum + '/' + patchNum + '/' + path;
page.show(this._computeDiffURL(this._changeNum,
this._patchRange,
fileList[idx]));
},
_paramsChanged: function(value) {
@ -229,6 +225,15 @@ limitations under the License.
}
},
_computeDiffURL: function(changeNum, patchRange, path) {
var patchStr = patchRange.patchNum;
if (patchRange.basePatchNum != null &&
patchRange.basePatchNum != 'PARENT') {
patchStr = patchRange.basePatchNum + '..' + patchRange.patchNum;
}
return '/c/' + changeNum + '/' + patchStr + '/' + path;
},
_computeAvailablePatches: function(revisions) {
var patchNums = [];
for (var rev in revisions) {
@ -255,10 +260,6 @@ limitations under the License.
return Changes.baseURL(changeNum, patchNum) + '/files';
},
_computeFileURL: function(changeNum, patchNum, path) {
return '/c/' + changeNum + '/' + patchNum + '/' + path;
},
_computeFileSelected: function(path, currentPath) {
return path == currentPath;
},

View File

@ -45,6 +45,37 @@ limitations under the License.
element.$.diff.auto = false;
});
// https://github.com/PolymerElements/iron-test-helpers/issues/33
function keyboardEventFor(type, keyIdentifier) {
var event = new CustomEvent(type, {
bubbles: true,
cancelable: true
});
event.keyIdentifier = keyIdentifier;
return event;
}
function keyEventOn(target, type, keyIdentifier) {
target.dispatchEvent(keyboardEventFor(type, keyIdentifier));
}
function keyDownOn(target, keyIdentifier) {
keyEventOn(target, 'keydown', keyIdentifier);
}
function keyUpOn(target, keyIdentifier) {
keyEventOn(target, 'keyup', keyIdentifier);
}
function pressAndReleaseKeyIdentifierOn(target, keyIdentifier) {
keyDownOn(target, keyIdentifier);
Polymer.Base.async(function() {
keyUpOn(target, keyIdentifier);
}, 1);
}
test('keyboard shortcuts', function() {
element._changeNum = '42';
element._patchRange = {
@ -78,37 +109,41 @@ limitations under the License.
'Should navigate to /c/42');
showStub.restore();
});
// https://github.com/PolymerElements/iron-test-helpers/issues/33
function keyboardEventFor(type, keyIdentifier) {
var event = new CustomEvent(type, {
bubbles: true,
cancelable: true
});
test('keyboard shortcuts with patch range', function() {
element._changeNum = '42';
element._patchRange = {
basePatchNum: '5',
patchNum: '10',
};
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt';
event.keyIdentifier = keyIdentifier;
var showStub = sinon.stub(page, 'show');
return event;
}
MockInteractions.pressAndReleaseKeyOn(element, 85); // 'u'
assert(showStub.lastCall.calledWithExactly('/c/42'),
'Should navigate to /c/42');
function keyEventOn(target, type, keyIdentifier) {
target.dispatchEvent(keyboardEventFor(type, keyIdentifier));
}
pressAndReleaseKeyIdentifierOn(element, '\U+005D'); // ']'
assert(showStub.lastCall.calledWithExactly('/c/42/5..10/wheatley.md'),
'Should navigate to /c/42/5..10/wheatley.md');
element._path = 'wheatley.md';
function keyDownOn(target, keyIdentifier) {
keyEventOn(target, 'keydown', keyIdentifier);
}
pressAndReleaseKeyIdentifierOn(element, '\U+005B'); // '['
assert(showStub.lastCall.calledWithExactly('/c/42/5..10/glados.txt'),
'Should navigate to /c/42/5..10/glados.txt');
element._path = 'glados.txt';
function keyUpOn(target, keyIdentifier) {
keyEventOn(target, 'keyup', keyIdentifier);
}
pressAndReleaseKeyIdentifierOn(element, '\U+005B'); // '['
assert(showStub.lastCall.calledWithExactly('/c/42/5..10/chell.go'),
'Should navigate to /c/42/5..10/chell.go');
element._path = 'chell.go';
function pressAndReleaseKeyIdentifierOn(target, keyIdentifier) {
keyDownOn(target, keyIdentifier);
Polymer.Base.async(function() {
keyUpOn(target, keyIdentifier);
}, 1);
}
pressAndReleaseKeyIdentifierOn(element, '\U+005B'); // '['
assert(showStub.lastCall.calledWithExactly('/c/42'),
'Should navigate to /c/42');
});
test('jump to file dropdown', function() {
@ -128,7 +163,32 @@ limitations under the License.
assert.equal(linkEls[0].getAttribute('data-key-nav'), '[');
assert.equal(linkEls[1].getAttribute('data-key-nav'), '');
assert.equal(linkEls[2].getAttribute('data-key-nav'), ']');
assert.equal(linkEls[0].getAttribute('href'), '/c/42/10/chell.go');
assert.equal(linkEls[1].getAttribute('href'), '/c/42/10/glados.txt');
assert.equal(linkEls[2].getAttribute('href'), '/c/42/10/wheatley.md');
});
test('jump to file dropdown with patch range', function() {
element._changeNum = '42';
element._patchRange = {
basePatchNum: '5',
patchNum: '10',
};
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt';
flushAsynchronousOperations();
var linkEls =
Polymer.dom(element.root).querySelectorAll('.dropdown-content > a');
assert.equal(linkEls.length, 3);
assert.isFalse(linkEls[0].hasAttribute('selected'));
assert.isTrue(linkEls[1].hasAttribute('selected'));
assert.isFalse(linkEls[2].hasAttribute('selected'));
assert.equal(linkEls[0].getAttribute('data-key-nav'), '[');
assert.equal(linkEls[1].getAttribute('data-key-nav'), '');
assert.equal(linkEls[2].getAttribute('data-key-nav'), ']');
assert.equal(linkEls[0].getAttribute('href'), '/c/42/5..10/chell.go');
assert.equal(linkEls[1].getAttribute('href'), '/c/42/5..10/glados.txt');
assert.equal(linkEls[2].getAttribute('href'), '/c/42/5..10/wheatley.md');
});
});
</script>