[dali-toolkit] fix set max character length when text already set 22/246622/12
authora.ghujeh <a.ghujeh@samsung.com>
Mon, 2 Nov 2020 14:05:47 +0000 (16:05 +0200)
committerabdullah <abdullahhasan10@gmail.com>
Tue, 6 Apr 2021 14:52:23 +0000 (17:52 +0300)
class SimpleApp : public ConnectionTracker
{
  Window window;
  TextField mTextField;
public:
  SimpleApp(Application& application)
  : mApplication(application)
  {
    mApplication.InitSignal().Connect(this, &SimpleApp::Create);
  }

  void Create(Application& application)
  {
    window = application.GetWindow();
    window.SetBackgroundColor(Vector4(0.04f, 0.345f, 0.392f, 1.0f));

    mTextField = TextField::New();
    mTextField.SetBackgroundColor(Color::WHITE);
    mTextField.SetProperty(TextField::Property::TEXT_COLOR, Color::BLACK);
    mTextField.SetProperty(TextField::Property::TEXT, "123456789");
    mTextField.SetProperty(TextField::Property::MAX_LENGTH, 3);
    mTextField.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
    mTextField.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
    mTextField.SetProperty(Actor::Property::SIZE, Vector2(140.f, 50.f));
    mTextField.SetProperty(Actor::Property::POSITION, Vector2(0.f, 0.f));

    window.Add(mTextField);
  }

private:
  Application& mApplication;
};

int DALI_EXPORT_API main(int argc, char** argv)
{
  Application application = Application::New(&argc, &argv);
  SimpleApp test(application);
  application.MainLoop();

  return 0;
}

Change-Id: I03ddf782eadb0d816e613aea6ef09a1133d7230e

automated-tests/src/dali-toolkit/utc-Dali-TextField.cpp
dali-toolkit/internal/text/text-controller-text-updater.cpp

index 05d0009..848326a 100644 (file)
@@ -3213,3 +3213,35 @@ int UtcDaliTextFieldPrimaryCursorPosition(void)
 
   END_TEST;
 }
+
+// test max length when set after setting long text
+int utcDaliTextFieldMaxCharactersReachedAfterSetText(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" utcDaliTextFieldMaxCharactersReachedAfterSetText");
+  TextField field = TextField::New();
+  DALI_TEST_CHECK( field );
+
+  application.GetScene().Add( field );
+
+  field.SetProperty(TextField::Property::TEXT, "123456789");
+
+  const int maxNumberOfCharacters = 3;
+  field.SetProperty( TextField::Property::MAX_LENGTH, maxNumberOfCharacters );
+
+  field.SetKeyInputFocus();
+
+  // connect to the text max lengh reached signal.
+  ConnectionTracker* testTracker = new ConnectionTracker();
+  bool maxLengthReachedSignal = false;
+  field.ConnectSignal( testTracker, "maxLengthReached",   CallbackFunctor(&maxLengthReachedSignal) );
+
+  application.ProcessEvent( GenerateKey( "a", "", "a", KEY_A_CODE, 0, 0, Integration::KeyEvent::DOWN, "a", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) );
+  application.ProcessEvent( GenerateKey( "a", "", "a", KEY_A_CODE, 0, 0, Integration::KeyEvent::DOWN, "a", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE ) );
+
+  DALI_TEST_CHECK( maxLengthReachedSignal );
+
+  DALI_TEST_EQUALS( field.GetProperty( TextField::Property::TEXT ).Get<std::string>(), "123456789", TEST_LOCATION );
+
+  END_TEST;
+}
\ No newline at end of file
index 6fb472e..672fd8a 100644 (file)
@@ -253,7 +253,8 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri
     const Length numberOfCharactersInModel = logicalModel->mText.Count();
 
     // Restrict new text to fit within Maximum characters setting.
-    Length maxSizeOfNewText = std::min((impl.mMaximumNumberOfCharacters - numberOfCharactersInModel), characterCount);
+    Length temp_length      = (impl.mMaximumNumberOfCharacters > numberOfCharactersInModel ? impl.mMaximumNumberOfCharacters - numberOfCharactersInModel : 0);
+    Length maxSizeOfNewText = std::min(temp_length, characterCount);
     maxLengthReached        = (characterCount > maxSizeOfNewText);
 
     // The cursor position.