1 /*
2 * Copyright (c) 2020-2021 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 "components/ui_radio_button.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20 #include "components/root_view.h"
21 using namespace testing::ext;
22 namespace OHOS {
23 class UIRadioButtonTest : public testing::Test {
24 public:
25 static void SetUpTestCase();
26 static void TearDownTestCase();
27 static UIRadioButton* radioBtn_;
28 };
29
30 UIRadioButton* UIRadioButtonTest::radioBtn_ = nullptr;
31
SetUpTestCase()32 void UIRadioButtonTest::SetUpTestCase()
33 {
34 if (radioBtn_ == nullptr) {
35 radioBtn_ = new UIRadioButton();
36 }
37 }
38
TearDownTestCase()39 void UIRadioButtonTest::TearDownTestCase()
40 {
41 if (radioBtn_ != nullptr) {
42 delete radioBtn_;
43 radioBtn_ = nullptr;
44 }
45 }
46
47 /**
48 * @tc.name: UIRadioButtonGetViewType_001
49 * @tc.desc: Verify GetViewType function, equal.
50 * @tc.type: FUNC
51 * @tc.require: AR000DSMQB
52 */
53 HWTEST_F(UIRadioButtonTest, UIRadioButtonGetViewType_001, TestSize.Level1)
54 {
55 if (radioBtn_ == nullptr) {
56 EXPECT_NE(0, 0);
57 return;
58 }
59 EXPECT_EQ(radioBtn_->GetViewType(), UI_RADIO_BUTTON);
60 }
61
62 /**
63 * @tc.name: UIRadioButtonGetName_001
64 * @tc.desc: Verify GetName function, equal.
65 * @tc.type: FUNC
66 * @tc.require: AR000F4E5I
67 */
68 HWTEST_F(UIRadioButtonTest, UIRadioButtonGetName_001, TestSize.Level1)
69 {
70 if (radioBtn_ == nullptr) {
71 EXPECT_NE(0, 0);
72 return;
73 }
74 const char* name = "group";
75 radioBtn_->SetName(name);
76 if (radioBtn_->GetName() == nullptr) {
77 EXPECT_NE(0, 0);
78 return;
79 }
80 EXPECT_EQ(strcmp(radioBtn_->GetName(), name), 0);
81 }
82
83 /**
84 * @tc.name: UIRadioButtonSetName_001
85 * @tc.desc: Verify SetName function, equal.
86 * @tc.type: FUNC
87 * @tc.require: SR000F3PEE
88 */
89 HWTEST_F(UIRadioButtonTest, UIRadioButtonSetName_001, TestSize.Level0)
90 {
91 if (radioBtn_ == nullptr) {
92 EXPECT_NE(0, 0);
93 return;
94 }
95 const char* name1 = "group1";
96 const char* name2 = "group2";
97 UIRadioButton* radioBtn1 = new UIRadioButton(name1);
98 UIRadioButton* radioBtn2 = new UIRadioButton();
99 radioBtn2->SetName(name1);
100 UIRadioButton* radioBtn3 = new UIRadioButton(name1);
101 UIRadioButton* radioBtn4 = new UIRadioButton(name2);
102 UIRadioButton* radioBtn5 = new UIRadioButton(name2);
103 UIViewGroup* view = static_cast<UIViewGroup*>(RootView::GetInstance());
104 view->Add(radioBtn1);
105 view->Add(radioBtn2);
106 view->Add(radioBtn3);
107 view->Add(radioBtn4);
108 view->Add(radioBtn5);
109
110 ClickEvent event({0, 0});
111 radioBtn2->OnClickEvent(event);
112 EXPECT_EQ(radioBtn1->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
113 EXPECT_EQ(radioBtn2->GetState(), UICheckBox::UICheckBoxState::SELECTED);
114 EXPECT_EQ(radioBtn3->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
115 EXPECT_EQ(radioBtn4->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
116 EXPECT_EQ(radioBtn5->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
117
118 radioBtn4->OnClickEvent(event);
119 EXPECT_EQ(radioBtn1->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
120 EXPECT_EQ(radioBtn2->GetState(), UICheckBox::UICheckBoxState::SELECTED);
121 EXPECT_EQ(radioBtn3->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
122 EXPECT_EQ(radioBtn4->GetState(), UICheckBox::UICheckBoxState::SELECTED);
123 EXPECT_EQ(radioBtn5->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
124
125 radioBtn1->OnClickEvent(event);
126 EXPECT_EQ(radioBtn1->GetState(), UICheckBox::UICheckBoxState::SELECTED);
127 EXPECT_EQ(radioBtn2->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
128 EXPECT_EQ(radioBtn3->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
129 EXPECT_EQ(radioBtn4->GetState(), UICheckBox::UICheckBoxState::SELECTED);
130 EXPECT_EQ(radioBtn5->GetState(), UICheckBox::UICheckBoxState::UNSELECTED);
131
132 view->Remove(radioBtn1);
133 view->Remove(radioBtn2);
134 view->Remove(radioBtn3);
135 view->Remove(radioBtn4);
136 view->Remove(radioBtn5);
137 delete radioBtn1;
138 delete radioBtn2;
139 delete radioBtn3;
140 delete radioBtn4;
141 delete radioBtn5;
142 }
143 } // namespace OHOS
144