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.h" | 10 #include "base/message_loop.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 } | 182 } |
183 | 183 |
184 base::FilePath GetHiDpiThemePath() { | 184 base::FilePath GetHiDpiThemePath() { |
185 base::FilePath test_path; | 185 base::FilePath test_path; |
186 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_path)) { | 186 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_path)) { |
187 NOTREACHED(); | 187 NOTREACHED(); |
188 return test_path; | 188 return test_path; |
189 } | 189 } |
190 test_path = test_path.AppendASCII("extensions"); | 190 test_path = test_path.AppendASCII("extensions"); |
191 test_path = test_path.AppendASCII("theme_hidpi"); | 191 test_path = test_path.AppendASCII("theme_hidpi"); |
192 return base::FilePath(test_path); | 192 return test_path; |
193 } | 193 } |
194 | 194 |
195 // Verifies the data in star gazing. We do this multiple times for different | 195 // Verifies the data in star gazing. We do this multiple times for different |
196 // BrowserThemePack objects to make sure it works in generated and mmapped | 196 // BrowserThemePack objects to make sure it works in generated and mmapped |
197 // mode correctly. | 197 // mode correctly. |
198 void VerifyStarGazing(BrowserThemePack* pack) { | 198 void VerifyStarGazing(BrowserThemePack* pack) { |
199 // First check that values we know exist, exist. | 199 // First check that values we know exist, exist. |
200 SkColor color; | 200 SkColor color; |
201 EXPECT_TRUE(pack->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT, | 201 EXPECT_TRUE(pack->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT, |
202 &color)); | 202 &color)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 EXPECT_FALSE(pack->GetTint(ThemeProperties::TINT_FRAME, &actual)); | 240 EXPECT_FALSE(pack->GetTint(ThemeProperties::TINT_FRAME, &actual)); |
241 } | 241 } |
242 | 242 |
243 void VerifyHiDpiTheme(BrowserThemePack* pack) { | 243 void VerifyHiDpiTheme(BrowserThemePack* pack) { |
244 // The high DPI theme defines the following images: | 244 // The high DPI theme defines the following images: |
245 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME)); | 245 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME)); |
246 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INACTIVE)); | 246 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INACTIVE)); |
247 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO)); | 247 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO)); |
248 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO_INACTIVE)); | 248 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO_INACTIVE)); |
249 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TOOLBAR)); | 249 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TOOLBAR)); |
| 250 // TODO(sschmitz): uncomment next two lines when ntp test data available. |
| 251 // EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_NTP_BACKGROUND)); |
| 252 // EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION)); |
250 | 253 |
251 // The high DPI theme does not define the following images: | 254 // The high DPI theme does not define the following images: |
252 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND)); | 255 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND)); |
253 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND)); | 256 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND)); |
254 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_INCOGNITO)); | 257 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_INCOGNITO)); |
255 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_V)); | 258 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_V)); |
256 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_NTP_BACKGROUND)); | |
257 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY)); | 259 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY)); |
258 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY_INACTIVE)); | 260 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY_INACTIVE)); |
259 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND)); | 261 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND)); |
260 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION)); | |
261 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_WINDOW_CONTROL_BACKGROUND)); | 262 EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_WINDOW_CONTROL_BACKGROUND)); |
262 | 263 |
263 // Compare some known pixel colors at know locations for a theme | 264 // Compare some known pixel colors at know locations for a theme |
264 // image where two different PNG files were specified for scales 100% | 265 // image where two different PNG files were specified for scales 100% |
265 // and 200% respectively. | 266 // and 200% respectively. |
266 int idr = IDR_THEME_FRAME; | 267 int idr = IDR_THEME_FRAME; |
267 gfx::Image image = pack->GetImageNamed(idr); | 268 gfx::Image image = pack->GetImageNamed(idr); |
268 EXPECT_FALSE(image.IsEmpty()); | 269 EXPECT_FALSE(image.IsEmpty()); |
269 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 270 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
270 ASSERT_TRUE(image_skia); | 271 ASSERT_TRUE(image_skia); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // We expect the same colors and at locations scaled by 2 | 344 // We expect the same colors and at locations scaled by 2 |
344 // since this bitmap was scaled by 2. | 345 // since this bitmap was scaled by 2. |
345 for (size_t i = 0; i < normal.size(); ++i) { | 346 for (size_t i = 0; i < normal.size(); ++i) { |
346 int xy = 2 * normal[i].first; | 347 int xy = 2 * normal[i].first; |
347 SkColor color = normal[i].second; | 348 SkColor color = normal[i].second; |
348 EXPECT_EQ(color, rep4.sk_bitmap().getColor(xy, xy)); | 349 EXPECT_EQ(color, rep4.sk_bitmap().getColor(xy, xy)); |
349 } | 350 } |
350 rep4.sk_bitmap().unlockPixels(); | 351 rep4.sk_bitmap().unlockPixels(); |
351 } | 352 } |
352 | 353 |
| 354 void VerifyHiDpiNtpTheme(BrowserThemePack* pack) { |
| 355 // The theme extension for this test uses the same image several times |
| 356 // in the input (manifest.json). |
| 357 // This (raw png) image has size 120x120. It has a black square |
| 358 // starting at (6,6). |
| 359 // For the NTP background this image is specified for scales: 100, 140 |
| 360 // and 200 percent. |
| 361 // For the NTP attribution this image is specified only for scale: 150 |
| 362 // percent. |
| 363 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_NTP_BACKGROUND)); |
| 364 EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION)); |
| 365 |
| 366 // The background image has the same png input for 100%, 140%, 200%. |
| 367 int idr = IDR_THEME_NTP_BACKGROUND; |
| 368 gfx::Image image = pack->GetImageNamed(idr); |
| 369 EXPECT_FALSE(image.IsEmpty()); |
| 370 EXPECT_EQ(120, image.Size().width()); |
| 371 EXPECT_EQ(120, image.Size().height()); |
| 372 |
| 373 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
| 374 ASSERT_TRUE(image_skia); |
| 375 |
| 376 // 100% was in input. |
| 377 const gfx::ImageSkiaRep& rep100 = image_skia->GetRepresentation( |
| 378 ui::SCALE_FACTOR_100P); |
| 379 ASSERT_FALSE(rep100.is_null()); |
| 380 EXPECT_EQ(120, rep100.sk_bitmap().width()); |
| 381 EXPECT_EQ(120, rep100.sk_bitmap().height()); |
| 382 rep100.sk_bitmap().lockPixels(); |
| 383 EXPECT_EQ(SK_ColorWHITE, rep100.sk_bitmap().getColor(5, 5)); |
| 384 EXPECT_EQ(SK_ColorBLACK, rep100.sk_bitmap().getColor(6, 6)); |
| 385 rep100.sk_bitmap().unlockPixels(); |
| 386 |
| 387 // 133% not in input, was scaled from 200%. |
| 388 const gfx::ImageSkiaRep& rep133 = image_skia->GetRepresentation( |
| 389 ui::SCALE_FACTOR_133P); |
| 390 ASSERT_FALSE(rep133.is_null()); |
| 391 EXPECT_EQ(80, rep133.sk_bitmap().width()); |
| 392 EXPECT_EQ(80, rep133.sk_bitmap().height()); |
| 393 rep133.sk_bitmap().lockPixels(); |
| 394 EXPECT_EQ(SK_ColorWHITE, rep133.sk_bitmap().getColor(3, 3)); |
| 395 EXPECT_EQ(SK_ColorBLACK, rep133.sk_bitmap().getColor(4, 4)); |
| 396 rep133.sk_bitmap().unlockPixels(); |
| 397 |
| 398 // 140% was in input. |
| 399 const gfx::ImageSkiaRep& rep140 = image_skia->GetRepresentation( |
| 400 ui::SCALE_FACTOR_140P); |
| 401 ASSERT_FALSE(rep140.is_null()); |
| 402 EXPECT_EQ(120, rep140.sk_bitmap().width()); |
| 403 EXPECT_EQ(120, rep140.sk_bitmap().height()); |
| 404 rep140.sk_bitmap().lockPixels(); |
| 405 EXPECT_EQ(SK_ColorWHITE, rep140.sk_bitmap().getColor(5, 5)); |
| 406 EXPECT_EQ(SK_ColorBLACK, rep140.sk_bitmap().getColor(6, 6)); |
| 407 rep140.sk_bitmap().unlockPixels(); |
| 408 |
| 409 // 150% not in input, was scaled from 200%. |
| 410 const gfx::ImageSkiaRep& rep150 = image_skia->GetRepresentation( |
| 411 ui::SCALE_FACTOR_150P); |
| 412 ASSERT_FALSE(rep150.is_null()); |
| 413 EXPECT_EQ(90, rep150.sk_bitmap().width()); |
| 414 EXPECT_EQ(90, rep150.sk_bitmap().height()); |
| 415 rep150.sk_bitmap().lockPixels(); |
| 416 EXPECT_EQ(SK_ColorWHITE, rep150.sk_bitmap().getColor(4, 4)); |
| 417 EXPECT_EQ(SK_ColorBLACK, rep150.sk_bitmap().getColor(5, 5)); |
| 418 rep150.sk_bitmap().unlockPixels(); |
| 419 |
| 420 // 180% not in input, was scaled from 200%. |
| 421 const gfx::ImageSkiaRep& rep180 = image_skia->GetRepresentation( |
| 422 ui::SCALE_FACTOR_180P); |
| 423 ASSERT_FALSE(rep180.is_null()); |
| 424 EXPECT_EQ(108, rep180.sk_bitmap().width()); |
| 425 EXPECT_EQ(108, rep180.sk_bitmap().height()); |
| 426 rep180.sk_bitmap().lockPixels(); |
| 427 EXPECT_EQ(SK_ColorWHITE, rep180.sk_bitmap().getColor(4, 4)); |
| 428 EXPECT_EQ(SK_ColorBLACK, rep180.sk_bitmap().getColor(5, 5)); |
| 429 rep180.sk_bitmap().unlockPixels(); |
| 430 |
| 431 // 200% was in input. |
| 432 const gfx::ImageSkiaRep& rep200 = image_skia->GetRepresentation( |
| 433 ui::SCALE_FACTOR_200P); |
| 434 ASSERT_FALSE(rep200.is_null()); |
| 435 EXPECT_EQ(120, rep200.sk_bitmap().width()); |
| 436 EXPECT_EQ(120, rep200.sk_bitmap().height()); |
| 437 rep200.sk_bitmap().lockPixels(); |
| 438 EXPECT_EQ(SK_ColorWHITE, rep200.sk_bitmap().getColor(5, 5)); |
| 439 EXPECT_EQ(SK_ColorBLACK, rep200.sk_bitmap().getColor(6, 6)); |
| 440 rep200.sk_bitmap().unlockPixels(); |
| 441 |
| 442 // Now process the image for NTP attribution. |
| 443 // The attribution image has only an input for 150%. |
| 444 |
| 445 idr = IDR_THEME_NTP_ATTRIBUTION; |
| 446 image = pack->GetImageNamed(idr); |
| 447 EXPECT_FALSE(image.IsEmpty()); |
| 448 EXPECT_EQ(80, image.Size().width()); |
| 449 EXPECT_EQ(80, image.Size().height()); |
| 450 |
| 451 image_skia = image.ToImageSkia(); |
| 452 ASSERT_TRUE(image_skia); |
| 453 |
| 454 // 100% was not in input, scaled from 150 |
| 455 const gfx::ImageSkiaRep& rpr100 = image_skia->GetRepresentation( |
| 456 ui::SCALE_FACTOR_100P); |
| 457 ASSERT_FALSE(rpr100.is_null()); |
| 458 EXPECT_EQ(80, rpr100.sk_bitmap().width()); |
| 459 EXPECT_EQ(80, rpr100.sk_bitmap().height()); |
| 460 rpr100.sk_bitmap().lockPixels(); |
| 461 EXPECT_EQ(SK_ColorWHITE, rpr100.sk_bitmap().getColor(3, 3)); |
| 462 EXPECT_EQ(SK_ColorBLACK, rpr100.sk_bitmap().getColor(4, 4)); |
| 463 rpr100.sk_bitmap().unlockPixels(); |
| 464 |
| 465 // 150% was in input |
| 466 const gfx::ImageSkiaRep& rpr150 = image_skia->GetRepresentation( |
| 467 ui::SCALE_FACTOR_150P); |
| 468 ASSERT_FALSE(rpr150.is_null()); |
| 469 EXPECT_EQ(120, rpr150.sk_bitmap().width()); |
| 470 EXPECT_EQ(120, rpr150.sk_bitmap().height()); |
| 471 rpr150.sk_bitmap().lockPixels(); |
| 472 EXPECT_EQ(SK_ColorWHITE, rpr150.sk_bitmap().getColor(5, 5)); |
| 473 EXPECT_EQ(SK_ColorBLACK, rpr150.sk_bitmap().getColor(6, 6)); |
| 474 rpr150.sk_bitmap().unlockPixels(); |
| 475 |
| 476 // 200% was not in input, scaled from 150 |
| 477 const gfx::ImageSkiaRep& rpr200 = image_skia->GetRepresentation( |
| 478 ui::SCALE_FACTOR_200P); |
| 479 ASSERT_FALSE(rpr200.is_null()); |
| 480 EXPECT_EQ(160, rpr200.sk_bitmap().width()); |
| 481 EXPECT_EQ(160, rpr200.sk_bitmap().height()); |
| 482 rpr200.sk_bitmap().lockPixels(); |
| 483 EXPECT_EQ(SK_ColorWHITE, rpr200.sk_bitmap().getColor(7, 7)); |
| 484 EXPECT_EQ(SK_ColorBLACK, rpr200.sk_bitmap().getColor(8, 8)); |
| 485 rpr200.sk_bitmap().unlockPixels(); |
| 486 } |
| 487 |
353 base::MessageLoop message_loop; | 488 base::MessageLoop message_loop; |
354 content::TestBrowserThread fake_ui_thread; | 489 content::TestBrowserThread fake_ui_thread; |
355 content::TestBrowserThread fake_file_thread; | 490 content::TestBrowserThread fake_file_thread; |
356 | 491 |
357 scoped_refptr<BrowserThemePack> theme_pack_; | 492 scoped_refptr<BrowserThemePack> theme_pack_; |
358 }; | 493 }; |
359 | 494 |
360 | 495 |
361 TEST_F(BrowserThemePackTest, DeriveUnderlineLinkColor) { | 496 TEST_F(BrowserThemePackTest, DeriveUnderlineLinkColor) { |
362 // If we specify a link color, but don't specify the underline color, the | 497 // If we specify a link color, but don't specify the underline color, the |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 BrowserThemePack::BuildFromDataPack( | 713 BrowserThemePack::BuildFromDataPack( |
579 file, "mblmlcbknbnfebdfjnolmcapmdofhmme"); | 714 file, "mblmlcbknbnfebdfjnolmcapmdofhmme"); |
580 ASSERT_TRUE(pack.get()); | 715 ASSERT_TRUE(pack.get()); |
581 VerifyStarGazing(pack.get()); | 716 VerifyStarGazing(pack.get()); |
582 } | 717 } |
583 } | 718 } |
584 | 719 |
585 TEST_F(BrowserThemePackTest, HiDpiThemeTest) { | 720 TEST_F(BrowserThemePackTest, HiDpiThemeTest) { |
586 std::vector<ui::ScaleFactor> scale_factors; | 721 std::vector<ui::ScaleFactor> scale_factors; |
587 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 722 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
| 723 scale_factors.push_back(ui::SCALE_FACTOR_133P); |
| 724 scale_factors.push_back(ui::SCALE_FACTOR_140P); |
| 725 scale_factors.push_back(ui::SCALE_FACTOR_150P); |
| 726 scale_factors.push_back(ui::SCALE_FACTOR_180P); |
588 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 727 scale_factors.push_back(ui::SCALE_FACTOR_200P); |
589 ui::test::ScopedSetSupportedScaleFactors test_scale_factors(scale_factors); | 728 ui::test::ScopedSetSupportedScaleFactors test_scale_factors(scale_factors); |
590 base::ScopedTempDir dir; | 729 base::ScopedTempDir dir; |
591 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 730 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
592 base::FilePath file = dir.path().AppendASCII("theme_data.pak"); | 731 base::FilePath file = dir.path().AppendASCII("theme_data.pak"); |
593 | 732 |
594 // Part 1: Build the pack from an extension. | 733 // Part 1: Build the pack from an extension. |
595 { | 734 { |
596 base::FilePath hidpi_path = GetHiDpiThemePath(); | 735 base::FilePath hidpi_path = GetHiDpiThemePath(); |
597 scoped_refptr<BrowserThemePack> pack; | 736 scoped_refptr<BrowserThemePack> pack; |
598 BuildFromUnpackedExtension(hidpi_path, pack); | 737 BuildFromUnpackedExtension(hidpi_path, pack); |
599 ASSERT_TRUE(pack->WriteToDisk(file)); | 738 ASSERT_TRUE(pack->WriteToDisk(file)); |
600 VerifyHiDpiTheme(pack.get()); | 739 VerifyHiDpiTheme(pack.get()); |
| 740 // TODO(sschmitz): uncomment the next line when test data available. |
| 741 // VerifyHiDpiNtpTheme(pack.get()); |
601 } | 742 } |
602 | 743 |
603 // Part 2: Try to read back the data pack that we just wrote to disk. | 744 // Part 2: Try to read back the data pack that we just wrote to disk. |
604 { | 745 { |
605 scoped_refptr<BrowserThemePack> pack = | 746 scoped_refptr<BrowserThemePack> pack = |
606 BrowserThemePack::BuildFromDataPack(file, "gllekhaobjnhgeag"); | 747 BrowserThemePack::BuildFromDataPack(file, "gllekhaobjnhgeag"); |
607 ASSERT_TRUE(pack.get()); | 748 ASSERT_TRUE(pack.get()); |
608 VerifyHiDpiTheme(pack.get()); | 749 VerifyHiDpiTheme(pack.get()); |
| 750 // TODO(sschmitz): uncomment the next line when test data available. |
| 751 // VerifyHiDpiNtpTheme(pack.get()); |
609 } | 752 } |
610 } | 753 } |
OLD | NEW |