BluetoothRouteTransitionTests.java revision cc74215b6e52d550f36b929319129a5dabb4b616
12afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu/*
22afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * Copyright (C) 2018 The Android Open Source Project
32afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu *
42afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * Licensed under the Apache License, Version 2.0 (the "License");
52afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * you may not use this file except in compliance with the License.
62afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * You may obtain a copy of the License at
72afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu *
82afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu *      http://www.apache.org/licenses/LICENSE-2.0
92afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu *
102afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * Unless required by applicable law or agreed to in writing, software
112afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * distributed under the License is distributed on an "AS IS" BASIS,
122afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * See the License for the specific language governing permissions and
142afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu * limitations under the License
152afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu */
162afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
172afe9027824dbe6d88fb953045e12496b0da4cbcHall Liupackage com.android.server.telecom.tests;
182afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
192afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport android.bluetooth.BluetoothDevice;
202afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport android.content.ContentResolver;
212afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport android.test.suitebuilder.annotation.SmallTest;
222afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
232afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport com.android.server.telecom.BluetoothHeadsetProxy;
242afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport com.android.server.telecom.TelecomSystem;
252afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport com.android.server.telecom.Timeouts;
262afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport com.android.server.telecom.bluetooth.BluetoothDeviceManager;
272afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport com.android.server.telecom.bluetooth.BluetoothRouteManager;
282afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
292afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport org.junit.Before;
302afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport org.junit.Test;
312afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport org.junit.runner.RunWith;
322afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport org.junit.runners.Parameterized;
332afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport org.mockito.Mock;
342afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
352afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport java.util.ArrayList;
362afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport java.util.Arrays;
372afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport java.util.Collection;
382afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport java.util.List;
392afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport java.util.Objects;
402afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
412afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static com.android.server.telecom.tests.BluetoothRouteManagerTest.DEVICE1;
422afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static com.android.server.telecom.tests.BluetoothRouteManagerTest.DEVICE2;
432afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static com.android.server.telecom.tests.BluetoothRouteManagerTest.DEVICE3;
442afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static com.android.server.telecom.tests.BluetoothRouteManagerTest.executeRoutingAction;
452afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static org.junit.Assert.assertEquals;
462afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static org.mockito.ArgumentMatchers.eq;
472afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static org.mockito.ArgumentMatchers.nullable;
482afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static org.mockito.Mockito.doAnswer;
492afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static org.mockito.Mockito.never;
502afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static org.mockito.Mockito.reset;
512afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static org.mockito.Mockito.verify;
522afe9027824dbe6d88fb953045e12496b0da4cbcHall Liuimport static org.mockito.Mockito.when;
532afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
542afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu@RunWith(Parameterized.class)
552afe9027824dbe6d88fb953045e12496b0da4cbcHall Liupublic class BluetoothRouteTransitionTests extends TelecomTestCase {
562afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private enum ListenerUpdate {
57cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        DEVICE_LIST_CHANGED, ACTIVE_DEVICE_PRESENT, ACTIVE_DEVICE_GONE,
582afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        AUDIO_CONNECTED, AUDIO_DISCONNECTED
592afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
602afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
612afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private static class BluetoothRouteTestParametersBuilder {
622afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private String name;
632afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private String initialBluetoothState;
642afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private BluetoothDevice initialDevice;
652afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private BluetoothDevice audioOnDevice;
662afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private int messageType;
6767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        private BluetoothDevice messageDevice;
682afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private ListenerUpdate[] expectedListenerUpdates;
692afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private int expectedBluetoothInteraction;
7067eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        private BluetoothDevice expectedConnectionDevice;
712afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private String expectedFinalStateName;
722afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        private BluetoothDevice[] connectedDevices;
73cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        // the active device as returned by BluetoothHeadset#getActiveDevice
74cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        private BluetoothDevice activeDevice = null;
752afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
762afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setName(String name) {
772afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.name = name;
782afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
792afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
802afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
812afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setInitialBluetoothState(
822afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                String initialBluetoothState) {
832afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.initialBluetoothState = initialBluetoothState;
842afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
852afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
862afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
872afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setInitialDevice(BluetoothDevice
882afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                initialDevice) {
892afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.initialDevice = initialDevice;
902afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
912afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
922afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
932afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setMessageType(int messageType) {
942afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.messageType = messageType;
952afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
962afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
972afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
9867eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        public BluetoothRouteTestParametersBuilder setMessageDevice(BluetoothDevice messageDevice) {
992afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.messageDevice = messageDevice;
1002afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
1012afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1022afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
1032afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setExpectedListenerUpdates(
1042afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                ListenerUpdate... expectedListenerUpdates) {
1052afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.expectedListenerUpdates = expectedListenerUpdates;
1062afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
1072afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1082afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
1092afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setExpectedBluetoothInteraction(
1102afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                int expectedBluetoothInteraction) {
1112afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.expectedBluetoothInteraction = expectedBluetoothInteraction;
1122afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
1132afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1142afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
11567eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        public BluetoothRouteTestParametersBuilder setExpectedConnectionDevice(
11667eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                BluetoothDevice expectedConnectionDevice) {
11767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu            this.expectedConnectionDevice = expectedConnectionDevice;
1182afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
1192afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1202afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
1212afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setExpectedFinalStateName(
1222afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                String expectedFinalStateName) {
1232afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.expectedFinalStateName = expectedFinalStateName;
1242afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
1252afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1262afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
1272afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setConnectedDevices(
1282afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                BluetoothDevice... connectedDevices) {
1292afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.connectedDevices = connectedDevices;
1302afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
1312afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1322afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
1332afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParametersBuilder setAudioOnDevice(BluetoothDevice device) {
1342afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.audioOnDevice = device;
1352afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return this;
1362afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1372afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
138cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        public BluetoothRouteTestParametersBuilder setActiveDevice(BluetoothDevice device) {
139cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu            this.activeDevice = device;
140cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu            return this;
141cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        }
142cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu
1432afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParameters build() {
1442afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return new BluetoothRouteTestParameters(name,
1452afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    initialBluetoothState,
1462afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    initialDevice,
1472afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    messageType,
1482afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    expectedListenerUpdates,
1492afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    expectedBluetoothInteraction,
15067eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                    expectedConnectionDevice,
1512afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    expectedFinalStateName,
1522afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    connectedDevices,
1532afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    messageDevice,
154cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                    audioOnDevice,
155cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                    activeDevice);
156cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu
1572afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1582afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
1592afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
1602afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private static class BluetoothRouteTestParameters {
1612afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public String name;
1622afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public String initialBluetoothState; // One of the state names or prefixes from BRM.
1632afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothDevice initialDevice; // null if we start from AudioOff
1642afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothDevice audioOnDevice; // The device (if any) that is active
1652afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public int messageType; // Any of the commands from the state machine
16667eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        public BluetoothDevice messageDevice; // The device that should be specified in the message.
1672afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public ListenerUpdate[] expectedListenerUpdates; // what the listener should expect.
1682afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public int expectedBluetoothInteraction; // NONE, CONNECT, or DISCONNECT
16967eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        public BluetoothDevice expectedConnectionDevice; // Expected device to connect to.
1702afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public String expectedFinalStateName; // Expected name of the final state.
1712afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothDevice[] connectedDevices; // array of connected devices
172cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        // the active device as returned by BluetoothHeadset#getActiveDevice
173cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        private BluetoothDevice activeDevice = null;
1742afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
1752afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public BluetoothRouteTestParameters(String name, String initialBluetoothState,
1762afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                BluetoothDevice initialDevice, int messageType, ListenerUpdate[]
17767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                expectedListenerUpdates, int expectedBluetoothInteraction, BluetoothDevice
17867eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                expectedConnectionDevice, String expectedFinalStateName,
17967eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                BluetoothDevice[] connectedDevices, BluetoothDevice messageDevice,
180cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                BluetoothDevice audioOnDevice, BluetoothDevice activeDevice) {
1812afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.name = name;
1822afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.initialBluetoothState = initialBluetoothState;
1832afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.initialDevice = initialDevice;
1842afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.messageType = messageType;
1852afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.expectedListenerUpdates = expectedListenerUpdates;
1862afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.expectedBluetoothInteraction = expectedBluetoothInteraction;
18767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu            this.expectedConnectionDevice = expectedConnectionDevice;
1882afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.expectedFinalStateName = expectedFinalStateName;
1892afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.connectedDevices = connectedDevices;
1902afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.messageDevice = messageDevice;
1912afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            this.audioOnDevice = audioOnDevice;
192cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu            this.activeDevice = activeDevice;
1932afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
1942afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
1952afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        @Override
1962afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        public String toString() {
1972afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return "BluetoothRouteTestParameters{" +
1982afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    "name='" + name + '\'' +
1992afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    ", initialBluetoothState='" + initialBluetoothState + '\'' +
2002afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    ", initialDevice=" + initialDevice +
2012afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    ", messageType=" + messageType +
2022afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    ", messageDevice='" + messageDevice + '\'' +
2032afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    ", expectedListenerUpdate=" + expectedListenerUpdates +
2042afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    ", expectedBluetoothInteraction=" + expectedBluetoothInteraction +
20567eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                    ", expectedConnectionDevice='" + expectedConnectionDevice + '\'' +
2062afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    ", expectedFinalStateName='" + expectedFinalStateName + '\'' +
2072afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    ", connectedDevices=" + Arrays.toString(connectedDevices) +
208cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                    ", activeDevice='" + activeDevice + '\'' +
2092afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    '}';
2102afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
2112afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
2122afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2132afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private static final int NONE = 1;
2142afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private static final int CONNECT = 2;
2152afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private static final int DISCONNECT = 3;
2162afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2172afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private static final int TEST_TIMEOUT = 1000;
2182afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2192afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private final BluetoothRouteTestParameters mParams;
2202afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @Mock private BluetoothDeviceManager mDeviceManager;
2212afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @Mock private BluetoothHeadsetProxy mHeadsetProxy;
2222afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @Mock private Timeouts.Adapter mTimeoutsAdapter;
2232afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @Mock private BluetoothRouteManager.BluetoothStateListener mListener;
2242afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2252afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @Override
2262afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @Before
2272afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    public void setUp() throws Exception {
2282afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        super.setUp();
2292afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
2302afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2312afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    public BluetoothRouteTransitionTests(BluetoothRouteTestParameters params) {
2322afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        mParams = params;
2332afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
2342afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2352afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @Test
2362afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @SmallTest
2372afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    public void testTransitions() {
2382afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        BluetoothRouteManager sm = setupStateMachine(
2392afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                mParams.initialBluetoothState, mParams.initialDevice);
2402afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
241cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        setupConnectedDevices(mParams.connectedDevices,
242cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                mParams.audioOnDevice, mParams.activeDevice);
243cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        sm.setActiveDeviceCacheForTesting(mParams.activeDevice);
2442afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2452afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        // Go through the utility methods for these two messages
2462afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        if (mParams.messageType == BluetoothRouteManager.NEW_DEVICE_CONNECTED) {
24767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu            sm.onDeviceAdded(mParams.messageDevice.getAddress());
248cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu            sm.onActiveDeviceChanged(mParams.messageDevice);
2492afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        } else if (mParams.messageType == BluetoothRouteManager.LOST_DEVICE) {
25067eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu            sm.onDeviceLost(mParams.messageDevice.getAddress());
251cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu            sm.onActiveDeviceChanged(null);
2522afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        } else {
25367eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu            executeRoutingAction(sm, mParams.messageType,
25467eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                    mParams.messageDevice == null ? null : mParams.messageDevice.getAddress());
2552afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
2562afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2572afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
2582afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        assertEquals(mParams.expectedFinalStateName, sm.getCurrentState().getName());
2592afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2602afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        for (ListenerUpdate lu : mParams.expectedListenerUpdates) {
2612afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            switch (lu) {
2622afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                case DEVICE_LIST_CHANGED:
2632afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    verify(mListener).onBluetoothDeviceListChanged();
2642afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    break;
265cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                case ACTIVE_DEVICE_PRESENT:
266cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                    verify(mListener).onBluetoothActiveDevicePresent();
2672afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    break;
268cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                case ACTIVE_DEVICE_GONE:
269cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                    verify(mListener).onBluetoothActiveDeviceGone();
2702afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    break;
2712afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                case AUDIO_CONNECTED:
2722afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    verify(mListener).onBluetoothAudioConnected();
2732afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    break;
2742afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                case AUDIO_DISCONNECTED:
2752afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    verify(mListener).onBluetoothAudioDisconnected();
2762afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    break;
2772afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            }
2782afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
2792afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2802afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        switch (mParams.expectedBluetoothInteraction) {
2812afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            case NONE:
28267eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                verify(mHeadsetProxy, never()).connectAudio();
28367eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                verify(mHeadsetProxy, never()).setActiveDevice(nullable(BluetoothDevice.class));
2842afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                verify(mHeadsetProxy, never()).disconnectAudio();
2852afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                break;
2862afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            case CONNECT:
28767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                verify(mHeadsetProxy).connectAudio();
28867eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                verify(mHeadsetProxy).setActiveDevice(mParams.expectedConnectionDevice);
2892afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                verify(mHeadsetProxy, never()).disconnectAudio();
2902afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                break;
2912afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            case DISCONNECT:
29267eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                verify(mHeadsetProxy, never()).connectAudio();
29367eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                verify(mHeadsetProxy, never()).setActiveDevice(nullable(BluetoothDevice.class));
2942afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                verify(mHeadsetProxy).disconnectAudio();
2952afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                break;
2962afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
2972afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
2982afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        sm.getHandler().removeMessages(BluetoothRouteManager.CONNECTION_TIMEOUT);
2992afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        sm.quitNow();
3002afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
3012afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
302cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu    private void setupConnectedDevices(BluetoothDevice[] devices,
303cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu            BluetoothDevice audioOnDevice, BluetoothDevice activeDevice) {
3042afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        when(mDeviceManager.getNumConnectedDevices()).thenReturn(devices.length);
3052afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        when(mDeviceManager.getConnectedDevices()).thenReturn(Arrays.asList(devices));
3062afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        when(mHeadsetProxy.getConnectedDevices()).thenReturn(Arrays.asList(devices));
307cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        when(mHeadsetProxy.getActiveDevice()).thenReturn(activeDevice);
308cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu        if (audioOnDevice != null) {
309cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu            when(mHeadsetProxy.isAudioConnected(eq(audioOnDevice))).thenReturn(true);
3102afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
3112afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        doAnswer(invocation -> {
3122afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            BluetoothDevice first = getFirstExcluding(devices,
3132afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                    (String) invocation.getArguments()[0]);
3142afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            return first == null ? null : first.getAddress();
3152afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }).when(mDeviceManager).getMostRecentlyConnectedDevice(nullable(String.class));
31667eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        for (BluetoothDevice device : devices) {
31767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu            when(mDeviceManager.getDeviceFromAddress(device.getAddress())).thenReturn(device);
31867eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        }
3192afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
3202afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
3212afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private BluetoothRouteManager setupStateMachine(String initialState,
3222afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            BluetoothDevice initialDevice) {
3232afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        resetMocks();
3242afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        BluetoothRouteManager sm = new BluetoothRouteManager(mContext,
3252afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                new TelecomSystem.SyncRoot() { }, mDeviceManager, mTimeoutsAdapter);
3262afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        sm.setListener(mListener);
3272afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        sm.setInitialStateForTesting(initialState, initialDevice);
3282afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
3292afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        resetMocks();
3302afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        return sm;
3312afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
3322afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
3332afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private void resetMocks() {
3342afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        reset(mDeviceManager, mListener, mHeadsetProxy, mTimeoutsAdapter);
3352afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        when(mDeviceManager.getHeadsetService()).thenReturn(mHeadsetProxy);
33667eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        when(mHeadsetProxy.connectAudio()).thenReturn(true);
33767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu        when(mHeadsetProxy.setActiveDevice(nullable(BluetoothDevice.class))).thenReturn(true);
3382afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        when(mTimeoutsAdapter.getRetryBluetoothConnectAudioBackoffMillis(
3392afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                nullable(ContentResolver.class))).thenReturn(100000L);
3402afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        when(mTimeoutsAdapter.getBluetoothPendingTimeoutMillis(
3412afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                nullable(ContentResolver.class))).thenReturn(100000L);
3422afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
3432afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
3442afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    private static BluetoothDevice getFirstExcluding(
3452afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            BluetoothDevice[] devices, String excludeAddress) {
3462afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        for (BluetoothDevice x : devices) {
3472afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            if (!Objects.equals(excludeAddress, x.getAddress())) {
3482afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                return x;
3492afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu            }
3502afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        }
3512afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        return null;
3522afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
3532afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
3542afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    @Parameterized.Parameters(name = "{0}")
3552afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    public static Collection<BluetoothRouteTestParameters> generateTestCases() {
3562afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        List<BluetoothRouteTestParameters> result = new ArrayList<>();
3572afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
3582afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("New device connected while audio off")
3592afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
3602afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(null)
3612afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE1)
3622afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.NEW_DEVICE_CONNECTED)
36367eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE1)
364cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedListenerUpdates(ListenerUpdate.DEVICE_LIST_CHANGED,
365cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                        ListenerUpdate.ACTIVE_DEVICE_PRESENT)
3662afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(NONE)
36767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setExpectedConnectionDevice(null)
3682afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
3692afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
3702afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
3712afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
372cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setName("Nonspecific connection request while audio off with BT-active device")
3732afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
3742afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(null)
3752afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE1)
376cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setActiveDevice(DEVICE1)
3772afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.CONNECT_HFP)
3782afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_CONNECTED)
3792afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(CONNECT)
380cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedConnectionDevice(DEVICE1)
3812afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX
382cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                        + ":" + DEVICE1)
3832afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
3842afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
3852afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
3862afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Connection to a device succeeds after pending")
3872afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX)
3882afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
3892afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setAudioOnDevice(DEVICE2)
3902afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE1)
3912afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.HFP_IS_ON)
39267eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE2)
3932afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_CONNECTED)
3942afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(NONE)
39567eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setExpectedConnectionDevice(null)
3962afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX
39767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                        + ":" + DEVICE2)
3982afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
3992afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
4002afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
4012afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Device loses HFP audio but remains connected. No fallback.")
4022afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX)
4032afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
4042afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2)
4052afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.HFP_LOST)
40667eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE2)
4072afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_DISCONNECTED)
4082afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(NONE)
40967eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setExpectedConnectionDevice(null)
4102afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
4112afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
4122afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
4132afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
414cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setName("Device loses HFP audio but remains connected."
415cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                        + " No fallback even though other devices available.")
4162afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX)
4172afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
4182afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE1, DEVICE3)
4192afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.HFP_LOST)
42067eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE2)
421cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_DISCONNECTED)
422cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedBluetoothInteraction(NONE)
423cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedConnectionDevice(null)
424cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
4252afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
4262afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
4272afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
428cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setName("Switch the device that audio is being routed to")
4292afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX)
4302afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
4312afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE1, DEVICE3)
4322afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.CONNECT_HFP)
43367eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE3)
4342afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_CONNECTED)
4352afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(CONNECT)
43667eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setExpectedConnectionDevice(DEVICE3)
4372afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX
43867eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                        + ":" + DEVICE3)
4392afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
4402afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
4412afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
4422afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Switch to another device before first device has connected")
4432afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX)
4442afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
4452afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE1, DEVICE3)
4462afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.CONNECT_HFP)
44767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE3)
4482afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_CONNECTED)
4492afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(CONNECT)
45067eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setExpectedConnectionDevice(DEVICE3)
4512afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX
45267eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                        + ":" + DEVICE3)
4532afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
4542afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
4552afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
4562afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Device gets disconnected while active. No fallback.")
4572afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX)
4582afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
459cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setActiveDevice(DEVICE2)
4602afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices()
4612afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.LOST_DEVICE)
46267eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE2)
4632afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_DISCONNECTED,
464cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                        ListenerUpdate.DEVICE_LIST_CHANGED, ListenerUpdate.ACTIVE_DEVICE_GONE)
4652afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(NONE)
46667eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setExpectedConnectionDevice(null)
4672afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
4682afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
4692afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
4702afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
471cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setName("Device gets disconnected while active."
472cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                        + " No fallback even though other devices available.")
4732afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX)
4742afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
4752afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE3)
4762afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.LOST_DEVICE)
47767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE2)
478cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_DISCONNECTED,
4792afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                        ListenerUpdate.DEVICE_LIST_CHANGED)
480cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedBluetoothInteraction(NONE)
481cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedConnectionDevice(null)
482cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
4832afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
4842afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
4852afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
4862afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Connection to DEVICE2 times out but device 1 still connected.")
4872afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX)
4882afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
4892afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE1)
4902afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setAudioOnDevice(DEVICE1)
4912afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.CONNECTION_TIMEOUT)
4922afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_CONNECTED)
4932afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(NONE)
4942afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX
49567eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                        + ":" + DEVICE1)
4962afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
4972afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
4982afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
4992afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("DEVICE1 somehow becomes active when DEVICE2 is still pending.")
5002afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX)
5012afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
5022afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE1)
5032afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setAudioOnDevice(DEVICE1)
5042afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.HFP_IS_ON)
50567eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE1)
5062afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_CONNECTED)
5072afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(NONE)
5082afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX
50967eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                        + ":" + DEVICE1)
5102afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
5112afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
5122afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
513cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setName("Device gets disconnected while pending."
514cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                        + " No fallback even though other devices available.")
5152afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX)
5162afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
5172afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE3)
5182afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.LOST_DEVICE)
51967eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE2)
520cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_DISCONNECTED,
5212afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                        ListenerUpdate.DEVICE_LIST_CHANGED)
522cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedBluetoothInteraction(NONE)
523cc74215b6e52d550f36b929319129a5dabb4b616Hall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
5242afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
5252afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
5262afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
5272afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Device gets disconnected while pending. No fallback.")
5282afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX)
5292afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
5302afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices()
5312afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.LOST_DEVICE)
53267eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE2)
5332afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_DISCONNECTED,
5342afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                        ListenerUpdate.DEVICE_LIST_CHANGED)
5352afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(NONE)
5362afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
5372afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
5382afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
5392afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
5402afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Audio routing requests HFP disconnection while a device is active")
5412afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX)
5422afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
5432afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE3)
5442afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.DISCONNECT_HFP)
5452afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_DISCONNECTED)
5462afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(DISCONNECT)
5472afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
5482afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
5492afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
5502afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
5512afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Audio routing requests HFP disconnection while a device is pending")
5522afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_CONNECTING_STATE_NAME_PREFIX)
5532afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(DEVICE2)
5542afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE3)
5552afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.DISCONNECT_HFP)
5562afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_DISCONNECTED)
5572afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(DISCONNECT)
5582afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
5592afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
5602afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
5612afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        result.add(new BluetoothRouteTestParametersBuilder()
5622afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setName("Bluetooth turns itself on.")
5632afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialBluetoothState(BluetoothRouteManager.AUDIO_OFF_STATE_NAME)
5642afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setInitialDevice(null)
5652afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setConnectedDevices(DEVICE2, DEVICE3)
5662afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setMessageType(BluetoothRouteManager.HFP_IS_ON)
56767eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                .setMessageDevice(DEVICE3)
5682afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedListenerUpdates(ListenerUpdate.AUDIO_CONNECTED)
5692afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedBluetoothInteraction(NONE)
5702afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .setExpectedFinalStateName(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX
57167eb3b08235d34f0f6411f1021808c97042ecbbcHall Liu                        + ":" + DEVICE3)
5722afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu                .build());
5732afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu
5742afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu        return result;
5752afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu    }
5762afe9027824dbe6d88fb953045e12496b0da4cbcHall Liu}
577