Commit b087dfcb authored by Omid Tourzan's avatar Omid Tourzan Committed by Commit Bot

[files-progress] Add transfer details unit tests.

Testing Speedometer formula and internals and new behaviors in
progress panel.

The setUpPage method of files_xf_elements_unittest.js changed to setUp
as it is not called per test case and causes conflict between tests.

Bug: 953308
Change-Id: I24c88e63cdcec99f73a5233e18d2d91dbac8d2fb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2317128
Commit-Queue: Omid Tourzan <oto@chromium.org>
Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Reviewed-by: default avatarAlex Danilo <adanilo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792496}
parent 29a076aa
......@@ -13,6 +13,43 @@ let progressCenter;
/** @type {!FileOperationHandler} */
let fileOperationHandler;
/**
* Mock JS Date.
*
* The stop() method should be called to restore original Date.
*/
class MockDate {
constructor() {
this.originalNow = Date.now;
Date.tick_ = 0;
Date.now = this.now;
}
/**
* Increases current timestamp.
*
* @param {number} msec Milliseconds to add to the current timestamp.
*/
tick(msec) {
Date.tick_ += msec;
}
/**
* @returns {number} Current timestamp of the mock object.
*/
now() {
return Date.tick_;
}
/**
* Restore original Date.now method.
*/
stop() {
Date.now = this.originalNow;
}
}
// Set up the test components.
function setUp() {
// Mock LoadTimeData strings.
......@@ -216,3 +253,56 @@ function testCopyUnexpectedError() {
assertEquals(true, item.single);
assertEquals(0, item.progressRateInPercent);
}
/**
* Tests Speedometer moving average calculations.
*/
function testSpeedometerMovingAverage() {
const bufferLength = 20;
const speedometer = new fileOperationUtil.Speedometer(bufferLength);
const mockDate = new MockDate();
speedometer.setTotalBytes(2000);
mockDate.tick(1000);
speedometer.update(100);
mockDate.tick(1000);
speedometer.update(300);
// Verify calculated instantaneous speeds.
assertArrayEquals([100, 200], speedometer.getBufferForTesting());
// Check moving average speed calculation.
assertEquals(150, speedometer.getCurrentSpeed());
// Check calculated remaining time.
assertEquals(12, speedometer.getRemainingTime());
mockDate.stop();
}
/**
* Tests Speedometer buffer ring rotate and substitute.
*/
function testSpeedometerBufferRing() {
const bufferLength = 20;
const speedometer = new fileOperationUtil.Speedometer(bufferLength);
const mockDate = new MockDate();
for (let i = 0; i < bufferLength; i++) {
mockDate.tick(1000);
speedometer.update(i * 100);
}
mockDate.tick(1000);
speedometer.update(2200);
const buffer = speedometer.getBufferForTesting();
// Check buffer not expanded more than the specified length.
assertEquals(bufferLength, buffer.length);
// Check first element replaced by recent calculated speed.
assertEquals(300, buffer[0]);
mockDate.stop();
}
\ No newline at end of file
......@@ -1419,7 +1419,7 @@ fileOperationUtil.Speedometer = class {
this.length_ = bufferLength;
/**
* @private {Array} internal buffer to track recent data.
* @private {!Array} internal buffer to track recent data.
*/
this.buffer_ = [];
......@@ -1479,6 +1479,15 @@ fileOperationUtil.Speedometer = class {
this.cma_ = Math.floor((this.cma_ * this.count_ + speed) / (this.count_));
}
/**
* Returns internal buffer for unit testing purposes.
*
* @returns {!Array} The internal buffer.
*/
getBufferForTesting() {
return this.buffer_;
}
/**
* Calculates and returns the current speed in bytes per second.
*/
......
......@@ -8,7 +8,7 @@ let displayPanel;
/**
* Adds a xf-display-panel element to the test page.
*/
function setUpPage() {
function setUp() {
document.body.innerHTML +=
'<xf-display-panel id="test-xf-display-panel"></xf-display-panel>';
displayPanel = assert(document.querySelector('#test-xf-display-panel'));
......@@ -449,3 +449,51 @@ async function testFilesDisplayPanelSummaryPanel(done) {
done();
}
function testFilesDisplayPanelTransferDetails() {
// Get the host display panel container element.
/** @type {!DisplayPanel|!Element} */
const displayPanel = assert(document.querySelector('#test-xf-display-panel'));
// Add a generic progress panel item to the display panel container.
const progressPanel = displayPanel.addPanelItem('testpanel1');
progressPanel.panelType = progressPanel.panelTypeProgress;
// Check detailed-panel is added when FilesTransferDetails enabled.
assertEquals('detailed-panel', progressPanel.getAttribute('detailed-panel'));
}
async function testFilesDisplayPanelTransferDetailsSummary(done) {
// Get the host display panel container element.
/** @type {!DisplayPanel|!Element} */
const displayPanel = assert(document.querySelector('#test-xf-display-panel'));
// Add two generic progress panel items to the display panel container.
const panel1 = displayPanel.addPanelItem('testpanel1');
panel1.panelType = panel1.panelTypeProgress;
const panel2 = displayPanel.addPanelItem('testpanel2');
panel2.panelType = panel2.panelTypeProgress;
const panelContainer = displayPanel.shadowRoot.querySelector('#panels');
assertTrue(panelContainer.hasAttribute('hidden'));
const summaryContainer = displayPanel.shadowRoot.querySelector('#summary');
const summaryPanelItem = summaryContainer.querySelector('#summary-panel');
// Check summary panel has both detailed-panel and detailed-summary attribute.
assertEquals(
'detailed-panel', summaryPanelItem.getAttribute('detailed-panel'));
assertEquals('', summaryPanelItem.getAttribute('detailed-summary'));
// Trigger expand of the summary panel by summary label.
const summaryLabel =
summaryPanelItem.shadowRoot.querySelector('.xf-panel-text');
summaryLabel.click();
// Confirm the panel container is no longer hidden.
assertFalse(panelContainer.hasAttribute('hidden'));
assertEquals('expanded', summaryPanelItem.getAttribute('data-category'));
done();
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment