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 /*
17 * Copyright (c) 2022 Huawei Device Co., Ltd.
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */
30
31 #include <gtest/gtest.h>
32 #include "hilog_wrapper.h"
33 #include "iremote_proxy.h"
34 #include "test_observer_proxy.h"
35
36 using namespace testing::ext;
37 using namespace OHOS;
38 using namespace OHOS::AAFwk;
39
40 namespace {
41 const std::string CMD = "ls -l";
42 } // namespace
43
44 class TestObserverProxyTest : public ::testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp() override;
49 void TearDown() override;
50 };
51
SetUpTestCase()52 void TestObserverProxyTest::SetUpTestCase()
53 {}
54
TearDownTestCase()55 void TestObserverProxyTest::TearDownTestCase()
56 {}
57
SetUp()58 void TestObserverProxyTest::SetUp()
59 {}
60
TearDown()61 void TestObserverProxyTest::TearDown()
62 {}
63
64 namespace OHOS {
65 class MockIRemoteObject : public IRemoteObject {
66 public:
MockIRemoteObject()67 MockIRemoteObject() : IRemoteObject(u"mock_i_remote_object") {}
68
~MockIRemoteObject()69 ~MockIRemoteObject() {}
70
GetObjectRefCount()71 int32_t GetObjectRefCount() override
72 {
73 return 0;
74 }
75
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)76 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
77 {
78 return 0;
79 }
80
IsProxyObject() const81 bool IsProxyObject() const override
82 {
83 return true;
84 }
85
CheckObjectLegality() const86 bool CheckObjectLegality() const override
87 {
88 return true;
89 }
90
AddDeathRecipient(const sptr<DeathRecipient> & recipient)91 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override
92 {
93 return true;
94 }
95
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)96 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override
97 {
98 return true;
99 }
100
Marshalling(Parcel & parcel) const101 bool Marshalling(Parcel &parcel) const override
102 {
103 return true;
104 }
105
AsInterface()106 sptr<IRemoteBroker> AsInterface() override
107 {
108 return nullptr;
109 }
110
Dump(int fd,const std::vector<std::u16string> & args)111 int Dump(int fd, const std::vector<std::u16string> &args) override
112 {
113 return 0;
114 }
115
GetObjectDescriptor() const116 std::u16string GetObjectDescriptor() const
117 {
118 std::u16string descriptor = std::u16string();
119 return descriptor;
120 }
121 };
122 }
123
124 /**
125 * @tc.number: Test_Observer_Proxy_Test_0100
126 * @tc.name: Test ExecuteShellCommand
127 * @tc.desc: Verify the ExecuteShellCommand fail because failed to read result.
128 */
129 HWTEST_F(TestObserverProxyTest, Test_Observer_Proxy_Test_0100, TestSize.Level1)
130 {
131 HILOG_INFO("Test_Observer_Proxy_Test_0100 start");
132 OHOS::sptr<OHOS::IRemoteObject> object = new OHOS::MockIRemoteObject();
133 TestObserverProxy testObserverProxy(object);
134 EXPECT_EQ(testObserverProxy.ExecuteShellCommand(CMD.c_str(), 0).stdResult.size(), 0);
135 HILOG_INFO("Test_Observer_Proxy_Test_0100 end");
136 }
137
138 /**
139 * @tc.number: Test_Observer_Proxy_Test_0200
140 * @tc.name: Test TestStatus
141 * @tc.desc: Verify the TestStatus process not crush.
142 */
143 HWTEST_F(TestObserverProxyTest, Test_Observer_Proxy_Test_0200, TestSize.Level1)
144 {
145 HILOG_INFO("Test_Observer_Proxy_Test_0200 start");
146 OHOS::sptr<OHOS::IRemoteObject> object = new OHOS::MockIRemoteObject();
147 ASSERT_NE(object, nullptr);
148 TestObserverProxy testObserverProxy(object);
149 testObserverProxy.TestStatus(CMD.c_str(), 0);
150 HILOG_INFO("Test_Observer_Proxy_Test_0200 end");
151 }
152
153 /**
154 * @tc.number: Test_Observer_Proxy_Test_0300
155 * @tc.name: Test TestFinished
156 * @tc.desc: Verify the TestFinished process not crush.
157 */
158 HWTEST_F(TestObserverProxyTest, Test_Observer_Proxy_Test_0300, TestSize.Level1)
159 {
160 HILOG_INFO("Test_Observer_Proxy_Test_0300 start");
161 OHOS::sptr<OHOS::IRemoteObject> object = new OHOS::MockIRemoteObject();
162 ASSERT_NE(object, nullptr);
163 TestObserverProxy testObserverProxy(object);
164 testObserverProxy.TestFinished(CMD.c_str(), 0);
165 HILOG_INFO("Test_Observer_Proxy_Test_0300 end");
166 }