Skip to content

Commit 83302af

Browse files
committed
Add tests, change how alert icon is checked
1 parent 5ddbc15 commit 83302af

2 files changed

Lines changed: 72 additions & 32 deletions

File tree

lib/js/src/manager/screen/utils/_PresentAlertOperation.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class _PresentAlertOperation extends _Task {
6363
this._listener = listener;
6464
this._isAlertPresented = false;
6565
this._alertSoftButtonClearListener = alertSoftButtonClearListener;
66-
this._uploadedImageNames = new Set(); // stores a set of image name strings
66+
this._alertIconUploaded = false;
6767

6868
this._alertView.canceledListener = () => {
6969
this.cancelAlert();
@@ -169,7 +169,7 @@ class _PresentAlertOperation extends _Task {
169169

170170
console.log('Uploading audio files for alert');
171171

172-
if (this._fileManager !== null || this._fileManager !== undefined) {
172+
if (this._fileManager !== null) {
173173
const successes = await this._fileManager.uploadFiles(filesToBeUploaded);
174174

175175
if (this.getState() === _Task.CANCELED) {
@@ -193,25 +193,19 @@ class _PresentAlertOperation extends _Task {
193193
async uploadImages (listener) {
194194
const artworksToBeUploaded = [];
195195

196-
if (this.supportsAlertIcon() && this._alertView.getIcon() !== null && this._alertView.getIcon() !== undefined) {
196+
if (this.supportsAlertIcon() && this._alertView.getIcon() !== null && this._alertView.getIcon() !== undefined && this._fileManager !== null) {
197197
if (this._fileManager.fileNeedsUpload(this._alertView.getIcon())) {
198-
// If the file is not uploaded, attempt to upload it
199198
artworksToBeUploaded.push(this._alertView.getIcon());
200199
} else if (this._fileManager.hasUploadedFile(this._alertView.getIcon()) || this._alertView.getIcon().isStaticIcon()) {
201-
// If the file is already uploaded, add it to the uploaded set so we can show it
202-
this._uploadedImageNames.add(this._alertView.getIcon().getName());
200+
this._alertIconUploaded = true;
203201
}
204202
}
205203

206204
if (this._alertView.getSoftButtons() !== null && this._alertView.getSoftButtons() !== undefined) {
207205
for (let index = 0; index < this._getSoftButtonCount(); index++) {
208206
const objectState = this._alertView.getSoftButtons()[index].getCurrentState();
209-
if (this.supportsSoftButtonImages() && objectState !== null && objectState !== undefined && objectState.getArtwork() !== null && objectState.getArtwork() !== undefined) {
210-
if (this._fileManager !== null && this._fileManager.fileNeedsUpload(objectState.getArtwork())) {
211-
artworksToBeUploaded.push(objectState.getArtwork());
212-
} else if (this._fileManager.hasUploadedFile(objectState.getArtwork()) || objectState.getArtwork().isStaticIcon()) {
213-
this._uploadedImageNames.add(objectState.getArtwork().getName());
214-
}
207+
if (this.supportsSoftButtonImages() && objectState !== null && objectState !== undefined && this._fileManager !== null && this._fileManager.fileNeedsUpload(objectState.getArtwork())) {
208+
artworksToBeUploaded.push(objectState.getArtwork());
215209
}
216210
}
217211
}
@@ -224,7 +218,7 @@ class _PresentAlertOperation extends _Task {
224218

225219
console.log('Uploading images for alert');
226220

227-
if (this._fileManager !== null && this._fileManager !== undefined) {
221+
if (this._fileManager !== null) {
228222
const successes = await this._fileManager.uploadArtworks(artworksToBeUploaded);
229223
if (this.getState() === _Task.CANCELED) {
230224
console.log('Operation canceled');
@@ -238,9 +232,19 @@ class _PresentAlertOperation extends _Task {
238232
} else {
239233
console.log('All alert images uploaded');
240234
}
241-
artworksToBeUploaded.forEach(artwork => {
242-
this._uploadedImageNames.add(artwork.getName());
243-
});
235+
236+
const artworkResults = [];
237+
for (let index = 0; index < successes.length; index++) {
238+
artworkResults.push({
239+
artwork: artworksToBeUploaded[index],
240+
success: successes[index],
241+
});
242+
}
243+
// only keep artworks that have been uploaded successfully by the file manager
244+
const artworkNamesSuccessfullyUploaded = artworkResults.filter(result => result.success).map(result => result.artwork.getName());
245+
if (artworkNamesSuccessfullyUploaded.includes(this._alertView.getIcon().getName())) {
246+
this._alertIconUploaded = true;
247+
}
244248
listener(true);
245249
return;
246250
}
@@ -315,7 +319,7 @@ class _PresentAlertOperation extends _Task {
315319
alert = this.assembleAlertText(alert);
316320
alert.setDuration(this._alertView.getTimeout() * 1000);
317321

318-
if (this._alertView.getIcon() !== null && this._alertView.getIcon() !== undefined && this._uploadedImageNames.has(this._alertView.getIcon().getName())) {
322+
if (this._alertIconUploaded) {
319323
alert.setAlertIcon(this._alertView.getIcon().getImageRPC());
320324
}
321325

tests/managers/screen/PresentAlertOperationTests.js

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ module.exports = function (appClient) {
2727
/**
2828
* Gets the windowCapability
2929
* @param {Number} numberOfAlertFields - number of lines
30+
* @param {Boolean} supportsAlertIcon - whether alert icon is supported by the capability
3031
* @returns {WindowCapability} - the capability
3132
*/
32-
function getWindowCapability (numberOfAlertFields) {
33+
function getWindowCapability (numberOfAlertFields, supportsAlertIcon) {
3334
const alertText1 = new SDL.rpc.structs.TextField();
3435
alertText1.setNameParam(SDL.rpc.enums.TextFieldName.alertText1);
3536
const alertText2 = new SDL.rpc.structs.TextField();
@@ -56,13 +57,13 @@ module.exports = function (appClient) {
5657
const windowCapability = new SDL.rpc.structs.WindowCapability();
5758
windowCapability.setTextFields(returnList);
5859

59-
const imageField = new SDL.rpc.structs.ImageField();
60-
imageField.setNameParam(SDL.rpc.enums.ImageFieldName.alertIcon);
61-
const imageFieldList = [];
62-
imageFieldList.push(imageField);
63-
windowCapability.setImageFields(imageFieldList);
64-
65-
windowCapability.setImageFields(imageFieldList);
60+
if (supportsAlertIcon) {
61+
const imageField = new SDL.rpc.structs.ImageField();
62+
imageField.setNameParam(SDL.rpc.enums.ImageFieldName.alertIcon);
63+
const imageFieldList = [];
64+
imageFieldList.push(imageField);
65+
windowCapability.setImageFields(imageFieldList);
66+
}
6667

6768
const softButtonCapabilities = new SDL.rpc.structs.SoftButtonCapabilities();
6869
softButtonCapabilities.setImageSupported(true);
@@ -136,7 +137,7 @@ module.exports = function (appClient) {
136137
alertView.setShowWaitIndicator(true);
137138
alertView.canceledListener = () => {};
138139

139-
defaultMainWindowCapability = getWindowCapability(3);
140+
defaultMainWindowCapability = getWindowCapability(3, true);
140141
speechCapabilities = [];
141142
speechCapabilities.push(SDL.rpc.enums.SpeechCapabilities.FILE);
142143
alertCompletionListener = new SDL.manager.screen.utils.AlertCompletionListener().setOnComplete((success, tryAgainTime) => {});
@@ -159,13 +160,13 @@ module.exports = function (appClient) {
159160
.setMinorVersion(0)
160161
.setPatchVersion(0);
161162
});
162-
let windowCapability = getWindowCapability(1);
163+
let windowCapability = getWindowCapability(1, true);
163164
let presentAlertOperation = new SDL.manager.screen.utils._PresentAlertOperation(lifecycleManager, alertView, windowCapability, speechCapabilities, fileManager, 1, () => {}, new SDL.manager.screen._AlertManagerBase._AlertSoftButtonClearListener().setOnButtonClear(() => {}));
164165
let alert = presentAlertOperation.alertRpc();
165166

166167
Validator.assertEquals(alert.getAlertText1(), `${alertView.getText()} - ${alertView.getSecondaryText()} - ${alertView.getTertiaryText()}`);
167168

168-
windowCapability = getWindowCapability(2);
169+
windowCapability = getWindowCapability(2, true);
169170

170171
presentAlertOperation = new SDL.manager.screen.utils._PresentAlertOperation(lifecycleManager, alertView, windowCapability, speechCapabilities, fileManager, 1, () => {}, new SDL.manager.screen._AlertManagerBase._AlertSoftButtonClearListener().setOnButtonClear(() => {}));
171172
alert = presentAlertOperation.alertRpc();
@@ -200,7 +201,7 @@ module.exports = function (appClient) {
200201
// Test Images need to be uploaded, sending text and uploading images
201202
await presentAlertOperation.onExecute();
202203

203-
// Verifies that uploadArtworks gets called only with the fist presentAlertOperation.onExecute call
204+
// Verifies that uploadArtworks gets called only with the first presentAlertOperation.onExecute call
204205
Validator.assertTrue(artStub.calledOnce);
205206
Validator.assertTrue(fileStub.calledOnce);
206207
Validator.assertTrue(alertStub.calledOnce);
@@ -235,7 +236,7 @@ module.exports = function (appClient) {
235236
// Test Images need to be uploaded, sending text and uploading images
236237
await presentAlertOperation._start();
237238

238-
// Verifies that uploadArtworks gets called only with the fist presentAlertOperation.onExecute call
239+
// Verifies that uploadArtworks gets called only with the first presentAlertOperation.onExecute call
239240
Validator.assertTrue(artStub.notCalled);
240241
Validator.assertTrue(fileStub.notCalled);
241242
Validator.assertTrue(alertStub.calledOnce);
@@ -263,7 +264,7 @@ module.exports = function (appClient) {
263264
// Test Images need to be uploaded, sending text and uploading images
264265
await presentAlertOperation._start();
265266

266-
// Verifies that uploadArtworks gets called only with the fist presentAlertOperation.onExecute call
267+
// Verifies that uploadArtworks gets called only with the first presentAlertOperation.onExecute call
267268
Validator.assertTrue(artStub.calledOnce);
268269
Validator.assertTrue(alertStub.calledOnce);
269270

@@ -289,9 +290,44 @@ module.exports = function (appClient) {
289290
});
290291

291292
it('testImageSetOnSuccessfulUpload', async function () {
292-
presentAlertOperation._uploadedImageNames.add(alertView.getIcon().getName());
293+
presentAlertOperation._alertIconUploaded = true;
293294
const alertRpc = presentAlertOperation.alertRpc();
294295
Validator.assertEquals(alertRpc.getAlertIcon().getValueParam(), alertView.getIcon().getName());
295296
});
297+
298+
it('testPresentStaticIcon', async function () {
299+
const alertStub = sinon.stub(lifecycleManager, 'sendRpcResolve')
300+
.callsFake(onAlertSuccess);
301+
const artStub = sinon.stub(fileManager, 'uploadArtworks')
302+
.callsFake(onArtworkUploadSuccess);
303+
const fileStub = sinon.stub(fileManager, 'uploadFiles')
304+
.callsFake(onArtworkUploadSuccess);
305+
const versionStub = sinon.stub(lifecycleManager, 'getSdlMsgVersion')
306+
.callsFake(function () {
307+
return new SDL.rpc.structs.SdlMsgVersion()
308+
.setMajorVersion(6)
309+
.setMinorVersion(0)
310+
.setPatchVersion(0);
311+
});
312+
313+
314+
testAlertArtwork = new SDL.manager.file.filetypes.SdlArtwork(SDL.manager.file.enums.StaticIconName.LEFT);
315+
testAlertArtwork.setStaticIcon(true);
316+
alertView.setIcon(testAlertArtwork);
317+
alertView.setSoftButtons([]);
318+
319+
presentAlertOperation = new SDL.manager.screen.utils._PresentAlertOperation(lifecycleManager, alertView, defaultMainWindowCapability, speechCapabilities, fileManager, 1, alertCompletionListener, alertSoftButtonClearListener);
320+
// Test Images need to be uploaded, sending text and uploading images
321+
await presentAlertOperation._start();
322+
323+
// upload artworks should not be called since this is a static icon
324+
Validator.assertTrue(artStub.notCalled);
325+
Validator.assertTrue(alertStub.calledOnce);
326+
327+
versionStub.restore();
328+
fileStub.restore();
329+
artStub.restore();
330+
alertStub.restore();
331+
});
296332
});
297333
};

0 commit comments

Comments
 (0)