2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-scene3d/public-api/loader/bvh-loader.h>
19 #include <dali-test-suite-utils.h>
24 using namespace Dali::Scene3D::Loader;
28 std::string ReadBufferFromFile(const std::string& url)
30 std::string rawString;
31 std::fstream fileStream;
33 fileStream.open(url, std::ios::in | std::ios::binary);
34 if(!fileStream.is_open())
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));
39 // get length of file:
40 fileStream.seekg(0, std::ios::end);
41 auto length = fileStream.tellg();
42 fileStream.seekg(0, std::ios::beg);
44 rawString.resize(length);
45 fileStream.read(rawString.data(), length);
53 int UtcDaliLoadBvh(void)
55 TestApplication application;
57 for(uint32_t tc = 0; tc < 2; ++tc)
59 AnimationDefinition animDef;
60 tet_printf("UtcDaliLoadBvh testcase %u\n", tc);
63 case 0: // Load bvh from url
65 animDef = LoadBvh(TEST_RESOURCE_DIR "/test.bvh", "testBvh");
68 case 1: // Load bvh from buffer stream.
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");
76 DALI_TEST_EQUAL(animDef.GetName(), "testBvh");
77 DALI_TEST_EQUAL(animDef.GetDuration(), 0.3f);
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);
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);
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);
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);
99 Actor root = Actor::New();
100 root.SetProperty(Actor::Property::NAME, "root");
102 Actor first = Actor::New();
103 first.SetProperty(Actor::Property::NAME, "first");
106 auto getActor = [&root](const Dali::Scene3D::Loader::AnimatedProperty& property) {
107 return root.FindChildByName(property.mNodeName);
110 Animation animation = animDef.ReAnimate(getActor);
111 DALI_TEST_EQUAL(animation.GetDuration(), animDef.GetDuration());
113 application.GetScene().Add(root);
115 application.SendNotification();
116 application.Render(20);
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);
125 application.SendNotification();
126 application.Render(1000);
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);
131 Vector3 rootWorldPositionAfter = root.GetProperty<Vector3>(Actor::Property::WORLD_POSITION);
132 Vector3 firstWorldPositionAfter = first.GetProperty<Vector3>(Actor::Property::WORLD_POSITION);
134 DALI_TEST_EQUALS(Vector3(0, 10, 0), rootWorldPositionAfter - rootWorldPositionBefore, TEST_LOCATION);
135 DALI_TEST_EQUALS(Vector3(10, 10, 0), firstWorldPositionAfter - firstWorldPositionBefore, TEST_LOCATION);
141 int UtcDaliLoadBvhFailed01(void)
143 TestApplication application;
145 AnimationDefinition animDef = LoadBvh("/nothing.bvh", "testBvh");
146 DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
150 int UtcDaliLoadBvhFailed02(void)
152 TestApplication application;
154 AnimationDefinition animDef = LoadBvhFromBuffer(nullptr, 0, "testBvh");
155 DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
159 int UtcDaliLoadBvhFailed03(void)
161 TestApplication application;
163 tet_infoline("Parse error for hierarchy1");
164 uint32_t caseHierarchyCount = 8;
165 for(uint32_t tc = 0; tc < caseHierarchyCount; ++tc)
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");
171 DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
174 uint32_t caseMotionCount = 4;
175 for(uint32_t tc = 0; tc < caseMotionCount; ++tc)
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");
181 DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);
185 tet_infoline("empty file");
186 AnimationDefinition animDef = LoadBvh(TEST_RESOURCE_DIR "/test-empty.bvh", "testBvh");
187 DALI_TEST_EQUALS(0u, animDef.GetPropertyCount(), TEST_LOCATION);