-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathComponentTests.cs
424 lines (357 loc) · 13 KB
/
ComponentTests.cs
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
using System.Collections.Generic;
using System.Linq;
using Alexa.NET.APL.Commands;
using Alexa.NET.APL.Components;
using Alexa.NET.APL.DataSources;
using Alexa.NET.Response;
using Alexa.NET.Response.APL;
using Alexa.NET.Response.Directive;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Alexa.NET.APL.Tests
{
public class ComponentTests
{
[Fact]
public void ComponentTypes()
{
var result = GenerateComponent("Container");
Assert.IsType<Container>(result);
result = GenerateComponent("Text");
Assert.IsType<Text>(result);
}
[Fact]
public void Bindings()
{
var component = Utility.ExampleFileContent<Text>("Binding.json");
component.When = APLValue.To<bool?>("${@viewportProfile == @hubLandscapeSmall}");
Assert.Equal(2, component.Bindings.Count);
var first = component.Bindings.First();
Assert.Equal("foo", first.Name);
Assert.Equal("27", first.Value);
var second = component.Bindings.Skip(1).First();
Assert.Equal("bar", second.Name);
Assert.Equal("${foo + 23}", second.Value);
}
[Fact]
public void RandomClassTest()
{
var component = GenerateComponent("random");
Assert.IsType<CustomComponent>(component);
Assert.Single(((CustomComponent)component).Properties);
}
[Fact]
public void VideoComponent()
{
Utility.AssertComponent<Video>("Video.json");
}
[Fact]
public void AalmadaTest()
{
var response = ResponseBuilder
.Ask("Welcome to my skill. How can I help", new Reprompt());
var json = JObject.FromObject(new APLDocument(APLDocumentVersion.V1_2));
var launchTemplateApl = JObject.FromObject(json);
var launchTemplateData = new ObjectDataSource
{
ObjectId = "launchScreen",
Properties = new Dictionary<string, object>(),
TopLevelData = new Dictionary<string, object>(),
};
launchTemplateData.Properties.Add("textContent", "My Skill");
launchTemplateData.Properties.Add("hintText", "Try, \"What can you do?\"");
var directive = CreateAplDirective(launchTemplateApl, ("launchTemplateData", launchTemplateData));
response.Response.Directives.Add(directive);
JsonConvert.SerializeObject(response);
}
static JsonDirective CreateAplDirective(JObject apl, params (string Key, ObjectDataSource Value)[] dataSources)
{
var directive = new JsonDirective(RenderDocumentDirective.APLDirectiveType);
directive.Properties.Add("document", apl);
var sources = new Dictionary<string, ObjectDataSource>();
foreach (var (key, dataSource) in dataSources)
sources.Add(key, dataSource);
directive.Properties.Add("dataSources", JObject.FromObject(sources));
return directive;
}
[Fact]
public void ContainerTest()
{
new Container
(
new Text("APL in C#")
{
FontSize = "24dp",
TextAlign = "Center",
},
new Image(
"https://images.example.com/photos/2143/lights-party-dancing-music.jpg?cs=srgb&dl=cheerful-club-concert-2143.jpg&fm=jpg")
{
Width = 400,
Height = 400,
}
);
}
[Fact]
public void APLComponentValue()
{
var text = new Text("Hello World")
{
Color = APLValue.To<string>("${color}"),
Disabled = APLValue.To<bool?>("${disabled}"),
FontSize = "24dp",
Left = new AbsoluteDimension(24, "vw"),
PaddingLeft = new RelativeDimension(5),
Top = "${top}",
Right = new APLDimensionValue(new AbsoluteDimension(345,"dp")),
Bottom = new APLDimensionValue("test")
};
var jobject = JObject.FromObject(text);
Assert.Equal("24dp", jobject.Value<string>("fontSize"));
Assert.Equal("24vw", jobject.Value<string>("left"));
Assert.Equal("5%", jobject.Value<string>("paddingLeft"));
Assert.Equal("${top}", jobject.Value<string>("top"));
Assert.Equal("345dp", jobject.Value<string>("right"));
Assert.Equal("test", jobject.Value<string>("bottom"));
}
[Fact]
public void ImageFilters()
{
Utility.AssertComponent<Image>("ImageFilters.json");
}
[Fact]
public void TimeText()
{
var timeText = new TimeText
{
Direction = TimeTextDirection.Down,
Format = "%M:%S",
Start = 1552070232
};
Assert.True(Utility.CompareJson(timeText, "TimeText.json"));
}
[Fact]
public void KeyboardEvent()
{
Utility.AssertComponent<APLComponent>("KeyboardTouchWrapper.json");
}
[Fact]
public void AlexaIconButton()
{
var control = new AlexaIconButton
{
ButtonSize = new AbsoluteDimension(72, "dp"),
VectorSource =
"M21.343,8.661l-7.895-7.105c-0.823-0.741-2.073-0.741-2.896,0L2.657,8.661C2.238,9.039,2,9.564,2,10.113V20c0,1.105,0.943,2,2.105,2H9v-9h6v9h4.895C21.057,22,22,21.105,22,20v-9.887C22,9.564,21.762,9.039,21.343,8.661z",
PrimaryAction = new APLCommand[]{new SetValue
{
ComponentId = "textToUpdate",
Property = "text",
Value = APLValue.To<string>("${exampleData.imageStyleText}")
}}.ToList()
};
Assert.True(Utility.CompareJson(control,"AlexaIconButton.json"));
}
[Fact]
public void AlexaImageListItem()
{
var control = new AlexaImageListItem
{
Theme = "dark",
PrimaryText = "${exampleData.primaryText}",
SecondaryText = "${exampleData.secondaryText}",
TertiaryText = "${exampleData.tertiaryText}",
ProviderText = "${exampleData.providerText}",
ImageProgressBarPercentage = 75,
ImageRoundedCorner = true,
ImageAspectRatio = AlexaImageAspectRatio.Square,
ImageSource = "${exampleData.imageSource}"
};
Assert.True(Utility.CompareJson(control,"AlexaImageListItem.json"));
}
[Fact]
public void AlexaRating()
{
var control = new AlexaRating
{
RatingSlotPadding = new AbsoluteDimension(0,"dp"),
RatingSlotMode = RatingSlotMode.Multiple,
RatingNumber = 3.5,
RatingText = "509 ratings",
Spacing = "@spacingMedium"
};
Assert.True(Utility.CompareJson(control,"AlexaRating.json"));
}
[Fact]
public void AlexaImageList()
{
var control = new AlexaImageList
{
ListItems = APLValue.To<IList<AlexaImageListItem>>("${imageListData.listItemsToShow}"),
DefaultImageSource = "https://d2o906d8ln7ui1.cloudfront.net/images/BT7_Background.png",
ImageBlurredBackground = true,
PrimaryAction = (new APLCommand[]
{
new SendEvent
{
Arguments = new[]{(object)"ListItemSelected", "${ordinal}"}.ToList()
}
}.ToList())
};
Assert.True(Utility.CompareJson(control,"AlexaImageList.json"));
}
[Fact]
public void AlexaLists()
{
var control = new AlexaLists
{
ListItems = APLValue.To<IList<AlexaListItem>>("${listData.listItemsToShow}"),
ListImagePrimacy = true,
DefaultImageSource = "https://d2o906d8ln7ui1.cloudfront.net/images/BT7_Background.png",
ImageBlurredBackground = true
};
Assert.True(Utility.CompareJson(control, "AlexaLists.json"));
}
[Fact]
public void AlexaPaginatedList()
{
Utility.AssertComponent<AlexaPaginatedList>("AlexaPaginatedList.json");
}
[Fact]
public void TickHandler()
{
Utility.AssertComponent<Container>("TickHandler.json");
}
[Fact]
public void ProgressBar()
{
Utility.AssertComponent<AlexaProgressBar>("AlexaProgressBar.json");
}
[Fact]
public void ProgressBarRadial()
{
Utility.AssertComponent<Container>("AlexaProgressBarRadial.json");
}
[Fact]
public void ProgressDots()
{
Utility.AssertComponent<AlexaProgressDots>("AlexaProgressDots.json");
}
[Fact]
public void Slider()
{
Utility.AssertComponent<AlexaSlider>("AlexaSlider.json");
}
[Fact]
public void SliderRadial()
{
Utility.AssertComponent<Container>("AlexaSliderRadial.json");
}
[Fact]
public void AlexaDetailRecipe()
{
Utility.AssertComponent<AlexaDetail>("AlexaDetailRecipe.json");
}
[Fact]
public void AlexaDetailTv()
{
Utility.AssertComponent<AlexaDetail>("AlexaDetailTv.json");
}
[Fact]
public void AlexaGridList()
{
Utility.AssertComponent<AlexaGridList>("AlexaGridList.json");
}
[Fact]
public void EditText()
{
Utility.AssertComponent<EditText>("EditText.json");
}
[Fact]
public void SwipeToAction()
{
Utility.AssertComponent<AlexaSwipeToAction>("AlexaSwipeToAction.json");
}
[Fact]
public void AlexaRadioButton()
{
Utility.AssertComponent<AlexaRadioButton>("AlexaRadioButton.json");
}
[Fact]
public void AlexaCheckbox()
{
Utility.AssertComponent<AlexaCheckbox>("AlexaCheckbox.json");
}
[Fact]
public void AlexaSwitch()
{
Utility.AssertComponent<AlexaSwitch>("AlexaSwitch.json");
}
[Fact]
public void GridSequence()
{
Utility.AssertComponent<GridSequence>("GridSequence.json");
}
[Fact]
public void Pager()
{
Utility.AssertComponent<Pager>("Pager.json");
}
[Fact]
public void AlexaIcon()
{
Utility.AssertComponent<AlexaIcon>("AlexaIcon.json");
}
[Fact]
public void AlexaCard()
{
Utility.AssertComponent<AlexaCard>("AlexaCard.json");
}
[Fact]
public void AlexaImageCaption()
{
Utility.AssertComponent<AlexaImageCaption>("AlexaImageCaption.json");
}
[Fact]
public void AlexaPhoto()
{
Utility.AssertComponent<AlexaPhoto>("AlexaPhoto.json");
}
[Fact]
public void AlexaTextWrapping()
{
Utility.AssertComponent<AlexaTextWrapping>("AlexaTextWrapping.json");
}
[Fact]
public void Frame()
{
Utility.AssertComponent<Frame>("Frame.json");
}
[Fact]
public void DictionaryBindingTest()
{
var rawContainer = new Container
{
Data = new[]{new Dictionary<string, object> { { "test", "thing" } }},
};
var dataBoundContainer = new Container
{
Data = APLValue.To<IList<object>>("$data.random.stuff")
};
var rawJson = JsonConvert.SerializeObject(rawContainer);
var boundJson = JsonConvert.SerializeObject(dataBoundContainer);
var newRaw = JsonConvert.DeserializeObject<APLComponent>(rawJson);
var newBound = JsonConvert.DeserializeObject<APLComponent>(boundJson);
var newRawContainer = Assert.IsType<Container>(newRaw);
var newBoundContainer = Assert.IsType<Container>(newBound);
Assert.Single((JObject)newRawContainer.Data.Value.First());
Assert.Equal("$data.random.stuff", newBoundContainer.Data.Expression);
}
private APLComponent GenerateComponent(string componentType)
{
var json = new JObject { { "type", componentType }, { "numbered", true } };
return JsonConvert.DeserializeObject<APLComponent>(json.ToString());
}
}
}