[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-BvhLoader.cpp
1 /*
2  * Copyright (c) 2023 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-scene3d/public-api/loader/bvh-loader.h>
19 #include <dali-test-suite-utils.h>
20
21 #include <fstream>
22
23 using namespace Dali;
24 using namespace Dali::Scene3D::Loader;
25
26 namespace
27 {
28 std::string ReadBufferFromFile(const std::string& url)
29 {
30   std::string  rawString;
31   std::fstream fileStream;
32
33   fileStream.open(url, std::ios::in | std::ios::binary);
34   if(!fileStream.is_open())
35   {
36     DALI_LOG_WARNING("stream open failed for: \"%s\", in mode: \"%d\".\n", url.c_str(), static_cast<int>(std::ios::in | std::ios::binary));
37   }
38
39   // get length of file:
40   fileStream.seekg(0, std::ios::end);
41   auto length = fileStream.tellg();
42   fileStream.seekg(0, std::ios::beg);
43
44   rawString.resize(length);
45   fileStream.read(rawString.data(), length);
46
47   fileStream.close();
48
49   return rawString;
50 }
51 } // namespace
52
53 int UtcDaliLoadBvh(void)
54 {
55   TestApplication application;
56
57   for(uint32_t tc = 0; tc < 2; ++tc)
58   {
59     AnimationDefinition animDef;
60     tet_printf("UtcDaliLoadBvh testcase %u\n", tc);
61     switch(tc)
62     {
63       case 0: // Load bvh from url
64       {
65         animDef = LoadBvh(TEST_RESOURCE_DIR "/test.bvh", "testBvh", false);
66         break;
67       }
68       case 1: // Load bvh from buffer stream.
69       {
70         std::string rawString = ReadBufferFromFile(TEST_RESOURCE_DIR "/test.bvh");
71         animDef               = LoadBvhFromBuffer(reinterpret_cast<uint8_t*>(rawString.data()), static_cast<int>(rawString.length()), "testBvh", false);
72         break;
73       }
74     }
75
76     DALI_TEST_EQUAL(animDef.GetName(), "testBvh");
77     DALI_TEST_EQUAL(animDef.GetDuration(), 0.3f);
78
79     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mNodeName, "root");
80     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mPropertyName, "position");
81     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mKeyFrames.GetType(), Property::Type::VECTOR3);
82     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mTimePeriod.durationSeconds, 0.3f);
83
84     DALI_TEST_EQUAL(animDef.GetPropertyAt(1).mNodeName, "root");
85     DALI_TEST_EQUAL(animDef.GetPropertyAt(1).mPropertyName, "orientation");
86     DALI_TEST_EQUAL(animDef.GetPropertyAt(1).mKeyFrames.GetType(), Property::Type::ROTATION);
87     DALI_TEST_EQUAL(animDef.GetPropertyAt(1).mTimePeriod.durationSeconds, 0.3f);
88
89     DALI_TEST_EQUAL(animDef.GetPropertyAt(2).mNodeName, "first");
90     DALI_TEST_EQUAL(animDef.GetPropertyAt(2).mPropertyName, "position");
91     DALI_TEST_EQUAL(animDef.GetPropertyAt(2).mKeyFrames.GetType(), Property::Type::VECTOR3);
92     DALI_TEST_EQUAL(animDef.GetPropertyAt(2).mTimePeriod.durationSeconds, 0.3f);
93
94     DALI_TEST_EQUAL(animDef.GetPropertyAt(3).mNodeName, "first");
95     DALI_TEST_EQUAL(animDef.GetPropertyAt(3).mPropertyName, "orientation");
96     DALI_TEST_EQUAL(animDef.GetPropertyAt(3).mKeyFrames.GetType(), Property::Type::ROTATION);
97     DALI_TEST_EQUAL(animDef.GetPropertyAt(3).mTimePeriod.durationSeconds, 0.3f);
98
99     Actor root = Actor::New();
100     root.SetProperty(Actor::Property::NAME, "root");
101
102     Actor first = Actor::New();
103     first.SetProperty(Actor::Property::NAME, "first");
104     root.Add(first);
105
106     auto getActor = [&root](const Dali::Scene3D::Loader::AnimatedProperty& property) {
107       return root.FindChildByName(property.mNodeName);
108     };
109
110     Animation animation = animDef.ReAnimate(getActor);
111     DALI_TEST_EQUAL(animation.GetDuration(), animDef.GetDuration());
112
113     application.GetScene().Add(root);
114
115     application.SendNotification();
116     application.Render(20);
117
118     DALI_TEST_EQUALS(Vector2(0, 0), root.GetProperty<Vector2>(Actor::Property::POSITION), TEST_LOCATION);
119     DALI_TEST_EQUALS(Vector2(0, 0), first.GetProperty<Vector2>(Actor::Property::POSITION), TEST_LOCATION);
120     Vector3 rootWorldPositionBefore  = root.GetProperty<Vector3>(Actor::Property::WORLD_POSITION);
121     Vector3 firstWorldPositionBefore = first.GetProperty<Vector3>(Actor::Property::WORLD_POSITION);
122
123     animation.Play();
124
125     application.SendNotification();
126     application.Render(1000);
127
128     DALI_TEST_EQUALS(Vector2(0, 10), root.GetProperty<Vector2>(Actor::Property::POSITION), TEST_LOCATION);
129     DALI_TEST_EQUALS(Vector2(10, 0), first.GetProperty<Vector2>(Actor::Property::POSITION), TEST_LOCATION);
130
131     Vector3 rootWorldPositionAfter  = root.GetProperty<Vector3>(Actor::Property::WORLD_POSITION);
132     Vector3 firstWorldPositionAfter = first.GetProperty<Vector3>(Actor::Property::WORLD_POSITION);
133
134     DALI_TEST_EQUALS(Vector3(0, 10, 0), rootWorldPositionAfter - rootWorldPositionBefore, TEST_LOCATION);
135     DALI_TEST_EQUALS(Vector3(10, 10, 0), firstWorldPositionAfter - firstWorldPositionBefore, TEST_LOCATION);
136   }
137
138   END_TEST;
139 }
140
141 int UtcDaliLoadBvhFailed01(void)
142 {
143   TestApplication application;
144
145   AnimationDefinition animDef = LoadBvh("/nothing.bvh", "testBvh", false);
146   DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
147   END_TEST;
148 }
149
150 int UtcDaliLoadBvhFailed02(void)
151 {
152   TestApplication application;
153
154   AnimationDefinition animDef = LoadBvhFromBuffer(nullptr, 0, "testBvh", false);
155   DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
156   END_TEST;
157 }
158
159 int UtcDaliLoadBvhFailed03(void)
160 {
161   TestApplication application;
162
163   tet_infoline("Parse error for hierarchy1");
164   uint32_t caseHierarchyCount = 8;
165   for(uint32_t tc = 0; tc < caseHierarchyCount; ++tc)
166   {
167     tet_printf("Parse error for hierarchy %u\n", tc);
168     std::ostringstream oss;
169     oss << TEST_RESOURCE_DIR << "/test-invalid-hierarchy" << tc << ".bvh";
170     AnimationDefinition animDef = LoadBvh(oss.str(), "testBvh", false);
171     DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
172   }
173
174   uint32_t caseMotionCount = 4;
175   for(uint32_t tc = 0; tc < caseMotionCount; ++tc)
176   {
177     tet_printf("Parse error for motion %u\n", tc);
178     std::ostringstream oss;
179     oss << TEST_RESOURCE_DIR << "/test-invalid-motion" << tc << ".bvh";
180     AnimationDefinition animDef = LoadBvh(oss.str(), "testBvh", false);
181     DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
182   }
183
184   {
185     tet_infoline("empty file");
186     AnimationDefinition animDef = LoadBvh(TEST_RESOURCE_DIR "/test-empty.bvh", "testBvh", false);
187     DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
188   }
189   END_TEST;
190 }