/
properties.js
121 lines (102 loc) · 3.91 KB
/
properties.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var win = Titanium.UI.currentWindow;
/**
* Result helper for checking a result against an expected value
* @param result The result to test
* @param expected The expected result
* @return String Indicating Success or Failure
*/
function resultHelper(result, expected) {
if (result instanceof Array) {
var sourceResult = JSON.stringify(result);
var expectedResult = JSON.stringify(expected);
return resultHelper(sourceResult, expectedResult);
}
if (result == expected) {
return "Test Success ("+result+"=="+expected+")";
} else {
return "Test Failure: result (" + result + ") != expected (" + expected + ")";
}
}
var l = Titanium.UI.createLabel({
text:'See Log for output',
height:'auto',
width:'auto',
font: {
fontSize: 24
}
});
win.add(l);
var array = [
{name:'Name 1', address:'1 Main St'},
{name:'Name 2', address:'2 Main St'},
{name:'Name 3', address:'3 Main St'},
{name:'Name 4', address:'4 Main St'}
];
//
// Test Default handling
//
//Valid Defaults
Titanium.API.debug('Bool: ' + resultHelper(Ti.App.Properties.getBool('whatever',true),true));
Titanium.API.debug('Double: ' + resultHelper(Ti.App.Properties.getDouble('whatever',2.5),2.5));
Titanium.API.debug('int: ' + resultHelper(Ti.App.Properties.getInt('whatever',1),1));
Titanium.API.debug('String: ' + resultHelper(Ti.App.Properties.getString('whatever',"Fred"),"Fred"));
// First StringList Test
var defaultString = new Array("testOne","testTwo");
Titanium.API.debug('StringList-1: ' + resultHelper(Ti.App.Properties.getList('whatever',defaultString),defaultString));
// Second StringList Test
defaultString = new Array();
Titanium.API.debug('StringList-2: ' + resultHelper(Ti.App.Properties.getList('whatever',defaultString),defaultString));
//No Defaults
Titanium.API.debug('Bool: ' + resultHelper(Ti.App.Properties.getBool('whatever'),null));
Titanium.API.debug('Double: ' + resultHelper(Ti.App.Properties.getDouble('whatever'),null));
Titanium.API.debug('int: ' + resultHelper(Ti.App.Properties.getInt('whatever'),null));
Titanium.API.debug('String: ' + resultHelper(Ti.App.Properties.getString('whatever'),null));
Titanium.API.debug('StringList: ' + resultHelper(Ti.App.Properties.getList('whatever'),null));
//
// Round-trip tests
//
//
// test setters
//
Titanium.App.Properties.setString('String','I am a String Value ');
Titanium.App.Properties.setInt('Int',10);
Titanium.App.Properties.setBool('Bool',true);
Titanium.App.Properties.setDouble('Double',10.6);
Titanium.App.Properties.setList('MyList',array);
//
// test getters
//
Titanium.API.info('String: '+ Titanium.App.Properties.getString('String'));
Titanium.API.info('Int: '+ Titanium.App.Properties.getString('Int'));
Titanium.API.info('Bool: '+ Titanium.App.Properties.getString('Bool'));
Titanium.API.info('Double: '+ Titanium.App.Properties.getString('Double'));
Titanium.API.info('List:');
var list = Titanium.App.Properties.getList('MyList');
for (var i=0;i<list.length;i++)
{
Titanium.API.info('row['+i+'].name=' + list[i].name + ' row['+i+'].address=' + list[i].address );
}
//
// test listProperties
//
var props = Titanium.App.Properties.listProperties();
for (var i=0;i<props.length;i++)
{
Titanium.API.info('property: ' + props[i]);
}
//
// test out remove property and setting to null
//
Titanium.App.Properties.setString('String',null);
Titanium.App.Properties.removeProperty('Int');
Titanium.API.info("String should be null - value = " + resultHelper(Titanium.App.Properties.getString('String'),null));
Titanium.API.info("Int should be null - value = " + resultHelper(Titanium.App.Properties.getString('Int'),null));
//
// application settings testing
//
if (Titanium.Platform.name != 'android')
{
Titanium.API.info("AppSetting Name = " + Titanium.App.Properties.getString('name_preference'));
Titanium.API.info("AppSetting Enabled = " + Titanium.App.Properties.getString('enabled_preference'));
Titanium.API.info("AppSetting Slider = " + Titanium.App.Properties.getString('slider_preference'));
}