Revert "[Tizen] Appendix log for ttrace + Print keycode and timestamp"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-CustomActor.cpp
index 235b5a8..1df10a6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 using namespace Dali;
 
+namespace Test
+{
+void Doubler(float& current, const PropertyInputContainer& inputs)
+{
+  current = 2.0f * inputs[0]->GetFloat();
+}
+} // namespace Test
+
 void custom_actor_test_startup(void)
 {
   test_return_value = TET_UNDEF;
@@ -1090,7 +1098,7 @@ int UtcDaliCustomActorImplSetTransparent(void)
   actor.SetProperty(Actor::Property::OPACITY, 0.1f);
   actor.SetProperty(Actor::Property::SIZE, Vector2(400, 400));
 
-  DALI_TEST_EQUALS(false, actor.GetTransparent(), TEST_LOCATION);
+  DALI_TEST_EQUALS(false, actor.IsTransparent(), TEST_LOCATION);
 
   actor.SetTransparent(true);
 
@@ -1098,7 +1106,7 @@ int UtcDaliCustomActorImplSetTransparent(void)
   application.SendNotification();
   application.Render();
 
-  DALI_TEST_EQUALS(true, actor.GetTransparent(), TEST_LOCATION);
+  DALI_TEST_EQUALS(true, actor.IsTransparent(), TEST_LOCATION);
 
   application.GetScene().Remove(actor);
   END_TEST;
@@ -1183,23 +1191,57 @@ int UtcDaliCustomActorImplCalculateChildSizeBase(void)
 int UtcDaliCustomActorImplRelayoutDependentOnChildrenBase(void)
 {
   TestApplication       application;
-  Test::TestCustomActor custom = Test::TestCustomActor::NewNegoSize();
-  custom.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::ALL_DIMENSIONS);
+  Test::TestCustomActor customNego    = Test::TestCustomActor::NewNegoSize();
+  Test::TestCustomActor customNotNego = Test::TestCustomActor::New();
+
+  // A custom actor with default flags has relayouting enabled on initialization,
+  // and the default resize policy is USE_NATURAL_SIZE.
+  bool v = customNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::ALL_DIMENSIONS);
+  DALI_TEST_CHECK(v == true);
 
-  bool v = custom.TestRelayoutDependentOnChildrenBase(Dali::Dimension::ALL_DIMENSIONS);
+  // A custom actor with size negotiation explicitly switched off has no relayouting,
+  // and will not have any relayout dependencies. However, default resize policy when
+  // there is no relayouting is to return USE_NATURAL_SIZE, so this will actually return true,
+  // and is consistent.
+  v = customNotNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::ALL_DIMENSIONS);
+  DALI_TEST_CHECK(v == true);
+
+  customNego.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::ALL_DIMENSIONS);
+  customNotNego.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::ALL_DIMENSIONS);
+
+  v = customNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::ALL_DIMENSIONS);
+  DALI_TEST_CHECK(v == true);
+  v = customNotNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::ALL_DIMENSIONS);
   DALI_TEST_CHECK(v == true);
 
   application.SendNotification();
   application.Render();
 
-  custom.SetResizePolicy(Dali::ResizePolicy::FIXED, Dali::Dimension::ALL_DIMENSIONS);
-  v = custom.TestRelayoutDependentOnChildrenBase(Dali::Dimension::WIDTH);
+  customNego.SetResizePolicy(Dali::ResizePolicy::FIXED, Dali::Dimension::ALL_DIMENSIONS);
+  customNotNego.SetResizePolicy(Dali::ResizePolicy::FIXED, Dali::Dimension::ALL_DIMENSIONS);
+  v = customNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::WIDTH);
+  DALI_TEST_CHECK(v == false);
+  v = customNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::HEIGHT);
+  DALI_TEST_CHECK(v == false);
+  v = customNotNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::WIDTH);
+  DALI_TEST_CHECK(v == false);
+  v = customNotNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::HEIGHT);
   DALI_TEST_CHECK(v == false);
 
-  // why is this here?
   application.SendNotification();
   application.Render();
 
+  customNego.SetResizePolicy(Dali::ResizePolicy::USE_NATURAL_SIZE, Dali::Dimension::WIDTH);
+  customNotNego.SetResizePolicy(Dali::ResizePolicy::USE_NATURAL_SIZE, Dali::Dimension::HEIGHT);
+  v = customNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::WIDTH);
+  DALI_TEST_CHECK(v == true);
+  v = customNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::HEIGHT);
+  DALI_TEST_CHECK(v == false);
+  v = customNotNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::WIDTH);
+  DALI_TEST_CHECK(v == false);
+  v = customNotNego.TestRelayoutDependentOnChildrenBase(Dali::Dimension::HEIGHT);
+  DALI_TEST_CHECK(v == true);
+
   END_TEST;
 }
 
@@ -1575,3 +1617,95 @@ int UtcDaliCustomActorPropertyRegistrationDefaultValue(void)
 
   END_TEST;
 }
+
+int UtcDaliCustomActorComponentPropertyConstraintsP(void)
+{
+  TestApplication application; // Need the type registry
+
+  // register our base and add a property with default value for it
+  Dali::TypeRegistration typeRegistration(typeid(UnregisteredCustomActor), typeid(Dali::CustomActor), nullptr);
+
+  auto derived = DerivedCustomActor::New();
+  application.GetScene().Add(derived);
+
+  // should have all actor properties
+  auto actorHandle = Actor::New();
+  DALI_TEST_EQUALS(derived.GetPropertyCount(), actorHandle.GetPropertyCount(), TEST_LOCATION);
+
+  // add a property in base class
+  const Property::Index foobarIndex = ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX;
+  const Property::Index fooIndex    = foobarIndex + 1;
+  const Property::Index barIndex    = foobarIndex + 2;
+
+  AnimatablePropertyRegistration(typeRegistration, "Foobar", foobarIndex, Vector2(10.0f, 20.0f));
+  AnimatablePropertyComponentRegistration(typeRegistration, "Foobar.x", fooIndex, foobarIndex, 0);
+  AnimatablePropertyComponentRegistration(typeRegistration, "Foobar.y", barIndex, foobarIndex, 1);
+
+  tet_infoline("Test the default values of the registered property");
+  // should be more properties now
+  DALI_TEST_EQUALS(derived.GetPropertyCount(), actorHandle.GetPropertyCount() + 3, TEST_LOCATION);
+  // check that the default value is set for base class
+  DALI_TEST_EQUALS(UnregisteredCustomActor::New().GetProperty(foobarIndex).Get<Vector2>(), Vector2(10.f, 20.0f), 0.0001f, TEST_LOCATION);
+  // check that the default value is set for the derived instance as well
+  DALI_TEST_EQUALS(derived.GetProperty(foobarIndex).Get<Vector2>(), Vector2(10.f, 20.0f), 0.0001f, TEST_LOCATION);
+
+  tet_infoline("Test that the components of the registered property can be constrained");
+
+  // Try constraining the properties
+  Constraint fooCons = Constraint::New<float>(derived, fooIndex, &Test::Doubler);
+  fooCons.AddSource(LocalSource(Actor::Property::POSITION_X));
+  fooCons.Apply();
+  Constraint barCons = Constraint::New<float>(derived, barIndex, &Test::Doubler);
+  barCons.AddSource(LocalSource(fooIndex));
+  barCons.Apply();
+
+  for(int i = 1; i < 10; ++i)
+  {
+    derived[Actor::Property::POSITION_X] = i * 1.0f;
+    application.SendNotification();
+    application.Render();
+    DALI_TEST_EQUALS(derived.GetCurrentProperty(foobarIndex).Get<Vector2>(), Vector2(i * 2.0f, i * 4.0f), 0.0001f, TEST_LOCATION);
+  }
+
+  // Add a Vector3 property and its components for completeness
+  const Property::Index vec3PropIndex  = barIndex + 1;
+  const Property::Index vec3xPropIndex = vec3PropIndex + 1;
+  const Property::Index vec3yPropIndex = vec3PropIndex + 2;
+  const Property::Index vec3zPropIndex = vec3PropIndex + 3;
+
+  AnimatablePropertyRegistration(typeRegistration, "vec3Prop", vec3PropIndex, Vector3(10.0f, 20.0f, 30.0f));
+  AnimatablePropertyComponentRegistration(typeRegistration, "vec3Prop.x", vec3xPropIndex, vec3PropIndex, 0);
+  AnimatablePropertyComponentRegistration(typeRegistration, "vec3Prop.y", vec3yPropIndex, vec3PropIndex, 1);
+  AnimatablePropertyComponentRegistration(typeRegistration, "vec3Prop.z", vec3zPropIndex, vec3PropIndex, 2);
+
+  tet_infoline("Test the default values of the registered vec3 property");
+  // should be more properties now
+  DALI_TEST_EQUALS(derived.GetPropertyCount(), actorHandle.GetPropertyCount() + 7, TEST_LOCATION);
+  // check that the default value is set for base class
+  DALI_TEST_EQUALS(UnregisteredCustomActor::New().GetProperty(vec3PropIndex).Get<Vector3>(), Vector3(10.f, 20.0f, 30.0f), 0.0001f, TEST_LOCATION);
+  // check that the default value is set for the derived instance as well
+  DALI_TEST_EQUALS(derived.GetProperty(vec3PropIndex).Get<Vector3>(), Vector3(10.f, 20.0f, 30.0f), 0.0001f, TEST_LOCATION);
+
+  tet_infoline("Test that the components of the registered property can be constrained");
+
+  // Try constraining the properties
+  Constraint vec3xConstraint = Constraint::New<float>(derived, vec3xPropIndex, &Test::Doubler);
+  vec3xConstraint.AddSource(LocalSource(Actor::Property::POSITION_X));
+  vec3xConstraint.Apply();
+  Constraint vec3yConstraint = Constraint::New<float>(derived, vec3yPropIndex, &Test::Doubler);
+  vec3yConstraint.AddSource(LocalSource(vec3xPropIndex));
+  vec3yConstraint.Apply();
+  Constraint vec3zConstraint = Constraint::New<float>(derived, vec3zPropIndex, &Test::Doubler);
+  vec3zConstraint.AddSource(LocalSource(vec3yPropIndex));
+  vec3zConstraint.Apply();
+
+  for(int i = 1; i < 10; ++i)
+  {
+    derived[Actor::Property::POSITION_X] = i * 1.0f;
+    application.SendNotification();
+    application.Render();
+    DALI_TEST_EQUALS(derived.GetCurrentProperty(vec3PropIndex).Get<Vector3>(), Vector3(i * 2.0f, i * 4.0f, i * 8.0f), 0.0001f, TEST_LOCATION);
+  }
+
+  END_TEST;
+}