-
Notifications
You must be signed in to change notification settings - Fork 31
/
switch.lua
287 lines (236 loc) · 7.19 KB
/
switch.lua
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
-- Abstract: widget.newSwitch() unit test
-- Code is MIT licensed; see https://www.coronalabs.com/links/code/license
---------------------------------------------------------------------------------------
local widget = require( "widget" )
local composer = require( "composer" )
local scene = composer.newScene()
local isGraphicsV1 = ( 1 == display.getDefault( "graphicsCompatibility" ) )
--Forward reference for test function timer
local testTimer = nil
function scene:create( event )
local group = self.view
local xAnchor, yAnchor
if not isGraphicsV1 then
xAnchor = display.contentCenterX
yAnchor = display.contentCenterY
else
xAnchor = 0
yAnchor = 0
end
local fontColor = 0
local background = display.newRect( xAnchor, yAnchor, display.contentWidth, display.contentHeight )
if widget.USE_IOS_THEME then
if isGraphicsV1 then background:setFillColor( 197, 204, 212, 255 )
else background:setFillColor( 197/255, 204/255, 212/255, 1 ) end
elseif widget.USE_ANDROID_HOLO_LIGHT_THEME then
if isGraphicsV1 then background:setFillColor( 255, 255, 255, 255 )
else background:setFillColor( 1, 1, 1, 1 ) end
elseif widget.USE_ANDROID_HOLO_DARK_THEME then
if isGraphicsV1 then background:setFillColor( 34, 34, 34, 255 )
else background:setFillColor( 34/255, 34/255, 34/255, 1 ) end
fontColor = 0.5
else
if isGraphicsV1 then background:setFillColor( 255, 255, 255, 255 )
else background:setFillColor( 1, 1, 1, 1 ) end
end
group:insert( background )
local backButtonPosition = 5
local backButtonSize = 34
local fontUsed = native.systemFont
--Button to return to unit test listing
local returnToListing = widget.newButton
{
id = "returnToListing",
left = display.contentWidth * 0.5,
top = backButtonPosition,
label = "Exit",
width = 200, height = backButtonSize,
cornerRadius = 8,
onRelease = function() composer.gotoScene( "unitTestListing" ) end;
}
returnToListing.x = display.contentCenterX
group:insert( returnToListing )
----------------------------------------------------------------------------------------------------------------
-- START OF UNIT TEST
----------------------------------------------------------------------------------------------------------------
--Toggle these defines to execute automated tests.
local TEST_REMOVE_SWITCH = false
local TEST_SET_STATE = false
local TEST_DELAY = 1000
local radioGroup = {}
local function onRadioPress( event )
local self = event.target
-- Turn off other radio buttons in this set
--[[
for k, v in pairs( radioGroup ) do
if radioGroup[k].id ~= self.id then
radioGroup[k]:setState( { isOn = false } )
end
end
--]]
print( self.id, "is on?:", self.isOn )
end
local function onCheckBoxPress( event )
local self = event.target
print( self.id, "is on?:", self.isOn )
end
local function onOnOffPress( event )
local self = event.target
print( self.id, "is on?:", self.isOn )
--display.remove( self )
end
local radioGroup = display.newGroup()
-- Create a radio switch
local radioButton = widget.newSwitch
{
left = 0,
top = 120,
style = "radio",
id = "Radio button1",
initialSwitchState = true,
onPress = onRadioPress,
}
radioButton.x = display.contentCenterX
radioGroup:insert( radioButton )
-- Create a radio switch
local radioButton2 = widget.newSwitch
{
left = 80,
top = 120,
style = "radio",
id = "Radio button2",
initialSwitchState = false,
onPress = onRadioPress,
}
radioGroup:insert( radioButton2 )
-- Create a radio switch
local radioButton3 = widget.newSwitch
{
left = 20,
top = 120,
style = "radio",
id = "Radio button3",
initialSwitchState = false,
onPress = onRadioPress,
}
radioGroup:insert( radioButton3 )
group:insert( radioGroup )
--
local radioButtonText1 = display.newText( "< Set 1", 0, 0, fontUsed, 18 )
radioButtonText1.x = 70 + radioButton.x + radioButtonText1.contentWidth * 0.5
radioButtonText1.y = radioButton.y - 2
radioButtonText1:setFillColor( fontColor )
group:insert( radioButtonText1 )
---- other
local otherRadioGroup = display.newGroup()
-- Create a radio switch
local radioButtonOther = widget.newSwitch
{
left = 20,
top = 160,
style = "radio",
id = "Radio button4",
initialSwitchState = false,
onPress = onRadioPress,
}
otherRadioGroup:insert( radioButtonOther )
-- Create a radio switch
local radioButtonOther2 = widget.newSwitch
{
left = 80,
top = 160,
style = "radio",
id = "Radio button4",
initialSwitchState = false,
onPress = onRadioPress,
}
otherRadioGroup:insert( radioButtonOther2 )
group:insert( otherRadioGroup )
--
local radioButtonText2 = display.newText( "< Set 2", 0, 0, fontUsed, 18 )
radioButtonText2.x = radioButtonText1.x
radioButtonText2.y = radioButtonOther2.y - 2
radioButtonText2:setFillColor( fontColor )
group:insert( radioButtonText2 )
-- Create a checkbox switch
local checkboxButton = widget.newSwitch
{
left = 0,
top = 200,
style = "checkbox",
id = "Checkbox button",
onPress = onCheckBoxPress,
}
checkboxButton.x = display.contentCenterX
group:insert( checkboxButton )
local onOffSwitch = widget.newSwitch
{
left = 0,
top = 300,
style = "onOff",
initialSwitchState = false,
onRelease = onOnOffPress
}
onOffSwitch.x = display.contentCenterX
group:insert( onOffSwitch )
-- Skinned On-Off Switch
--[[local onOffSwitchOptions = {
frames =
{
{ x=0, y=0, width=160, height=44 },
{ x=0, y=45, width=42, height=42 },
{ x=44, y=45, width=42, height=42 },
{ x=88, y=44, width=96, height=44 }
},
sheetContentWidth = 184,
sheetContentHeight = 88
}
local onOffSwitchSheet = graphics.newImageSheet( "unitTestAssets/on-off-sheet.png", onOffSwitchOptions )
local switch = widget.newSwitch {
left = 100,
top = 200,
style = "onOff",
sheet = onOffSwitchSheet,
onOffMask = "unitTestAssets/switch-mask.png",
onOffBackgroundFrame = 1,
onOffBackgroundWidth = 160,
onOffBackgroundHeight = 44,
onOffOverlayFrame = 4,
onOffOverlayWidth = 96,
onOffOverlayHeight = 44,
onOffHandleDefaultFrame = 2,
onOffHandleOverFrame = 2,
}--]]
----------------------------------------------------------------------------------------------------------------
-- TESTS
----------------------------------------------------------------------------------------------------------------
-- Test removing switch
if TEST_REMOVE_SWITCH then
timer.performWithDelay( 100, function()
display.remove( radioButton )
display.remove( checkboxButton )
display.remove( onOffSwitch )
TEST_DELAY = TEST_DELAY + TEST_DELAY
end )
end
-- Test toggling the switch programatically
if TEST_SET_STATE then
timer.performWithDelay( 1000, function()
onOffSwitch:setState( { isOn = false, isAnimated = true, onComplete = onOnOffPress } )
checkboxButton:setState( { isOn = true, isAnimated = true, onComplete = onCheckBoxPress } )
end)
end
end
function scene:hide( event )
if ( "did" == event.phase ) then
--Cancel test timer if active
if testTimer ~= nil then
timer.cancel( testTimer )
testTimer = nil
end
composer.removeHidden( false )
end
end
scene:addEventListener( "create", scene )
scene:addEventListener( "hide", scene )
return scene