297b6f46084d61901ed8c2ad41d53e96be677e6f
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Tests / Tizen.NUI.Devel.Tests / testcase / internal / Xaml / TSApplyPropertiesVisitor.cs
1 using NUnit.Framework;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Xml;
6 using Tizen.NUI.Binding;
7 using Tizen.NUI.Xaml;
8
9 namespace Tizen.NUI.Devel.Tests
10 {
11     using tlog = Tizen.Log;
12
13     internal class MockNameSpaceResolver : IXmlNamespaceResolver
14     {
15         public global::System.Collections.Generic.IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope)
16         {
17             throw new NotImplementedException();
18         }
19
20         public string LookupNamespace(string prefix)
21         {
22             return "";
23         }
24
25         public string LookupPrefix(string namespaceName)
26         {
27             return "";
28         }
29     }
30
31     [TestFixture]
32     [Description("internal/Xaml/ApplyPropertiesVisitor")]
33     public class InternalApplyPropertiesVisitorTest
34     {
35         private const string tag = "NUITEST";
36         private ApplyPropertiesVisitor visitor;
37
38         public class MarkupExtension : IMarkupExtension
39         {
40             public object ProvideValue(global::System.IServiceProvider serviceProvider)
41             {
42                 return "provided value";
43             }
44         }
45
46         public class ArrayListOwner
47         {
48             public ArrayList ArrayList { get; } = new ArrayList();
49         }
50
51         internal class INodeImpl : INode
52         {
53             public global::System.Collections.Generic.List<string> IgnorablePrefixes { get; set; }
54             public global::System.Xml.IXmlNamespaceResolver NamespaceResolver => new INodeImpl().NamespaceResolver;
55             public INode Parent { get; set; }
56             public void Accept(IXamlNodeVisitor visitor, INode parentNode) { }
57             public INode Clone() { return new INodeImpl(); }
58         }
59
60         [SetUp]
61         public void Init()
62         {
63             tlog.Info(tag, "Init() is called!");
64             visitor = new ApplyPropertiesVisitor(new HydrationContext(), false);
65         }
66
67         [TearDown]
68         public void Destroy()
69         {
70             visitor = null;
71             tlog.Info(tag, "Destroy() is called!");
72         }
73         [Test]
74         [Category("P1")]
75         [Description("ApplyPropertiesVisitor ApplyPropertiesVisitor")]
76         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.ApplyPropertiesVisitor C")]
77         [Property("SPEC_URL", "-")]
78         [Property("CRITERIA", "CONSTR")]
79         public void ApplyPropertiesVisitorConstructor()
80         {
81             tlog.Debug(tag, $"ApplyPropertiesVisitorConstructor START");
82
83             HydrationContext context = new HydrationContext();
84             Assert.IsNotNull(context, "null HydrationContext");
85
86             ApplyPropertiesVisitor applyPropertiesVisitor = new ApplyPropertiesVisitor(context, false);
87             Assert.IsNotNull(applyPropertiesVisitor, "null ApplyPropertiesVisitor");
88             Assert.IsInstanceOf<ApplyPropertiesVisitor>(applyPropertiesVisitor, "Should return ApplyPropertiesVisitor instance.");
89
90             tlog.Debug(tag, $"ApplyPropertiesVisitorConstructor END");
91         }
92                 
93         [Test]
94         [Category("P1")]
95         [Description("ApplyPropertiesVisitor VisitingMode")]
96         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.VisitingMode A")]
97         [Property("SPEC_URL", "-")]
98         [Property("CRITERIA", "PRW")]
99         public void ApplyPropertiesVisitorVisitingMode()
100         {
101             tlog.Debug(tag, $"ApplyPropertiesVisitorVisitingMode START");
102
103             try
104             {
105                 var result = visitor.VisitingMode;
106                 tlog.Debug(tag, "VisitingMode : " + result);
107             }
108             catch (Exception e)
109             {
110                 tlog.Debug(tag, e.Message.ToString());
111                 Assert.Fail("Caught Exception : Failed!");
112             }
113
114             tlog.Debug(tag, $"ApplyPropertiesVisitorVisitingMode END");
115         }
116
117         [Test]
118         [Category("P1")]
119         [Description("ApplyPropertiesVisitor StopOnDataTemplate")]
120         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.StopOnDataTemplate A")]
121         [Property("SPEC_URL", "-")]
122         [Property("CRITERIA", "PRW")]
123         public void ApplyPropertiesVisitorStopOnDataTemplate()
124         {
125             tlog.Debug(tag, $"ApplyPropertiesVisitorStopOnDataTemplate START");
126
127             try
128             {
129                 var result = visitor.StopOnDataTemplate;
130                 tlog.Debug(tag, "StopOnDataTemplate : " + result);
131             }
132             catch (Exception e)
133             {
134                 tlog.Debug(tag, e.Message.ToString());
135                 Assert.Fail("Caught Exception : Failed!");
136             }
137
138             tlog.Debug(tag, $"ApplyPropertiesVisitorStopOnDataTemplate END");
139         }
140
141         [Test]
142         [Category("P1")]
143         [Description("ApplyPropertiesVisitor StopOnResourceDictionary ")]
144         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.StopOnResourceDictionary A")]
145         [Property("SPEC_URL", "-")]
146         [Property("CRITERIA", "PRW")]
147         public void ApplyPropertiesVisitorStopOnResourceDictionary()
148         {
149             tlog.Debug(tag, $"ApplyPropertiesVisitorStopOnResourceDictionary START");
150
151             try
152             {
153                 var result = visitor.StopOnResourceDictionary;
154                 tlog.Debug(tag, "StopOnResourceDictionary : " + result);
155             }
156             catch (Exception e)
157             {
158                 tlog.Debug(tag, e.Message.ToString());
159                 Assert.Fail("Caught Exception : Failed!");
160             }
161
162             tlog.Debug(tag, $"ApplyPropertiesVisitorStopOnResourceDictionary END");
163         }
164
165         [Test]
166         [Category("P1")]
167         [Description("ApplyPropertiesVisitor VisitNodeOnDataTemplate ")]
168         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.VisitNodeOnDataTemplate A")]
169         [Property("SPEC_URL", "-")]
170         [Property("CRITERIA", "PRW")]
171         public void ApplyPropertiesVisitorVisitNodeOnDataTemplate()
172         {
173             tlog.Debug(tag, $"ApplyPropertiesVisitorVisitNodeOnDataTemplate START");
174
175             try
176             {
177                 var result = visitor.VisitNodeOnDataTemplate;
178                 tlog.Debug(tag, "VisitNodeOnDataTemplate : " + result);
179             }
180             catch (Exception e)
181             {
182                 tlog.Debug(tag, e.Message.ToString());
183                 Assert.Fail("Caught Exception : Failed!");
184             }
185
186             tlog.Debug(tag, $"ApplyPropertiesVisitorVisitNodeOnDataTemplate END");
187         }
188
189         [Test]
190         [Category("P1")]
191         [Description("ApplyPropertiesVisitor SkipChildren")]
192         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.SkipChildren M")]
193         [Property("SPEC_URL", "-")]
194         [Property("CRITERIA", "MR")]
195         public void ApplyPropertiesVisitorSkipChildren()
196         {
197             tlog.Debug(tag, $"ApplyPropertiesVisitorSkipChildren START");
198
199             try
200             {
201                 var child = new INodeImpl();
202                 Assert.IsNotNull(child, "null INodeImpl object.");
203
204                 var parent = new INodeImpl();
205                 Assert.IsNotNull(parent, "null INodeImpl object.");
206
207                 var result = visitor.SkipChildren(child, parent);
208                 tlog.Debug(tag, "SkipChildren : " + result);
209             }
210             catch (Exception e)
211             {
212                 tlog.Debug(tag, e.Message.ToString());
213                 Assert.Fail("Caught Exception : Failed!");
214             }
215
216             tlog.Debug(tag, $"ApplyPropertiesVisitorSkipChildren END");
217         }
218
219         public class IXmlNamespaceResolverImplement : IXmlNamespaceResolver
220         {
221             public IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope)
222             {
223                 throw new NotImplementedException();
224             }
225
226             public string LookupNamespace(string prefix)
227             {
228                 throw new NotImplementedException();
229             }
230
231             public string LookupPrefix(string namespaceName)
232             {
233                 throw new NotImplementedException();
234             }
235         }
236
237         [Test]
238         [Category("P1")]
239         [Description("ApplyPropertiesVisitor IsResourceDictionary")]
240         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.IsResourceDictionary M")]
241         [Property("SPEC_URL", "-")]
242         [Property("CRITERIA", "MR")]
243         public void ApplyPropertiesVisitorIsResourceDictionary()
244         {
245             tlog.Debug(tag, $"ApplyPropertiesVisitorIsResourceDictionary START");
246
247             try
248             {
249                 HydrationContext context = new HydrationContext();
250                 IList<XmlType> list = new List<XmlType>();
251                 XmlType xmlType = new XmlType("myNameSpace", "myName", list);
252                 Assert.IsNotNull(xmlType, "null XmlType");
253                 IXmlNamespaceResolverImplement i1 = new IXmlNamespaceResolverImplement();
254                 Assert.IsNotNull(i1, "null IXmlNamespaceResolverImplement");
255                 ElementNode n1 = new ElementNode(xmlType, "myNameSpace", i1);
256                 Assert.IsNotNull(n1, "null ElementNode");
257                 context.Types[n1] = typeof(ResourceDictionary);
258                 ApplyPropertiesVisitor a2 = new ApplyPropertiesVisitor(context, false);
259                 Assert.IsNotNull(a2, "null ApplyPropertiesVisitor");
260                 bool b1 = a2.IsResourceDictionary(n1);
261                 Assert.True(b1, "Should be true");
262             }
263             catch (Exception e)
264             {
265                 tlog.Debug(tag, e.Message.ToString());
266                 Assert.Fail("Caught Exception" + e.ToString());
267             }
268             tlog.Debug(tag, $"ApplyPropertiesVisitorIsResourceDictionary END");
269
270         }
271
272         [Test]
273         [Category("P1")]
274         [Description("ApplyPropertiesVisitor Visit")]
275         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.Visit M")]
276         [Property("SPEC_URL", "-")]
277         [Property("CRITERIA", "MR")]
278         public void ApplyPropertiesVisitorVisit1()
279         {
280             tlog.Debug(tag, $"ApplyPropertiesVisitorVisit1 START");
281
282             try
283             {
284                 HydrationContext context = new HydrationContext();
285                 object o1 = new object();
286                 IXmlNamespaceResolverImplement i1 = new IXmlNamespaceResolverImplement();
287                 Assert.IsNotNull(i1, "null IXmlNamespaceResolverImplement");
288                 ValueNode valueNode = new ValueNode(o1, i1);
289                 Assert.IsNotNull(valueNode, "null ValueNode");
290
291                 INodeImpl nodeImplement = new INodeImpl();
292                 Assert.IsNotNull(nodeImplement, "null INodeImplement");
293                 context.Values[valueNode] = o1;
294                 context.Values[nodeImplement] = o1;
295                 ApplyPropertiesVisitor a2 = new ApplyPropertiesVisitor(context, false);
296                 Assert.IsNotNull(a2, "null ApplyPropertiesVisitor");
297                 a2.Visit(valueNode, nodeImplement);
298             }
299             catch (Exception e)
300             {
301                 tlog.Debug(tag, e.Message.ToString());
302                 Assert.Fail("Caught Exception" + e.ToString());
303             }
304             tlog.Debug(tag, $"ApplyPropertiesVisitorVisit1 END");
305         }
306
307         [Test]
308         [Category("P1")]
309         [Description("ApplyPropertiesVisitor Visit")]
310         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.Visit M")]
311         [Property("SPEC_URL", "-")]
312         [Property("CRITERIA", "MR")]
313         public void ApplyPropertiesVisitorVisit2()
314         {
315             tlog.Debug(tag, $"ApplyPropertiesVisitorVisit2 START");
316
317             try
318             {
319                 HydrationContext context = new HydrationContext();
320                 object o1 = new object();
321                 INodeImpl nodeImplement = new INodeImpl();
322                 Assert.IsNotNull(nodeImplement, "null INodeImplement");
323
324                 IList<XmlType> list = null;
325                 XmlType xmlType = new XmlType("myNameSpace", "myName", list);
326                 Assert.IsNotNull(xmlType, "null XmlType");
327                 IXmlNamespaceResolverImplement ix1 = new IXmlNamespaceResolverImplement();
328                 Assert.IsNotNull(ix1, "null IXmlNamespaceResolverImplement");
329                 ElementNode n1 = new ElementNode(xmlType, "myNameSpace", ix1);
330                 Assert.IsNotNull(n1, "null ElementNode");
331                 context.Values[n1] = o1;
332                 context.Values[nodeImplement] = o1;
333                 ApplyPropertiesVisitor a2 = new ApplyPropertiesVisitor(context, false);
334                 Assert.IsNotNull(a2, "null ApplyPropertiesVisitor");
335                 a2.Visit(n1, nodeImplement);
336             }
337             catch (Exception e)
338             {
339                 tlog.Debug(tag, e.Message.ToString());
340                 Assert.Fail("Caught Exception" + e.ToString());
341             }
342             tlog.Debug(tag, $"ApplyPropertiesVisitorVisit2 END");
343
344         }
345
346         [Test]
347         [Category("P1")]
348         [Description("ApplyPropertiesVisitor TryGetPropertyName")]
349         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.TryGetPropertyName M")]
350         [Property("SPEC_URL", "-")]
351         [Property("CRITERIA", "MR")]
352         public void ApplyPropertiesVisitorTryGetPropertyName()
353         {
354             tlog.Debug(tag, $"ApplyPropertiesVisitorTryGetPropertyName START");
355
356             try
357             {
358                 INodeImpl n1 = new INodeImpl();
359                 Assert.IsNotNull(n1, "null INodeImplement");
360                 INodeImpl n2 = new INodeImpl();
361                 Assert.IsNotNull(n2, "null INodeImplement");
362                 XmlName xmlName = new XmlName();
363                 Assert.IsNotNull(xmlName, "null XmlName");
364                 ApplyPropertiesVisitor.TryGetPropertyName(n1, n2, out xmlName);
365             }
366             catch (Exception e)
367             {
368                 Tizen.Log.Error(tag, "Caught Exception" + e.ToString());
369                 Assert.Fail("Caught Exception" + e.ToString());
370             }
371
372             tlog.Debug(tag, $"ApplyPropertiesVisitorTryGetPropertyName END");
373         }
374
375         [Test]
376         [Category("P1")]
377         [Description("ApplyPropertiesVisitor IsCollectionItem")]
378         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.IsCollectionItem M")]
379         [Property("SPEC_URL", "-")]
380         [Property("CRITERIA", "MR")]
381         public void ApplyPropertiesVisitorIsCollectionItem()
382         {
383             tlog.Debug(tag, $"ApplyPropertiesVisitorIsCollectionItem START");
384
385             try
386             {
387                 INodeImpl n1 = new INodeImpl();
388                 Assert.IsNotNull(n1, "null INodeImplement");
389                 INodeImpl n2 = new INodeImpl();
390                 Assert.IsNotNull(n2, "null INodeImplement");
391
392                 ApplyPropertiesVisitor.IsCollectionItem(n1, n2);
393             }
394             catch (Exception e)
395             {
396                 Tizen.Log.Error(tag, "Caught Exception" + e.ToString());
397                 Assert.Fail("Caught Exception" + e.ToString());
398             }
399
400             tlog.Debug(tag, $"ApplyPropertiesVisitorIsCollectionItem END");
401         }
402
403         //[Test]
404         //[Category("P1")]
405         //[Description("ApplyPropertiesVisitor GetContentPropertyName")]
406         //[Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.GetContentPropertyName M")]
407         //[Property("SPEC_URL", "-")]
408         //[Property("CRITERIA", "MR")]
409         //public void ApplyPropertiesVisitorGetContentPropertyName()
410         //{
411         //    tlog.Debug(tag, $"ApplyPropertiesVisitorGetContentPropertyName START");
412
413         //    try
414         //    {
415         //        System.Reflection.TypeInfo typeInfo = new System.Reflection.TypeInfo();
416         //        ApplyPropertiesVisitor.GetContentPropertyName(typeInfo);
417         //    }
418         //    catch (Exception e)
419         //    {
420         //        Tizen.Log.Error(tag, "Caught Exception" + e.ToString());
421         //        Assert.Fail("Caught Exception" + e.ToString());
422         //    }
423
424         //    tlog.Debug(tag, $"ApplyPropertiesVisitorGetContentPropertyName END");
425         //    Assert.Pass("ApplyPropertiesVisitorGetContentPropertyName");
426         //}
427
428         public class IXmlLineInfoImplement : IXmlLineInfo
429         {
430             public int LineNumber => throw new NotImplementedException();
431
432             public int LinePosition => throw new NotImplementedException();
433
434             public bool HasLineInfo()
435             {
436                 throw new NotImplementedException();
437             }
438         }
439
440         //[Test]
441         //[Category("P1")]
442         //[Description("ApplyPropertiesVisitor SetPropertyValue")]
443         //[Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.SetPropertyValue M")]
444         //[Property("SPEC_URL", "-")]
445         //[Property("CRITERIA", "MR")]
446         //public void ApplyPropertiesVisitorSetPropertyValue()
447         //{
448         //    tlog.Debug(tag, $"ApplyPropertiesVisitorSetPropertyValue START");
449
450         //    try
451         //    {
452         //        object o1 = new object();
453         //        XmlName xmlName = new XmlName();
454         //        Assert.IsNotNull(xmlName, "null XmlName");
455         //        object value = new object();
456         //        object rootElement = new object();
457         //        INodeImplement nodeImplement = new INodeImplement();
458         //        Assert.IsNotNull(nodeImplement, "null INodeImplement");
459         //        HydrationContext context = new HydrationContext();
460         //        Assert.IsNotNull(context, "null HydrationContext");
461         //        IXmlLineInfoImplement xmlLineInfoImplement = new IXmlLineInfoImplement();
462         //        Assert.IsNotNull(xmlLineInfoImplement, "null IXmlLineInfoImplement");
463
464         //        ApplyPropertiesVisitor.SetPropertyValue(o1, xmlName, value, rootElement, nodeImplement, context, xmlLineInfoImplement);
465         //    }
466         //    catch (Exception e)
467         //    {
468         //        tlog.Debug(tag, e.Message.ToString());
469         //        Assert.Fail("Caught Exception" + e.ToString());
470         //    }
471         //    tlog.Debug(tag, $"ApplyPropertiesVisitorSetPropertyValue END");
472         //}
473
474         [Test]
475         [Category("P1")]
476         [Description("ApplyPropertiesVisitor GetPropertyValue")]
477         [Property("SPEC", "Tizen.NUI.ApplyPropertiesVisitor.GetPropertyValue M")]
478         [Property("SPEC_URL", "-")]
479         [Property("CRITERIA", "MR")]
480         public void ApplyPropertiesVisitorGetPropertyValue()
481         {
482             tlog.Debug(tag, $"ApplyPropertiesVisitorGetPropertyValue START");
483
484             try
485             {
486                 const string NAMESPACE = "clr-namespace:Tizen.NUI.Devel.Tests;assembly=Tizen.NUI.Devel.Tests";
487                 var resolver = new MockNameSpaceResolver();
488                 var type = new XmlType(NAMESPACE, "InternalApplyPropertiesVisitorTest+MarkupExtension", null);
489                 var listNode = new ListNode(new[]
490                 {
491                     new ElementNode(type, NAMESPACE, resolver),
492                     new ElementNode(type, NAMESPACE, resolver)
493                 }, resolver);
494                 var rootElement = new ArrayListOwner();
495                 var rootType = new XmlType(NAMESPACE, "InternalApplyPropertiesVisitorTest+ArrayListOwner", null);
496                 var rootNode = new XamlLoader.RuntimeRootNode(rootType, rootElement, null);
497                 var context = new HydrationContext { RootElement = rootElement };
498
499                 rootNode.Properties.Add(new XmlName(null, "ArrayList"), listNode);
500                 rootNode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);
501                 rootNode.Accept(new CreateValuesVisitor(context), null);
502                 rootNode.Accept(new ApplyPropertiesVisitor(context), null);
503
504                 Assert.AreEqual("provided value", rootElement.ArrayList[0]);
505                 Assert.AreEqual("provided value", rootElement.ArrayList[1]);
506             }
507             catch (Exception e)
508             {
509                 tlog.Debug(tag, e.Message.ToString());
510                 Assert.Fail("Caught Exception" + e.ToString());
511             }
512             tlog.Debug(tag, $"ApplyPropertiesVisitorGetPropertyValue END");
513         }
514     }
515 }