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 "location_config_manager_test.h"
17
18 #include <cstdio>
19
20 #include "common_utils.h"
21 #include "constant_definition.h"
22 #include "location_config_manager.h"
23 #include "location_log.h"
24
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace Location {
28 const int STATE_UNKNOWN = -1;
29 const int PRIVACY_TYPE_INVALID_LEFT = -1;
30 const int PRIVACY_TYPE_INVALID_RIGHT = 3;
SetUp()31 void LocationConfigManagerTest::SetUp()
32 {
33 }
34
TearDown()35 void LocationConfigManagerTest::TearDown()
36 {
37 }
38
GetLocationSwitchConfigPath()39 std::string LocationConfigManagerTest::GetLocationSwitchConfigPath()
40 {
41 int userId = 0;
42 bool ret = CommonUtils::GetCurrentUserId(userId);
43 if (!ret) {
44 LBSLOGE(LOCATOR, "GetCurrentUserId failed");
45 }
46 std::string filePath = LOCATION_DIR + SWITCH_CONFIG_NAME + "_" + std::to_string(userId) + ".conf";
47 return filePath;
48 }
49
50 HWTEST_F(LocationConfigManagerTest, LocationConfigManagerInitTest001, TestSize.Level1)
51 {
52 GTEST_LOG_(INFO)
53 << "LocationConfigManagerTest, LocationConfigManagerInitTest001, TestSize.Level1";
54 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerInitTest001 begin");
55 EXPECT_EQ(0, LocationConfigManager::GetInstance().Init());
56 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerInitTest001 end");
57 }
58
59 HWTEST_F(LocationConfigManagerTest, LocationConfigManagerSwitchStateTest001, TestSize.Level1)
60 {
61 GTEST_LOG_(INFO)
62 << "LocationConfigManagerTest, LocationConfigManagerSwitchStateTest001, TestSize.Level1";
63 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerSwitchStateTest001 begin");
64 EXPECT_EQ(0,
65 LocationConfigManager::GetInstance().SetLocationSwitchState(STATE_CLOSE));
66 EXPECT_EQ(STATE_CLOSE,
67 LocationConfigManager::GetInstance().GetLocationSwitchState());
68
69 EXPECT_EQ(0,
70 LocationConfigManager::GetInstance().SetLocationSwitchState(STATE_OPEN));
71 EXPECT_EQ(STATE_OPEN,
72 LocationConfigManager::GetInstance().GetLocationSwitchState());
73
74 EXPECT_EQ(-1,
75 LocationConfigManager::GetInstance().SetLocationSwitchState(STATE_UNKNOWN));
76 EXPECT_EQ(STATE_OPEN,
77 LocationConfigManager::GetInstance().GetLocationSwitchState());
78 EXPECT_EQ(true,
79 LocationConfigManager::GetInstance().IsExistFile(GetLocationSwitchConfigPath()));
80 remove(GetLocationSwitchConfigPath().c_str());
81 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerSwitchStateTest001 end");
82 }
83
84 HWTEST_F(LocationConfigManagerTest, LocationConfigManagerPrivacyTypeStateTest001, TestSize.Level1)
85 {
86 GTEST_LOG_(INFO)
87 << "LocationConfigManagerTest, LocationConfigManagerPrivacyTypeStateTest001, TestSize.Level1";
88 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerPrivacyTypeStateTest001 begin");
89 bool isConfirmed = false;
90 EXPECT_EQ(ERRCODE_INVALID_PARAM,
91 LocationConfigManager::GetInstance().SetPrivacyTypeState(PRIVACY_TYPE_INVALID_LEFT, true));
92 EXPECT_EQ(ERRCODE_INVALID_PARAM,
93 LocationConfigManager::GetInstance().GetPrivacyTypeState(PRIVACY_TYPE_INVALID_LEFT, isConfirmed));
94
95 EXPECT_EQ(ERRCODE_INVALID_PARAM,
96 LocationConfigManager::GetInstance().SetPrivacyTypeState(PRIVACY_TYPE_INVALID_RIGHT, true));
97 EXPECT_EQ(ERRCODE_INVALID_PARAM,
98 LocationConfigManager::GetInstance().GetPrivacyTypeState(PRIVACY_TYPE_INVALID_RIGHT, isConfirmed));
99
100 EXPECT_EQ(ERRCODE_SUCCESS,
101 LocationConfigManager::GetInstance().SetPrivacyTypeState(PRIVACY_TYPE_STARTUP, true));
102 EXPECT_EQ(ERRCODE_SUCCESS,
103 LocationConfigManager::GetInstance().GetPrivacyTypeState(PRIVACY_TYPE_STARTUP, isConfirmed));
104 EXPECT_EQ(true, isConfirmed);
105
106 EXPECT_EQ(ERRCODE_SUCCESS,
107 LocationConfigManager::GetInstance().SetPrivacyTypeState(PRIVACY_TYPE_CORE_LOCATION, false));
108 EXPECT_EQ(ERRCODE_SUCCESS,
109 LocationConfigManager::GetInstance().GetPrivacyTypeState(PRIVACY_TYPE_CORE_LOCATION, isConfirmed));
110 EXPECT_EQ(false, isConfirmed);
111 remove(LocationConfigManager::GetInstance().GetPrivacyTypeConfigPath(PRIVACY_TYPE_STARTUP).c_str());
112 remove(LocationConfigManager::GetInstance().GetPrivacyTypeConfigPath(PRIVACY_TYPE_CORE_LOCATION).c_str());
113 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerPrivacyTypeStateTest001 end");
114 }
115
116 HWTEST_F(LocationConfigManagerTest, LocationConfigManagerIsExistFileTest001, TestSize.Level1)
117 {
118 GTEST_LOG_(INFO)
119 << "LocationConfigManagerTest, LocationConfigManagerIsExistFileTest001, TestSize.Level1";
120 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerIsExistFileTest001 begin");
121 EXPECT_EQ(false,
122 LocationConfigManager::GetInstance().IsExistFile("invalid_path"));
123 int userId = 0;
124 CommonUtils::GetCurrentUserId(userId);
125 std::string configPath = LOCATION_DIR + SWITCH_CONFIG_NAME + "_" + std::to_string(userId) + ".conf";
126 EXPECT_EQ(false,
127 LocationConfigManager::GetInstance().IsExistFile("/wrongpath" + configPath));
128 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerIsExistFileTest001 end");
129 }
130
131 HWTEST_F(LocationConfigManagerTest, LocationConfigManagerCreateFileTest001, TestSize.Level1)
132 {
133 GTEST_LOG_(INFO)
134 << "LocationConfigManagerTest, LocationConfigManagerCreateFileTest001, TestSize.Level1";
135 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerCreateFileTest001 begin");
136 EXPECT_EQ(true, LocationConfigManager::GetInstance().CreateFile("filename", "filedata"));
137 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerCreateFileTest001 end");
138 }
139
140 HWTEST_F(LocationConfigManagerTest, LocationConfigManagerPrivacyTypeConfigTest001, TestSize.Level1)
141 {
142 GTEST_LOG_(INFO)
143 << "LocationConfigManagerTest, LocationConfigManagerCreateFileTest001, TestSize.Level1";
144 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerCreateFileTest001 begin");
145 EXPECT_NE("", LocationConfigManager::GetInstance().GetPrivacyTypeConfigPath(PRIVACY_TYPE_INVALID_LEFT));
146 LBSLOGI(LOCATOR, "[LocationConfigManagerTest] LocationConfigManagerCreateFileTest001 end");
147 }
148 } // namespace Location
149 } // namespace OHOS