OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/gfx/color_analysis.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/skia/include/core/SkColor.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 const unsigned char k1x1White[] = { |
| 15 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, |
| 16 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, |
| 17 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 18 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, |
| 19 0xde, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, |
| 20 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, |
| 21 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, |
| 22 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, 0x01, 0x00, |
| 23 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74, |
| 24 0x49, 0x4d, 0x45, 0x07, 0xdb, 0x02, 0x11, 0x15, |
| 25 0x16, 0x1b, 0xaa, 0x58, 0x38, 0x76, 0x00, 0x00, |
| 26 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f, |
| 27 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72, |
| 28 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, |
| 29 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57, |
| 30 0x81, 0x0e, 0x17, 0x00, 0x00, 0x00, 0x0c, 0x49, |
| 31 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff, |
| 32 0xff, 0x3f, 0x00, 0x05, 0xfe, 0x02, 0xfe, 0xdc, |
| 33 0xcc, 0x59, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x49, |
| 34 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 |
| 35 }; |
| 36 |
| 37 const unsigned char k1x3BlueWhite[] = { |
| 38 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, |
| 39 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, |
| 40 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, |
| 41 0x08, 0x02, 0x00, 0x00, 0x00, 0xdd, 0xbf, 0xf2, |
| 42 0xd5, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, |
| 43 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, |
| 44 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, |
| 45 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, 0x01, 0x00, |
| 46 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74, |
| 47 0x49, 0x4d, 0x45, 0x07, 0xdb, 0x02, 0x12, 0x01, |
| 48 0x0a, 0x2c, 0xfd, 0x08, 0x64, 0x66, 0x00, 0x00, |
| 49 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f, |
| 50 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72, |
| 51 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, |
| 52 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57, |
| 53 0x81, 0x0e, 0x17, 0x00, 0x00, 0x00, 0x14, 0x49, |
| 54 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff, |
| 55 0xff, 0x3f, 0x13, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 56 0xc3, 0x7f, 0x00, 0x1e, 0xfd, 0x03, 0xff, 0xde, |
| 57 0x72, 0x58, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x49, |
| 58 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 |
| 59 }; |
| 60 |
| 61 const unsigned char k1x3BlueRed[] = { |
| 62 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, |
| 63 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, |
| 64 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, |
| 65 0x08, 0x02, 0x00, 0x00, 0x00, 0xdd, 0xbf, 0xf2, |
| 66 0xd5, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, |
| 67 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, |
| 68 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, |
| 69 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, 0x01, 0x00, |
| 70 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74, |
| 71 0x49, 0x4d, 0x45, 0x07, 0xdb, 0x02, 0x12, 0x01, |
| 72 0x07, 0x09, 0x03, 0xa2, 0xce, 0x6c, 0x00, 0x00, |
| 73 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f, |
| 74 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72, |
| 75 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, |
| 76 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57, |
| 77 0x81, 0x0e, 0x17, 0x00, 0x00, 0x00, 0x14, 0x49, |
| 78 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, |
| 79 0xc0, 0xc0, 0xc4, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
| 80 0xf0, 0x1f, 0x00, 0x0c, 0x10, 0x02, 0x01, 0x2c, |
| 81 0x8f, 0x8b, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x49, |
| 82 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 |
| 83 }; |
| 84 |
| 85 class MockKMeanImageSampler : public color_utils::KMeanImageSampler { |
| 86 public: |
| 87 MockKMeanImageSampler() : current_result_index_(0) { |
| 88 } |
| 89 |
| 90 MockKMeanImageSampler(const std::vector<int>& samples) |
| 91 : prebaked_sample_results_(samples), |
| 92 current_result_index_(0) { |
| 93 } |
| 94 |
| 95 virtual ~MockKMeanImageSampler() { |
| 96 } |
| 97 |
| 98 void AddSample(int sample) { |
| 99 prebaked_sample_results_.push_back(sample); |
| 100 } |
| 101 |
| 102 void Reset() { |
| 103 prebaked_sample_results_.clear(); |
| 104 ResetCounter(); |
| 105 } |
| 106 |
| 107 void ResetCounter() { |
| 108 current_result_index_ = 0; |
| 109 } |
| 110 |
| 111 virtual int GetSample(int width, int height) { |
| 112 if (current_result_index_ >= prebaked_sample_results_.size()) { |
| 113 current_result_index_ = 0; |
| 114 } |
| 115 |
| 116 if (prebaked_sample_results_.empty()) { |
| 117 return 0; |
| 118 } |
| 119 |
| 120 return prebaked_sample_results_[current_result_index_++]; |
| 121 } |
| 122 |
| 123 protected: |
| 124 std::vector<int> prebaked_sample_results_; |
| 125 size_t current_result_index_; |
| 126 }; |
| 127 |
| 128 } // namespace |
| 129 |
| 130 class ColorAnalysisTest : public testing::Test { |
| 131 }; |
| 132 |
| 133 TEST_F(ColorAnalysisTest, CalculatePNGKMeanAllWhite) { |
| 134 MockKMeanImageSampler test_sampler; |
| 135 test_sampler.AddSample(0); |
| 136 |
| 137 scoped_refptr<RefCountedBytes> png( |
| 138 new RefCountedBytes( |
| 139 std::vector<unsigned char>( |
| 140 k1x1White, |
| 141 k1x1White + sizeof(k1x1White) / sizeof(unsigned char)))); |
| 142 |
| 143 SkColor color = CalculateKMeanColorOfPNG(png, 100, 600, test_sampler); |
| 144 |
| 145 EXPECT_EQ(color, SK_ColorWHITE); |
| 146 } |
| 147 |
| 148 TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreWhite) { |
| 149 MockKMeanImageSampler test_sampler; |
| 150 test_sampler.AddSample(0); |
| 151 test_sampler.AddSample(1); |
| 152 test_sampler.AddSample(2); |
| 153 |
| 154 scoped_refptr<RefCountedBytes> png( |
| 155 new RefCountedBytes( |
| 156 std::vector<unsigned char>( |
| 157 k1x3BlueWhite, |
| 158 k1x3BlueWhite + sizeof(k1x3BlueWhite) / sizeof(unsigned char)))); |
| 159 |
| 160 SkColor color = CalculateKMeanColorOfPNG(png, 100, 600, test_sampler); |
| 161 |
| 162 EXPECT_EQ(color, SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF)); |
| 163 } |
| 164 |
| 165 TEST_F(ColorAnalysisTest, CalculatePNGKMeanPickMostCommon) { |
| 166 MockKMeanImageSampler test_sampler; |
| 167 test_sampler.AddSample(0); |
| 168 test_sampler.AddSample(1); |
| 169 test_sampler.AddSample(2); |
| 170 |
| 171 scoped_refptr<RefCountedBytes> png( |
| 172 new RefCountedBytes( |
| 173 std::vector<unsigned char>( |
| 174 k1x3BlueRed, |
| 175 k1x3BlueRed + sizeof(k1x3BlueRed) / sizeof(unsigned char)))); |
| 176 |
| 177 SkColor color = CalculateKMeanColorOfPNG(png, 100, 600, test_sampler); |
| 178 |
| 179 EXPECT_EQ(color, SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00)); |
| 180 } |
OLD | NEW |