Add UTCs for public-api
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Constrainer.cpp
1 /*
2  * Copyright (c) 2014 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 <iostream>
19
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali/devel-api/animation/path-constrainer.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26 using namespace Dali::Internal;
27
28 namespace
29 {
30 static void SetupLinearConstrainerUniformProgress( Dali::LinearConstrainer& linearConstrainer)
31 {
32   Dali::Property::Array points;
33   points.Resize(3);
34   points[0] = 0.0f;
35   points[1] = 1.0f;
36   points[2] = 0.0f;
37   linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::VALUE, points );
38 }
39
40 static void SetupLinearConstrainerNonUniformProgress( Dali::LinearConstrainer& linearConstrainer)
41 {
42   Dali::Property::Array points;
43   points.Resize(3);
44   points[0] = 0.0f;
45   points[1] = 1.0f;
46   points[2] = 0.0f;
47   linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::VALUE, points );
48
49   points[0] = 0.0f;
50   points[1] = 0.25f;
51   points[2] = 1.0f;
52   linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::PROGRESS, points );
53 }
54
55 } // anonymous namespace
56
57 int UtcLinearConstrainerApply(void)
58 {
59   TestApplication application;
60
61   Dali::Actor actor = Dali::Actor::New();
62
63   // Register a float property
64   Property::Index index = actor.RegisterProperty( "t", 0.0f );
65
66   Dali::Stage::GetCurrent().Add(actor);
67
68
69   //Create a LinearConstrainer without specifying progress for values
70   Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
71   SetupLinearConstrainerUniformProgress( linearConstrainer );
72
73   //Apply the linear constraint to the actor's position. The source property for the constraint will be the custom property "t"
74   Vector2 range( 0.0f, 1.0f );
75   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
76
77   //Create an animation to animate the custom property
78   float durationSeconds(1.0f);
79   Dali::Animation animation = Dali::Animation::New(durationSeconds);
80   animation.AnimateTo(Dali::Property(actor,index),1.0f);
81   animation.Play();
82
83   application.SendNotification();
84   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
85
86   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
87
88   application.SendNotification();
89   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
90   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
91
92   application.SendNotification();
93   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
94   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
95
96   application.SendNotification();
97   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
98   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
99
100   application.SendNotification();
101   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
102   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
103
104   //Setup a LinearConstrainer specifying the progress for each value
105   linearConstrainer.Remove(actor);
106   SetupLinearConstrainerNonUniformProgress( linearConstrainer );
107   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
108
109   actor.SetProperty(index,0.0f);
110   animation.Play();
111   application.SendNotification();
112   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
113
114   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
115
116   application.SendNotification();
117   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
118   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 2.0f/3.0f, Math::MACHINE_EPSILON_1, TEST_LOCATION );
119
120   application.SendNotification();
121   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
122   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f/3.0f, Math::MACHINE_EPSILON_1, TEST_LOCATION );
123
124   application.SendNotification();
125   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
126   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
127
128   application.SendNotification();
129   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
130   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
131
132   END_TEST;
133 }
134