1#  Copyright (C) 2023 The Android Open Source Project
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#       http://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
15import logging
16
17from bluetooth_test import bluetooth_base_test
18from mobly import asserts
19from utilities.media_utils import MediaUtils
20from utilities.common_utils import CommonUtils
21from utilities.main_utils import common_main
22from utilities import constants
23
24
25class IsAbleToSwitchAppTest(bluetooth_base_test.BluetoothBaseTest):
26
27    def setup_class(self):
28        super().setup_class()
29        self.media_utils = MediaUtils(self.target, self.discoverer)
30        self.common_utils = CommonUtils(self.target, self.discoverer)
31
32    def setup_test(self):
33        self.common_utils.grant_local_mac_address_permission()
34        self.common_utils.enable_wifi_on_phone_device()
35        self.bt_utils.pair_primary_to_secondary()
36        super().enable_recording()
37        self.media_utils.enable_bt_media_debugging_logs()
38
39    def test_media_is_able_to_switch_app(self):
40        """Tests validating song title on HU after switch media app"""
41        self.media_utils.open_media_app_on_hu()
42        self.call_utils.handle_bluetooth_audio_pop_up()
43        self.media_utils.open_youtube_music_app()
44        current_phone_song_title = self.media_utils.get_song_title_from_phone()
45        current_hu_song_title = self.media_utils.get_song_title_from_hu()
46        asserts.assert_true(current_phone_song_title == current_hu_song_title,
47                            'Invalid song titles. '
48                            'Song title on phone device and HU should be the same')
49
50        # Open Media apps menu
51        self.media_utils.open_media_apps_menu()
52
53        # Assert YouTube Music and Bluetooth Audio apps are present
54        asserts.assert_true(
55            self.common_utils.has_ui_element_with_text(constants.BLUETOOTH_AUDIO_APP),
56            '<' + constants.BLUETOOTH_AUDIO_APP + '> app should be present on Media app page')
57        asserts.assert_true(
58            self.common_utils.has_ui_element_with_text(constants.YOUTUBE_MUSIC_APP),
59            '<' + constants.YOUTUBE_MUSIC_APP + '> app should be present on Media app page')
60
61        # Open YouTube Music app on HU
62        self.media_utils.open_youtube_music_app_on_hu()
63        current_phone_next_song_title = self.media_utils.get_song_title_from_phone()
64        current_hu_next_song_title = self.media_utils.get_song_title_from_hu()
65        asserts.assert_true(current_phone_next_song_title == current_hu_next_song_title,
66                            'Invalid song titles. '
67                            'Song title on phone device and HU should be the same')
68
69        # Open Media apps menu
70        self.media_utils.open_media_apps_menu()
71        self.media_utils.open_bluetooth_audio_app_on_hu()
72        current_phone_bt_audio_song_title = self.media_utils.get_song_title_from_phone()
73        current_hu_bt_audio_song_title = self.media_utils.get_song_title_from_hu()
74        asserts.assert_true(current_phone_bt_audio_song_title == current_hu_bt_audio_song_title,
75                            'Invalid song titles. '
76                            'Song title on phone device and HU should be the same')
77
78
79    def teardown_test(self):
80        self.media_utils.get_bt_dumpsys_metadata()
81        #  Close YouTube Music app
82        self.media_utils.close_youtube_music_app()
83        self.call_utils.press_home()
84        super().teardown_test()
85
86
87if __name__ == '__main__':
88    common_main()
89