| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/themes/browser_theme_pack.h" | 5 #include "chrome/browser/themes/browser_theme_pack.h" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/json/json_file_value_serializer.h" | 8 #include "base/json/json_file_value_serializer.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 // Maps image ids to maps of scale factors to file paths. | 30 // Maps image ids to maps of scale factors to file paths. |
| 31 // A similar typedef in BrowserThemePack is private. | 31 // A similar typedef in BrowserThemePack is private. |
| 32 typedef std::map<int, TestScaleFactorToFileMap> TestFilePathMap; | 32 typedef std::map<int, TestScaleFactorToFileMap> TestFilePathMap; |
| 33 | 33 |
| 34 class BrowserThemePackTest : public ::testing::Test { | 34 class BrowserThemePackTest : public ::testing::Test { |
| 35 public: | 35 public: |
| 36 BrowserThemePackTest() | 36 BrowserThemePackTest() |
| 37 : message_loop(), | 37 : message_loop(), |
| 38 fake_ui_thread(BrowserThread::UI, &message_loop), | 38 fake_ui_thread(BrowserThread::UI, &message_loop), |
| 39 fake_file_thread(BrowserThread::FILE, &message_loop) { | 39 fake_file_thread(BrowserThread::FILE, &message_loop), |
| 40 std::vector<ui::ScaleFactor> scale_factors; | 40 theme_pack_(new BrowserThemePack) { |
| 41 scale_factors.push_back(ui::SCALE_FACTOR_100P); | |
| 42 scale_factors.push_back(ui::SCALE_FACTOR_200P); | |
| 43 scoped_set_supported_scale_factors_.reset( | |
| 44 new ui::test::ScopedSetSupportedScaleFactors(scale_factors)); | |
| 45 theme_pack_ = new BrowserThemePack(); | |
| 46 } | 41 } |
| 47 | 42 |
| 48 // Transformation for link underline colors. | 43 // Transformation for link underline colors. |
| 49 SkColor BuildThirdOpacity(SkColor color_link) { | 44 SkColor BuildThirdOpacity(SkColor color_link) { |
| 50 return SkColorSetA(color_link, SkColorGetA(color_link) / 3); | 45 return SkColorSetA(color_link, SkColorGetA(color_link) / 3); |
| 51 } | 46 } |
| 52 | 47 |
| 53 void GenerateDefaultFrameColor(std::map<int, SkColor>* colors, | 48 void GenerateDefaultFrameColor(std::map<int, SkColor>* colors, |
| 54 int color, int tint) { | 49 int color, int tint) { |
| 55 (*colors)[color] = HSLShift( | 50 (*colors)[color] = HSLShift( |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 263 |
| 269 // Compare some known pixel colors at know locations for a theme | 264 // Compare some known pixel colors at know locations for a theme |
| 270 // image where two different PNG files were specified for scales 100% | 265 // image where two different PNG files were specified for scales 100% |
| 271 // and 200% respectively. | 266 // and 200% respectively. |
| 272 int idr = IDR_THEME_FRAME; | 267 int idr = IDR_THEME_FRAME; |
| 273 gfx::Image image = pack->GetImageNamed(idr); | 268 gfx::Image image = pack->GetImageNamed(idr); |
| 274 EXPECT_FALSE(image.IsEmpty()); | 269 EXPECT_FALSE(image.IsEmpty()); |
| 275 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 270 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
| 276 ASSERT_TRUE(image_skia); | 271 ASSERT_TRUE(image_skia); |
| 277 // Scale 100%. | 272 // Scale 100%. |
| 278 const gfx::ImageSkiaRep& rep1 = image_skia->GetRepresentation(1.0f); | 273 const gfx::ImageSkiaRep& rep1 = image_skia->GetRepresentation( |
| 274 ui::SCALE_FACTOR_100P); |
| 279 ASSERT_FALSE(rep1.is_null()); | 275 ASSERT_FALSE(rep1.is_null()); |
| 280 EXPECT_EQ(80, rep1.sk_bitmap().width()); | 276 EXPECT_EQ(80, rep1.sk_bitmap().width()); |
| 281 EXPECT_EQ(80, rep1.sk_bitmap().height()); | 277 EXPECT_EQ(80, rep1.sk_bitmap().height()); |
| 282 rep1.sk_bitmap().lockPixels(); | 278 rep1.sk_bitmap().lockPixels(); |
| 283 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor( 4, 4)); | 279 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor( 4, 4)); |
| 284 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor( 8, 8)); | 280 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor( 8, 8)); |
| 285 EXPECT_EQ(SkColorSetRGB( 0, 241, 237), rep1.sk_bitmap().getColor(16, 16)); | 281 EXPECT_EQ(SkColorSetRGB( 0, 241, 237), rep1.sk_bitmap().getColor(16, 16)); |
| 286 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor(24, 24)); | 282 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor(24, 24)); |
| 287 EXPECT_EQ(SkColorSetRGB( 0, 241, 237), rep1.sk_bitmap().getColor(32, 32)); | 283 EXPECT_EQ(SkColorSetRGB( 0, 241, 237), rep1.sk_bitmap().getColor(32, 32)); |
| 288 rep1.sk_bitmap().unlockPixels(); | 284 rep1.sk_bitmap().unlockPixels(); |
| 289 // Scale 200%. | 285 // Scale 200%. |
| 290 const gfx::ImageSkiaRep& rep2 = image_skia->GetRepresentation(2.0f); | 286 const gfx::ImageSkiaRep& rep2 = image_skia->GetRepresentation( |
| 287 ui::SCALE_FACTOR_200P); |
| 291 ASSERT_FALSE(rep2.is_null()); | 288 ASSERT_FALSE(rep2.is_null()); |
| 292 EXPECT_EQ(160, rep2.sk_bitmap().width()); | 289 EXPECT_EQ(160, rep2.sk_bitmap().width()); |
| 293 EXPECT_EQ(160, rep2.sk_bitmap().height()); | 290 EXPECT_EQ(160, rep2.sk_bitmap().height()); |
| 294 rep2.sk_bitmap().lockPixels(); | 291 rep2.sk_bitmap().lockPixels(); |
| 295 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep2.sk_bitmap().getColor( 4, 4)); | 292 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep2.sk_bitmap().getColor( 4, 4)); |
| 296 EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor( 8, 8)); | 293 EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor( 8, 8)); |
| 297 EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(16, 16)); | 294 EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(16, 16)); |
| 298 EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(24, 24)); | 295 EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(24, 24)); |
| 299 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep2.sk_bitmap().getColor(32, 32)); | 296 EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep2.sk_bitmap().getColor(32, 32)); |
| 300 rep2.sk_bitmap().unlockPixels(); | 297 rep2.sk_bitmap().unlockPixels(); |
| 301 | 298 |
| 302 // TODO(sschmitz): I plan to remove the following (to the end of the fct) | 299 // TODO(sschmitz): I plan to remove the following (to the end of the fct) |
| 303 // Reason: this test may be brittle. It depends on details of how we scale | 300 // Reason: this test may be brittle. It depends on details of how we scale |
| 304 // an 100% image to an 200% image. If there's filtering etc, this test would | 301 // an 100% image to an 200% image. If there's filtering etc, this test would |
| 305 // break. Also High DPI is new, but scaling from 100% to 200% is not new | 302 // break. Also High DPI is new, but scaling from 100% to 200% is not new |
| 306 // and need not be tested here. But in the interrim it is useful to verify | 303 // and need not be tested here. But in the interrim it is useful to verify |
| 307 // that this image was scaled and did not appear in the input. | 304 // that this image was scaled and did not appear in the input. |
| 308 | 305 |
| 309 // Compare pixel colors and locations for a theme image that had | 306 // Compare pixel colors and locations for a theme image that had |
| 310 // only one PNG file specified (for scale 100%). The representation | 307 // only one PNG file specified (for scale 100%). The representation |
| 311 // for scale of 200% was computed. | 308 // for scale of 200% was computed. |
| 312 idr = IDR_THEME_FRAME_INCOGNITO_INACTIVE; | 309 idr = IDR_THEME_FRAME_INCOGNITO_INACTIVE; |
| 313 image = pack->GetImageNamed(idr); | 310 image = pack->GetImageNamed(idr); |
| 314 EXPECT_FALSE(image.IsEmpty()); | 311 EXPECT_FALSE(image.IsEmpty()); |
| 315 image_skia = image.ToImageSkia(); | 312 image_skia = image.ToImageSkia(); |
| 316 ASSERT_TRUE(image_skia); | 313 ASSERT_TRUE(image_skia); |
| 317 // Scale 100%. | 314 // Scale 100%. |
| 318 const gfx::ImageSkiaRep& rep3 = image_skia->GetRepresentation(1.0f); | 315 const gfx::ImageSkiaRep& rep3 = image_skia->GetRepresentation( |
| 316 ui::SCALE_FACTOR_100P); |
| 319 ASSERT_FALSE(rep3.is_null()); | 317 ASSERT_FALSE(rep3.is_null()); |
| 320 EXPECT_EQ(80, rep3.sk_bitmap().width()); | 318 EXPECT_EQ(80, rep3.sk_bitmap().width()); |
| 321 EXPECT_EQ(80, rep3.sk_bitmap().height()); | 319 EXPECT_EQ(80, rep3.sk_bitmap().height()); |
| 322 rep3.sk_bitmap().lockPixels(); | 320 rep3.sk_bitmap().lockPixels(); |
| 323 // We take samples of colors and locations along the diagonal whenever | 321 // We take samples of colors and locations along the diagonal whenever |
| 324 // the color changes. Note these colors are slightly different from | 322 // the color changes. Note these colors are slightly different from |
| 325 // the input PNG file due to input processing. | 323 // the input PNG file due to input processing. |
| 326 std::vector<std::pair<int, SkColor> > normal; | 324 std::vector<std::pair<int, SkColor> > normal; |
| 327 int xy = 0; | 325 int xy = 0; |
| 328 SkColor color = rep3.sk_bitmap().getColor(xy, xy); | 326 SkColor color = rep3.sk_bitmap().getColor(xy, xy); |
| 329 normal.push_back(std::make_pair(xy, color)); | 327 normal.push_back(std::make_pair(xy, color)); |
| 330 for (int xy = 0; xy < 40; ++xy) { | 328 for (int xy = 0; xy < 40; ++xy) { |
| 331 SkColor next_color = rep3.sk_bitmap().getColor(xy, xy); | 329 SkColor next_color = rep3.sk_bitmap().getColor(xy, xy); |
| 332 if (next_color != color) { | 330 if (next_color != color) { |
| 333 color = next_color; | 331 color = next_color; |
| 334 normal.push_back(std::make_pair(xy, color)); | 332 normal.push_back(std::make_pair(xy, color)); |
| 335 } | 333 } |
| 336 } | 334 } |
| 337 EXPECT_EQ(static_cast<size_t>(9), normal.size()); | 335 EXPECT_EQ(static_cast<size_t>(9), normal.size()); |
| 338 rep3.sk_bitmap().unlockPixels(); | 336 rep3.sk_bitmap().unlockPixels(); |
| 339 // Scale 200%. | 337 // Scale 200%. |
| 340 const gfx::ImageSkiaRep& rep4 = image_skia->GetRepresentation(2.0f); | 338 const gfx::ImageSkiaRep& rep4 = image_skia->GetRepresentation( |
| 339 ui::SCALE_FACTOR_200P); |
| 341 ASSERT_FALSE(rep4.is_null()); | 340 ASSERT_FALSE(rep4.is_null()); |
| 342 EXPECT_EQ(160, rep4.sk_bitmap().width()); | 341 EXPECT_EQ(160, rep4.sk_bitmap().width()); |
| 343 EXPECT_EQ(160, rep4.sk_bitmap().height()); | 342 EXPECT_EQ(160, rep4.sk_bitmap().height()); |
| 344 rep4.sk_bitmap().lockPixels(); | 343 rep4.sk_bitmap().lockPixels(); |
| 345 // We expect the same colors and at locations scaled by 2 | 344 // We expect the same colors and at locations scaled by 2 |
| 346 // since this bitmap was scaled by 2. | 345 // since this bitmap was scaled by 2. |
| 347 for (size_t i = 0; i < normal.size(); ++i) { | 346 for (size_t i = 0; i < normal.size(); ++i) { |
| 348 int xy = 2 * normal[i].first; | 347 int xy = 2 * normal[i].first; |
| 349 SkColor color = normal[i].second; | 348 SkColor color = normal[i].second; |
| 350 EXPECT_EQ(color, rep4.sk_bitmap().getColor(xy, xy)); | 349 EXPECT_EQ(color, rep4.sk_bitmap().getColor(xy, xy)); |
| 351 } | 350 } |
| 352 rep4.sk_bitmap().unlockPixels(); | 351 rep4.sk_bitmap().unlockPixels(); |
| 353 } | 352 } |
| 354 | 353 |
| 355 base::MessageLoop message_loop; | 354 base::MessageLoop message_loop; |
| 356 content::TestBrowserThread fake_ui_thread; | 355 content::TestBrowserThread fake_ui_thread; |
| 357 content::TestBrowserThread fake_file_thread; | 356 content::TestBrowserThread fake_file_thread; |
| 358 | 357 |
| 359 typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors> | |
| 360 ScopedSetSupportedScaleFactors; | |
| 361 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_; | |
| 362 scoped_refptr<BrowserThemePack> theme_pack_; | 358 scoped_refptr<BrowserThemePack> theme_pack_; |
| 363 }; | 359 }; |
| 364 | 360 |
| 365 | 361 |
| 366 TEST_F(BrowserThemePackTest, DeriveUnderlineLinkColor) { | 362 TEST_F(BrowserThemePackTest, DeriveUnderlineLinkColor) { |
| 367 // If we specify a link color, but don't specify the underline color, the | 363 // If we specify a link color, but don't specify the underline color, the |
| 368 // theme provider should create one. | 364 // theme provider should create one. |
| 369 std::string color_json = "{ \"ntp_link\": [128, 128, 128]," | 365 std::string color_json = "{ \"ntp_link\": [128, 128, 128]," |
| 370 " \"ntp_section_link\": [128, 128, 128] }"; | 366 " \"ntp_section_link\": [128, 128, 128] }"; |
| 371 LoadColorJSON(color_json); | 367 LoadColorJSON(color_json); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 { | 577 { |
| 582 scoped_refptr<BrowserThemePack> pack = | 578 scoped_refptr<BrowserThemePack> pack = |
| 583 BrowserThemePack::BuildFromDataPack( | 579 BrowserThemePack::BuildFromDataPack( |
| 584 file, "mblmlcbknbnfebdfjnolmcapmdofhmme"); | 580 file, "mblmlcbknbnfebdfjnolmcapmdofhmme"); |
| 585 ASSERT_TRUE(pack.get()); | 581 ASSERT_TRUE(pack.get()); |
| 586 VerifyStarGazing(pack.get()); | 582 VerifyStarGazing(pack.get()); |
| 587 } | 583 } |
| 588 } | 584 } |
| 589 | 585 |
| 590 TEST_F(BrowserThemePackTest, HiDpiThemeTest) { | 586 TEST_F(BrowserThemePackTest, HiDpiThemeTest) { |
| 587 std::vector<ui::ScaleFactor> scale_factors; |
| 588 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
| 589 scale_factors.push_back(ui::SCALE_FACTOR_200P); |
| 590 ui::test::ScopedSetSupportedScaleFactors test_scale_factors(scale_factors); |
| 591 base::ScopedTempDir dir; | 591 base::ScopedTempDir dir; |
| 592 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 592 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 593 base::FilePath file = dir.path().AppendASCII("theme_data.pak"); | 593 base::FilePath file = dir.path().AppendASCII("theme_data.pak"); |
| 594 | 594 |
| 595 // Part 1: Build the pack from an extension. | 595 // Part 1: Build the pack from an extension. |
| 596 { | 596 { |
| 597 base::FilePath hidpi_path = GetHiDpiThemePath(); | 597 base::FilePath hidpi_path = GetHiDpiThemePath(); |
| 598 scoped_refptr<BrowserThemePack> pack; | 598 scoped_refptr<BrowserThemePack> pack; |
| 599 BuildFromUnpackedExtension(hidpi_path, pack); | 599 BuildFromUnpackedExtension(hidpi_path, pack); |
| 600 ASSERT_TRUE(pack->WriteToDisk(file)); | 600 ASSERT_TRUE(pack->WriteToDisk(file)); |
| 601 VerifyHiDpiTheme(pack.get()); | 601 VerifyHiDpiTheme(pack.get()); |
| 602 } | 602 } |
| 603 | 603 |
| 604 // Part 2: Try to read back the data pack that we just wrote to disk. | 604 // Part 2: Try to read back the data pack that we just wrote to disk. |
| 605 { | 605 { |
| 606 scoped_refptr<BrowserThemePack> pack = | 606 scoped_refptr<BrowserThemePack> pack = |
| 607 BrowserThemePack::BuildFromDataPack(file, "gllekhaobjnhgeag"); | 607 BrowserThemePack::BuildFromDataPack(file, "gllekhaobjnhgeag"); |
| 608 ASSERT_TRUE(pack.get()); | 608 ASSERT_TRUE(pack.get()); |
| 609 VerifyHiDpiTheme(pack.get()); | 609 VerifyHiDpiTheme(pack.get()); |
| 610 } | 610 } |
| 611 } | 611 } |
| OLD | NEW |