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 #define private public
16 #define protected public
17 
18 #include "cellular_data_error.h"
19 #include "cellular_data_service.h"
20 #include "data_access_token.h"
21 #include "gtest/gtest.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 using namespace testing::ext;
26 const int32_t DEFAULT_SIM_SLOT_ID = 0;
27 
28 class CellularDataServiceTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp();
33     void TearDown();
34     std::shared_ptr<CellularDataService> service = DelayedSingleton<CellularDataService>::GetInstance();
35 };
SetUpTestCase()36 void CellularDataServiceTest::SetUpTestCase() {}
37 
TearDownTestCase()38 void CellularDataServiceTest::TearDownTestCase() {}
39 
SetUp()40 void CellularDataServiceTest::SetUp() {}
41 
TearDown()42 void CellularDataServiceTest::TearDown() {}
43 
44 /**
45  * @tc.number   CellularDataService_001
46  * @tc.name     test function branch
47  * @tc.desc     Function test
48  */
49 HWTEST_F(CellularDataServiceTest, CellularDataService_001, TestSize.Level0)
50 {
51     DataAccessToken token;
52     service->OnStart();
53     service->isInitSuccess_ = true;
54     bool dataEnabled = false;
55     bool dataRoamingEnabled = false;
56     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->IsCellularDataEnabled(dataEnabled));
57     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableCellularData(false));
58     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableCellularData(true));
59     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableIntelligenceSwitch(false));
60     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableIntelligenceSwitch(true));
61     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->GetCellularDataState());
62     ASSERT_EQ(static_cast<int32_t>(DisConnectionReason::REASON_CHANGE_CONNECTION),
63         service->GetApnState(DEFAULT_SIM_SLOT_ID, std::string()));
64     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->IsCellularDataRoamingEnabled(DEFAULT_SIM_SLOT_ID, dataRoamingEnabled));
65     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->EnableCellularDataRoaming(DEFAULT_SIM_SLOT_ID, true));
66     NetRequest request;
67     request.ident = "simId12";
68     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->RequestNet(request));
69     request.ident = "simId2";
70     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->ReleaseNet(request));
71     auto event = AppExecFwk::InnerEvent::Get(0);
72     service->DispatchEvent(DEFAULT_SIM_SLOT_ID, event);
73     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->HandleApnChanged(DEFAULT_SIM_SLOT_ID));
74 }
75 
76 /**
77  * @tc.number   CellularDataService_002
78  * @tc.name     test function branch
79  * @tc.desc     Function test
80  */
81 HWTEST_F(CellularDataServiceTest, CellularDataService_002, TestSize.Level0)
82 {
83     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->GetCellularDataFlowType());
84     ASSERT_EQ("default slotId: -1", service->GetStateMachineCurrentStatusDump());
85     service->GetFlowDataInfoDump();
86     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->StrategySwitch(DEFAULT_SIM_SLOT_ID, false));
87     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->HasInternetCapability(DEFAULT_SIM_SLOT_ID, 0));
88     DisConnectionReason reason = DisConnectionReason::REASON_NORMAL;
89     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->ClearAllConnections(DEFAULT_SIM_SLOT_ID, reason));
90     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->ChangeConnectionForDsds(DEFAULT_SIM_SLOT_ID, false));
91     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->ChangeConnectionForDsds(DEFAULT_SIM_SLOT_ID, true));
92     ApnItem::Attribute apnAttr;
93     ASSERT_NE(TELEPHONY_ERR_SUCCESS, service->GetDataConnApnAttr(DEFAULT_SIM_SLOT_ID, apnAttr));
94     std::string ipType;
95     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->GetDataConnIpType(DEFAULT_SIM_SLOT_ID, ipType));
96     ASSERT_EQ(TELEPHONY_ERR_SUCCESS, service->IsNeedDoRecovery(DEFAULT_SIM_SLOT_ID, true));
97     service->OnStop();
98 }
99 } // namespace Telephony
100 } // namespace OHOS