1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core_ai;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Test ai image class methods.
|
|
|
21 |
*
|
|
|
22 |
* @package core_ai
|
|
|
23 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
* @covers \core_ai\ai_image
|
|
|
26 |
*/
|
|
|
27 |
final class ai_image_test extends \advanced_testcase {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Test get_predominant_color.
|
|
|
31 |
*/
|
|
|
32 |
public function test_get_predominant_color(): void {
|
|
|
33 |
$x = 0;
|
|
|
34 |
$y = 180;
|
|
|
35 |
$width = 20;
|
|
|
36 |
$height = 20;
|
|
|
37 |
|
|
|
38 |
// Test for black image.
|
|
|
39 |
$imagepath = self::get_fixture_path(__NAMESPACE__, 'black.png'); // Get the test image from the fixtures file.
|
|
|
40 |
$image = new ai_image($imagepath);
|
|
|
41 |
// We're working with a private method here, so we need to use reflection.
|
|
|
42 |
$method = new \ReflectionMethod($image, 'get_predominant_color');
|
|
|
43 |
$color = $method->invoke($image, $x, $y, $width, $height);
|
|
|
44 |
|
|
|
45 |
$this->assertEquals(0, $color['red']);
|
|
|
46 |
$this->assertEquals(0, $color['green']);
|
|
|
47 |
$this->assertEquals(0, $color['blue']);
|
|
|
48 |
|
|
|
49 |
// Test for white image.
|
|
|
50 |
$imagepath = self::get_fixture_path(__NAMESPACE__, 'white.png');
|
|
|
51 |
$image = new ai_image($imagepath);
|
|
|
52 |
// We're working with a private method here, so we need to use reflection.
|
|
|
53 |
$method = new \ReflectionMethod($image, 'get_predominant_color');
|
|
|
54 |
$color = $method->invoke($image, $x, $y, $width, $height);
|
|
|
55 |
|
|
|
56 |
$this->assertEquals(255, $color['red']);
|
|
|
57 |
$this->assertEquals(255, $color['green']);
|
|
|
58 |
$this->assertEquals(255, $color['blue']);
|
|
|
59 |
|
|
|
60 |
// Test for grey image.
|
|
|
61 |
$imagepath = self::get_fixture_path(__NAMESPACE__, 'grey.png');
|
|
|
62 |
$image = new ai_image($imagepath);
|
|
|
63 |
// We're working with a private method here, so we need to use reflection.
|
|
|
64 |
$method = new \ReflectionMethod($image, 'get_predominant_color');
|
|
|
65 |
$color = $method->invoke($image, $x, $y, $width, $height);
|
|
|
66 |
|
|
|
67 |
$this->assertEquals(128, $color['red']);
|
|
|
68 |
$this->assertEquals(128, $color['green']);
|
|
|
69 |
$this->assertEquals(128, $color['blue']);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Test is_color_dark.
|
|
|
74 |
*/
|
|
|
75 |
public function test_is_color_dark(): void {
|
|
|
76 |
// Load an image as it is required for class instantiation.
|
|
|
77 |
// The color of the image is not important for this test.
|
|
|
78 |
$imagepath = self::get_fixture_path(__NAMESPACE__, 'black.png');
|
|
|
79 |
$image = new ai_image($imagepath);
|
|
|
80 |
// We're working with a private method here, so we need to use reflection.
|
|
|
81 |
$method = new \ReflectionMethod($image, 'is_color_dark');
|
|
|
82 |
$color = ['red' => 0, 'green' => 0, 'blue' => 0];
|
|
|
83 |
$result = $method->invoke($image, $color);
|
|
|
84 |
|
|
|
85 |
$this->assertTrue($result);
|
|
|
86 |
|
|
|
87 |
$image = new ai_image($imagepath);
|
|
|
88 |
// We're working with a private method here, so we need to use reflection.
|
|
|
89 |
$method = new \ReflectionMethod($image, 'is_color_dark');
|
|
|
90 |
$color = ['red' => 255, 'green' => 255, 'blue' => 255];
|
|
|
91 |
$result = $method->invoke($image, $color);
|
|
|
92 |
|
|
|
93 |
$this->assertFalse($result);
|
|
|
94 |
|
|
|
95 |
$imagepath = self::get_fixture_path(__NAMESPACE__, 'grey.png');
|
|
|
96 |
$image = new ai_image($imagepath);
|
|
|
97 |
// We're working with a private method here, so we need to use reflection.
|
|
|
98 |
$method = new \ReflectionMethod($image, 'is_color_dark');
|
|
|
99 |
$color = ['red' => 128, 'green' => 128, 'blue' => 128];
|
|
|
100 |
$result = $method->invoke($image, $color);
|
|
|
101 |
|
|
|
102 |
$this->assertTrue($result);
|
|
|
103 |
}
|
|
|
104 |
}
|