1 /*
2 * Copyright (c) 2024 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 "window_display_change_adapter.h"
19 #include "window_manager_hilog.h"
20 #include "iremote_object_mocker.h"
21
22 #include "window_display_change_adapter.cpp"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 class TestDisplayInfoChangedListener : public IDisplayInfoChangedListener {
30 public:
31 DisplayId displayId_ { 0 };
32 float density_ { 0.0f };
33 DisplayOrientation orientation_ { DisplayOrientation::UNKNOWN };
34
OnDisplayInfoChange(const sptr<IRemoteObject> & token,DisplayId displayId,float density,DisplayOrientation orientation)35 void OnDisplayInfoChange(const sptr<IRemoteObject>& token, DisplayId displayId, float density,
36 DisplayOrientation orientation) override
37 {
38 displayId_ = displayId;
39 density_ = density;
40 orientation_ = orientation;
41 }
42 };
43
44 class WindowDisplayChangeAdapterTest : public testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp() override;
49 void TearDown() override;
50 };
SetUpTestCase()51 void WindowDisplayChangeAdapterTest::SetUpTestCase()
52 {
53 }
54
TearDownTestCase()55 void WindowDisplayChangeAdapterTest::TearDownTestCase()
56 {
57 }
58
SetUp()59 void WindowDisplayChangeAdapterTest::SetUp()
60 {
61 }
62
TearDown()63 void WindowDisplayChangeAdapterTest::TearDown()
64 {
65 }
66
67 namespace {
68 /**
69 *@tc.name: OnChange
70 *@tc.desc: Window Display Information Change
71 *@tc.type: FUNC
72 */
73 HWTEST_F(WindowDisplayChangeAdapterTest, OnChange, Function | SmallTest | Level2)
74 {
75 sptr<IRemoteObject> targetToken = new (std::nothrow) IRemoteObjectMocker();
76 ASSERT_NE(nullptr, targetToken);
77 sptr<TestDisplayInfoChangedListener> listener = new (std::nothrow) TestDisplayInfoChangedListener();
78 ASSERT_NE(nullptr, listener);
79 sptr<WindowDisplayChangeAdapter> window = new WindowDisplayChangeAdapter(targetToken, listener);
80 ASSERT_NE(nullptr, window);
81 DisplayId displayId = 0;
82 window->OnChange(displayId);
83 }
84
85 /**
86 *@tc.name: OnDisplayInfoChange
87 *@tc.desc: notify Display Information Change
88 *@tc.type: FUNC
89 */
90 HWTEST_F(WindowDisplayChangeAdapterTest, OnDisplayInfoChange, Function | SmallTest | Level2)
91 {
92 sptr<IRemoteObject> targetToken = new (std::nothrow) IRemoteObjectMocker();
93 ASSERT_NE(nullptr, targetToken);
94 sptr<TestDisplayInfoChangedListener> listener = new (std::nothrow) TestDisplayInfoChangedListener();
95 ASSERT_NE(nullptr, listener);
96 sptr<WindowDisplayChangeAdapter> window = new WindowDisplayChangeAdapter(targetToken, listener);
97 ASSERT_NE(nullptr, window);
98
99 DisplayId displayId = 1;
100 float density = 0.2f;
101 DisplayOrientation orientation = DisplayOrientation::UNKNOWN;
102
103 window->OnDisplayInfoChange(targetToken, displayId, density, orientation);
104 // not need notify display info, data is not changed
105 window->OnDisplayInfoChange(targetToken, displayId, density, orientation);
106
107 ASSERT_EQ(displayId, listener->displayId_);
108 ASSERT_EQ(density, listener->density_);
109 ASSERT_EQ(orientation, listener->orientation_);
110 }
111 }
112 } // namespace Rosen
113 } // namespace OHOS