[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-MotionValue.cpp
1 /*
2  * Copyright (c) 2024 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 #include <dali-toolkit-test-suite-utils.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <stdlib.h>
21 #include <iostream>
22
23 #include <dali-scene3d/public-api/model-motion/motion-value.h>
24 #include <dali/devel-api/animation/key-frames-devel.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28 using namespace Dali::Scene3D;
29
30 void model_motion_motion_value_startup(void)
31 {
32   test_return_value = TET_UNDEF;
33 }
34
35 void model_motion_motion_value_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 namespace
41 {
42 } // namespace
43
44 // Positive test case for a method
45 int UtcDaliMotionValueNew(void)
46 {
47   ToolkitTestApplication application;
48   tet_infoline(" UtcDaliMotionValueNew");
49
50   MotionValue motionValue = MotionValue::New();
51   DALI_TEST_CHECK(motionValue);
52   END_TEST;
53 }
54
55 // Positive test case for a method
56 int UtcDaliMotionValueDownCast(void)
57 {
58   ToolkitTestApplication application;
59   tet_infoline(" UtcDaliMotionValueDownCast");
60
61   MotionValue motionValue = MotionValue::New();
62   BaseHandle  handle(motionValue);
63
64   MotionValue motionValue2 = MotionValue::DownCast(handle);
65   DALI_TEST_CHECK(motionValue);
66   DALI_TEST_CHECK(motionValue2);
67   DALI_TEST_CHECK(motionValue2 == motionValue);
68   END_TEST;
69 }
70
71 int UtcDaliMotionValueTypeRegistry(void)
72 {
73   ToolkitTestApplication application;
74
75   TypeRegistry typeRegistry = TypeRegistry::Get();
76   DALI_TEST_CHECK(typeRegistry);
77
78   TypeInfo typeInfo = typeRegistry.GetTypeInfo("MotionValue");
79   DALI_TEST_CHECK(typeInfo);
80
81   BaseHandle handle = typeInfo.CreateInstance();
82   DALI_TEST_CHECK(handle);
83
84   MotionValue motionValue = MotionValue::DownCast(handle);
85   DALI_TEST_CHECK(motionValue);
86
87   END_TEST;
88 }
89
90 int UtcDaliMotionValueCopyAndAssignment(void)
91 {
92   ToolkitTestApplication application;
93
94   MotionValue motionValue = MotionValue::New(1);
95   DALI_TEST_CHECK(motionValue);
96   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::PROPERTY_VALUE);
97
98   MotionValue copy(motionValue);
99   DALI_TEST_CHECK(motionValue == copy);
100   DALI_TEST_CHECK(copy.GetValueType() == MotionValue::ValueType::PROPERTY_VALUE);
101
102   MotionValue assign;
103   DALI_TEST_CHECK(!assign);
104
105   assign = copy;
106   DALI_TEST_CHECK(assign == motionValue);
107   DALI_TEST_CHECK(assign.GetValueType() == MotionValue::ValueType::PROPERTY_VALUE);
108
109   END_TEST;
110 }
111
112 int UtcDaliMotionValueMoveConstructor(void)
113 {
114   ToolkitTestApplication application;
115
116   MotionValue motionValue = MotionValue::New(3.0f);
117   DALI_TEST_EQUALS(1, motionValue.GetBaseObject().ReferenceCount(), TEST_LOCATION);
118   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::PROPERTY_VALUE);
119
120   MotionValue moved = std::move(motionValue);
121   DALI_TEST_CHECK(moved);
122   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
123   DALI_TEST_CHECK(moved.GetValueType() == MotionValue::ValueType::PROPERTY_VALUE);
124   DALI_TEST_CHECK(!motionValue);
125
126   END_TEST;
127 }
128
129 int UtcDaliMotionValueMoveAssignment(void)
130 {
131   ToolkitTestApplication application;
132
133   MotionValue motionValue = MotionValue::New(KeyFrames::New());
134   DALI_TEST_EQUALS(1, motionValue.GetBaseObject().ReferenceCount(), TEST_LOCATION);
135   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::KEY_FRAMES);
136
137   MotionValue moved;
138   moved = std::move(motionValue);
139   DALI_TEST_CHECK(moved);
140   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
141   DALI_TEST_CHECK(moved.GetValueType() == MotionValue::ValueType::KEY_FRAMES);
142
143   END_TEST;
144 }
145
146 // Method test
147
148 int UtcDaliMotionValueGetSetValue(void)
149 {
150   ToolkitTestApplication application;
151
152   MotionValue motionValue = MotionValue::New();
153   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::INVALID);
154   DALI_TEST_CHECK(motionValue.GetPropertyValue().GetType() == Property::Type::NONE);
155   DALI_TEST_CHECK(!motionValue.GetKeyFrames());
156
157   float expectValue = 3.0f;
158   motionValue.SetValue(expectValue);
159   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::PROPERTY_VALUE);
160   DALI_TEST_CHECK(motionValue.GetPropertyValue().GetType() == Property::Type::FLOAT);
161   DALI_TEST_CHECK(motionValue.GetPropertyValue().Get<float>() == expectValue);
162
163   // Check the converted KeyFrames
164   auto convertedKeyFrames = motionValue.GetKeyFrames();
165   DALI_TEST_CHECK(convertedKeyFrames);
166   auto convertedKeyFrameCount = DevelKeyFrames::GetKeyFrameCount(convertedKeyFrames);
167   for(auto i = 0u; i < convertedKeyFrameCount; ++i)
168   {
169     float           progress;
170     Property::Value value;
171     DevelKeyFrames::GetKeyFrame(convertedKeyFrames, i, progress, value);
172
173     // Check all value has same as expect property.
174     DALI_TEST_EQUALS(value.Get<float>(), expectValue, TEST_LOCATION);
175   }
176
177   KeyFrames expectKeyFrames = KeyFrames::New();
178   expectValue               = 2.0f;
179   expectKeyFrames.Add(0.0f, expectValue - 1.0f);
180   expectKeyFrames.Add(1.0f, expectValue);
181   motionValue.SetValue(expectKeyFrames);
182
183   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::KEY_FRAMES);
184   DALI_TEST_CHECK(motionValue.GetKeyFrames() == expectKeyFrames);
185
186   // Check property value return last frame value.
187   DALI_TEST_CHECK(motionValue.GetPropertyValue().GetType() == Property::Type::FLOAT);
188   DALI_TEST_EQUALS(motionValue.GetPropertyValue().Get<float>(), expectValue, TEST_LOCATION);
189
190   // Check invalidate value.
191   motionValue.Invalidate();
192   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::INVALID);
193   DALI_TEST_CHECK(motionValue.GetPropertyValue().GetType() == Property::Type::NONE);
194   DALI_TEST_CHECK(!motionValue.GetKeyFrames());
195
196   motionValue.SetValue(expectKeyFrames);
197   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::KEY_FRAMES);
198   DALI_TEST_CHECK(motionValue.GetKeyFrames() == expectKeyFrames);
199
200   // Check invalidate value by Clear().
201   motionValue.Clear();
202   DALI_TEST_CHECK(motionValue.GetValueType() == MotionValue::ValueType::INVALID);
203   DALI_TEST_CHECK(motionValue.GetPropertyValue().GetType() == Property::Type::NONE);
204   DALI_TEST_CHECK(!motionValue.GetKeyFrames());
205
206   END_TEST;
207 }