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 #define private public
17 #define protected public
18
19 #include <gtest/gtest.h>
20
21 #include "mock/mock_sim_manager.h"
22 #include "core_manager_inner.h"
23 #include "cellular_data_handler.h"
24 #include "cellular_data_incall_observer.h"
25 #include "cellular_data_setting_observer.h"
26 #include "cellular_data_rdb_observer.h"
27 #include "cellular_data_roaming_observer.h"
28 #include "cellular_data_settings_rdb_helper.h"
29
30 namespace OHOS {
31 namespace Telephony {
32 using namespace testing::ext;
33 using ::testing::_;
34 using ::testing::Mock;
35 using ::testing::Return;
36
37 class CellularDataObserverTest : public testing::Test {
38 public:
39 CellularDataObserverTest() = default;
40 ~CellularDataObserverTest() = default;
41
TearDownTestCase()42 static void TearDownTestCase()
43 {
44 if (CoreManagerInner::GetInstance().simManager_ != nullptr) {
45 CoreManagerInner::GetInstance().simManager_ = nullptr;
46 std::cout << "CellularDataObserverTest set simManager_ nullptr" << std::endl;
47 }
48 }
49
createCellularDataHandler()50 static std::shared_ptr<CellularDataHandler> createCellularDataHandler()
51 {
52 EventFwk::CommonEventSubscribeInfo subscriberInfo;
53 subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
54 std::shared_ptr<CellularDataHandler> cellularDataHandler =
55 std::make_shared<CellularDataHandler>(subscriberInfo, 0);
56 return cellularDataHandler;
57 }
58 };
59
HWTEST_F(CellularDataObserverTest, CellularDataIncallObserver_01, Function | MediumTest | Level1)60 HWTEST_F(CellularDataObserverTest, CellularDataIncallObserver_01, Function | MediumTest | Level1)
61 {
62 std::shared_ptr<CellularDataHandler> cellularDataHandler = nullptr;
63 auto cellularDataIncallObserver = std::make_shared<CellularDataIncallObserver>(cellularDataHandler);
64 cellularDataIncallObserver->OnChange();
65 ASSERT_TRUE(cellularDataHandler == nullptr);
66
67 // GetValue success, cellularDataHandler is nullptr
68 std::shared_ptr<CellularDataSettingsRdbHelper> settingHelper = CellularDataSettingsRdbHelper::GetInstance();
69 Uri uri(CELLULAR_DATA_SETTING_DATA_INCALL_URI);
70 ASSERT_TRUE(settingHelper->PutValue(uri, CELLULAR_DATA_COLUMN_INCALL, 1) == TELEPHONY_ERR_SUCCESS);
71 cellularDataIncallObserver->OnChange();
72 ASSERT_TRUE(cellularDataHandler == nullptr);
73
74 // GetValue success, cellularDataHandler is not nullptr
75 cellularDataHandler = CellularDataObserverTest::createCellularDataHandler();
76 cellularDataIncallObserver = std::make_shared<CellularDataIncallObserver>(cellularDataHandler);
77 cellularDataIncallObserver->OnChange();
78 ASSERT_FALSE(cellularDataHandler == nullptr);
79 }
80
HWTEST_F(CellularDataObserverTest, CellularDataRdbObserver_02, Function | MediumTest | Level1)81 HWTEST_F(CellularDataObserverTest, CellularDataRdbObserver_02, Function | MediumTest | Level1)
82 {
83 std::shared_ptr<CellularDataHandler> cellularDataHandler = nullptr;
84 auto cellularDataRdbObserver = std::make_shared<CellularDataRdbObserver>(cellularDataHandler);
85 cellularDataRdbObserver->OnChange();
86 ASSERT_TRUE(cellularDataHandler == nullptr);
87
88 // cellularDataHandler is not nullptr
89 cellularDataHandler = CellularDataObserverTest::createCellularDataHandler();
90 cellularDataRdbObserver = std::make_shared<CellularDataRdbObserver>(cellularDataHandler);
91 cellularDataRdbObserver->OnChange();
92 ASSERT_FALSE(cellularDataHandler == nullptr);
93 }
94
HWTEST_F(CellularDataObserverTest, CellularDataRoamingObserver_03, Function | MediumTest | Level1)95 HWTEST_F(CellularDataObserverTest, CellularDataRoamingObserver_03, Function | MediumTest | Level1)
96 {
97 // mock sim manager
98 ASSERT_TRUE(CoreManagerInner::GetInstance().simManager_ == nullptr);
99 MockSimManager *mockSimManager = new MockSimManager();
100 std::shared_ptr<MockSimManager> mockSimManagerPtr(mockSimManager);
101 CoreManagerInner::GetInstance().OnInit(nullptr, mockSimManagerPtr, nullptr);
102 ASSERT_FALSE(CoreManagerInner::GetInstance().simManager_ == nullptr);
103 ASSERT_EQ(CoreManagerInner::GetInstance().simManager_, mockSimManagerPtr);
104
105 // get sim id failed
106 EXPECT_CALL(*mockSimManager, GetSimId(_)).WillOnce(Return(-1));
107 std::shared_ptr<CellularDataHandler> cellularDataHandler = nullptr;
108 auto cellularDataRoamingObserver = std::make_shared<CellularDataRoamingObserver>(cellularDataHandler, 0);
109 cellularDataRoamingObserver->OnChange();
110 ASSERT_TRUE(cellularDataHandler == nullptr);
111
112 // get sim id success, GetValue failed
113 EXPECT_CALL(*mockSimManager, GetSimId(_)).WillOnce(Return(1));
114 cellularDataRoamingObserver->OnChange();
115 ASSERT_TRUE(cellularDataHandler == nullptr);
116
117 // get sim id success, GetValue success, cellularDataHandler is nullptr
118 EXPECT_CALL(*mockSimManager, GetSimId(_)).WillOnce(Return(1));
119 std::shared_ptr<CellularDataSettingsRdbHelper> settingHelper = CellularDataSettingsRdbHelper::GetInstance();
120 Uri uri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(1));
121 std::string col = std::string(CELLULAR_DATA_COLUMN_ROAMING) + std::to_string(1);
122 ASSERT_TRUE(settingHelper->PutValue(uri, col, 1) == TELEPHONY_ERR_SUCCESS);
123 cellularDataRoamingObserver->OnChange();
124 ASSERT_TRUE(cellularDataHandler == nullptr);
125
126 // get sim id success, GetValue success, cellularDataHandler is nullptr
127 EXPECT_CALL(*mockSimManager, GetSimId(_)).WillOnce(Return(1));
128 cellularDataHandler = CellularDataObserverTest::createCellularDataHandler();
129 cellularDataRoamingObserver = std::make_shared<CellularDataRoamingObserver>(cellularDataHandler, 0);
130 cellularDataRoamingObserver->OnChange();
131 ASSERT_FALSE(cellularDataHandler == nullptr);
132
133 Mock::VerifyAndClearExpectations(mockSimManager);
134 }
135
HWTEST_F(CellularDataObserverTest, CellularDataSettingObserver_04, Function | MediumTest | Level1)136 HWTEST_F(CellularDataObserverTest, CellularDataSettingObserver_04, Function | MediumTest | Level1)
137 {
138 std::shared_ptr<CellularDataHandler> cellularDataHandler = nullptr;
139 auto cellularDataSettingObserver = std::make_shared<CellularDataSettingObserver>(cellularDataHandler);
140 cellularDataSettingObserver->OnChange();
141 ASSERT_TRUE(cellularDataHandler == nullptr);
142
143 // GetValue success, cellularDataHandler is nullptr
144 std::shared_ptr<CellularDataSettingsRdbHelper> settingHelper = CellularDataSettingsRdbHelper::GetInstance();
145 Uri uri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
146 ASSERT_TRUE(settingHelper->PutValue(uri, CELLULAR_DATA_COLUMN_ENABLE, 1) == TELEPHONY_ERR_SUCCESS);
147 cellularDataSettingObserver->OnChange();
148 ASSERT_TRUE(cellularDataHandler == nullptr);
149
150 // GetValue success, cellularDataHandler is not nullptr
151 cellularDataHandler = CellularDataObserverTest::createCellularDataHandler();
152 cellularDataSettingObserver = std::make_shared<CellularDataSettingObserver>(cellularDataHandler);
153 cellularDataSettingObserver->OnChange();
154 ASSERT_FALSE(cellularDataHandler == nullptr);
155 }
156
157 } // namespace Telephony
158 } // namespace OHOS