1"""
2  Copyright (C) 2023 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
18  Test Steps:
191. Go to Dialer >Contacts and click on '>' icon in front of contact name to see the contact details
202. Verify address information stored in phone contacts rendered in dialer app successfully
21
22"""
23
24import logging
25
26from mobly import asserts
27from bluetooth_test import bluetooth_base_test
28from mobly.controllers import android_device
29
30from utilities.main_utils import common_main
31from utilities import constants
32from utilities import spectatio_utils
33from utilities import bt_utils
34
35
36class ImportAddressDetailsTest(bluetooth_base_test.BluetoothBaseTest):
37    VCF_ADDRESS_HEADER = "ADR"
38
39    def get_first_address(self, vcf_path):
40        """ Reads the first address from the given vcf file'"""
41
42        with open(vcf_path, mode='r') as vcf_file:
43            for line in vcf_file:
44                if line.startswith(self.VCF_ADDRESS_HEADER):
45                    return line
46
47
48    def setup_test(self):
49        # Upload contacts to phone device
50        file_path = constants.PATH_TO_CONTACTS_VCF_FILE
51        self.call_utils.upload_vcf_contacts_to_device(self.target, file_path)
52
53        # Pair the devices
54        self.bt_utils.pair_primary_to_secondary()
55        super().enable_recording()
56
57    def test_import_address_details(self):
58        # Open the dialer app, and then the contacts page
59        self.call_utils.open_phone_app()
60        self.call_utils.wait_with_log(2)
61        self.call_utils.open_contacts()
62        self.call_utils.wait_with_log(2)
63        self.call_utils.open_first_contact_details()
64        self.call_utils.wait_with_log(2)
65
66        # Import the first contact's address from the discoverer device.
67        display_address = self.call_utils.get_home_address_from_details()
68
69        # Import the list of contact addresses from the VCF file.
70        vcf_line = self.get_first_address(constants.PATH_TO_CONTACTS_VCF_FILE)
71
72        # Confirm that these two lists contain the same data.
73        asserts.assert_true(
74            self.compare_display_address_to_vcf_line(display_address, vcf_line),
75            ("Displayed address does not match address stored in VCF file: " +
76             "\n\tDisplayed address: %s" +
77             "\n\tVCF address: %s") % (display_address, vcf_line))
78
79    def teardown_test(self):
80        # Turn Bluetooth off on both devices after test finishes.
81        super().teardown_test()
82
83    def compare_display_address_to_vcf_line(self, display_address, vcf_address):
84        """Confirm that each portion of a display-able street address appears in the vcf line.
85            Comparison is done portion-by-portion because the VCF line contains metadata and delimiters,
86            meaning this comparison could hypothetically give a false positive if parts of the address are entirely composed
87            of other parts of the address (for example, a house number being identical to the zip code."""
88        parts = display_address.split()
89        for part in parts:
90            if not part in vcf_address:
91                logging.info("\tAddress mismatch: %s not found in %s" % (part, vcf_address))
92                return False
93        return True
94
95
96if __name__ == '__main__':
97    common_main()
98