1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 
18 #include "app_debug_info.h"
19 #include "parcel.h"
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26     const std::string STRING_BUNDLE_NAME = "bundleName";
27     constexpr int INT_PID = 10;
28     constexpr int INT_UID = 12345;
29 }
30 class AppDebugInfoTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp() override;
35     void TearDown() override;
36 
37     sptr<AppDebugInfo> appDebugInfo_;
38 };
39 
SetUpTestCase(void)40 void AppDebugInfoTest::SetUpTestCase(void)
41 {}
42 
TearDownTestCase(void)43 void AppDebugInfoTest::TearDownTestCase(void)
44 {}
45 
SetUp()46 void AppDebugInfoTest::SetUp()
47 {
48     appDebugInfo_ = new AppDebugInfo();
49 }
50 
TearDown()51 void AppDebugInfoTest::TearDown()
52 {}
53 
54 /**
55  * @tc.name: ReadFromParcel_0100
56  * @tc.desc: Verify read data from parcel normally.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(AppDebugInfoTest, ReadFromParcel_0100, TestSize.Level1)
60 {
61     EXPECT_NE(appDebugInfo_, nullptr);
62     Parcel data;
63     data.WriteString(STRING_BUNDLE_NAME);
64     data.WriteInt32(INT_PID);
65     data.WriteInt32(INT_UID);
66 
67     EXPECT_TRUE(appDebugInfo_->ReadFromParcel(data));
68     EXPECT_EQ(appDebugInfo_->bundleName, STRING_BUNDLE_NAME);
69 }
70 
71 /**
72  * @tc.name: UnMarshalling_0100
73  * @tc.desc: Verify unmarshall parcel normally.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(AppDebugInfoTest, UnMarshalling_0100, TestSize.Level1)
77 {
78     EXPECT_NE(appDebugInfo_, nullptr);
79     Parcel data;
80     data.WriteString(STRING_BUNDLE_NAME);
81     data.WriteInt32(INT_PID);
82     data.WriteInt32(INT_UID);
83 
84     auto info = appDebugInfo_->Unmarshalling(data);
85     EXPECT_EQ(info->bundleName, STRING_BUNDLE_NAME);
86     EXPECT_EQ(info->pid, INT_PID);
87     EXPECT_EQ(info->uid, INT_UID);
88 }
89 
90 /**
91  * @tc.name: Marshalling_0100
92  * @tc.desc: Verify marshall into parcel normally.
93  * @tc.type: FUNC
94  */
95 HWTEST_F(AppDebugInfoTest, Marshalling_0100, TestSize.Level1)
96 {
97     EXPECT_NE(appDebugInfo_, nullptr);
98     Parcel data;
99     appDebugInfo_->bundleName = STRING_BUNDLE_NAME;
100     appDebugInfo_->uid = INT_UID;
101     appDebugInfo_->pid = INT_PID;
102 
103     EXPECT_TRUE(appDebugInfo_->Marshalling(data));
104     EXPECT_EQ(data.ReadString(), STRING_BUNDLE_NAME);
105     EXPECT_EQ(data.ReadInt32(), INT_PID);
106     EXPECT_EQ(data.ReadInt32(), INT_UID);
107 }
108 } // namespace AppExecFwk
109 } // namespace OHOS
110