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 <cstring>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include "iremote_stub.h"
21 #include "location_callback_adapter_impl.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::NWeb {
27 class LocationCallbackAdapterTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase(void)35 void LocationCallbackAdapterTest::SetUpTestCase(void)
36 {}
37 
TearDownTestCase(void)38 void LocationCallbackAdapterTest::TearDownTestCase(void)
39 {}
40 
SetUp(void)41 void LocationCallbackAdapterTest::SetUp(void)
42 {}
43 
TearDown(void)44 void LocationCallbackAdapterTest::TearDown(void)
45 {}
46 
47 class LocationCallbackAdapterMock : public LocationCallbackAdapter {
48 public:
49     LocationCallbackAdapterMock() = default;
50 
OnLocationReport(const std::shared_ptr<LocationInfo> location)51     void OnLocationReport(
52         const std::shared_ptr<LocationInfo> location)  override
53     {}
54 
OnLocatingStatusChange(const int status)55     void OnLocatingStatusChange(const int status) override
56     {}
57 
OnErrorReport(const int errorCode)58     void OnErrorReport(const int errorCode) override
59     {}
60 };
61 
62 /**
63  * @tc.name: LocationCallbackAdapterTest_GetInstance_001
64  * @tc.desc: OnRemoteRequest.
65  * @tc.type: FUNC
66  * @tc.require:
67  */
68 HWTEST_F(LocationCallbackAdapterTest, LocationCallbackAdapterTest_GetInstance_001, TestSize.Level1)
69 {
70     std::shared_ptr<LocationCallbackAdapter> adapter = std::make_shared<LocationCallbackAdapterMock>();
71     EXPECT_NE(adapter, nullptr);
72     auto locationImpl = std::make_shared<LocationCallbackImpl>(adapter);
73     EXPECT_NE(locationImpl, nullptr);
74     OHOS::MessageParcel data;
75     OHOS::MessageParcel reply;
76     OHOS::MessageOption option;
77     std::u16string name = locationImpl->GetDescriptor();
78     data.WriteInterfaceToken(name);
79     uint32_t code = 0;
80     int result = locationImpl->OnRemoteRequest(code, data, reply, option);
81     EXPECT_EQ(result, 0);
82     code += 1;
83     data.WriteInterfaceToken(name);
84     result = locationImpl->OnRemoteRequest(code, data, reply, option);
85     EXPECT_EQ(result, 0);
86     code += 1;
87     data.WriteInterfaceToken(name);
88     result = locationImpl->OnRemoteRequest(code, data, reply, option);
89     EXPECT_EQ(result, 0);
90     code += 1;
91     data.WriteInterfaceToken(name);
92     result = locationImpl->OnRemoteRequest(code, data, reply, option);
93     EXPECT_EQ(result, 0);
94 
95     std::unique_ptr<OHOS::Location::Location> location = std::make_unique<OHOS::Location::Location>();
96     EXPECT_NE(location, nullptr);
97     locationImpl->OnLocationReport(location);
98     locationImpl->OnLocatingStatusChange(0);
99     locationImpl->OnErrorReport(0);
100 }
101 
102 /**
103  * @tc.name: LocationCallbackAdapterTest_GetInstance_002
104  * @tc.desc: OnRemoteRequest.
105  * @tc.type: FUNC
106  * @tc.require:
107  */
108 HWTEST_F(LocationCallbackAdapterTest, LocationCallbackAdapterTest_GetInstance_002, TestSize.Level1)
109 {
110     auto locationImpl = std::make_shared<LocationCallbackImpl>(nullptr);
111     EXPECT_NE(locationImpl, nullptr);
112     OHOS::MessageParcel data;
113     OHOS::MessageParcel reply;
114     OHOS::MessageOption option;
115     uint32_t code = 0;
116     int result = locationImpl->OnRemoteRequest(code, data, reply, option);
117     EXPECT_EQ(result, -1);
118     std::u16string name = locationImpl->GetDescriptor();
119     data.WriteInterfaceToken(name);
120     result = locationImpl->OnRemoteRequest(code, data, reply, option);
121     EXPECT_EQ(result, -1);
122 
123     std::unique_ptr<OHOS::Location::Location> location = std::make_unique<OHOS::Location::Location>();
124     EXPECT_NE(location, nullptr);
125     locationImpl->OnLocationReport(location);
126     locationImpl->OnLocatingStatusChange(0);
127     locationImpl->OnErrorReport(0);
128 }
129 } // namespace OHOS
130