diff --git a/providers/base/data/images/image_quality_bad.jpg b/providers/base/data/images/image_quality_bad.jpg new file mode 100644 index 0000000000..dea1b8d5d7 Binary files /dev/null and b/providers/base/data/images/image_quality_bad.jpg differ diff --git a/providers/base/data/images/image_quality_good.jpg b/providers/base/data/images/image_quality_good.jpg new file mode 100644 index 0000000000..443f37e73a Binary files /dev/null and b/providers/base/data/images/image_quality_good.jpg differ diff --git a/providers/base/tests/test_camera_quality.py b/providers/base/tests/test_camera_quality.py new file mode 100644 index 0000000000..1acdb41991 --- /dev/null +++ b/providers/base/tests/test_camera_quality.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 +# +# This file is part of Checkbox. +# +# Copyright 2023 Canonical Ltd. +# Written by: +# Fernando Bravo +# +# Checkbox is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# Checkbox is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Checkbox. If not, see . +# + +import os +import unittest +from unittest.mock import patch + +import cv2 + +from bin.camera_quality_test import brisque + +default_dir = os.path.join(os.path.dirname(__file__), '../data') +data_dir = os.getenv('PLAINBOX_PROVIDER_DATA', default=default_dir) + + +class CameraQualityTests(unittest.TestCase): + """This class provides test cases for the camera_quality_test module.""" + + # Setup the patch for all the tests + def setUp(self): + self.patcher = patch('cv2.VideoCapture') + self.mock_capture = self.patcher.start() + + def tearDown(self): + self.patcher.stop() + + def test_device_not_opened(self): + """ + The test should fail if the camera device is not opened. + """ + + # Set the mock + self.mock_capture.return_value.isOpened.return_value = False + + assert brisque() == 1 + + def test_grab_not_available(self): + """ + The test should fail if the camera device can't grab an image. + """ + + # Set the mock + self.mock_capture.return_value.isOpened.return_value = True + self.mock_capture.return_value.grab.return_value = False + + assert brisque() == 1 + + def test_image_not_read(self): + """ + The test should fail if the camera device can't read the image. + """ + + # Set the mock + self.mock_capture.return_value.isOpened.return_value = True + self.mock_capture.return_value.grab.return_value = True + self.mock_capture.return_value.read.return_value = (False, None) + + assert brisque() == 1 + + def test_save_image(self): + """ + Check if the test can save the image. + """ + + # Set the mock + img_path = os.path.join(data_dir, "images/image_quality_good.jpg") + img = cv2.imread(img_path) + self.mock_capture.return_value.isOpened.return_value = True + self.mock_capture.return_value.grab.return_value = True + self.mock_capture.return_value.read.return_value = (True, img) + + assert brisque(save=True) == 0 + + def test_good_image(self): + """ + Check if the test passes with a valid image. + """ + + # Set the mock + img_path = os.path.join(data_dir, "images/image_quality_good.jpg") + img = cv2.imread(img_path) + self.mock_capture.return_value.isOpened.return_value = True + self.mock_capture.return_value.grab.return_value = True + self.mock_capture.return_value.read.return_value = (True, img) + + assert brisque() == 0 + + def test_bad_image(self): + """ + Check if the test does not pass with a bad image. + """ + + # Set the mock + img_path = os.path.join(data_dir, "images/image_quality_bad.jpg") + img = cv2.imread(img_path) + self.mock_capture.return_value.isOpened.return_value = True + self.mock_capture.return_value.grab.return_value = True + self.mock_capture.return_value.read.return_value = (True, img) + + assert brisque() == 1 + + @patch('checkbox_support.vendor.brisque.brisque.BRISQUE.score') + def test_invalid_image(self, mock_score): + """ + Check if the test passes with a valid image. + """ + + # Set the mock + img_path = os.path.join(data_dir, "images/image_quality_good.jpg") + img = cv2.imread(img_path) + self.mock_capture.return_value.isOpened.return_value = True + self.mock_capture.return_value.grab.return_value = True + self.mock_capture.return_value.read.return_value = (True, img) + mock_score.return_value = float('nan') + + assert brisque() == 1