Replace registered visuals only when replacement is ready
[platform/core/uifw/dali-toolkit.git] / plugins / dali-sharp / test / 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 /*
11  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
12  *
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  * http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *
25  */
26
27 using Dali;
28 using Dali.Constants;
29 using System;
30 using System.Timers;
31
32 namespace Test
33 {
34     class AnimationTest
35     {
36         private View _colorView;
37         private View _textView;
38
39         private bool _textAnimationComplete;
40
41         private Dali.Application _currentApplication;
42
43         private AnimationTest(Dali.Application application)
44         {
45             _currentApplication = application;
46             _textAnimationComplete = false;
47             _currentApplication.Initialized += Initialize;
48         }
49
50         private void Initialize(object source, NUIApplicationInitEventArgs e)
51         {
52             AnimationTest01();
53             StartTimer(4000);
54         }
55
56         private void AnimationFinished(object sender, EventArgs e)
57         {
58           Console.WriteLine("Visual AnimationFinished");
59
60           Property.Map colorVisual = new Property.Map();
61           colorVisual.Add( Visual.Property.Type, new Property.Value( (int)Visual.Type.Color ))
62                           .Add( ColorVisualProperty.MixColor, new Property.Value( Color.Blue ));
63           _colorView.Background = colorVisual;
64         }
65
66         private void TextAnimationFinished(object sender, EventArgs e)
67         {
68           Console.WriteLine("Text AnimationFinished");
69           _textAnimationComplete = true;
70         }
71
72         /**
73          * Test animates a text visual automatically and logs result to console once animation finished signal emitted.
74          *
75          * Manually pressing the button starts animating a visual and changes it color at end of animation.
76          *
77          * Tests Animation signal logic.
78          */
79
80         private void AnimationTest01()
81         {
82             Console.WriteLine("AnimationTest");
83
84             _textAnimationComplete = false;
85
86             Window window = Window.Instance;
87             window.BackgroundColor = Color.White;
88             Size2D windowSize = window.Size;
89
90             // Create Color Visual
91             _colorView = new View();
92             _colorView.Size = new Vector3(120.0f, 120.0f, 0.0f);
93             _colorView.WidthResizePolicy = "FIXED";
94             _colorView.HeightResizePolicy = "FIXED";
95             Property.Map colorVisual = new Property.Map();
96             colorVisual.Add( Visual.Property.Type, new Property.Value( (int)Visual.Type.Color ))
97                        .Add( ColorVisualProperty.MixColor, new Property.Value( Color.Green ));
98             _colorView.Background = colorVisual;
99
100             Window.Instance.Add(_colorView);
101
102             Animation _animation = new Animation(1000); // 1000 milli-second of duration
103             _animation.StartTime = 500;
104             _animation.EndTime = 1000;
105             _animation.TargetProperty = "Position";
106             _animation.Destination = new Vector3(150.0f, 150.0f, 0.0f);
107             _animation.AnimateTo(_colorView);
108             _animation.Finished += AnimationFinished;
109
110             PushButton buttonWithSimpleTooltip = new PushButton();
111             buttonWithSimpleTooltip.LabelText = "Press to Animate";
112             buttonWithSimpleTooltip.UnselectedColor = new Vector4(0.6f, 0.92f, 1.0f, 1.0f);
113             buttonWithSimpleTooltip.SelectedColor = new Vector4(0.6f, 0.7f, 1.0f, 1.0f);
114             buttonWithSimpleTooltip.WidthResizePolicy = "USE_NATURAL_SIZE";
115             buttonWithSimpleTooltip.Position = new Position(windowSize.Width /2 , 0, 0);
116
117             Window.Instance.Add(buttonWithSimpleTooltip);
118             buttonWithSimpleTooltip.SetFocusable(true);
119             buttonWithSimpleTooltip.TooltipText = "RePosition Square and change color to Blue";
120             buttonWithSimpleTooltip.Clicked += (obj, ee) =>
121             {
122               _animation.Play();
123               return true;
124             };
125
126             _textView = new View();
127             _textView.WidthResizePolicy = "USE_NATURAL_SIZE";
128             Property.Map textVisual = new Property.Map();
129             textVisual.Add(Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Text))
130                       .Add(Dali.Constants.TextVisualProperty.Text, new Property.Value("Moved Text"))
131                       .Add(Dali.Constants.TextVisualProperty.PointSize, new Property.Value(24));
132
133             _textView.Background = textVisual;
134             _textView.Position = new Position(0, 200, 0);
135             Window.Instance.Add(_textView);
136
137             Animation textAnimation = new Animation(3000); // 3000 milli-second of duration
138
139             textAnimation.TargetProperty = "Position";
140             textAnimation.Destination = new Vector3(windowSize.Width/2, 200.0f, 0.0f);
141             textAnimation.AnimateTo(_textView);
142             textAnimation.Finished += TextAnimationFinished;
143
144             textAnimation.Play();
145         }
146
147         private void StartTimer(int duration)
148         {
149            System.Timers.Timer timer = new System.Timers.Timer(duration);
150            timer.Elapsed += OnTimerElasped;
151            timer.AutoReset = false;
152            timer.Start();
153         }
154
155         private void OnTimerElasped(System.Object source, ElapsedEventArgs e)
156         {
157           if (_textAnimationComplete == true)
158           {
159             Console.WriteLine("PASSED.");
160           }
161           else
162           {
163             Console.WriteLine("FAILED");
164           }
165         }
166
167         static void Main(string[] args)
168         {
169            Application application = Application.NewApplication();
170            AnimationTest test = new AnimationTest(application);
171            application.MainLoop();
172         }
173     }
174 }