Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created December 7, 2016 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hansemannn/108aaa09c478f53be9174d2d169e94fb to your computer and use it in GitHub Desktop.
Save hansemannn/108aaa09c478f53be9174d2d169e94fb to your computer and use it in GitHub Desktop.
Advanced test-case for fixing all memory-leaks in Titanium (TIMOB-23542)
var win = Ti.UI.createWindow({
backgroundColor: '#fff',
title: 'TIMOB-23542'
});
Ti.App.iOS.addEventListener("shortcutitemclick", function(e) {
Ti.API.info(e);
});
var items = [{
identifier: "calendar",
title: "Ti.Calendar"
}, {
identifier: "getAsset",
title: "Ti.Filesystem.getAsset"
}, {
identifier: "nativeModule",
title: "Native module"
}, {
identifier: "requestPhotoGalleryPermissions",
title: "Ti.Media.requestPhotoGalleryPermissions"
}, {
identifier: "shortcutitems",
title: "Ti.App.iOS.shortcutitems"
}, {
identifier: "scheduleLocalNotification",
title: "Ti.App.iOS.scheduleLocalNotification"
}, {
identifier: "searchQuery",
title: "Ti.App.iOS.SearchQuery"
}, {
identifier: "previewContext",
title: "Ti.App.iOS.PreviewContext"
}, {
identifier: "hintTextColor",
title: "Ti.UI.TextField.hintTextColor"
}, {
identifier: "webViewData",
title: "Ti.UI.WebView.data"
}, {
identifier: "livePhotoBadge",
title: "Ti.UI.iOS.LivePhotoBadge"
}, {
identifier: "activityIndicatorStyle",
title: "Ti.UI.ActivityIndicatorStyle"
}];
var list = Ti.UI.createListView({
style: Ti.UI.iOS.ListViewStyle.GROUPED
});
var section = Ti.UI.createListSection();
var cells = [];
for (var i = 0; i < items.length; i++) {
var item = items[i];
cells.push({
properties: {
itemId: item.identifier,
title: item.title,
height: 43
}
});
}
section.setItems(cells);
list.setSections([section])
list.addEventListener('itemclick', function(e) {
try {
tests[e.itemId]();
} catch (_e) {
Ti.API.error("Cannot invoke test method: test_" + e.itemId + ": " + _e);
}
this.deselectItem(e.sectionIndex, e.itemIndex);
});
win.add(list);
var nav = Ti.UI.iOS.createNavigationWindow({
window: win
});
nav.open();
var tests = {
calendar: function() {
if (!Ti.Calendar.hasCalendarPermissions()) {
Ti.Calendar.requestCalendarPermissions(function(e) {
if (!e.success) {
Ti.API.error("Cannot grant calendar permissions!");
return;
}
Ti.API.info(Ti.Calendar.allCalendars);
});
} else {
Ti.API.info(Ti.Calendar.allCalendars);
}
},
getAsset: function() {
// Resolves to the md5-hash of the asset in the asset catalog
// Note: needs to be called from a bundled SDK, since the CLI generates the hashes
var img = Ti.Filesystem.getAsset("LaunchLogo");
var win2 = Ti.UI.createWindow({
title: "Test getAsset()",
backgroundColor: "#fff"
});
win2.add(Ti.UI.createImageView({
image: img
}));
nav.openWindow(win2, {
modal: true
});
},
nativeModule: function() {
// Note: Include ti.facebook to check an example for modules
// Note: needs to be called from a bundled SDK, since the CLI links the modules
var Facebook = require("ti.facebook");
},
requestPhotoGalleryPermissions: function() {
if (!Ti.Media.hasPhotoGalleryPermissions()) {
Ti.Media.requestPhotoGalleryPermissions(function(e) {
if (!e.success) {
Ti.API.error("Cannot grant photo gallery permissions!");
return;
}
alert("Permissions granted!");
});
} else {
alert("Permissions granted!");
}
},
shortcutitems: function() {
/** Add the following shortcuts to your tiapp.xml / <ios> / <plist> first:
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeSearch</string>
<key>UIApplicationShortcutItemTitle</key>
<string>My title</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>My subtitle</string>
<key>UIApplicationShortcutItemType</key>
<string>my_identifier</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict/>
</dict>
</array>
**/
alert("Click on shortcuts and watch the event");
},
scheduleLocalNotification: function() {
Ti.App.iOS.scheduleLocalNotification({
date: new Date(new Date().getTime() + 3000),
title: "New Notification",
// TODO: Should be invalid to trigger, currently still valid
region: {
latitude: 0,
longitude: 0
}
});
},
searchQuery: function() {
// Should just not crash on creation
var searchQuery = Ti.App.iOS.createSearchQuery({
queryString: 'title == "Titanium*"',
attributes: ["title", "displayName", "keywords", "contentType"]
});
},
previewContext: function() {
// TODO: dealloc gets called after initializing
// Check with 5.5.x for possible regression
if (!Ti.UI.iOS.forceTouchSupported) {
alert("Please use a 3D-Touch capable device!");
return;
}
var previewContext = Ti.UI.iOS.createPreviewContext({
preview: Ti.UI.createView({
backgroundColor: "red"
}),
contentHeight: 300
});
var win2 = Ti.UI.createWindow({
title: "Test previewContext",
backgroundColor: "#fff"
});
var btn = Ti.UI.createButton({
title: 'Trigger',
width: 300,
height: 40,
backgroundColor: "red",
previewContext: previewContext
});
previewContext.addEventListener("pop", function(e) {
Ti.API.info(e);
});
win2.add(btn);
nav.openWindow(win2, {
modal: true
});
},
hintTextColor: function() {
var win2 = Ti.UI.createWindow({
title: "Test hintTextColor",
backgroundColor: "#fff"
});
var field = Ti.UI.createTextField({
width: 300,
height: 40,
backgroundColor: "#f0f0f0",
hintText: "Enter E-Mail ...",
hintTextColor: "red"
});
win2.add(field);
nav.openWindow(win2, {
modal: true
});
},
webViewData: function() {
var _data = Ti.Filesystem.getFile(Ti.Filesystem.getResourcesDirectory(), 'examples/webview_logging.html');
if (!_data) {
alert("Please add a file called \"examples/webview_logging.html\" to the resources directory to test this");
return;
}
var win2 = Ti.UI.createWindow({
title: "Test webView data",
backgroundColor: "#fff"
});
var web = Ti.UI.createWebView({
data: _data.read() // TODO: Currently doesn't trigger the blob but the file contents, need to adjust the test-case
});
win2.add(web);
nav.openWindow(win2, {
modal: true
});
},
livePhotoBadge: function() {
var win2 = Ti.UI.createWindow({
title: "Test Live Photo badge",
backgroundColor: "#333"
});
win2.add(Ti.UI.createImageView({
image: Ti.UI.iOS.createLivePhotoBadge(Ti.UI.iOS.LIVEPHOTO_BADGE_OPTIONS_OVER_CONTENT),
top: 40
}));
win2.add(Ti.UI.createImageView({
image: Ti.UI.iOS.createLivePhotoBadge(Ti.UI.iOS.LIVEPHOTO_BADGE_OPTIONS_LIVE_OFF),
top: 120
}));
nav.openWindow(win2, {
modal: true
});
},
activityIndicatorStyle: function() {
var win2 = Ti.UI.createWindow({
title: "Test Activity Indicator",
backgroundColor: "#333"
});
var loader = Ti.UI.createActivityIndicator({
style: Ti.UI.ActivityIndicatorStyle.BIG
});
win2.add(loader);
nav.openWindow(win2, {
modal: true
});
loader.show();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment