[dali_1.2.30] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / dali-test.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.Runtime.InteropServices;
20 using Dali;
21
22 namespace MyCSharpExample
23 {
24     class MyView : View
25     {
26         private string _myOwnName;
27         public int _myCurrentValue;
28
29         public MyView()
30         {
31             _myCurrentValue = 0;
32         }
33
34         public string MyOwnName
35         {
36             get
37             {
38                 return _myOwnName;
39             }
40             set
41             {
42               _myOwnName = value;
43             }
44         }
45     }
46
47     class MyButton : PushButton
48     {
49         private string _myOwnName;
50         public int _myCurrentValue;
51
52         public MyButton()
53         {
54             _myCurrentValue = 0;
55         }
56
57         public string MyOwnName
58         {
59             get
60             {
61                 return _myOwnName;
62             }
63             set
64             {
65               _myOwnName = value;
66             }
67         }
68     }
69
70     class MySpin : Spin
71     {
72         private string _myOwnName;
73         public int _myCurrentValue;
74
75         public MySpin()
76         {
77             _myCurrentValue = 0;
78         }
79
80         public string MyOwnName
81         {
82             get
83             {
84                 return _myOwnName;
85             }
86             set
87             {
88               _myOwnName = value;
89             }
90         }
91     }
92
93     class Example
94     {
95         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
96         delegate void CallbackDelegate(IntPtr appPtr); // void, void delgate
97
98         private Dali.Application _application;
99
100         public Example(Dali.Application application)
101         {
102             _application = application;
103             Console.WriteLine( "InitSignal connection count = " + _application.InitSignal().GetConnectionCount() );
104
105             _application.Initialized += Initialize;
106             Console.WriteLine( "InitSignal connection count = " + _application.InitSignal().GetConnectionCount() );
107         }
108
109         public void Initialize(object source, NUIApplicationInitEventArgs e)
110         {
111             NavigationPropertiesTests();
112
113             OperatorTests();
114
115             CustomViewPropertyTest();
116
117             Handle handle = new Handle();
118             int myPropertyIndex = handle.RegisterProperty("myProperty", new Property.Value(10.0f), Property.AccessMode.READ_WRITE);
119             float myProperty = 0.0f;
120             handle.GetProperty(myPropertyIndex).Get(ref myProperty);
121             Console.WriteLine( "myProperty value: " + myProperty );
122
123             int myPropertyIndex2 = handle.RegisterProperty("myProperty2", new Property.Value(new Size(5.0f, 5.0f)), Property.AccessMode.READ_WRITE);
124             Size myProperty2 = new Size(0.0f, 0.0f);
125             handle.GetProperty(myPropertyIndex2).Get(myProperty2);
126             Console.WriteLine( "myProperty2 value: " + myProperty2.W + ", " + myProperty2.H );
127
128             Actor actor = new Actor();
129             actor.Size = new Position(200.0f, 200.0f, 0.0f);
130             actor.Name = "MyActor";
131             actor.Color = new Color(1.0f, 0.0f, 1.0f, 0.8f);
132             Console.WriteLine("Actor id: {0}", actor.GetId());
133             Console.WriteLine("Actor size: " + actor.Size.X + ", " + actor.Size.Y);
134             Console.WriteLine("Actor name: " + actor.Name);
135
136             Stage stage = Stage.GetCurrent();
137             stage.BackgroundColor = Color.White;
138             Size stageSize = stage.Size;
139             Console.WriteLine("Stage size: " + stageSize.W + ", " + stageSize.H);
140             stage.Add(actor);
141
142             TextLabel text = new TextLabel("Hello Mono World");
143             text.ParentOrigin = NDalic.ParentOriginCenter;
144             text.AnchorPoint = NDalic.AnchorPointCenter;
145             text.HorizontalAlignment = "CENTER";
146             stage.Add(text);
147
148             Console.WriteLine( "Text label text:  " + text.Text );
149
150             Console.WriteLine( "Text label point size:  " + text.PointSize );
151             text.PointSize = 32.0f;
152             Console.WriteLine( "Text label new point size:  " + text.PointSize );
153
154             RectanglePaddingClassTest();
155
156             Console.WriteLine( " *************************" );
157             Size Size = new Size(100, 50);
158             Console.WriteLine( "    Created " + Size );
159             Console.WriteLine( "    Size x =  " + Size.W + ", y = " + Size.H );
160             Size += new Size(20, 20);
161             Console.WriteLine( "    Size x =  " + Size.W + ", y = " + Size.H );
162             Size.W += 10;
163             Size.H += 10;
164             Console.WriteLine( "    Size width =  " + Size.W + ", height = " + Size.H );
165
166             Console.WriteLine( " *************************" );
167             Position Position = new Position(20, 100, 50);
168             Console.WriteLine( "    Created " + Position );
169             Console.WriteLine( "    Position x =  " + Position.X + ", y = " + Position.Y + ", z = " + Position.Z );
170             Position += new Position(20, 20, 20);
171             Console.WriteLine( "    Position x =  " + Position.X + ", y = " + Position.Y + ", z = " + Position.Z );
172             Position.X += 10;
173             Position.Y += 10;
174             Position.Z += 10;
175             Console.WriteLine( "    Position width =  " + Position.X + ", height = " + Position.Y + ", depth = " + Position.Z );
176             Position parentOrigin = new Dali.Position(NDalic.ParentOriginBottomRight);
177             Console.WriteLine( "    parentOrigin x =  " + parentOrigin.X + ", y = " + parentOrigin.Y + ", z = " + parentOrigin.Z );
178
179             Console.WriteLine( " *************************" );
180             Color color = new Color(20, 100, 50, 200);
181             Console.WriteLine( "    Created " + color );
182             Console.WriteLine( "    Color R =  " + color.R + ", G = " + color.G + ", B = " + color.B + ", A = " + color.A );
183             color += new Color(20, 20, 20, 20);
184             Console.WriteLine( "    Color R =  " + color.R + ", G = " + color.G + ", B = " + color.B + ", A = " + color.A );
185             color.R += 10;
186             color.G += 10;
187             color.B += 10;
188             color.A += 10;
189             Console.WriteLine( "    Color r =  " + color.R + ", g = " + color.G + ", b = " + color.B + ", a = " + color.A );
190
191             ViewDownCastTest();
192         }
193
194         public void RectanglePaddingClassTest()
195         {
196             using (Rectangle r1 = new Rectangle(2, 5, 20, 30))
197             {
198                 Console.WriteLine( "    Created " + r1 );
199                 Console.WriteLine( "    IsEmpty() =  " + r1.IsEmpty() );
200                 Console.WriteLine( "    Left =  " + r1.Left() );
201                 Console.WriteLine( "    Right =  " + r1.Right() );
202                 Console.WriteLine( "    Top  = " + r1.Top() );
203                 Console.WriteLine( "    Bottom  = " + r1.Bottom() );
204                 Console.WriteLine( "    Area  = " + r1.Area() );
205             }
206
207             Console.WriteLine( " *************************" );
208
209             using (Rectangle r2 = new Rectangle(2, 5, 20, 30))
210             {
211                 Console.WriteLine( "    Created " + r2 );
212                 r2.Set(1,1,40,40);
213                 Console.WriteLine( "    IsEmpty() =  " + r2.IsEmpty() );
214                 Console.WriteLine( "    Left =  " + r2.Left() );
215                 Console.WriteLine( "    Right =  " + r2.Right() );
216                 Console.WriteLine( "    Top  = " + r2.Top() );
217                 Console.WriteLine( "    Bottom  = " + r2.Bottom() );
218                 Console.WriteLine( "    Area  = " + r2.Area() );
219             }
220
221             Console.WriteLine( " *************************" );
222
223             Rectangle r3 = new Rectangle(10, 10, 20, 20);
224             Rectangle r4 = new Rectangle(10, 10, 20, 20);
225
226             if (r3 == r4)
227             {
228                 Console.WriteLine("r3 == r4");
229             }
230             else
231             {
232                 Console.WriteLine("r3 != r4");
233             }
234
235             r4 = new Rectangle(12, 10, 20, 20);
236
237             if (r3 == r4)
238             {
239                 Console.WriteLine("r3 == r4");
240             }
241             else
242             {
243                 Console.WriteLine("r3 != r4");
244             }
245
246             PaddingType p1 = new PaddingType(10.5f, 10.7f, 20.8f, 20.8f);
247             PaddingType p2 = new PaddingType(10.5f, 10.7f, 20.8f, 20.8f);
248
249             if (p1 == p2)
250             {
251                 Console.WriteLine("p1 == p2");
252             }
253             else
254             {
255                 Console.WriteLine("p1 != p2");
256             }
257
258             p2 = new PaddingType(12.0f, 10.7f, 20.2f, 20.0f);
259
260             if (p1 == p2)
261             {
262                 Console.WriteLine("p1 == p2");
263             }
264             else
265             {
266                 Console.WriteLine("p1 != p2");
267             }
268         }
269
270         public void NavigationPropertiesTests()
271         {
272             View view = new View();
273             View leftView, rightView, upView, downView, tmpView;
274
275             leftView = new View();
276             leftView.Name = "leftView";
277             rightView = new View();
278             rightView.Name = "rightView";
279             upView = new View();
280             upView.Name = "upView";
281             downView = new View();
282             downView.Name = "downView";
283
284             Stage.Instance.Add(leftView);
285             Stage.Instance.Add(rightView);
286             Stage.Instance.Add(upView);
287             Stage.Instance.Add(downView);
288
289             view.LeftFocusableView = leftView;
290             tmpView = view.LeftFocusableView;
291             if (string.Compare(tmpView.Name, "leftView") == 0)
292             {
293                 Console.WriteLine("Passed: LeftFocusedView = " + tmpView.Name);
294             }
295             else
296             {
297                 Console.WriteLine("Failed: LeftFocusedView = " + tmpView.Name);
298             }
299
300             view.RightFocusableView = rightView;
301             tmpView = view.RightFocusableView;
302             if (string.Compare(tmpView.Name, "rightView") == 0)
303             {
304                 Console.WriteLine("Passed: RightFocusedView = " + tmpView.Name);
305             }
306             else
307             {
308                 Console.WriteLine("Failed: RightFocusedView = " + tmpView.Name);
309             }
310
311             Stage.Instance.Add(view);
312
313             view.UpFocusableView = upView;
314             tmpView = view.UpFocusableView;
315             if (string.Compare(tmpView.Name, "upView") == 0)
316             {
317                 Console.WriteLine("Passed: UpFocusedView = " + tmpView.Name);
318             }
319             else
320             {
321                 Console.WriteLine("Failed: UpFocusedView = " + tmpView.Name);
322             }
323
324             view.DownFocusableView = downView;
325             tmpView = view.DownFocusableView;
326             if (string.Compare(tmpView.Name, "downView") == 0)
327             {
328                 Console.WriteLine("Passed: DownFocusedView = " + tmpView.Name);
329             }
330             else
331             {
332                 Console.WriteLine("Failed: DownFocusedView = " + tmpView.Name);
333             }
334
335             Stage.Instance.Remove(leftView);
336             tmpView = view.LeftFocusableView;
337             if (!tmpView)
338             {
339                 Console.WriteLine("Passed: NULL LeftFocusedView");
340             }
341             else
342             {
343                 Console.WriteLine("Failed: LeftFocusedView = " + tmpView.Name);
344             }
345         }
346
347         public void OperatorTests()
348         {
349             Actor actor = new Actor();
350             Actor differentActor = new Actor();
351             Actor actorSame = actor;
352             Actor nullActor = null;
353
354             // test the true operator
355             if ( actor )
356             {
357                 Console.WriteLine ("BaseHandle Operator true (actor) : test passed ");
358             }
359             else
360             {
361                 Console.WriteLine ("BaseHandle Operator true (actor): test failed ");
362             }
363
364             Actor parent = actor.GetParent ();
365
366             if ( parent )
367             {
368                 Console.WriteLine ("Handle with Empty body  :failed ");
369             }
370             else
371             {
372                 Console.WriteLine ("Valid with Empty body  :passed ");
373             }
374
375             actor.Add( differentActor );
376
377             // here we test two different C# objects, which on the native side have the same body/ ref-object
378             if ( actor == differentActor.GetParent() )
379             {
380                 Console.WriteLine ("actor == differentActor.GetParent() :passed ");
381             }
382             else
383             {
384                 Console.WriteLine ("actor == differentActor.GetParent() :failed ");
385             }
386
387             if ( differentActor == differentActor.GetParent() )
388             {
389                 Console.WriteLine ("differentActor == differentActor.GetParent() :failed ");
390             }
391             else
392             {
393                 Console.WriteLine ("differentActor == differentActor.GetParent() :passed ");
394             }
395
396             if ( nullActor )
397             {
398                 Console.WriteLine ("BaseHandle Operator true (nullActor) : test failed ");
399             }
400             else
401             {
402                 Console.WriteLine ("BaseHandle Operator true (nullActor): test passed ");
403             }
404
405             // ! operator
406             if ( !actor )
407             {
408                 Console.WriteLine ("BaseHandle Operator !(actor) : test failed ");
409             }
410             else
411             {
412                 Console.WriteLine ("BaseHandle Operator !(actor): test passed ");
413             }
414
415             if ( !nullActor )
416             {
417                 Console.WriteLine ("BaseHandle Operator !(nullActor) : test passed ");
418             }
419             else
420             {
421                 Console.WriteLine ("BaseHandle Operator !(nullActor): test failed ");
422             }
423
424             // Note: operator false only used inside & operator
425             // test equality operator ==
426             if ( actor == actorSame )
427             {
428                 Console.WriteLine ("BaseHandle Operator  (actor == actorSame) : test passed");
429             }
430             else
431             {
432                 Console.WriteLine ("BaseHandle Operator  (actor == actorSame) : test failed");
433             }
434
435             if ( actor == differentActor )
436             {
437                 Console.WriteLine ("BaseHandle Operator (actor == differentActor) : test failed");
438             }
439             else
440             {
441                 Console.WriteLine ("BaseHandle Operator (actor == differentActor) : test passed");
442             }
443
444             if ( actor == nullActor )
445             {
446                 Console.WriteLine ("BaseHandle Operator (actor == nullActor) : test failed");
447             }
448             else
449             {
450                 Console.WriteLine ("BaseHandle Operator (actor == nullActor) : test passed");
451             }
452
453             if ( nullActor == nullActor )
454             {
455                 Console.WriteLine ("BaseHandle Operator (nullActor == nullActor) : test passed");
456             }
457             else
458             {
459                 Console.WriteLine ("BaseHandle Operator (nullActor == nullActor) : test failed");
460             }
461
462             // test || operator
463             if ( actor || actorSame )
464             {
465                 Console.WriteLine ("BaseHandle Operator (actor || actorSame) : test passed");
466             }
467             else
468             {
469                 Console.WriteLine ("BaseHandle Operator (actor || actorSame) : test failed");
470             }
471
472             if ( actor || nullActor )
473             {
474                 Console.WriteLine ("BaseHandle Operator (actor || nullActor) : test passed");
475             }
476             else
477             {
478                 Console.WriteLine ("BaseHandle Operator (actor || nullActor) : test failed");
479             }
480
481             if ( nullActor || nullActor )
482             {
483                 Console.WriteLine ("BaseHandle Operator (nullActor || nullActor) : test failed");
484             }
485             else
486             {
487                 Console.WriteLine ("BaseHandle Operator (nullActor || nullActor) : test passed");
488             }
489
490             // test && operator
491             if ( actor && actorSame )
492             {
493                 Console.WriteLine ("BaseHandle Operator (actor && actorSame) : test passed");
494             }
495             else
496             {
497                 Console.WriteLine ("BaseHandle Operator (actor && actorSame) : test failed");
498             }
499
500             if ( actor && nullActor )
501             {
502                 Console.WriteLine ("BaseHandle Operator (actor && nullActor) : test failed");
503             }
504             else
505             {
506                 Console.WriteLine ("BaseHandle Operator (actor && nullActor) : test passed");
507             }
508
509             if ( nullActor && nullActor )
510             {
511                 Console.WriteLine ("BaseHandle Operator (nullActor && nullActor) : test failed");
512             }
513             else
514             {
515                 Console.WriteLine ("BaseHandle Operator (nullActor && nullActor) : test passed");
516             }
517         }
518
519         public void CustomViewPropertyTest()
520         {
521             // Create a Spin control
522             Spin spin = new Spin();
523
524             // Background property
525             Property.Map background = new Property.Map();
526             background.Add( Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Color) )
527                       .Add( Dali.Constants.ColorVisualProperty.MixColor, new Property.Value(Color.Red) );
528             spin.Background = background;
529
530             background = spin.Background;
531             Vector4 backgroundColor = new Vector4();
532             background.Find(Dali.Constants.ColorVisualProperty.MixColor).Get(backgroundColor);
533             if( backgroundColor == Color.Red )
534             {
535                 Console.WriteLine ("Custom View Background property : test passed");
536             }
537             else
538             {
539                 Console.WriteLine ("Custom View Background property : test failed");
540             }
541
542             // BackgroundColor property
543             spin.BackgroundColor = Color.Yellow;
544             if(spin.BackgroundColor == Color.Yellow)
545             {
546                 Console.WriteLine ("Custom View BackgroundColor property : test passed");
547             }
548             else
549             {
550                 Console.WriteLine ("Custom View BackgroundColor property : test failed");
551             }
552
553             // BackgroundImage property
554             spin.BackgroundImage = "background-image.jpg";
555             if(spin.BackgroundImage == "background-image.jpg")
556             {
557                 Console.WriteLine ("Custom View BackgroundImage property : test passed");
558             }
559             else
560             {
561                 Console.WriteLine ("Custom View BackgroundImage property : test failed");
562             }
563
564             // StyleName property
565             spin.StyleName = "MyCustomStyle";
566             if(spin.StyleName == "MyCustomStyle")
567             {
568                 Console.WriteLine ("Custom View StyleName property : test passed");
569             }
570             else
571             {
572                 Console.WriteLine ("Custom View StyleName property : test failed");
573             }
574         }
575
576         public void ViewDownCastTest()
577         {
578           View container = new View();
579           container.Position = new Position(-800.0f, -800.0f, 0.0f);
580           Stage.GetCurrent().Add(container);
581
582           // Test downcast for native control
583           TextLabel myLabel = new TextLabel();
584           myLabel.Name = "MyLabelName";
585           myLabel.Text = "MyText";
586
587           Console.WriteLine("myLabel.Name = " + myLabel.Name + ", Text = " + myLabel.Text);
588
589           container.Add(myLabel);
590
591           Actor myLabelActor  = container.FindChildByName("MyLabelName");
592           if(myLabelActor)
593           {
594             TextLabel newLabel = View.DownCast<TextLabel>(myLabelActor);
595             if(newLabel)
596             {
597               Console.WriteLine("Downcast to TextLabel successful: newLabel Name = " + newLabel.Name + ", Text = " + newLabel.Text);
598             }
599             else
600             {
601               Console.WriteLine("Downcast to TextLabel failed!");
602             }
603           }
604
605           // Test downcast for class directly inherited from View
606           MyView myView = new MyView();
607           myView.Name = "MyViewName";
608           myView.MyOwnName = "MyOwnViewName";
609           myView._myCurrentValue = 5;
610
611           Console.WriteLine("myView.Name = " + myView.Name + ", MyOwnName = " + myView.MyOwnName + ", myCurrentValue = " + myView._myCurrentValue);
612
613           container.Add(myView);
614
615           Actor myViewActor  = container.FindChildByName("MyViewName");
616           if(myViewActor)
617           {
618             MyView newView = View.DownCast<MyView>(myViewActor);
619             if(newView)
620             {
621               Console.WriteLine("Downcast to MyView successful: newView Name = " + newView.Name + ", MyOwnName = " + newView.MyOwnName + ", myCurrentValue = " + newView._myCurrentValue);
622             }
623             else
624             {
625               Console.WriteLine("Downcast to MyView failed!");
626             }
627           }
628
629           // Test downcast for class directly inherited from native control
630           MyButton myButton = new MyButton();
631           myButton.Name = "MyButtonName";
632           myButton.MyOwnName = "MyOwnViewName";
633           myButton.LabelText = "MyLabelText";
634           myButton._myCurrentValue = 5;
635
636           Console.WriteLine("myButton.Name = " + myButton.Name + ", MyOwnName = " + myButton.MyOwnName + ", LabelText = " + myButton.LabelText + ", myCurrentValue = " + myButton._myCurrentValue);
637
638           container.Add(myButton);
639
640           Actor myButtonActor  = container.FindChildByName("MyButtonName");
641           if(myButtonActor)
642           {
643             MyButton newButton = View.DownCast<MyButton>(myButtonActor);
644             if(newButton)
645             {
646               Console.WriteLine("Downcast to MyButton successful: newButton Name = " + newButton.Name + ", MyOwnName = " + newButton.MyOwnName + ", LabelText = " + myButton.LabelText + ", myCurrentValue = " + newButton._myCurrentValue);
647             }
648             else
649             {
650               Console.WriteLine("Downcast to MyButton failed!");
651             }
652           }
653
654           // Test downcast for a CustomView
655           Spin spin = new Spin();
656           spin.Name = "SpinName";
657           spin.MaxValue = 8888;
658
659           Console.WriteLine("spin.Name = " + spin.Name + ", MaxValue = " + spin.MaxValue);
660
661           container.Add(spin);
662
663           Actor spinActor  = container.FindChildByName("SpinName");
664           if(spinActor)
665           {
666             Spin newSpin = View.DownCast<Spin>(spinActor);
667             if(newSpin)
668             {
669               Console.WriteLine("Downcast to Spin successful: newSpin Name = " + newSpin.Name + ", MaxValue = " + newSpin.MaxValue);
670             }
671             else
672             {
673               Console.WriteLine("Downcast to Spin failed!");
674             }
675           }
676
677           // Test downcast for class inherited from a CustomView
678           MySpin mySpin = new MySpin();
679           mySpin.Name = "MySpinName";
680           mySpin.MyOwnName = "MyOwnSpinName";
681           mySpin.MaxValue = 8888;
682           mySpin._myCurrentValue = 5;
683
684           Console.WriteLine("mySpin.Name = " + mySpin.Name + ", MyOwnName = " + mySpin.MyOwnName + ", MaxValue = " + mySpin.MaxValue + ", currentValue = " + mySpin._myCurrentValue);
685
686           container.Add(mySpin);
687
688           Actor mySpinActor  = container.FindChildByName("MySpinName");
689           if(mySpinActor)
690           {
691             MySpin newSpin = View.DownCast<MySpin>(mySpinActor);
692             if(newSpin)
693             {
694               Console.WriteLine("Downcast to MySpin successful: newSpin Name = " + newSpin.Name + ", MyOwnName = " + newSpin.MyOwnName + ", MaxValue = " + newSpin.MaxValue + ", currentValue = " + newSpin._myCurrentValue);
695             }
696             else
697             {
698               Console.WriteLine("Downcast to MySpin failed!");
699             }
700           }
701         }
702
703         public void MainLoop()
704         {
705             _application.MainLoop ();
706         }
707
708         /// <summary>
709         /// The main entry point for the application.
710         /// </summary>
711         [STAThread]
712         static void Main(string[] args)
713         {
714             Console.WriteLine ("Hello Mono World");
715
716             Example example = new Example(Application.NewApplication());
717             example.MainLoop ();
718         }
719     }
720 }