• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "view/page/view_proxy.h"
18 #include "view/component/label_btn_adapter.h"
19 #include "view/component/text_label_adapter.h"
20 
21 using namespace Updater;
22 using namespace testing::ext;
23 
24 namespace {
25 class UpdaterUiViewProxyUnitTest : public testing::Test {
26 public:
SetUpTestCase(void)27     static void SetUpTestCase(void) {}
TearDownTestCase(void)28     static void TearDownTestCase(void) {}
SetUp()29     void SetUp() override {}
TearDown()30     void TearDown() override {}
31 };
32 
33 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_when_view_is_nullptr, TestSize.Level0)
34 {
35     ViewProxy viewproxy {};
36 
37     std::string err;
38     viewproxy.As(err);
39     EXPECT_EQ(err, " view is null");
40 }
41 
42 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_when_view_is_nullptr_with_custom_err_msg, TestSize.Level0)
43 {
44     ViewProxy viewproxy { nullptr, "A" };
45 
46     std::string err;
47     viewproxy.As(err);
48     EXPECT_EQ(err, "A view is null");
49 
50     err = "";
51     viewproxy.As<Updater::TextLabelAdapter>(err);
52     EXPECT_EQ(err, "A view is null");
53 }
54 
55 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_when_view_type_not_matched, TestSize.Level0)
56 {
57     std::unique_ptr<ComponentInterface> label = std::make_unique<TextLabelAdapter>();
58     OHOS::UIView *real = label->GetOhosView();
59     ViewProxy viewproxy { std::move(label), "label1" };
60 
61     std::string err {};
62     EXPECT_NE(viewproxy.As<LabelBtnAdapter>(err), real);
63     EXPECT_EQ(err, "label1 view's real type not matched");
64 
65     err = "";
66     EXPECT_EQ(viewproxy.As<TextLabelAdapter>(err), real);
67     EXPECT_EQ(err, "");
68 }
69 
70 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_when_view_type_not_matched_02, TestSize.Level0)
71 {
72     std::unique_ptr<ComponentInterface> label = std::make_unique<TextLabelAdapter>();
73     OHOS::UIView *real = label->GetOhosView();
74     ViewProxy viewproxy { std::move(label), "label1" };
75 
76     std::string err {};
77     EXPECT_NE(viewproxy.As<OHOS::UILabelButton>(err), real);
78     EXPECT_EQ(err, "label1 view's real type not matched");
79 
80     err = "";
81     EXPECT_EQ(viewproxy.As<OHOS::UILabel>(err), real);
82     EXPECT_EQ(err, "");
83 }
84 
85 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_without_err_out_param, TestSize.Level0)
86 {
87     std::unique_ptr<ComponentInterface> label = std::make_unique<TextLabelAdapter>();
88     OHOS::UIView *real = label->GetOhosView();
89     ViewProxy viewproxy { std::move(label), "label1" };
90 
91     EXPECT_NE(viewproxy.As<LabelBtnAdapter>(), real);
92     EXPECT_EQ(viewproxy.As<TextLabelAdapter>(), real);
93 }
94 
95 HWTEST_F(UpdaterUiViewProxyUnitTest, test_operator_arrow, TestSize.Level0)
96 {
97     OHOS::UIView *dummy = ViewProxy {}.operator->();
98 
99     ViewProxy viewproxy { std::make_unique<TextLabelAdapter>(), "label1" };
100     EXPECT_NE(viewproxy.operator->(), dummy);
101     EXPECT_EQ(ViewProxy {}.operator->(), dummy);
102 }
103 }