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 "authorize_helper.h"
19 #include "mmi_log.h"
20
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "AuthorizeHelperTest"
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 using namespace testing::ext;
28 } // namespace
29
30 class AuthorizeHelperTest : public testing::Test {
31 public:
SetUpTestCase(void)32 static void SetUpTestCase(void) {}
TearDownTestCase(void)33 static void TearDownTestCase(void) {}
SetUp()34 void SetUp() {}
TearDown()35 void TearDown() {}
36 };
37
AuthorizeExitFunTest(int32_t)38 void AuthorizeExitFunTest(int32_t) {}
39
40 /**
41 * @tc.name: AuthorizeHelperTest_OnClientDeath
42 * @tc.desc: Test OnClientDeath
43 * @tc.type: FUNC
44 * @tc.require:
45 */
46 HWTEST_F(AuthorizeHelperTest, ClientDeathHandlerTest_OnClientDeath, TestSize.Level1)
47 {
48 CALL_TEST_DEBUG;
49 AuthorizeHelper authorizeHelper;
50 int32_t pid = 10;
51 authorizeHelper.pid_ = 11;
52 EXPECT_NO_FATAL_FAILURE(authorizeHelper.OnClientDeath(pid));
53 pid = 11;
54 EXPECT_NO_FATAL_FAILURE(authorizeHelper.OnClientDeath(pid));
55 }
56
57 /**
58 * @tc.name: AuthorizeHelperTest_AuthorizeProcessExit
59 * @tc.desc: Test AuthorizeProcessExit
60 * @tc.type: FUNC
61 * @tc.require:
62 */
63 HWTEST_F(AuthorizeHelperTest, AuthorizeHelperTest_AuthorizeProcessExit, TestSize.Level1)
64 {
65 CALL_TEST_DEBUG;
66 AuthorizeHelper authorizeHelper;
67 authorizeHelper.exitCallback_ = AuthorizeExitFunTest;
68 EXPECT_NO_FATAL_FAILURE(authorizeHelper.AuthorizeProcessExit());
69 }
70
71 /**
72 * @tc.name: AuthorizeHelperTest_AuthorizeProcessExit_001
73 * @tc.desc: Test AuthorizeProcessExit
74 * @tc.type: FUNC
75 * @tc.require:
76 */
77 HWTEST_F(AuthorizeHelperTest, AuthorizeHelperTest_AuthorizeProcessExit_001, TestSize.Level1)
78 {
79 CALL_TEST_DEBUG;
80 AuthorizeHelper authorizeHelper;
81 EXPECT_NO_FATAL_FAILURE(authorizeHelper.AuthorizeProcessExit());
82 }
83
84 /**
85 * @tc.name: AuthorizeHelperTest_AddAuthorizeProcess_001
86 * @tc.desc: Test the funcation AddAuthorizeProcess
87 * @tc.type: FUNC
88 * @tc.require:
89 */
90 HWTEST_F(AuthorizeHelperTest, AuthorizeHelperTest_AddAuthorizeProcess_001, TestSize.Level1)
91 {
92 CALL_TEST_DEBUG;
93 AuthorizeHelper authorizeHelper;
94 int32_t pid = -1;
95 AuthorizeExitCallback exitCallback = nullptr;
96 authorizeHelper.isInit_ = false;
97 int32_t ret = authorizeHelper.AddAuthorizeProcess(pid, exitCallback);
98 EXPECT_EQ(ret, RET_ERR);
99 authorizeHelper.isInit_ = true;
100 ret = authorizeHelper.AddAuthorizeProcess(pid, exitCallback);
101 EXPECT_EQ(ret, RET_ERR);
102 pid = 0;
103 ret = authorizeHelper.AddAuthorizeProcess(pid, exitCallback);
104 EXPECT_EQ(ret, RET_ERR);
105 pid = 1;
106 authorizeHelper.state_ = AuthorizeState::STATE_UNAUTHORIZE;
107 authorizeHelper.pid_ = -1;
108 ret = authorizeHelper.AddAuthorizeProcess(pid, exitCallback);
109 EXPECT_EQ(ret, RET_OK);
110 authorizeHelper.pid_ = 2;
111 ret = authorizeHelper.AddAuthorizeProcess(pid, exitCallback);
112 EXPECT_EQ(ret, RET_ERR);
113 authorizeHelper.state_ = AuthorizeState::STATE_SELECTION_AUTHORIZE;
114 ret = authorizeHelper.AddAuthorizeProcess(pid, exitCallback);
115 EXPECT_EQ(ret, RET_ERR);
116 authorizeHelper.pid_ = 1;
117 ret = authorizeHelper.AddAuthorizeProcess(pid, exitCallback);
118 EXPECT_EQ(ret, RET_OK);
119 authorizeHelper.state_ = AuthorizeState::STATE_AUTHORIZE;
120 ret = authorizeHelper.AddAuthorizeProcess(pid, exitCallback);
121 EXPECT_EQ(ret, RET_OK);
122 }
123
124 /**
125 * @tc.name: AuthorizeHelperTest_CancelAuthorize_001
126 * @tc.desc: Test the funcation CancelAuthorize
127 * @tc.type: FUNC
128 * @tc.require:
129 */
130 HWTEST_F(AuthorizeHelperTest, AuthorizeHelperTest_CancelAuthorize_001, TestSize.Level1)
131 {
132 CALL_TEST_DEBUG;
133 AuthorizeHelper authorizeHelper;
134 int32_t pid = -1;
135 EXPECT_NO_FATAL_FAILURE(authorizeHelper.CancelAuthorize(pid));
136 pid = 1;
137 authorizeHelper.pid_ = 0;
138 EXPECT_NO_FATAL_FAILURE(authorizeHelper.CancelAuthorize(pid));
139 authorizeHelper.pid_ = 1;
140 EXPECT_NO_FATAL_FAILURE(authorizeHelper.CancelAuthorize(pid));
141 }
142 } // namespace MMI
143 } // namespace OHOS