1 /*
2  * Copyright (c) 2023 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 "screen_off_time_plugin.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "edm_data_ability_utils_mock.h"
21 #include "edm_ipc_interface_code.h"
22 #include "iplugin_manager.h"
23 #include "utils.h"
24 
25 using namespace testing::ext;
26 using namespace testing;
27 
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 
32 class ScreenOffTimePluginTest : public testing::TestWithParam<std::pair<int32_t, int32_t>> {
33 protected:
34     static void SetUpTestSuite(void);
35 
36     static void TearDownTestSuite(void);
37 };
38 
SetUpTestSuite(void)39 void ScreenOffTimePluginTest::SetUpTestSuite(void)
40 {
41     Utils::SetEdmServiceEnable();
42     Utils::SetEdmInitialEnv();
43 }
44 
TearDownTestSuite(void)45 void ScreenOffTimePluginTest::TearDownTestSuite(void)
46 {
47     Utils::SetEdmServiceDisable();
48     Utils::ResetTokenTypeAndUid();
49     ASSERT_TRUE(Utils::IsOriginalUTEnv());
50     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
51 }
52 
53 INSTANTIATE_TEST_CASE_P(TestOnSetPolicy, ScreenOffTimePluginTest,
54     testing::ValuesIn(std::vector<std::pair<int32_t, int32_t>>({
55         {-1, ERR_OK},
56         {-2, EdmReturnErrCode::PARAM_ERROR},
57         {-30000, EdmReturnErrCode::PARAM_ERROR},
58         {0, EdmReturnErrCode::PARAM_ERROR},
59         {1, EdmReturnErrCode::PARAM_ERROR},
60         {14999, EdmReturnErrCode::PARAM_ERROR},
61         {15000, ERR_OK},
62         {36000000, ERR_OK},
63         {30000, ERR_OK},
64     })));
65 
66 /**
67  * @tc.name: TestOnSetPolicy
68  * @tc.desc: Test OnSetPolicy function.
69  * @tc.type: FUNC
70  */
71 HWTEST_P(ScreenOffTimePluginTest, TestOnSetPolicy, TestSize.Level1)
72 {
73     ScreenOffTimePlugin plugin;
74     std::pair<int32_t, int32_t> keyValue = GetParam();
75     std::cout << "ScreenOffTimePluginTest " << keyValue.first << " " << keyValue.second << std::endl;
76     ErrCode code = plugin.OnSetPolicy(keyValue.first);
77     EXPECT_TRUE(code == keyValue.second);
78 }
79 
80 /**
81  * @tc.name: TestOnGetPolicy
82  * @tc.desc: Test OnGetPolicy function.
83  * @tc.type: FUNC
84  */
85 HWTEST_F(ScreenOffTimePluginTest, TestOnGetPolicy, TestSize.Level1)
86 {
87     std::shared_ptr<IPlugin> plugin = ScreenOffTimePlugin::GetPlugin();
88     std::string policyValue{"TestString"};
89     MessageParcel data;
90     MessageParcel reply;
91 
92     EdmDataAbilityUtils::SetResult("SYSTEM_ABNORMALLY");
93     ErrCode code = plugin->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
94     EXPECT_TRUE(code == EdmReturnErrCode::SYSTEM_ABNORMALLY);
95 
96     EdmDataAbilityUtils::SetResult("test success");
97     code = plugin->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
98     EXPECT_TRUE(code == ERR_OK);
99 }
100 
101 } // namespace TEST
102 } // namespace EDM
103 } // namespace OHOS