AttributedStringTest.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #include <memory>
  8. #include <assert.h>
  9. #include <gtest/gtest.h>
  10. #include <react/attributedstring/TextAttributes.h>
  11. #include <react/attributedstring/conversions.h>
  12. #include <react/attributedstring/primitives.h>
  13. #include <react/graphics/conversions.h>
  14. namespace facebook {
  15. namespace react {
  16. #ifdef ANDROID
  17. TEST(AttributedStringTest, testToDynamic) {
  18. auto attString = new AttributedString();
  19. auto fragment = new AttributedString::Fragment();
  20. fragment->string = "test";
  21. auto text = new TextAttributes();
  22. text->foregroundColor = {
  23. colorFromComponents({100 / 255.0, 153 / 255.0, 200 / 255.0, 1.0})};
  24. text->opacity = 0.5;
  25. text->fontStyle = FontStyle::Italic;
  26. text->fontWeight = FontWeight::Thin;
  27. text->fontVariant = FontVariant::TabularNums;
  28. fragment->textAttributes = *text;
  29. attString->prependFragment(*fragment);
  30. auto result = toDynamic(*attString);
  31. assert(result["string"] == fragment->string);
  32. auto textAttribute = result["fragments"][0]["textAttributes"];
  33. assert(textAttribute["foregroundColor"] == toDynamic(text->foregroundColor));
  34. assert(textAttribute["opacity"] == text->opacity);
  35. assert(textAttribute["fontStyle"] == toString(*text->fontStyle));
  36. assert(textAttribute["fontWeight"] == toString(*text->fontWeight));
  37. }
  38. #endif
  39. } // namespace react
  40. } // namespace facebook