• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "raw_parcel_callback_stub.h"
26 
27 namespace OHOS {
28 namespace Telephony {
29 using namespace testing::ext;
30 using ::testing::_;
31 using ::testing::DoAll;
32 using ::testing::Return;
33 using ::testing::AtLeast;
34 using ::testing::SetArgReferee;
35 
36 class CellularDataDumpHelperTest : public testing::Test {
37 public:
CellularDataDumpHelperTest()38     CellularDataDumpHelperTest()
39     {
40     }
41     ~CellularDataDumpHelperTest() = default;
TearDownTestCase()42     static void TearDownTestCase()
43     {
44     }
SetUp()45     void SetUp()
46     {
47         mockCoreService = sptr<MockCoreService>::MakeSptr();
48         DelayedRefSingleton<CoreServiceClient>::GetInstance().proxy_ = mockCoreService;
49     }
TearDown()50     void TearDown()
51     {
52         DelayedRefSingleton<CoreServiceClient>::GetInstance().proxy_ = nullptr;
53     }
54     sptr<MockCoreService> mockCoreService;
55 };
56 
57 HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_01, Function | MediumTest | Level1)
58 {
59     CellularDataDumpHelper help;
60     std::vector<std::string> args = {"cellular_data", "--help"};
61     std::string result = "";
62     help.Dump(args, result);
63     std::cout << "CellularDataDumpHelper_01 result: " << result << std::endl;
64     ASSERT_FALSE(result.find("CellularData") == std::string::npos);
65     ASSERT_FALSE(result.find("Usage:dump <command> [options]") == std::string::npos);
66 }
67 
68 HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_02, Function | MediumTest | Level1)
69 {
70     CellularDataDumpHelper help;
71     std::vector<std::string> args = {"cellular_data", "help"};
72     std::string result = "";
73     EXPECT_CALL(*mockCoreService, HasSimCard(_, _))
__anon01d8e5d80102(int32_t slotId, const sptr<IRawParcelCallback> &callback) 74         .WillRepeatedly(testing::Invoke([](int32_t slotId, const sptr<IRawParcelCallback> &callback) {
75             sptr<RawParcelCallbackStub> cb = static_cast<RawParcelCallbackStub*>(callback.GetRefPtr());
76             MessageParcel data;
77             MessageParcel reply;
78             MessageOption option;
79             data.WriteInterfaceToken(u"OHOS.Telephony.IRawParcelCallback");
80             data.WriteInt32(0);
81             data.WriteBool(false); // hasSimCard = false
82             cb->OnRemoteRequest(0, data, reply, option);
83             return 0;
84         }));
85 
86     help.Dump(args, result);
87     std::cout << "CellularDataDumpHelper_02 result: " << result << std::endl;
88     ASSERT_FALSE(result.find("Ohos cellular data service") == std::string::npos);
89     ASSERT_TRUE(result.find("CellularDataRoamingEnabled") == std::string::npos);
90 }
91 
92 HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_03, Function | MediumTest | Level1)
93 {
94     CellularDataDumpHelper help;
95     std::vector<std::string> args = {"cellular_data_1", "--help"};
96     std::string result = "";
97     EXPECT_CALL(*mockCoreService, HasSimCard(_, _))
__anon01d8e5d80202(int32_t slotId, const sptr<IRawParcelCallback> &callback) 98         .WillRepeatedly(testing::Invoke([](int32_t slotId, const sptr<IRawParcelCallback> &callback) {
99             sptr<RawParcelCallbackStub> cb = static_cast<RawParcelCallbackStub*>(callback.GetRefPtr());
100             MessageParcel data;
101             MessageParcel reply;
102             MessageOption option;
103             data.WriteInterfaceToken(u"OHOS.Telephony.IRawParcelCallback");
104             data.WriteInt32(0);
105             data.WriteBool(false); // hasSimCard = false
106             cb->OnRemoteRequest(0, data, reply, option);
107             return 0;
108         }));
109     help.Dump(args, result);
110     std::cout << "CellularDataDumpHelper_03 result: " << result << std::endl;
111     ASSERT_FALSE(result.find("Ohos cellular data service") == std::string::npos);
112     ASSERT_TRUE(result.find("CellularDataRoamingEnabled") == std::string::npos);
113 }
114 
115 HWTEST_F(CellularDataDumpHelperTest, CellularDataDumpHelper_04, Function | MediumTest | Level1)
116 {
117     maxSlotCount_ = 2;
118     EXPECT_CALL(*mockCoreService, HasSimCard(_, _))
__anon01d8e5d80302(int32_t slotId, const sptr<IRawParcelCallback> &callback) 119         .WillOnce(testing::Invoke([](int32_t slotId, const sptr<IRawParcelCallback> &callback) {
120             sptr<RawParcelCallbackStub> cb = static_cast<RawParcelCallbackStub*>(callback.GetRefPtr());
121             MessageParcel data;
122             MessageParcel reply;
123             MessageOption option;
124             data.WriteInterfaceToken(u"OHOS.Telephony.IRawParcelCallback");
125             data.WriteInt32(0);
126             data.WriteBool(true); // hasSimCard = false
127             cb->OnRemoteRequest(0, data, reply, option);
128             return 0;
129         }))
130 
__anon01d8e5d80402(int32_t slotId, const sptr<IRawParcelCallback> &callback) 131         .WillOnce(testing::Invoke([](int32_t slotId, const sptr<IRawParcelCallback> &callback) {
132             sptr<RawParcelCallbackStub> cb = static_cast<RawParcelCallbackStub*>(callback.GetRefPtr());
133             MessageParcel data;
134             MessageParcel reply;
135             MessageOption option;
136             data.WriteInterfaceToken(u"OHOS.Telephony.IRawParcelCallback");
137             data.WriteInt32(0);
138             data.WriteBool(false); // hasSimCard = false
139             cb->OnRemoteRequest(0, data, reply, option);
140             return 0;
141         }));
142     CellularDataDumpHelper help;
143     std::vector<std::string> args = {"cellular_data_1", "help"};
144     std::string result = "";
145     help.Dump(args, result);
146     std::cout << "CellularDataDumpHelper_04 result: " << result << std::endl;
147     ASSERT_FALSE(result.find("Ohos cellular data service") == std::string::npos);
148     ASSERT_FALSE(result.find("CellularDataRoamingEnabled") == std::string::npos);
149     maxSlotCount_ = 0;
150 }
151 
152 }  // namespace Telephony
153 }  // namespace OHOS