180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480922886Sopenharmony_ci * you may not use this file except in compliance with the License.
580922886Sopenharmony_ci * You may obtain a copy of the License at
680922886Sopenharmony_ci *
780922886Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880922886Sopenharmony_ci *
980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280922886Sopenharmony_ci * See the License for the specific language governing permissions and
1380922886Sopenharmony_ci * limitations under the License.
1480922886Sopenharmony_ci */
1580922886Sopenharmony_ci
1680922886Sopenharmony_ci#include <gtest/gtest.h>
1780922886Sopenharmony_ci#include "av_router.h"
1880922886Sopenharmony_ci#include "avsession_errors.h"
1980922886Sopenharmony_ci#include "avsession_log.h"
2080922886Sopenharmony_ci#include "avsession_service.h"
2180922886Sopenharmony_ci
2280922886Sopenharmony_ciusing namespace testing::ext;
2380922886Sopenharmony_cinamespace OHOS {
2480922886Sopenharmony_cinamespace AVSession {
2580922886Sopenharmony_ci
2680922886Sopenharmony_cistatic std::shared_ptr<AVSessionService> g_AVSessionService;
2780922886Sopenharmony_ci
2880922886Sopenharmony_ciclass AVRouterTest : public testing::Test {
2980922886Sopenharmony_cipublic:
3080922886Sopenharmony_ci    static void SetUpTestCase();
3180922886Sopenharmony_ci    static void TearDownTestCase();
3280922886Sopenharmony_ci    void SetUp() override;
3380922886Sopenharmony_ci    void TearDown() override;
3480922886Sopenharmony_ci};
3580922886Sopenharmony_ci
3680922886Sopenharmony_civoid AVRouterTest::SetUpTestCase()
3780922886Sopenharmony_ci{
3880922886Sopenharmony_ci    SLOGI("set up AVSessionServiceTest");
3980922886Sopenharmony_ci    system("killall -9 com.example.hiMusicDemo");
4080922886Sopenharmony_ci    g_AVSessionService = std::make_shared<AVSessionService>(OHOS::AVSESSION_SERVICE_ID);
4180922886Sopenharmony_ci}
4280922886Sopenharmony_ci
4380922886Sopenharmony_civoid AVRouterTest::TearDownTestCase()
4480922886Sopenharmony_ci{}
4580922886Sopenharmony_ci
4680922886Sopenharmony_civoid AVRouterTest::SetUp()
4780922886Sopenharmony_ci{}
4880922886Sopenharmony_ci
4980922886Sopenharmony_civoid AVRouterTest::TearDown()
5080922886Sopenharmony_ci{}
5180922886Sopenharmony_ci
5280922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
5380922886Sopenharmony_ci
5480922886Sopenharmony_ci/**
5580922886Sopenharmony_ci* @tc.name: StartCastDiscovery001
5680922886Sopenharmony_ci* @tc.desc: start cast discovery for default cast type "local"
5780922886Sopenharmony_ci* @tc.type: FUNC
5880922886Sopenharmony_ci* @tc.require: NA
5980922886Sopenharmony_ci*/
6080922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, StartCastDiscovery001, TestSize.Level1)
6180922886Sopenharmony_ci{
6280922886Sopenharmony_ci    SLOGI("StartCastDiscovery001 begin");
6380922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().StartCastDiscovery(ProtocolType::TYPE_LOCAL, {});
6480922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
6580922886Sopenharmony_ci    SLOGI("StartCastDiscovery001 end");
6680922886Sopenharmony_ci}
6780922886Sopenharmony_ci
6880922886Sopenharmony_ci/**
6980922886Sopenharmony_ci* @tc.name: StartCastDiscovery002
7080922886Sopenharmony_ci* @tc.desc: start cast discovery for invalid cast type
7180922886Sopenharmony_ci* @tc.type: FUNC
7280922886Sopenharmony_ci* @tc.require: NA
7380922886Sopenharmony_ci*/
7480922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, StartCastDiscovery002, TestSize.Level1)
7580922886Sopenharmony_ci{
7680922886Sopenharmony_ci    SLOGI("StartCastDiscovery001 begin");
7780922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().StartCastDiscovery(-1, {});
7880922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
7980922886Sopenharmony_ci    SLOGI("StartCastDiscovery001 end");
8080922886Sopenharmony_ci}
8180922886Sopenharmony_ci
8280922886Sopenharmony_ci/**
8380922886Sopenharmony_ci* @tc.name: StopCastDiscovery001
8480922886Sopenharmony_ci* @tc.desc: stop cast discovery success
8580922886Sopenharmony_ci* @tc.type: FUNC
8680922886Sopenharmony_ci* @tc.require: NA
8780922886Sopenharmony_ci*/
8880922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, StopCastDiscovery001, TestSize.Level1)
8980922886Sopenharmony_ci{
9080922886Sopenharmony_ci    SLOGI("StopCastDiscovery001 begin");
9180922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().StartCastDiscovery(ProtocolType::TYPE_LOCAL, {});
9280922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
9380922886Sopenharmony_ci    ret = AVRouter::GetInstance().StopCastDiscovery();
9480922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
9580922886Sopenharmony_ci    SLOGI("StopCastDiscovery001 end");
9680922886Sopenharmony_ci}
9780922886Sopenharmony_ci
9880922886Sopenharmony_ci/**
9980922886Sopenharmony_ci* @tc.name: SetDiscoverable001
10080922886Sopenharmony_ci* @tc.desc: setDiscoverable true
10180922886Sopenharmony_ci* @tc.type: FUNC
10280922886Sopenharmony_ci* @tc.require: NA
10380922886Sopenharmony_ci*/
10480922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, SetDiscoverable001, TestSize.Level1)
10580922886Sopenharmony_ci{
10680922886Sopenharmony_ci    SLOGI("SetDiscoverable001 begin");
10780922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().SetDiscoverable(true);
10880922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
10980922886Sopenharmony_ci    SLOGI("SetDiscoverable001 end");
11080922886Sopenharmony_ci}
11180922886Sopenharmony_ci
11280922886Sopenharmony_ci/**
11380922886Sopenharmony_ci* @tc.name: OnDeviceAvailable001
11480922886Sopenharmony_ci* @tc.desc: StartCast success
11580922886Sopenharmony_ci* @tc.type: FUNC
11680922886Sopenharmony_ci* @tc.require: NA
11780922886Sopenharmony_ci*/
11880922886Sopenharmony_ciHWTEST_F(AVRouterTest, OnDeviceAvailable001, TestSize.Level1)
11980922886Sopenharmony_ci{
12080922886Sopenharmony_ci    SLOGI("OnDeviceAvailable001 begin");
12180922886Sopenharmony_ci    OutputDeviceInfo outputDeviceInfo;
12280922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().OnDeviceAvailable(outputDeviceInfo);
12380922886Sopenharmony_ci    EXPECT_NE(ret, AVSESSION_SUCCESS);
12480922886Sopenharmony_ci    SLOGI("OnDeviceAvailable001 end");
12580922886Sopenharmony_ci}
12680922886Sopenharmony_ci
12780922886Sopenharmony_ci/**
12880922886Sopenharmony_ci* @tc.name: OnCastServerDied001
12980922886Sopenharmony_ci* @tc.desc: setDiscoverable true
13080922886Sopenharmony_ci* @tc.type: FUNC
13180922886Sopenharmony_ci* @tc.require: NA
13280922886Sopenharmony_ci*/
13380922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, OnCastServerDied001, TestSize.Level1)
13480922886Sopenharmony_ci{
13580922886Sopenharmony_ci    SLOGI("OnCastServerDied001 begin");
13680922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().OnCastServerDied(-1);
13780922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_ERROR);
13880922886Sopenharmony_ci    SLOGI("OnCastServerDied001 end");
13980922886Sopenharmony_ci}
14080922886Sopenharmony_ci
14180922886Sopenharmony_ci/**
14280922886Sopenharmony_ci* @tc.name: StartDeviceLogging001
14380922886Sopenharmony_ci* @tc.desc: test StartDeviceLogging
14480922886Sopenharmony_ci* @tc.type: FUNC
14580922886Sopenharmony_ci* @tc.require: NA
14680922886Sopenharmony_ci*/
14780922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, StartDeviceLogging001, TestSize.Level1)
14880922886Sopenharmony_ci{
14980922886Sopenharmony_ci    SLOGI("StartDeviceLogging001 begin");
15080922886Sopenharmony_ci    int32_t fd = 1;
15180922886Sopenharmony_ci    uint32_t maxSize = 10;
15280922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().StartDeviceLogging(fd, maxSize);
15380922886Sopenharmony_ci    AVRouter::GetInstance().StopDeviceLogging();
15480922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
15580922886Sopenharmony_ci    SLOGI("StartDeviceLogging001 end");
15680922886Sopenharmony_ci}
15780922886Sopenharmony_ci
15880922886Sopenharmony_ci/**
15980922886Sopenharmony_ci* @tc.name: StartDeviceLogging002
16080922886Sopenharmony_ci* @tc.desc: test StartDeviceLogging
16180922886Sopenharmony_ci* @tc.type: FUNC
16280922886Sopenharmony_ci* @tc.require: NA
16380922886Sopenharmony_ci*/
16480922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, StartDeviceLogging002, TestSize.Level1)
16580922886Sopenharmony_ci{
16680922886Sopenharmony_ci    SLOGI("StartDeviceLogging002 begin");
16780922886Sopenharmony_ci    int32_t fd = 1;
16880922886Sopenharmony_ci    uint32_t maxSize = 10;
16980922886Sopenharmony_ci    AVRouter::GetInstance().Init(g_AVSessionService.get());
17080922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().StartDeviceLogging(fd, maxSize);
17180922886Sopenharmony_ci    AVRouter::GetInstance().StopDeviceLogging();
17280922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
17380922886Sopenharmony_ci    SLOGI("StartDeviceLogging002 end");
17480922886Sopenharmony_ci}
17580922886Sopenharmony_ci
17680922886Sopenharmony_ci/**
17780922886Sopenharmony_ci* @tc.name: OnDeviceLogEvent001
17880922886Sopenharmony_ci* @tc.desc: test OnDeviceLogEvent
17980922886Sopenharmony_ci* @tc.type: FUNC
18080922886Sopenharmony_ci* @tc.require: NA
18180922886Sopenharmony_ci*/
18280922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, OnDeviceLogEvent001, TestSize.Level1)
18380922886Sopenharmony_ci{
18480922886Sopenharmony_ci    SLOGI("OnDeviceLogEvent001 begin");
18580922886Sopenharmony_ci    DeviceLogEventCode eventId = DEVICE_LOG_FULL;
18680922886Sopenharmony_ci    int64_t param = 0;
18780922886Sopenharmony_ci    AVRouter::GetInstance().Init(nullptr);
18880922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().OnDeviceLogEvent(eventId, param);
18980922886Sopenharmony_ci    EXPECT_EQ(ret, ERR_SERVICE_NOT_EXIST);
19080922886Sopenharmony_ci    SLOGI("OnDeviceLogEvent001 end");
19180922886Sopenharmony_ci}
19280922886Sopenharmony_ci
19380922886Sopenharmony_ci/**
19480922886Sopenharmony_ci* @tc.name: OnDeviceLogEvent002
19580922886Sopenharmony_ci* @tc.desc: test OnDeviceLogEvent
19680922886Sopenharmony_ci* @tc.type: FUNC
19780922886Sopenharmony_ci* @tc.require: NA
19880922886Sopenharmony_ci*/
19980922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, OnDeviceLogEvent002, TestSize.Level1)
20080922886Sopenharmony_ci{
20180922886Sopenharmony_ci    SLOGI("OnDeviceLogEvent002 begin");
20280922886Sopenharmony_ci    DeviceLogEventCode eventId = DEVICE_LOG_FULL;
20380922886Sopenharmony_ci    int64_t param = 0;
20480922886Sopenharmony_ci    AVRouter::GetInstance().Init(g_AVSessionService.get());
20580922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().OnDeviceLogEvent(eventId, param);
20680922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
20780922886Sopenharmony_ci    SLOGI("OnDeviceLogEvent002 end");
20880922886Sopenharmony_ci}
20980922886Sopenharmony_ci
21080922886Sopenharmony_ci/**
21180922886Sopenharmony_ci* @tc.name: OnDeviceOffline001
21280922886Sopenharmony_ci* @tc.desc: test OnDeviceOffline
21380922886Sopenharmony_ci* @tc.type: FUNC
21480922886Sopenharmony_ci* @tc.require: NA
21580922886Sopenharmony_ci*/
21680922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, OnDeviceOffline001, TestSize.Level1)
21780922886Sopenharmony_ci{
21880922886Sopenharmony_ci    SLOGI("OnDeviceOffline001 begin");
21980922886Sopenharmony_ci    std::string deviceId = "***";
22080922886Sopenharmony_ci    AVRouter::GetInstance().Init(nullptr);
22180922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().OnDeviceOffline(deviceId);
22280922886Sopenharmony_ci    EXPECT_EQ(ret, ERR_SERVICE_NOT_EXIST);
22380922886Sopenharmony_ci    SLOGI("OnDeviceOffline001 end");
22480922886Sopenharmony_ci}
22580922886Sopenharmony_ci
22680922886Sopenharmony_ci/**
22780922886Sopenharmony_ci* @tc.name: OnDeviceOffline002
22880922886Sopenharmony_ci* @tc.desc: test OnDeviceOffline
22980922886Sopenharmony_ci* @tc.type: FUNC
23080922886Sopenharmony_ci* @tc.require: NA
23180922886Sopenharmony_ci*/
23280922886Sopenharmony_cistatic HWTEST_F(AVRouterTest, OnDeviceOffline002, TestSize.Level1)
23380922886Sopenharmony_ci{
23480922886Sopenharmony_ci    SLOGI("OnDeviceOffline002 begin");
23580922886Sopenharmony_ci    AVRouter::GetInstance().Init(g_AVSessionService.get());
23680922886Sopenharmony_ci    std::string deviceId = "***";
23780922886Sopenharmony_ci    auto ret = AVRouter::GetInstance().OnDeviceOffline(deviceId);
23880922886Sopenharmony_ci    EXPECT_EQ(ret, AVSESSION_SUCCESS);
23980922886Sopenharmony_ci    SLOGI("OnDeviceOffline002 end");
24080922886Sopenharmony_ci}
24180922886Sopenharmony_ci#endif
24280922886Sopenharmony_ci}
24380922886Sopenharmony_ci}