• 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 Updater::ViewProxy;
22 using Updater::TextLabelAdapter;
23 using Updater::LabelBtnAdapter;
24 using namespace testing::ext;
25 
26 namespace {
27 class UpdaterUiViewProxyUnitTest : public testing::Test {
28 public:
SetUpTestCase(void)29     static void SetUpTestCase(void) {}
TearDownTestCase(void)30     static void TearDownTestCase(void) {}
SetUp()31     void SetUp() override {}
TearDown()32     void TearDown() override {}
33 };
34 
35 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_when_view_is_nullptr, TestSize.Level0)
36 {
37     ViewProxy viewproxy {};
38 
39     std::string err;
40     viewproxy.As(err);
41     EXPECT_EQ(err, " view is null");
42 }
43 
44 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_when_view_is_nullptr_with_custom_err_msg, TestSize.Level0)
45 {
46     ViewProxy viewproxy { nullptr, "A" };
47 
48     std::string err;
49     viewproxy.As(err);
50     EXPECT_EQ(err, "A view is null");
51 
52     err = "";
53     viewproxy.As<Updater::TextLabelAdapter>(err);
54     EXPECT_EQ(err, "A view is null");
55 }
56 
57 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_when_view_type_not_matched, TestSize.Level0)
58 {
59     std::unique_ptr<OHOS::UIView> label = std::make_unique<TextLabelAdapter>();
60     OHOS::UIView *real = label.get();
61     ViewProxy viewproxy { std::move(label), "label1" };
62 
63     std::string err {};
64     EXPECT_NE(viewproxy.As<LabelBtnAdapter>(err), real);
65     EXPECT_EQ(err, "label1 view's real type not matched");
66 
67     err = "";
68     EXPECT_EQ(viewproxy.As<TextLabelAdapter>(err), real);
69     EXPECT_EQ(err, "");
70 }
71 
72 HWTEST_F(UpdaterUiViewProxyUnitTest, test_as_without_err_out_param, TestSize.Level0)
73 {
74     std::unique_ptr<OHOS::UIView> label = std::make_unique<TextLabelAdapter>();
75     OHOS::UIView *real = label.get();
76     ViewProxy viewproxy { std::move(label), "label1" };
77 
78     EXPECT_NE(viewproxy.As<LabelBtnAdapter>(), real);
79     EXPECT_EQ(viewproxy.As<TextLabelAdapter>(), real);
80 }
81 
82 HWTEST_F(UpdaterUiViewProxyUnitTest, test_operator_arrow, TestSize.Level0)
83 {
84     OHOS::UIView *dummy = ViewProxy {}.operator->();
85 
86     ViewProxy viewproxy { std::make_unique<TextLabelAdapter>(), "label1" };
87     EXPECT_NE(viewproxy.operator->(), dummy);
88     EXPECT_EQ(ViewProxy {}.operator->(), dummy);
89 }
90 }