1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
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.
15  */
16 
17 package com.android.systemui.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.any;
22 import static org.mockito.Mockito.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.testing.AndroidTestingRunner;
28 import android.testing.TestableLooper;
29 import android.view.View;
30 import android.widget.Button;
31 import android.widget.TextView;
32 
33 import androidx.test.filters.SmallTest;
34 
35 import com.android.internal.logging.UiEventLogger;
36 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
37 import com.android.settingslib.bluetooth.LocalBluetoothManager;
38 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
39 import com.android.systemui.R;
40 import com.android.systemui.broadcast.BroadcastSender;
41 import com.android.systemui.SysuiTestCase;
42 import com.android.systemui.media.dialog.MediaOutputDialogFactory;
43 
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.MockitoAnnotations;
49 
50 @SmallTest
51 @RunWith(AndroidTestingRunner.class)
52 @TestableLooper.RunWithLooper(setAsMainLooper = true)
53 public class BroadcastDialogTest extends SysuiTestCase {
54 
55     private static final String CURRENT_BROADCAST_APP = "Music";
56     private static final String SWITCH_APP = "System UI";
57     private static final String TEST_PACKAGE = "com.android.systemui";
58     private final LocalBluetoothManager mLocalBluetoothManager = mock(LocalBluetoothManager.class);
59     private final LocalBluetoothProfileManager mLocalBluetoothProfileManager = mock(
60             LocalBluetoothProfileManager.class);
61     private final LocalBluetoothLeBroadcast mLocalBluetoothLeBroadcast = mock(
62             LocalBluetoothLeBroadcast.class);
63     private final BroadcastSender mBroadcastSender = mock(BroadcastSender.class);
64     private BroadcastDialog mBroadcastDialog;
65     private View mDialogView;
66     private TextView mTitle;
67     private TextView mSubTitle;
68     private Button mSwitchBroadcastAppButton;
69     private Button mChangeOutputButton;
70 
71     @Before
setUp()72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74         when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
75         when(mLocalBluetoothProfileManager.getLeAudioBroadcastProfile()).thenReturn(null);
76         mBroadcastDialog = new BroadcastDialog(mContext, mock(MediaOutputDialogFactory.class),
77                 mLocalBluetoothManager, CURRENT_BROADCAST_APP, TEST_PACKAGE,
78                 mock(UiEventLogger.class), mBroadcastSender);
79         mBroadcastDialog.show();
80         mDialogView = mBroadcastDialog.mDialogView;
81     }
82 
83     @After
tearDown()84     public void tearDown() {
85         mBroadcastDialog.dismiss();
86     }
87 
88     @Test
onCreate_withCurrentApp_titleIsCurrentAppName()89     public void onCreate_withCurrentApp_titleIsCurrentAppName() {
90         mTitle = mDialogView.requireViewById(R.id.dialog_title);
91 
92         assertThat(mTitle.getText().toString()).isEqualTo(mContext.getString(
93                 R.string.bt_le_audio_broadcast_dialog_title, CURRENT_BROADCAST_APP));
94     }
95 
96     @Test
onCreate_withCurrentApp_subTitleIsSwitchAppName()97     public void onCreate_withCurrentApp_subTitleIsSwitchAppName() {
98         mSubTitle = mDialogView.requireViewById(R.id.dialog_subtitle);
99 
100         assertThat(mSubTitle.getText()).isEqualTo(
101                 mContext.getString(R.string.bt_le_audio_broadcast_dialog_sub_title, SWITCH_APP));
102     }
103 
104     @Test
onCreate_withCurrentApp_switchBtnIsSwitchAppName()105     public void onCreate_withCurrentApp_switchBtnIsSwitchAppName() {
106         mSwitchBroadcastAppButton = mDialogView.requireViewById(R.id.switch_broadcast);
107 
108         assertThat(mSwitchBroadcastAppButton.getText().toString()).isEqualTo(
109                 mContext.getString(R.string.bt_le_audio_broadcast_dialog_switch_app, SWITCH_APP));
110     }
111 
112     @Test
onClick_withChangeOutput_dismissBroadcastDialog()113     public void onClick_withChangeOutput_dismissBroadcastDialog() {
114         mChangeOutputButton = mDialogView.requireViewById(R.id.change_output);
115         mChangeOutputButton.performClick();
116 
117         assertThat(mBroadcastDialog.isShowing()).isFalse();
118     }
119 
120     @Test
onClick_withSwitchBroadcast_stopCurrentBroadcast()121     public void onClick_withSwitchBroadcast_stopCurrentBroadcast() {
122         when(mLocalBluetoothProfileManager.getLeAudioBroadcastProfile()).thenReturn(
123                 mLocalBluetoothLeBroadcast);
124         mSwitchBroadcastAppButton = mDialogView.requireViewById(R.id.switch_broadcast);
125         mSwitchBroadcastAppButton.performClick();
126 
127         verify(mLocalBluetoothLeBroadcast).stopLatestBroadcast();
128     }
129 
130     @Test
onClick_withSwitchBroadcast_stopCurrentBroadcastFailed()131     public void onClick_withSwitchBroadcast_stopCurrentBroadcastFailed() {
132         mSwitchBroadcastAppButton = mDialogView.requireViewById(R.id.switch_broadcast);
133         mSwitchBroadcastAppButton.performClick();
134 
135         assertThat(mSwitchBroadcastAppButton.getText().toString()).isEqualTo(
136                 mContext.getString(R.string.bt_le_audio_broadcast_dialog_switch_app, SWITCH_APP));
137     }
138 
139     @Test
handleLeBroadcastStopped_withBroadcastProfileNull_doRefreshButton()140     public void handleLeBroadcastStopped_withBroadcastProfileNull_doRefreshButton() {
141         when(mLocalBluetoothProfileManager.getLeAudioBroadcastProfile()).thenReturn(null);
142         mSwitchBroadcastAppButton = mDialogView.requireViewById(R.id.switch_broadcast);
143 
144         mBroadcastDialog.handleLeBroadcastStopped();
145 
146         assertThat(mSwitchBroadcastAppButton.getText().toString()).isEqualTo(
147                 mContext.getString(R.string.bt_le_audio_broadcast_dialog_switch_app, SWITCH_APP));
148     }
149 
150     @Test
handleLeBroadcastStopped_withBroadcastProfile_doStartBroadcast()151     public void handleLeBroadcastStopped_withBroadcastProfile_doStartBroadcast() {
152         when(mLocalBluetoothProfileManager.getLeAudioBroadcastProfile()).thenReturn(
153                 mLocalBluetoothLeBroadcast);
154 
155         mBroadcastDialog.handleLeBroadcastStopped();
156 
157         verify(mLocalBluetoothLeBroadcast).startBroadcast(eq(SWITCH_APP), any());
158     }
159 
160     @Test
handleLeBroadcastMetadataChanged_withNotLaunchFlag_doNotDismissDialog()161     public void handleLeBroadcastMetadataChanged_withNotLaunchFlag_doNotDismissDialog() {
162 
163         mBroadcastDialog.handleLeBroadcastMetadataChanged();
164 
165         assertThat(mBroadcastDialog.isShowing()).isTrue();
166     }
167 
168     @Test
handleLeBroadcastMetadataChanged_withLaunchFlag_dismissDialog()169     public void handleLeBroadcastMetadataChanged_withLaunchFlag_dismissDialog() {
170 
171         mBroadcastDialog.handleLeBroadcastStarted();
172         mBroadcastDialog.handleLeBroadcastMetadataChanged();
173 
174         assertThat(mBroadcastDialog.isShowing()).isFalse();
175     }
176 }
177