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 #define private public
19 #define protected public
20 #include "app_launch_data.h"
21 #undef private
22 #undef protected
23
24 using namespace testing;
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 constexpr bool isDebugApp = true;
30 constexpr bool noDebugApp = false;
31 }
32 class AppLaunchDataTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38 sptr<AppLaunchData> launchData_;
39 };
40
SetUpTestCase()41 void AppLaunchDataTest::SetUpTestCase()
42 {}
43
TearDownTestCase()44 void AppLaunchDataTest::TearDownTestCase()
45 {}
46
SetUp()47 void AppLaunchDataTest::SetUp()
48 {
49 launchData_ = new AppLaunchData();
50 }
51
TearDown()52 void AppLaunchDataTest::TearDown()
53 {}
54
55 /**
56 * @tc.name: SetDebugApp_0100
57 * @tc.desc: AppLaunchData SetDebugApp, verify if AppLaunchData startup successfully.
58 * @tc.type: FUNC
59 */
60 HWTEST_F(AppLaunchDataTest, SetDebugApp_0100, TestSize.Level1)
61 {
62 EXPECT_NE(launchData_, nullptr);
63 launchData_->SetDebugApp(isDebugApp);
64 EXPECT_EQ(launchData_->debugApp_, isDebugApp);
65 launchData_->SetDebugApp(noDebugApp);
66 EXPECT_EQ(launchData_->debugApp_, noDebugApp);
67 }
68
69 /**
70 * @tc.name: GetDebugApp_0100
71 * @tc.desc: AppLaunchData GetDebugApp, verify if AppLaunchData startup successfully.
72 * @tc.type: FUNC
73 */
74 HWTEST_F(AppLaunchDataTest, GetDebugApp_0100, TestSize.Level1)
75 {
76 EXPECT_NE(launchData_, nullptr);
77 launchData_->debugApp_ = isDebugApp;
78 EXPECT_EQ(launchData_->GetDebugApp(), isDebugApp);
79 launchData_->debugApp_ = noDebugApp;
80 EXPECT_EQ(launchData_->GetDebugApp(), noDebugApp);
81 }
82
83 /**
84 * @tc.name: Marshalling_0100
85 * @tc.desc: Verify if launchData marshalls Sequenceable object into Parcel normally.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(AppLaunchDataTest, Marshalling_0100, TestSize.Level1)
89 {
90 EXPECT_NE(launchData_, nullptr);
91 Parcel parcel;
92 int32_t recordId = 0;
93 int32_t uId = 0;
94 int32_t appIndex = 0;
95 launchData_->recordId_ = recordId;
96 launchData_->uId_ = uId;
97 launchData_->appIndex_ = appIndex;
98 launchData_->userTestRecord_ = nullptr;
99 launchData_->debugApp_ = false;
100
101 EXPECT_TRUE(launchData_->Marshalling(parcel));
102
103 parcel.ReadParcelable<ApplicationInfo>();
104 parcel.ReadParcelable<Profile>();
105 parcel.ReadParcelable<ProcessInfo>();
106
107 // respectively: recordId, uId, appIndex, valid_, debugApp_
108 EXPECT_EQ(parcel.ReadInt32(), recordId);
109 EXPECT_EQ(parcel.ReadInt32(), uId);
110 EXPECT_EQ(parcel.ReadInt32(), appIndex);
111 EXPECT_FALSE(parcel.ReadBool());
112 EXPECT_FALSE(parcel.ReadBool());
113 }
114
115 /**
116 * @tc.name: ReadFromParcel_0100
117 * @tc.desc: Verify if launchData reads Sequenceable object from Parcel normally.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(AppLaunchDataTest, ReadFromParcel_0100, TestSize.Level1)
121 {
122 EXPECT_NE(launchData_, nullptr);
123 Parcel parcel;
124 sptr<ApplicationInfo> applicationInfo = new ApplicationInfo();
125 sptr<Profile> profile = new Profile();
126 sptr<ProcessInfo> processInfo = new ProcessInfo();
127 int32_t recordId = 0;
128 int32_t uId = 0;
129 int32_t appIndex = 0;
130 bool valid = false;
131 bool isDebug = true;
132
133 parcel.WriteParcelable(applicationInfo);
134 parcel.WriteParcelable(profile);
135 parcel.WriteParcelable(processInfo);
136 parcel.WriteInt32(recordId);
137 parcel.WriteInt32(uId);
138 parcel.WriteInt32(appIndex);
139 parcel.WriteBool(valid);
140 parcel.WriteBool(isDebug);
141
142 EXPECT_TRUE(launchData_->ReadFromParcel(parcel));
143 EXPECT_EQ(launchData_->recordId_, recordId);
144 EXPECT_EQ(launchData_->uId_, uId);
145 EXPECT_EQ(launchData_->appIndex_, appIndex);
146 EXPECT_EQ(launchData_->debugApp_, isDebug);
147 }
148 } // namespace AppExecFwk
149 } // namespace OHOS
150