Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): guard access to some toast properties #12815

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});
});
});