File indexing completed on 2024-05-19 05:42:06

0001 // ct_lvtgclr_colormanagement.t.cpp                                    -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtclr_colormanagement.h>
0021 
0022 #include <catch2-local-includes.h>
0023 
0024 #include <iostream>
0025 
0026 using namespace Codethink::lvtclr;
0027 
0028 // Extends ColorManagement to avoid touching 'Preferences' directly
0029 class ColorManagementForTesting : public ColorManagement {
0030   public:
0031     ColorManagementForTesting(): ColorManagement(false)
0032     {
0033     }
0034 
0035     void setColorBlindMode(bool new_value)
0036     {
0037         if (enableColorBlindMode != new_value) {
0038             enableColorBlindMode = new_value;
0039             resetCaches();
0040         }
0041     }
0042 
0043     void setColorBlindFill(bool new_value)
0044     {
0045         if (enableColorBlindFill != new_value) {
0046             enableColorBlindFill = new_value;
0047             resetCaches();
0048         }
0049     }
0050 
0051   protected:
0052     [[nodiscard]] bool isColorBlindModeActive() const override
0053     {
0054         return enableColorBlindMode;
0055     }
0056     [[nodiscard]] bool isUsingColorBlindFill() const override
0057     {
0058         return enableColorBlindFill;
0059     }
0060 
0061   private:
0062     bool enableColorBlindMode = false;
0063     bool enableColorBlindFill = false;
0064 };
0065 
0066 TEST_CASE("Get color from manager")
0067 {
0068     auto c = ColorManagementForTesting{};
0069     c.setColorBlindMode(false);
0070     auto some_color = c.getColorFor("some id");
0071 
0072     // Getting color for the same id, should return the same color
0073     REQUIRE(c.getColorFor("some id") == some_color);
0074     // But getting for a different id, should return a diferent color
0075     REQUIRE(c.getColorFor("some other id") != some_color);
0076 
0077     // If color blind mode is activated, generate a new color
0078     c.setColorBlindMode(true);
0079     REQUIRE(c.getColorFor("some id") != some_color);
0080     c.setColorBlindMode(false);
0081     REQUIRE(c.getColorFor("some id") == some_color);
0082 
0083     // User is able to override the color for a given id...
0084     auto new_color = QColor("#123456");
0085     c.setColorFor("some id", new_color);
0086     REQUIRE(c.getColorFor("some id") == new_color);
0087     // ...and this color is always preferred
0088     c.setColorBlindMode(true);
0089     REQUIRE(c.getColorFor("some id") == new_color);
0090 }
0091 
0092 TEST_CASE("Chance base color")
0093 {
0094     auto c = ColorManagementForTesting{};
0095     c.setColorBlindMode(false);
0096     auto some_color = c.getColorFor("some id");
0097 
0098     // Changing the base color should affect the automatic color generation
0099     REQUIRE(c.getColorFor("some id") == some_color);
0100     c.setBaseColor(QColor{0, 0, 0});
0101     REQUIRE(c.getColorFor("some id") != some_color);
0102 
0103     // But not the user defined color
0104     auto new_color = QColor("#123456");
0105     c.setColorFor("some id", new_color);
0106     REQUIRE(c.getColorFor("some id") == new_color);
0107     c.setBaseColor(QColor{0, 255, 0});
0108     REQUIRE(c.getColorFor("some id") == new_color);
0109 }
0110 
0111 TEST_CASE("Get fill pattern from manager")
0112 {
0113     auto c = ColorManagementForTesting{};
0114     c.setColorBlindFill(false);
0115     auto some_fill = c.fillPattern("some id");
0116 
0117     // With colorFill disabled, all ids return the same fill
0118     REQUIRE(c.fillPattern("some id") == some_fill);
0119     REQUIRE(c.fillPattern("some other id") == some_fill);
0120 
0121     // With colorFill enabled, fill patterns may be different for each id
0122     c.setColorBlindFill(true);
0123     some_fill = c.fillPattern("some id");
0124     REQUIRE(c.fillPattern("some id") == some_fill);
0125     REQUIRE(c.fillPattern("some other id") != some_fill);
0126 }