• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Copyright (C) 2025 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
16import os
17import subprocess
18from mobly import base_test
19from mobly import test_runner
20from mobly import config_parser
21from mobly.controllers import android_device
22from devices import DeviceFactory
23from pathlib import Path
24from utilities.main_utils import common_main
25
26class AndroidFlashTest(base_test.BaseTestClass):
27
28    def setup_class(self):
29        # Setup required for the entire test class.
30        logging.info('Setting up test class.')
31        # Registers the android devices controllers with Mobly.
32        self.android_devices = self.register_controller(android_device)
33        # Retrieves the build path from the test configuration.
34        self.device = self.user_params['device']
35        self.serial = self.user_params['serial']
36        self.image_dir = Path(self.user_params['image_dir'])
37        self.wipe = self.user_params['wipe']
38        self.hw_variant = self.user_params['hw_variant']
39        self.b = self.user_params['b']
40        self.diag = self.user_params['diag']
41        self.preserve_keys = self.user_params['preserve_keys']
42
43    def test_flash_device(self):
44        # Check that there is at least one device available.
45        if not self.android_devices:
46            raise AssertionError('No Android devices are registered. Please make '
47                                 'sure at least one device is connected and '
48                                 'configured properly.')
49        ad = self.android_devices[0]  # Get the first device object.
50
51        # Record the build fingerprint before flashing.
52        before_flash_fingerprint = ad.adb.getprop('ro.build.fingerprint')
53        logging.info('Fingerprint before flash: %s', before_flash_fingerprint)
54
55        # Command to source your custom bashrc file and then run the flash command.
56        Device = DeviceFactory.get_device(self.device)
57        flash_result = Device.flash(ad.serial, self.image_dir, self.wipe, self.hw_variant, self.b, self.diag, self.preserve_keys)
58
59        # Reboot the device and wait until it's back online.
60        ad.reboot()
61        ad.wait_for_boot_completion()
62
63        # Check the build fingerprint after flashing.
64        after_flash_fingerprint = ad.adb.getprop('ro.build.fingerprint')
65        logging.info('Fingerprint after flash: %s', after_flash_fingerprint)
66
67if __name__ == '__main__':
68    common_main()
69
70