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