Skip to content

Commit

Permalink
fix(android): guard access to some toast properties (#12826)
Browse files Browse the repository at this point in the history
Fixes TIMOB-28448
  • Loading branch information
build committed May 24, 2021
1 parent 8ea405a commit 765a1ec
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ public TiUINotification(TiViewProxy proxy)
@Override
public void processProperties(KrollDict d)
{

float horizontalMargin = toast.getHorizontalMargin();
float verticalMargin = toast.getVerticalMargin();
int offsetX = toast.getXOffset();
int offsetY = toast.getYOffset();
int gravity = toast.getGravity();

if (proxy.hasProperty(TiC.PROPERTY_MESSAGE)) {
toast.setText(TiConvert.toString(proxy.getProperty(TiC.PROPERTY_MESSAGE)));
}
Expand All @@ -49,30 +42,43 @@ public void processProperties(KrollDict d)
toast.setDuration(duration);
}

//float horizontalMargin, float verticalMargin
if (proxy.hasProperty("horizontalMargin")) {
horizontalMargin = TiConvert.toFloat(proxy.getProperty("horizontalMargin"));
}
// Only change layout if we have custom margins
if (proxy.hasProperty("horizontalMargin") || proxy.hasProperty("verticalMargin")) {
float horizontalMargin = toast.getHorizontalMargin();
float verticalMargin = toast.getVerticalMargin();

if (proxy.hasProperty("verticalMargin")) {
verticalMargin = TiConvert.toFloat(proxy.getProperty("verticalMargin"));
}
// float horizontalMargin, float verticalMargin
if (proxy.hasProperty("horizontalMargin")) {
horizontalMargin = TiConvert.toFloat(proxy.getProperty("horizontalMargin"));
}

toast.setMargin(horizontalMargin, verticalMargin);
if (proxy.hasProperty("verticalMargin")) {
verticalMargin = TiConvert.toFloat(proxy.getProperty("verticalMargin"));
}

if (proxy.hasProperty("offsetX")) {
offsetX = TiConvert.toInt(proxy.getProperty("offsetX"));
toast.setMargin(horizontalMargin, verticalMargin);
}

if (proxy.hasProperty("offsetY")) {
offsetY = TiConvert.toInt(proxy.getProperty("offsetY"));
}
// Only change gravity if we have custom offsets / gravity
if (proxy.hasProperty("offsetX") || proxy.hasProperty("offsetY") || proxy.hasProperty("gravity")) {
int offsetX = toast.getXOffset();
int offsetY = toast.getYOffset();
int gravity = toast.getGravity();

if (proxy.hasProperty("gravity")) {
gravity = TiConvert.toInt(proxy.getProperty("gravity"));
}
if (proxy.hasProperty("offsetX")) {
offsetX = TiConvert.toInt(proxy.getProperty("offsetX"));
}

if (proxy.hasProperty("offsetY")) {
offsetY = TiConvert.toInt(proxy.getProperty("offsetY"));
}

toast.setGravity(gravity, offsetX, offsetY);
if (proxy.hasProperty("gravity")) {
gravity = TiConvert.toInt(proxy.getProperty("gravity"));
}

toast.setGravity(gravity, offsetX, offsetY);
}

super.processProperties(d);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Resources/ti.ui.notification.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2021-Present by Axway. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
/* eslint promise/no-callback-in-promise: "off" */
'use strict';
const should = require('./utilities/assertions');

describe('Titanium.UI.Notification', function () {
this.timeout(10000);

let window;
afterEach(done => {
if (window && !window.closed) {
window.close().then(() => done()).catch(_e => done());
} else {
window = null;
done();
}
});

describe('methods', () => {
describe('#show()', () => {
it('is a Function', () => {
const notification = Ti.UI.createNotification({
title: 'Hello world',
duration: Ti.UI.NOTIFICATION_DURATION_SHORT
});

should(notification).have.a.property('show').which.is.a.Function();
});

it('does not crash', finish => {
const notification = Ti.UI.createNotification({
title: 'Hello world',
duration: Ti.UI.NOTIFICATION_DURATION_SHORT
});

window = Ti.UI.createWindow();
window.open().then(notification.show).catch(finish);
});
});
});
});

0 comments on commit 765a1ec

Please sign in to comment.