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 "cellular_data_dump_helper.h"
22 #include "core_service_client.h"
23 #include "mock/mock_core_service.h"
24 #include "telephony_types.h"
25 
26 namespace OHOS {
27 namespace Telephony {
28 using namespace testing::ext;
29 using ::testing::_;
30 using ::testing::DoAll;
31 using ::testing::Return;
32 using ::testing::AtLeast;
33 using ::testing::SetArgReferee;
34 
35 class CellularDataDumpHelperTest : public testing::Test {
36 public:
CellularDataDumpHelperTest()37     CellularDataDumpHelperTest()
38     {
39         mockCoreService = new MockCoreService();
40         sptr<ICoreService> mockCoreServicePtr(mockCoreService);
41         DelayedRefSingleton<CoreServiceClient>::GetInstance().proxy_ = mockCoreServicePtr;
42     }
43     ~CellularDataDumpHelperTest() = default;
TearDownTestCase()44     static void TearDownTestCase()
45     {
46         if (DelayedRefSingleton<CoreServiceClient>::GetInstance().proxy_ != nullptr) {
47             DelayedRefSingleton<CoreServiceClient>::GetInstance().proxy_ = nullptr;
48             std::cout << "CellularDataDumpHelperTest set proxy_ nullptr" << std::endl;
49         }
50     }
51     MockCoreService *mockCoreService;
52 };
53 
HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_01, Function | MediumTest | Level1)54 HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_01, Function | MediumTest | Level1)
55 {
56     CellularDataDumpHelper help;
57     std::vector<std::string> args = {"cellular_data", "--help"};
58     std::string result = "";
59     help.Dump(args, result);
60     std::cout << "CellularDataDumpHelper_01 result: " << result << std::endl;
61     ASSERT_FALSE(result.find("CellularData") == std::string::npos);
62     ASSERT_FALSE(result.find("Usage:dump <command> [options]") == std::string::npos);
63 }
64 
HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_02, Function | MediumTest | Level1)65 HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_02, Function | MediumTest | Level1)
66 {
67     CellularDataDumpHelper help;
68     std::vector<std::string> args = {"cellular_data", "help"};
69     std::string result = "";
70     EXPECT_CALL(*mockCoreService, HasSimCard(_, _))
71         .Times(AtLeast(0))
72         .WillRepeatedly(DoAll(SetArgReferee<1>(false), Return(0)));
73     help.Dump(args, result);
74     std::cout << "CellularDataDumpHelper_02 result: " << result << std::endl;
75     ASSERT_FALSE(result.find("Ohos cellular data service") == std::string::npos);
76     ASSERT_TRUE(result.find("CellularDataRoamingEnabled") == std::string::npos);
77 }
78 
HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_03, Function | MediumTest | Level1)79 HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_03, Function | MediumTest | Level1)
80 {
81     CellularDataDumpHelper help;
82     std::vector<std::string> args = {"cellular_data_1", "--help"};
83     std::string result = "";
84     EXPECT_CALL(*mockCoreService, HasSimCard(_, _))
85         .Times(AtLeast(0))
86         .WillRepeatedly(DoAll(SetArgReferee<1>(false), Return(0)));
87     help.Dump(args, result);
88     std::cout << "CellularDataDumpHelper_03 result: " << result << std::endl;
89     ASSERT_FALSE(result.find("Ohos cellular data service") == std::string::npos);
90     ASSERT_TRUE(result.find("CellularDataRoamingEnabled") == std::string::npos);
91 }
92 
HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_04, Function | MediumTest | Level1)93 HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_04, Function | MediumTest | Level1)
94 {
95     maxSlotCount_ = 2;
96     EXPECT_CALL(*mockCoreService, HasSimCard(0, _)).WillOnce(DoAll(SetArgReferee<1>(true), Return(0)));
97     EXPECT_CALL(*mockCoreService, HasSimCard(1, _)).WillOnce(DoAll(SetArgReferee<1>(false), Return(0)));
98     CellularDataDumpHelper help;
99     std::vector<std::string> args = {"cellular_data_1", "help"};
100     std::string result = "";
101     help.Dump(args, result);
102     std::cout << "CellularDataDumpHelper_04 result: " << result << std::endl;
103     ASSERT_FALSE(result.find("Ohos cellular data service") == std::string::npos);
104     ASSERT_FALSE(result.find("CellularDataRoamingEnabled") == std::string::npos);
105     maxSlotCount_ = 0;
106 }
107 
108 }  // namespace Telephony
109 }  // namespace OHOS