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 sinkDev_ 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 "daudio_source_service_test.h"
17 
18 #include "audio_event.h"
19 #include "daudio_errorcode.h"
20 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace DistributedHardware {
SetUpTestCase(void)25 void DAudioSourceServiceTest::SetUpTestCase(void) {}
26 
TearDownTestCase(void)27 void DAudioSourceServiceTest::TearDownTestCase(void) {}
28 
SetUp()29 void DAudioSourceServiceTest::SetUp()
30 {
31     uint32_t saId = 6666;
32     bool runOnCreate = true;
33 
34     sourceSrv_ = std::make_shared<DAudioSourceService>(saId, runOnCreate);
35 }
36 
TearDown()37 void DAudioSourceServiceTest::TearDown()
38 {
39     sourceSrv_ = nullptr;
40 }
41 
42 /**
43  * @tc.name: OnStart_001
44  * @tc.desc: Verify the OnStart function.
45  * @tc.type: FUNC
46  * @tc.require: AR000H0E5F
47  */
48 HWTEST_F(DAudioSourceServiceTest, OnStart_001, TestSize.Level1)
49 {
50     sourceSrv_->isServiceStarted_ = true;
51     sourceSrv_->OnStart();
52     EXPECT_EQ(ERR_DH_AUDIO_SA_LOAD_FAILED, sourceSrv_->ReleaseSource());
53 }
54 
55 /**
56  * @tc.name: InitSource_001
57  * @tc.desc: Verify the InitSource function.
58  * @tc.type: FUNC
59  * @tc.require: AR000H0E5F
60  */
61 HWTEST_F(DAudioSourceServiceTest, InitSource_001, TestSize.Level1)
62 {
63     std::string param = "source";
64     sptr<IDAudioIpcCallback> callback = nullptr;
65     EXPECT_NE(DH_SUCCESS, sourceSrv_->InitSource(param, callback));
66     sourceSrv_->ReleaseSource();
67 }
68 
69 /**
70  * @tc.name: ReleaseSource_001
71  * @tc.desc: Verify the ReleaseSource function.
72  * @tc.type: FUNC
73  * @tc.require: AR000H0E5F
74  */
75 HWTEST_F(DAudioSourceServiceTest, ReleaseSource_001, TestSize.Level1)
76 {
77     EXPECT_EQ(ERR_DH_AUDIO_SA_LOAD_FAILED, sourceSrv_->ReleaseSource());
78 }
79 
80 /**
81  * @tc.name: ConfigDistributedHardware_001
82  * @tc.desc: Verify the ConfigDistributedHardware function.
83  * @tc.type: FUNC
84  * @tc.require: AR000H0E5F
85  */
86 HWTEST_F(DAudioSourceServiceTest, ConfigDistributedHardware_001, TestSize.Level1)
87 {
88     std::string devId = "daef";
89     std::string dhId = "1";
90     std::string key = "key";
91     std::string value = "1";
92 
93     EXPECT_EQ(DH_SUCCESS, sourceSrv_->ConfigDistributedHardware(devId, dhId, key, value));
94     int32_t eventType = 1;
95     std::string content = "content";
96     sourceSrv_->DAudioNotify(devId, dhId, eventType, content);
97     std::string reqId = "reg";
98     EXPECT_NE(DH_SUCCESS, sourceSrv_->UnregisterDistributedHardware(devId, dhId, reqId));
99 }
100 
101 /**
102  * @tc.name: Dump_001
103  * @tc.desc: Verify the Dump function.
104  * @tc.type: FUNC
105  * @tc.require: AR000H0E5F
106  */
107 HWTEST_F(DAudioSourceServiceTest, Dump_001, TestSize.Level1)
108 {
109     int32_t fd = 1;
110     std::vector<std::u16string> args;
111     EXPECT_EQ(DH_SUCCESS, sourceSrv_->Dump(fd, args));
112     std::u16string order = u"--sourceDevId";
113     args.push_back(order);
114     EXPECT_EQ(ERR_DH_AUDIO_BAD_VALUE, sourceSrv_->Dump(fd, args));
115     args.pop_back();
116     order = u"-h";
117     args.push_back(order);
118     EXPECT_EQ(DH_SUCCESS, sourceSrv_->Dump(fd, args));
119     args.pop_back();
120     order = u"--stopDump";
121     args.push_back(order);
122     EXPECT_EQ(DH_SUCCESS, sourceSrv_->Dump(fd, args));
123     args.pop_back();
124     order = u"--illegal";
125     args.push_back(order);
126     EXPECT_EQ(DH_SUCCESS, sourceSrv_->Dump(fd, args));
127 }
128 } // DistributedHardware
129 } // OHOS
130