|  | 
|  | 1 | +from mobly import base_test | 
|  | 2 | +from mobly import test_runner | 
|  | 3 | +from mobly.controllers import android_device | 
|  | 4 | +import time | 
|  | 5 | + | 
|  | 6 | +class CameraTest(base_test.BaseTestClass): | 
|  | 7 | + def setup_class(self): | 
|  | 8 | + # This will run before any test method starts | 
|  | 9 | + print(">>> CameraTest start") | 
|  | 10 | + self.ads = self.register_controller(android_device) | 
|  | 11 | + self.dut = self.ads[0] | 
|  | 12 | + | 
|  | 13 | + def test_open_camera(self): | 
|  | 14 | + print(">>> Opening Camera app") | 
|  | 15 | + # Launch Camera app using adb shell | 
|  | 16 | + self.dut.adb.shell('am start -a android.media.action.IMAGE_CAPTURE') | 
|  | 17 | + time.sleep(2) # Give time for the camera app to open | 
|  | 18 | + | 
|  | 19 | + def test_take_photo(self): | 
|  | 20 | + print(">>> Taking a photo") | 
|  | 21 | + # Simulate pressing the camera shutter button | 
|  | 22 | + self.dut.adb.shell('input keyevent 27') | 
|  | 23 | + time.sleep(3) # Wait for the photo to be taken and saved | 
|  | 24 | + | 
|  | 25 | + def test_save_photo(self): | 
|  | 26 | + print(">>> Ensuring the Camera directory exists") | 
|  | 27 | + # Ensure the Camera directory exists on the device | 
|  | 28 | + #PATH = 'card \DCIM\Camera' | 
|  | 29 | + self.dut.adb.shell('mkdir -p /card/DCIM/Camera') | 
|  | 30 | + | 
|  | 31 | + time.sleep(5) # Wait to make sure the photo has been saved | 
|  | 32 | + | 
|  | 33 | + # List files in the Camera directory to confirm photo exists | 
|  | 34 | + photos = self.dut.adb.shell('ls /card/DCIM/Camera').decode('utf-8') # Decode bytes to string | 
|  | 35 | + if "No such file or directory" in photos: | 
|  | 36 | + print(">>> No photos found in Camera directory.") | 
|  | 37 | + return | 
|  | 38 | + | 
|  | 39 | + # Copy the first photo found to a new file name | 
|  | 40 | + self.dut.adb.shell('cp /card/DCIM/Camera/*.jpg /card/DCIM/Camera/photo_taken_1.jpg') | 
|  | 41 | + time.sleep(1) # Ensure the copy is complete | 
|  | 42 | + | 
|  | 43 | + # Verify the photo has been copied and saved | 
|  | 44 | + result = self.dut.adb.shell('ls /card/DCIM/Camera/photo_taken_1.jpg') | 
|  | 45 | + print(f"Photo saved at: {result}") | 
|  | 46 | + | 
|  | 47 | + def test_exit_camera(self): | 
|  | 48 | + print(">>> Exiting Camera app") | 
|  | 49 | + # Exit the Camera app using the back button | 
|  | 50 | + self.dut.adb.shell('input keyevent 4') # Press back button | 
|  | 51 | + time.sleep(1) # Ensure the transition happens | 
|  | 52 | + | 
|  | 53 | + def teardown_class(self): | 
|  | 54 | + # This will run after all tests have finished | 
|  | 55 | + print(">>> CameraTest completed") | 
|  | 56 | + | 
|  | 57 | +if __name__ == '__main__': | 
|  | 58 | + test_runner.main() | 
0 commit comments