/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "gtest/gtest.h" #include "drawing_bitmap.h" #include "drawing_brush.h" #include "drawing_canvas.h" #include "drawing_color.h" #include "drawing_font.h" #include "drawing_font_collection.h" #include "drawing_path.h" #include "drawing_pen.h" #include "drawing_text_declaration.h" #include "drawing_text_typography.h" #ifndef USE_GRAPHIC_TEXT_GINE #include "rosen_text/ui/typography.h" #include "rosen_text/ui/typography_create.h" #else #include "rosen_text/typography.h" #include "rosen_text/typography_create.h" #endif #include #include #ifndef USE_GRAPHIC_TEXT_GINE using namespace rosen; #else using namespace OHOS::Rosen; #endif using namespace testing; using namespace testing::ext; namespace OHOS { class OHDrawingTypographyLargeValueTest : public testing::Test { }; /* * @tc.name: OHDrawingTypographyLargeValueTest016 * @tc.desc: test for typography * @tc.type: FUNC */ HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTest016, TestSize.Level1) { OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle(); OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection()); EXPECT_TRUE(handler != nullptr); OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00)); double fontSize = 30; OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize); OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400); OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC); const char* fontFamilies[] = {"Roboto"}; OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies); OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle); const char* text = "OpenHarmony\n"; OH_Drawing_TypographyHandlerAddText(handler, text); OH_Drawing_TypographyHandlerPopTextStyle(handler); OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler); const float indents[] = {1.2, 3.4}; OH_Drawing_TypographySetIndents(typography, 2, indents); float indent = 3.4; EXPECT_EQ(indent, OH_Drawing_TypographyGetIndentsWithIndex(typography, 1)); double maxWidth = 2160.0; OH_Drawing_TypographyLayout(typography, maxWidth); EXPECT_EQ(maxWidth, OH_Drawing_TypographyGetMaxWidth(typography)); double position[2] = {10.0, 15.0}; OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate(); OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; uint32_t width = 20; uint32_t height = 40; OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat); EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(cBitmap)); EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(cBitmap)); OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate(); OH_Drawing_CanvasBind(cCanvas, cBitmap); OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF)); EXPECT_EQ(OH_Drawing_TypographyGetHeight(typography) != 0.0, true); EXPECT_EQ(OH_Drawing_TypographyGetLongestLine(typography) != 0.0, true); EXPECT_EQ(OH_Drawing_TypographyGetMinIntrinsicWidth(typography) <= OH_Drawing_TypographyGetMaxIntrinsicWidth(typography), true); EXPECT_EQ(OH_Drawing_TypographyGetAlphabeticBaseline(typography) != 0.0, true); EXPECT_EQ(OH_Drawing_TypographyGetIdeographicBaseline(typography) != 0.0, true); OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]); OH_Drawing_DestroyTypography(typography); OH_Drawing_DestroyTypographyHandler(handler); } /* * @tc.name: OHDrawingTypographyLargeValueTest026 * @tc.desc: test for typography and txtStyle * @tc.type: FUNC */ HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTest026, TestSize.Level1) { OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle(); OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection()); EXPECT_TRUE(handler != nullptr); OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00)); double fontSize = 30; OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize); OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400); bool halfLeading = true; OH_Drawing_SetTextStyleHalfLeading(txtStyle, halfLeading); const char* fontFamilies[] = {"Roboto"}; OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies); OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle); const char* text = "OpenHarmony\n"; OH_Drawing_TypographyHandlerAddText(handler, text); OH_Drawing_PlaceholderSpan placeholderSpan = {20, 40, ALIGNMENT_OFFSET_AT_BASELINE, TEXT_BASELINE_ALPHABETIC, 10}; OH_Drawing_TypographyHandlerAddPlaceholder(handler, &placeholderSpan); OH_Drawing_TypographyHandlerPopTextStyle(handler); OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler); double maxWidth = 2160.0; OH_Drawing_TypographyLayout(typography, maxWidth); double position[2] = {10.0, 15.0}; OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate(); OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; uint32_t width = 20; uint32_t height = 40; OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat); OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate(); OH_Drawing_CanvasBind(cCanvas, cBitmap); OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF)); EXPECT_EQ(OH_Drawing_TypographyDidExceedMaxLines(typography) != true, true); OH_Drawing_RectHeightStyle heightStyle = RECT_HEIGHT_STYLE_TIGHT; OH_Drawing_RectWidthStyle widthStyle = RECT_WIDTH_STYLE_TIGHT; EXPECT_EQ(OH_Drawing_TypographyGetRectsForRange(typography, 1, 2, heightStyle, widthStyle) != nullptr, true); EXPECT_EQ(OH_Drawing_TypographyGetRectsForPlaceholders(typography) != nullptr, true); EXPECT_EQ(OH_Drawing_TypographyGetGlyphPositionAtCoordinate(typography, 1, 0) != nullptr, true); EXPECT_EQ(OH_Drawing_TypographyGetGlyphPositionAtCoordinateWithCluster(typography, 1, 0) != nullptr, true); EXPECT_EQ(OH_Drawing_TypographyGetWordBoundary(typography, 1) != nullptr, true); EXPECT_EQ(OH_Drawing_TypographyGetLineTextRange(typography, 1, true) != nullptr, true); EXPECT_EQ(OH_Drawing_TypographyGetLineCount(typography) != 0, true); OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]); OH_Drawing_DestroyTypography(typography); OH_Drawing_DestroyTypographyHandler(handler); } /* * @tc.name: OHDrawingTypographyLargeValueTest027 * @tc.desc: test for getting line info for text typography * @tc.type: FUNC */ HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTest027, TestSize.Level1) { OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle(); OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection()); EXPECT_TRUE(handler != nullptr); OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00)); double fontSize = 30; OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize); OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400); bool halfLeading = true; OH_Drawing_SetTextStyleHalfLeading(txtStyle, halfLeading); const char* fontFamilies[] = {"Roboto"}; OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies); OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle); const char* text = "OpenHarmony\n"; OH_Drawing_TypographyHandlerAddText(handler, text); OH_Drawing_TypographyHandlerPopTextStyle(handler); OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler); double maxWidth = 2160.0; OH_Drawing_TypographyLayout(typography, maxWidth); double position[2] = {10.0, 15.0}; OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate(); OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; uint32_t width = 20; uint32_t height = 40; OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat); OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate(); OH_Drawing_CanvasBind(cCanvas, cBitmap); OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF)); OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]); int lineNum = 0; bool oneLine = true; bool includeWhitespace = true; OH_Drawing_LineMetrics lineMetrics; EXPECT_EQ(OH_Drawing_TypographyGetLineInfo(typography, lineNum, oneLine, includeWhitespace, nullptr), false); EXPECT_EQ(OH_Drawing_TypographyGetLineInfo(typography, -1, oneLine, includeWhitespace, &lineMetrics), false); EXPECT_EQ(OH_Drawing_TypographyGetLineInfo(typography, lineNum, oneLine, includeWhitespace, &lineMetrics), true); OH_Drawing_DestroyTypography(typography); OH_Drawing_DestroyTypographyHandler(handler); } /* * @tc.name: OHDrawingTypographyLargeValueTest042 * @tc.desc: test for text shadow for textstyle * @tc.type: FUNC */ HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTest042, TestSize.Level1) { OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle(); OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection()); EXPECT_TRUE(handler != nullptr); OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00)); double fontSize = 30; OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize); OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400); bool halfLeading = true; OH_Drawing_SetTextStyleHalfLeading(txtStyle, halfLeading); const char* fontFamilies[] = {"Roboto"}; OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies); OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle); const char* text = "OpenHarmony\n"; OH_Drawing_TypographyHandlerAddText(handler, text); OH_Drawing_TypographyHandlerPopTextStyle(handler); OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler); double maxWidth = 2160.0; OH_Drawing_TypographyLayout(typography, maxWidth); double position[2] = {10.0, 15.0}; OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate(); OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; uint32_t width = 20; uint32_t height = 40; OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat); OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate(); OH_Drawing_CanvasBind(cCanvas, cBitmap); OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF)); EXPECT_EQ(OH_Drawing_TextStyleGetShadows(txtStyle) != nullptr, true); OH_Drawing_TextStyleClearShadows(txtStyle); OH_Drawing_TextShadow* textshadows = OH_Drawing_TextStyleGetShadows(txtStyle); OH_Drawing_DestroyTextShadows(textshadows); OH_Drawing_DestroyTextShadows(nullptr); OH_Drawing_TextStyleAddShadow(txtStyle, nullptr); OH_Drawing_TextStyleAddShadow(txtStyle, OH_Drawing_CreateTextShadow()); EXPECT_EQ(OH_Drawing_TextStyleGetShadowWithIndex(txtStyle, 0) != nullptr, true); EXPECT_EQ(OH_Drawing_TextStyleGetShadowWithIndex(txtStyle, 10000000) == nullptr, true); EXPECT_EQ(OH_Drawing_TextStyleGetShadowWithIndex(nullptr, 0) == nullptr, true); EXPECT_EQ(OH_Drawing_TextStyleGetShadowCount(txtStyle), 1); EXPECT_EQ(OH_Drawing_TextStyleGetShadowCount(nullptr), 0); OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]); OH_Drawing_DestroyTypography(typography); OH_Drawing_DestroyTypographyHandler(handler); } /* * @tc.name: OHDrawingTypographyLargeValueTestWithIndent * @tc.desc: test for typography * @tc.type: FUNC */ HWTEST_F(OHDrawingTypographyLargeValueTest, OHDrawingTypographyLargeValueTestWithIndent, TestSize.Level1) { OH_Drawing_TypographyStyle* typoStyle = OH_Drawing_CreateTypographyStyle(); OH_Drawing_TextStyle* txtStyle = OH_Drawing_CreateTextStyle(); OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection()); EXPECT_TRUE(handler != nullptr); OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00)); double fontSize = 30; OH_Drawing_SetTextStyleFontSize(txtStyle, fontSize); OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400); OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC); const char* fontFamilies[] = {"Roboto"}; OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies); OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle); const char* text = "OpenHarmony\n"; OH_Drawing_TypographyHandlerAddText(handler, text); OH_Drawing_TypographyHandlerPopTextStyle(handler); OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler); const float indents[] = {1.2, 3.4}; OH_Drawing_TypographySetIndents(typography, 2, indents); float indent = 3.4; EXPECT_EQ(indent, OH_Drawing_TypographyGetIndentsWithIndex(typography, 1)); double maxWidth = 2160.0; OH_Drawing_TypographyLayout(typography, maxWidth); EXPECT_EQ(maxWidth, OH_Drawing_TypographyGetMaxWidth(typography)); double position[2] = {10.0, 15.0}; OH_Drawing_Bitmap* cBitmap = OH_Drawing_BitmapCreate(); OH_Drawing_BitmapFormat cFormat {COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE}; uint32_t width = 20; uint32_t height = 40; OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat); EXPECT_EQ(width, OH_Drawing_BitmapGetWidth(cBitmap)); EXPECT_EQ(height, OH_Drawing_BitmapGetHeight(cBitmap)); OH_Drawing_Canvas* cCanvas = OH_Drawing_CanvasCreate(); OH_Drawing_CanvasBind(cCanvas, cBitmap); OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF)); EXPECT_EQ(OH_Drawing_TypographyGetHeight(typography) != 0.0, true); EXPECT_EQ(OH_Drawing_TypographyGetLongestLineWithIndent(typography) != 0.0, true); EXPECT_EQ(OH_Drawing_TypographyGetMinIntrinsicWidth(typography) <= OH_Drawing_TypographyGetMaxIntrinsicWidth(typography), true); EXPECT_EQ(OH_Drawing_TypographyGetAlphabeticBaseline(typography) != 0.0, true); EXPECT_EQ(OH_Drawing_TypographyGetIdeographicBaseline(typography) != 0.0, true); OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]); OH_Drawing_DestroyTypography(typography); OH_Drawing_DestroyTypographyHandler(handler); } }