1# Copyright 2021-2023 Google LLC 2# 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# https://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# Imports 17# ----------------------------------------------------------------------------- 18import pytest 19import pytest_asyncio 20import logging 21 22from bumble import device 23from bumble.profiles import vcp 24from .test_utils import TwoDevices 25 26# ----------------------------------------------------------------------------- 27# Logging 28# ----------------------------------------------------------------------------- 29logger = logging.getLogger(__name__) 30 31 32# ----------------------------------------------------------------------------- 33@pytest_asyncio.fixture 34async def vcp_client(): 35 devices = TwoDevices() 36 devices[0].add_service( 37 vcp.VolumeControlService(volume_setting=32, muted=1, volume_flags=1) 38 ) 39 40 await devices.setup_connection() 41 42 assert devices.connections[0] 43 assert devices.connections[1] 44 45 # Mock encryption. 46 devices.connections[0].encryption = 1 47 devices.connections[1].encryption = 1 48 49 peer = device.Peer(devices.connections[1]) 50 vcp_client = await peer.discover_service_and_create_proxy( 51 vcp.VolumeControlServiceProxy 52 ) 53 yield vcp_client 54 55 56# ----------------------------------------------------------------------------- 57@pytest.mark.asyncio 58async def test_init_service(vcp_client: vcp.VolumeControlServiceProxy): 59 assert (await vcp_client.volume_flags.read_value()) == 1 60 assert (await vcp_client.volume_state.read_value()) == (32, 1, 0) 61 62 63# ----------------------------------------------------------------------------- 64@pytest.mark.asyncio 65async def test_relative_volume_down(vcp_client: vcp.VolumeControlServiceProxy): 66 await vcp_client.volume_control_point.write_value( 67 bytes([vcp.VolumeControlPointOpcode.RELATIVE_VOLUME_DOWN, 0]) 68 ) 69 assert (await vcp_client.volume_state.read_value()) == (16, 1, 1) 70 71 72# ----------------------------------------------------------------------------- 73@pytest.mark.asyncio 74async def test_relative_volume_up(vcp_client: vcp.VolumeControlServiceProxy): 75 await vcp_client.volume_control_point.write_value( 76 bytes([vcp.VolumeControlPointOpcode.RELATIVE_VOLUME_UP, 0]) 77 ) 78 assert (await vcp_client.volume_state.read_value()) == (48, 1, 1) 79 80 81# ----------------------------------------------------------------------------- 82@pytest.mark.asyncio 83async def test_unmute_relative_volume_down(vcp_client: vcp.VolumeControlServiceProxy): 84 await vcp_client.volume_control_point.write_value( 85 bytes([vcp.VolumeControlPointOpcode.UNMUTE_RELATIVE_VOLUME_DOWN, 0]) 86 ) 87 assert (await vcp_client.volume_state.read_value()) == (16, 0, 1) 88 89 90# ----------------------------------------------------------------------------- 91@pytest.mark.asyncio 92async def test_unmute_relative_volume_up(vcp_client: vcp.VolumeControlServiceProxy): 93 await vcp_client.volume_control_point.write_value( 94 bytes([vcp.VolumeControlPointOpcode.UNMUTE_RELATIVE_VOLUME_UP, 0]) 95 ) 96 assert (await vcp_client.volume_state.read_value()) == (48, 0, 1) 97 98 99# ----------------------------------------------------------------------------- 100@pytest.mark.asyncio 101async def test_set_absolute_volume(vcp_client: vcp.VolumeControlServiceProxy): 102 await vcp_client.volume_control_point.write_value( 103 bytes([vcp.VolumeControlPointOpcode.SET_ABSOLUTE_VOLUME, 0, 255]) 104 ) 105 assert (await vcp_client.volume_state.read_value()) == (255, 1, 1) 106 107 108# ----------------------------------------------------------------------------- 109@pytest.mark.asyncio 110async def test_mute(vcp_client: vcp.VolumeControlServiceProxy): 111 await vcp_client.volume_control_point.write_value( 112 bytes([vcp.VolumeControlPointOpcode.MUTE, 0]) 113 ) 114 assert (await vcp_client.volume_state.read_value()) == (32, 1, 0) 115 116 117# ----------------------------------------------------------------------------- 118@pytest.mark.asyncio 119async def test_unmute(vcp_client: vcp.VolumeControlServiceProxy): 120 await vcp_client.volume_control_point.write_value( 121 bytes([vcp.VolumeControlPointOpcode.UNMUTE, 0]) 122 ) 123 assert (await vcp_client.volume_state.read_value()) == (32, 0, 1) 124