Add coverageRangeChanged to notify all related listeners

Change-Id: Ie3f72165ab2e7d43c3506b973f4843cd87ece006
This commit is contained in:
Tao Zhou
2019-12-13 15:14:31 +01:00
parent 3206b1d882
commit 3dba39e269
3 changed files with 116 additions and 53 deletions

View File

@@ -312,22 +312,7 @@
} }
this._coverageRanges = []; this._coverageRanges = [];
const {changeNum, path, patchRange: {basePatchNum, patchNum}} = this; this._getCoverageData();
this.$.jsAPI.getCoverageRanges(
changeNum, path, basePatchNum, patchNum
).
then(coverageRanges => {
if (changeNum !== this.changeNum ||
path !== this.path ||
basePatchNum !== this.patchRange.basePatchNum ||
patchNum !== this.patchRange.patchNum) {
return;
}
this._coverageRanges = coverageRanges;
}).catch(err => {
console.warn('Loading coverage ranges failed: ', err);
});
const diffRequest = this._getDiff() const diffRequest = this._getDiff()
.then(diff => { .then(diff => {
this._loadedWhitespaceLevel = whitespaceLevel; this._loadedWhitespaceLevel = whitespaceLevel;
@@ -346,7 +331,7 @@
return this._loadDiffAssets(diff); return this._loadDiffAssets(diff);
}); });
// Not waiting for getCoverageRanges intentionally as // Not waiting for coverage ranges intentionally as
// plugin loading should not block the content rendering // plugin loading should not block the content rendering
return Promise.all([diffRequest, assetRequest]) return Promise.all([diffRequest, assetRequest])
.then(results => { .then(results => {
@@ -387,6 +372,49 @@
.then(() => { this._loading = false; }); .then(() => { this._loading = false; });
} }
_getCoverageData() {
const {changeNum, path, patchRange: {basePatchNum, patchNum}} = this;
this.$.jsAPI.getCoverageAnnotationApi().
then(coverageAnnotationApi => {
if (!coverageAnnotationApi) return;
const provider = coverageAnnotationApi.getCoverageProvider();
return provider(changeNum, path, basePatchNum, patchNum)
.then(coverageRanges => {
if (!coverageRanges ||
changeNum !== this.changeNum ||
path !== this.path ||
basePatchNum !== this.patchRange.basePatchNum ||
patchNum !== this.patchRange.patchNum) {
return;
}
const existingCoverageRanges = this._coverageRanges;
this._coverageRanges = coverageRanges;
// Notify with existing coverage ranges
// in case there is some existing coverage data that needs to be removed
existingCoverageRanges.forEach(range => {
coverageAnnotationApi.notify(
path,
range.code_range.start_line,
range.code_range.end_line,
range.side);
});
// Notify with new coverage data
coverageRanges.forEach(range => {
coverageAnnotationApi.notify(
path,
range.code_range.start_line,
range.code_range.end_line,
range.side);
});
});
}).catch(err => {
console.warn('Loading coverage ranges failed: ', err);
});
}
_getFilesWeblinks(diff) { _getFilesWeblinks(diff) {
if (!this.commitRange) { if (!this.commitRange) {
return {}; return {};

View File

@@ -1429,5 +1429,69 @@ limitations under the License.
assert.isFalse(element.$.syntaxLayer.enabled); assert.isFalse(element.$.syntaxLayer.enabled);
}); });
}); });
suite('coverage layer', () => {
let notifyStub;
setup(() => {
notifyStub = sinon.stub();
stub('gr-js-api-interface', {
getCoverageAnnotationApi() {
return Promise.resolve({
notify: notifyStub,
getCoverageProvider() {
return () => Promise.resolve([
{
type: 'COVERED',
side: 'right',
code_range: {
start_line: 1,
end_line: 2,
},
},
{
type: 'NOT_COVERED',
side: 'right',
code_range: {
start_line: 3,
end_line: 4,
},
},
]);
},
});
},
});
element = fixture('basic');
const prefs = {
line_length: 10,
show_tabs: true,
tab_size: 4,
context: -1,
};
element.diff = {
content: [{
a: ['foo'],
}],
};
element.patchRange = {};
element.prefs = prefs;
});
test('getCoverageAnnotationApi should be called', done => {
element.reload();
flush(() => {
assert.isTrue(element.$.jsAPI.getCoverageAnnotationApi.calledOnce);
done();
});
});
test('coverageRangeChanged should be called', done => {
element.reload();
flush(() => {
assert.equal(notifyStub.callCount, 2);
done();
});
});
});
}); });
</script> </script>

View File

@@ -253,44 +253,15 @@
* Retrieves coverage data possibly provided by a plugin. * Retrieves coverage data possibly provided by a plugin.
* *
* Will wait for plugins to be loaded. If multiple plugins offer a coverage * Will wait for plugins to be loaded. If multiple plugins offer a coverage
* provider, the first one is used. If no plugin offers a coverage provider, * provider, the first one is returned. If no plugin offers a coverage provider,
* will resolve to []. * will resolve to null.
* *
* @param {string|number} changeNum * @return {!Promise<?GrAnnotationActionsInterface>}
* @param {string} path
* @param {string|number} basePatchNum
* @param {string|number} patchNum
* @return {!Promise<!Array<!Gerrit.CoverageRange>>}
*/ */
getCoverageRanges(changeNum, path, basePatchNum, patchNum) { getCoverageAnnotationApi() {
return Gerrit.awaitPluginsLoaded().then(() => { return Gerrit.awaitPluginsLoaded()
for (const annotationApi of .then(() => this._getEventCallbacks(EventType.ANNOTATE_DIFF)
this._getEventCallbacks(EventType.ANNOTATE_DIFF)) { .find(api => api.getCoverageProvider()));
const provider = annotationApi.getCoverageProvider();
// Only one coverage provider makes sense. If there are more, then we
// simply ignore them.
if (provider) {
return annotationApi;
}
}
return null;
}).then(annotationApi => {
if (!annotationApi) return [];
const provider = annotationApi.getCoverageProvider();
return provider(changeNum, path, basePatchNum, patchNum)
.then(ranges => {
ranges = ranges || [];
// Notify with the coverage data.
ranges.forEach(range => {
annotationApi.notify(
path,
range.code_range.start_line,
range.code_range.end_line,
range.side);
});
return ranges;
});
});
} }
getAdminMenuLinks() { getAdminMenuLinks() {