1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "gtest/gtest.h" 17 18 #include "base/utils/system_properties.h" 19 #include "core/components/piece/piece_theme.h" 20 #include "core/pipeline/base/constants.h" 21 #include "frameworks/bridge/common/dom/dom_piece.h" 22 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h" 23 24 using namespace testing; 25 using namespace testing::ext; 26 27 namespace OHOS::Ace::Framework { 28 namespace { 29 30 const std::string PIECE_CONTENT_VALUE = "Chips"; 31 const std::string PIECE_ICON_SRC = "test.svg"; 32 33 } // namespace 34 35 class DomPieceTest : public testing::Test { 36 public: SetUpTestCase()37 static void SetUpTestCase() {} TearDownTestCase()38 static void TearDownTestCase() {} SetUp()39 void SetUp() {} TearDown()40 void TearDown() {} 41 }; 42 43 /** 44 * @tc.name: CreateDOMNodeFromDsl001 45 * @tc.desc: Verify that DomPiece can create PieceComponent correctly with all attributes set. 46 * @tc.type: FUNC 47 */ 48 HWTEST_F(DomPieceTest, CreateDOMNodeFromDsl001, TestSize.Level1) 49 { 50 if (SystemProperties::GetDeviceType() == DeviceType::PHONE) { 51 /** 52 * @tc.steps: step1. the json string of DomPiece with all attributes set. 53 */ 54 const std::string jsonPieceStr = "" 55 "{ " 56 " \"tag\": \"piece\", " 57 " \"attr\": [{ " 58 " \"content\" : \"Chips\" " 59 " }, " 60 " { " 61 " \"closable\" : \"true\" " 62 " }, " 63 " { " 64 " \"icon\" : \"test.svg\" " 65 " }], " 66 " \"event\": [ \"close\" ] " 67 "}"; 68 69 /** 70 * @tc.steps: step2. call JsonUtil interface and create DomPiece. 71 */ 72 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonPieceStr); 73 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot); 74 auto pieceComponent = AceType::DynamicCast<PieceComponent>(boxChild->GetChild()); 75 76 /** 77 * @tc.steps: step3. Verify whether all attributes of PieceComponent are as excepted. 78 * @tc.expected: step3. All attributes of PieceComponent are as excepted. 79 */ 80 EXPECT_TRUE(pieceComponent); 81 EXPECT_TRUE(pieceComponent->ShowDelete()); 82 EXPECT_TRUE(pieceComponent->GetContent() == PIECE_CONTENT_VALUE); 83 EXPECT_TRUE(pieceComponent->GetIcon() == PIECE_ICON_SRC); 84 EXPECT_TRUE(pieceComponent->GetOnDelete() == std::to_string(domNodeRoot->GetNodeId())); 85 } 86 } 87 88 /** 89 * @tc.name: CreateDOMNodeFromDsl002 90 * @tc.desc: Verify that DomPiece can create PieceComponent correctly with no attribute set. 91 * @tc.type: FUNC 92 */ 93 HWTEST_F(DomPieceTest, CreateDOMNodeFromDsl002, TestSize.Level1) 94 { 95 if (SystemProperties::GetDeviceType() == DeviceType::PHONE) { 96 /** 97 * @tc.steps: step1. the json string of DomPiece with no attribute set. 98 */ 99 const std::string jsonPieceStr = "" 100 "{ " 101 " \"tag\": \"piece\" " 102 "}"; 103 104 /** 105 * @tc.steps: step2. call JsonUtil interface and create DomPiece. 106 */ 107 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonPieceStr); 108 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot); 109 auto pieceComponent = AceType::DynamicCast<PieceComponent>(boxChild->GetChild()); 110 111 /** 112 * @tc.steps: step3. Verify whether all attributes of PieceComponent are as excepted. 113 * @tc.expected: step3. All attributes of PieceComponent are as excepted. 114 */ 115 EXPECT_TRUE(pieceComponent); 116 EXPECT_TRUE(!pieceComponent->ShowDelete()); 117 EXPECT_TRUE(pieceComponent->GetContent().empty()); 118 EXPECT_TRUE(pieceComponent->GetIcon().empty()); 119 } 120 } 121 122 } // namespace OHOS::Ace::Framework