1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (C) 2025 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15import os 16import time 17import pytest 18from utils import GP, check_cmd_block, check_hdc_cmd, load_gp, get_server_pid_from_file 19 20 21class TestHdcServer: 22 @pytest.mark.L0 23 def test_hdc_server_foreground(self): 24 port = os.getenv('OHOS_HDC_SERVER_PORT') 25 if port is None: 26 port = 8710 27 assert check_hdc_cmd("start") 28 assert check_hdc_cmd("-l5 kill", "Kill server finish") 29 assert check_cmd_block(f"{GP.hdc_exe} -m", f"port: {port}", timeout=5) 30 assert check_hdc_cmd("start") 31 time.sleep(3) # sleep 3s to wait for the device to connect channel 32 33 @pytest.mark.L0 34 def test_server_kill(self): 35 assert check_hdc_cmd("start") 36 pid_first = get_server_pid_from_file() 37 assert check_hdc_cmd("-l5 kill", "Kill server finish") 38 assert not check_hdc_cmd("start", "start server") 39 time.sleep(2) # sleep 1s to wait for the server 40 pid_second = get_server_pid_from_file() 41 42 assert check_hdc_cmd("-l5 kill -r", "Kill server finish") 43 time.sleep(2) # sleep 1s to wait for the server 44 pid_third = get_server_pid_from_file() 45 assert not check_hdc_cmd("start -r", "start server") 46 time.sleep(1) # sleep 1s to wait for the server 47 pid_forth = get_server_pid_from_file() 48 assert pid_first != pid_second 49 assert pid_second != pid_third 50 assert pid_third != pid_forth 51 assert check_hdc_cmd("checkserver", "Ver") 52 time.sleep(2) # sleep 2s to wait for the server