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 # Mock encryption. 43 devices.connections[0].encryption = 1 44 devices.connections[1].encryption = 1 45 46 peer = device.Peer(devices.connections[1]) 47 vcp_client = await peer.discover_service_and_create_proxy( 48 vcp.VolumeControlServiceProxy 49 ) 50 yield vcp_client 51 52 53# ----------------------------------------------------------------------------- 54@pytest.mark.asyncio 55async def test_init_service(vcp_client: vcp.VolumeControlServiceProxy): 56 assert (await vcp_client.volume_flags.read_value()) == 1 57 assert (await vcp_client.volume_state.read_value()) == (32, 1, 0) 58 59 60# ----------------------------------------------------------------------------- 61@pytest.mark.asyncio 62async def test_relative_volume_down(vcp_client: vcp.VolumeControlServiceProxy): 63 await vcp_client.volume_control_point.write_value( 64 bytes([vcp.VolumeControlPointOpcode.RELATIVE_VOLUME_DOWN, 0]) 65 ) 66 assert (await vcp_client.volume_state.read_value()) == (16, 1, 1) 67 68 69# ----------------------------------------------------------------------------- 70@pytest.mark.asyncio 71async def test_relative_volume_up(vcp_client: vcp.VolumeControlServiceProxy): 72 await vcp_client.volume_control_point.write_value( 73 bytes([vcp.VolumeControlPointOpcode.RELATIVE_VOLUME_UP, 0]) 74 ) 75 assert (await vcp_client.volume_state.read_value()) == (48, 1, 1) 76 77 78# ----------------------------------------------------------------------------- 79@pytest.mark.asyncio 80async def test_unmute_relative_volume_down(vcp_client: vcp.VolumeControlServiceProxy): 81 await vcp_client.volume_control_point.write_value( 82 bytes([vcp.VolumeControlPointOpcode.UNMUTE_RELATIVE_VOLUME_DOWN, 0]) 83 ) 84 assert (await vcp_client.volume_state.read_value()) == (16, 0, 1) 85 86 87# ----------------------------------------------------------------------------- 88@pytest.mark.asyncio 89async def test_unmute_relative_volume_up(vcp_client: vcp.VolumeControlServiceProxy): 90 await vcp_client.volume_control_point.write_value( 91 bytes([vcp.VolumeControlPointOpcode.UNMUTE_RELATIVE_VOLUME_UP, 0]) 92 ) 93 assert (await vcp_client.volume_state.read_value()) == (48, 0, 1) 94 95 96# ----------------------------------------------------------------------------- 97@pytest.mark.asyncio 98async def test_set_absolute_volume(vcp_client: vcp.VolumeControlServiceProxy): 99 await vcp_client.volume_control_point.write_value( 100 bytes([vcp.VolumeControlPointOpcode.SET_ABSOLUTE_VOLUME, 0, 255]) 101 ) 102 assert (await vcp_client.volume_state.read_value()) == (255, 1, 1) 103 104 105# ----------------------------------------------------------------------------- 106@pytest.mark.asyncio 107async def test_mute(vcp_client: vcp.VolumeControlServiceProxy): 108 await vcp_client.volume_control_point.write_value( 109 bytes([vcp.VolumeControlPointOpcode.MUTE, 0]) 110 ) 111 assert (await vcp_client.volume_state.read_value()) == (32, 1, 0) 112 113 114# ----------------------------------------------------------------------------- 115@pytest.mark.asyncio 116async def test_unmute(vcp_client: vcp.VolumeControlServiceProxy): 117 await vcp_client.volume_control_point.write_value( 118 bytes([vcp.VolumeControlPointOpcode.UNMUTE, 0]) 119 ) 120 assert (await vcp_client.volume_state.read_value()) == (32, 0, 1) 121