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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/synchronization/waitable_event.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
15 #include "chrome/browser/themes/theme_properties.h" | 16 #include "chrome/browser/themes/theme_properties.h" |
16 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/grit/theme_resources.h" | 18 #include "chrome/grit/theme_resources.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "ui/gfx/color_utils.h" | 21 #include "ui/gfx/color_utils.h" |
21 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
22 #include "ui/gfx/image/image_skia.h" | 23 #include "ui/gfx/image/image_skia.h" |
23 #include "ui/gfx/image/image_skia_rep.h" | 24 #include "ui/gfx/image/image_skia_rep.h" |
24 | 25 |
25 using extensions::Extension; | 26 using extensions::Extension; |
26 | 27 |
27 // Maps scale factors (enum values) to file path. | 28 // Maps scale factors (enum values) to file path. |
28 // A similar typedef in BrowserThemePack is private. | 29 // A similar typedef in BrowserThemePack is private. |
29 typedef std::map<ui::ScaleFactor, base::FilePath> TestScaleFactorToFileMap; | 30 typedef std::map<ui::ScaleFactor, base::FilePath> TestScaleFactorToFileMap; |
30 | 31 |
31 // Maps image ids to maps of scale factors to file paths. | 32 // Maps image ids to maps of scale factors to file paths. |
32 // A similar typedef in BrowserThemePack is private. | 33 // A similar typedef in BrowserThemePack is private. |
33 typedef std::map<int, TestScaleFactorToFileMap> TestFilePathMap; | 34 typedef std::map<int, TestScaleFactorToFileMap> TestFilePathMap; |
34 | 35 |
35 class BrowserThemePackTest : public ::testing::Test { | 36 class BrowserThemePackTest : public ::testing::Test { |
36 public: | 37 public: |
37 BrowserThemePackTest() { | 38 BrowserThemePackTest() |
| 39 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
| 40 theme_pack_(new BrowserThemePack()) { |
38 std::vector<ui::ScaleFactor> scale_factors; | 41 std::vector<ui::ScaleFactor> scale_factors; |
39 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 42 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
40 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 43 scale_factors.push_back(ui::SCALE_FACTOR_200P); |
41 scoped_set_supported_scale_factors_.reset( | 44 scoped_set_supported_scale_factors_.reset( |
42 new ui::test::ScopedSetSupportedScaleFactors(scale_factors)); | 45 new ui::test::ScopedSetSupportedScaleFactors(scale_factors)); |
43 theme_pack_ = new BrowserThemePack(); | |
44 } | 46 } |
| 47 ~BrowserThemePackTest() override {} |
45 | 48 |
46 // Transformation for link underline colors. | 49 // Transformation for link underline colors. |
47 SkColor BuildThirdOpacity(SkColor color_link) { | 50 SkColor BuildThirdOpacity(SkColor color_link) { |
48 return SkColorSetA(color_link, SkColorGetA(color_link) / 3); | 51 return SkColorSetA(color_link, SkColorGetA(color_link) / 3); |
49 } | 52 } |
50 | 53 |
51 void GenerateDefaultFrameColor(std::map<int, SkColor>* colors, | 54 void GenerateDefaultFrameColor(std::map<int, SkColor>* colors, |
52 int color, int tint, bool otr) { | 55 int color, int tint, bool otr) { |
53 (*colors)[color] = HSLShift( | 56 (*colors)[color] = HSLShift( |
54 ThemeProperties::GetDefaultColor(ThemeProperties::COLOR_FRAME, false), | 57 ThemeProperties::GetDefaultColor(ThemeProperties::COLOR_FRAME, false), |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 base::DictionaryValue* value, | 138 base::DictionaryValue* value, |
136 TestFilePathMap* out_file_paths) { | 139 TestFilePathMap* out_file_paths) { |
137 theme_pack_->ParseImageNamesFromJSON(value, base::FilePath(), | 140 theme_pack_->ParseImageNamesFromJSON(value, base::FilePath(), |
138 out_file_paths); | 141 out_file_paths); |
139 | 142 |
140 // Build the source image list for HasCustomImage(). | 143 // Build the source image list for HasCustomImage(). |
141 theme_pack_->BuildSourceImagesArray(*out_file_paths); | 144 theme_pack_->BuildSourceImagesArray(*out_file_paths); |
142 } | 145 } |
143 | 146 |
144 bool LoadRawBitmapsTo(const TestFilePathMap& out_file_paths) { | 147 bool LoadRawBitmapsTo(const TestFilePathMap& out_file_paths) { |
145 return theme_pack_->LoadRawBitmapsTo(out_file_paths, | 148 return theme_pack_->LoadRawBitmapsTo(out_file_paths, &theme_pack_->images_); |
146 &theme_pack_->images_on_ui_thread_); | |
147 } | 149 } |
148 | 150 |
149 // This function returns void in order to be able use ASSERT_... | 151 // This function returns void in order to be able use ASSERT_... |
150 // The BrowserThemePack is returned in |pack|. | 152 // The BrowserThemePack is returned in |pack|. |
151 void BuildFromUnpackedExtension(const base::FilePath& extension_path, | 153 void BuildFromUnpackedExtension(const base::FilePath& extension_path, |
152 scoped_refptr<BrowserThemePack>& pack) { | 154 scoped_refptr<BrowserThemePack>* pack) { |
153 base::FilePath manifest_path = | 155 io_waiter_.reset(new base::WaitableEvent( |
154 extension_path.AppendASCII("manifest.json"); | 156 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
155 std::string error; | 157 base::WaitableEvent::InitialState::NOT_SIGNALED)); |
156 JSONFileValueDeserializer deserializer(manifest_path); | 158 content::BrowserThread::PostTask( |
157 std::unique_ptr<base::DictionaryValue> valid_value = | 159 content::BrowserThread::IO, FROM_HERE, |
158 base::DictionaryValue::From(deserializer.Deserialize(NULL, &error)); | 160 base::Bind(&BrowserThemePackTest::DoBuildFromUnpackedExtension, |
159 EXPECT_EQ("", error); | 161 base::Unretained(this), extension_path, pack)); |
160 ASSERT_TRUE(valid_value.get()); | 162 io_waiter_->Wait(); |
161 scoped_refptr<Extension> extension( | 163 ASSERT_TRUE((*pack)->is_valid()); |
162 Extension::Create( | |
163 extension_path, | |
164 extensions::Manifest::INVALID_LOCATION, | |
165 *valid_value, | |
166 Extension::REQUIRE_KEY, | |
167 &error)); | |
168 ASSERT_TRUE(extension.get()); | |
169 ASSERT_EQ("", error); | |
170 pack = BrowserThemePack::BuildFromExtension(extension.get()); | |
171 ASSERT_TRUE(pack.get()); | |
172 } | 164 } |
173 | 165 |
174 base::FilePath GetStarGazingPath() { | 166 base::FilePath GetStarGazingPath() { |
175 base::FilePath test_path; | 167 base::FilePath test_path; |
176 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_path)) { | 168 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_path)) { |
177 NOTREACHED(); | 169 NOTREACHED(); |
178 return test_path; | 170 return test_path; |
179 } | 171 } |
180 | 172 |
181 test_path = test_path.AppendASCII("profiles"); | 173 test_path = test_path.AppendASCII("profiles"); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 EXPECT_EQ(160, rep4.sk_bitmap().height()); | 332 EXPECT_EQ(160, rep4.sk_bitmap().height()); |
341 // We expect the same colors and at locations scaled by 2 | 333 // We expect the same colors and at locations scaled by 2 |
342 // since this bitmap was scaled by 2. | 334 // since this bitmap was scaled by 2. |
343 for (size_t i = 0; i < normal.size(); ++i) { | 335 for (size_t i = 0; i < normal.size(); ++i) { |
344 int xy = 2 * normal[i].first; | 336 int xy = 2 * normal[i].first; |
345 SkColor color = normal[i].second; | 337 SkColor color = normal[i].second; |
346 EXPECT_EQ(color, rep4.sk_bitmap().getColor(xy, xy)); | 338 EXPECT_EQ(color, rep4.sk_bitmap().getColor(xy, xy)); |
347 } | 339 } |
348 } | 340 } |
349 | 341 |
350 content::TestBrowserThreadBundle test_browser_thread_bundle_; | 342 protected: |
351 | |
352 typedef std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> | 343 typedef std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> |
353 ScopedSetSupportedScaleFactors; | 344 ScopedSetSupportedScaleFactors; |
354 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_; | 345 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_; |
| 346 |
| 347 void DoBuildFromUnpackedExtension(const base::FilePath& extension_path, |
| 348 scoped_refptr<BrowserThemePack>* pack) { |
| 349 base::FilePath manifest_path = extension_path.AppendASCII("manifest.json"); |
| 350 std::string error; |
| 351 JSONFileValueDeserializer deserializer(manifest_path); |
| 352 std::unique_ptr<base::DictionaryValue> valid_value = |
| 353 base::DictionaryValue::From(deserializer.Deserialize(NULL, &error)); |
| 354 EXPECT_EQ("", error); |
| 355 ASSERT_TRUE(valid_value.get()); |
| 356 scoped_refptr<Extension> extension(Extension::Create( |
| 357 extension_path, extensions::Manifest::INVALID_LOCATION, *valid_value, |
| 358 Extension::REQUIRE_KEY, &error)); |
| 359 ASSERT_TRUE(extension.get()); |
| 360 ASSERT_EQ("", error); |
| 361 *pack = new BrowserThemePack; |
| 362 BrowserThemePack::BuildFromExtension(extension.get(), *pack); |
| 363 io_waiter_->Signal(); |
| 364 } |
| 365 |
| 366 content::TestBrowserThreadBundle thread_bundle_; |
355 scoped_refptr<BrowserThemePack> theme_pack_; | 367 scoped_refptr<BrowserThemePack> theme_pack_; |
| 368 std::unique_ptr<base::WaitableEvent> io_waiter_; |
356 }; | 369 }; |
357 | 370 |
358 // 'ntp_section' used to correspond to ThemeProperties::COLOR_NTP_SECTION, | 371 // 'ntp_section' used to correspond to ThemeProperties::COLOR_NTP_SECTION, |
359 // but COLOR_NTP_SECTION was since removed because it was never used. | 372 // but COLOR_NTP_SECTION was since removed because it was never used. |
360 // While it was in use, COLOR_NTP_HEADER used 'ntp_section' as a fallback when | 373 // While it was in use, COLOR_NTP_HEADER used 'ntp_section' as a fallback when |
361 // 'ntp_header' was absent. We still preserve this fallback for themes that | 374 // 'ntp_header' was absent. We still preserve this fallback for themes that |
362 // relied on this. | 375 // relied on this. |
363 TEST_F(BrowserThemePackTest, UseSectionColorAsNTPHeader) { | 376 TEST_F(BrowserThemePackTest, UseSectionColorAsNTPHeader) { |
364 std::string color_json = "{ \"ntp_section\": [190, 190, 190] }"; | 377 std::string color_json = "{ \"ntp_section\": [190, 190, 190] }"; |
365 LoadColorJSON(color_json); | 378 LoadColorJSON(color_json); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 // tricky memory stuff that BrowserThemePack does. | 575 // tricky memory stuff that BrowserThemePack does. |
563 TEST_F(BrowserThemePackTest, CanBuildAndReadPack) { | 576 TEST_F(BrowserThemePackTest, CanBuildAndReadPack) { |
564 base::ScopedTempDir dir; | 577 base::ScopedTempDir dir; |
565 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 578 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
566 base::FilePath file = dir.GetPath().AppendASCII("data.pak"); | 579 base::FilePath file = dir.GetPath().AppendASCII("data.pak"); |
567 | 580 |
568 // Part 1: Build the pack from an extension. | 581 // Part 1: Build the pack from an extension. |
569 { | 582 { |
570 base::FilePath star_gazing_path = GetStarGazingPath(); | 583 base::FilePath star_gazing_path = GetStarGazingPath(); |
571 scoped_refptr<BrowserThemePack> pack; | 584 scoped_refptr<BrowserThemePack> pack; |
572 BuildFromUnpackedExtension(star_gazing_path, pack); | 585 BuildFromUnpackedExtension(star_gazing_path, &pack); |
573 ASSERT_TRUE(pack->WriteToDisk(file)); | 586 ASSERT_TRUE(pack->WriteToDisk(file)); |
574 VerifyStarGazing(pack.get()); | 587 VerifyStarGazing(pack.get()); |
575 } | 588 } |
576 | 589 |
577 // Part 2: Try to read back the data pack that we just wrote to disk. | 590 // Part 2: Try to read back the data pack that we just wrote to disk. |
578 { | 591 { |
579 scoped_refptr<BrowserThemePack> pack = | 592 scoped_refptr<BrowserThemePack> pack = |
580 BrowserThemePack::BuildFromDataPack( | 593 BrowserThemePack::BuildFromDataPack( |
581 file, "mblmlcbknbnfebdfjnolmcapmdofhmme"); | 594 file, "mblmlcbknbnfebdfjnolmcapmdofhmme"); |
582 ASSERT_TRUE(pack.get()); | 595 ASSERT_TRUE(pack.get()); |
583 VerifyStarGazing(pack.get()); | 596 VerifyStarGazing(pack.get()); |
584 } | 597 } |
585 } | 598 } |
586 | 599 |
587 TEST_F(BrowserThemePackTest, HiDpiThemeTest) { | 600 TEST_F(BrowserThemePackTest, HiDpiThemeTest) { |
588 base::ScopedTempDir dir; | 601 base::ScopedTempDir dir; |
589 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 602 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
590 base::FilePath file = dir.GetPath().AppendASCII("theme_data.pak"); | 603 base::FilePath file = dir.GetPath().AppendASCII("theme_data.pak"); |
591 | 604 |
592 // Part 1: Build the pack from an extension. | 605 // Part 1: Build the pack from an extension. |
593 { | 606 { |
594 base::FilePath hidpi_path = GetHiDpiThemePath(); | 607 base::FilePath hidpi_path = GetHiDpiThemePath(); |
595 scoped_refptr<BrowserThemePack> pack; | 608 scoped_refptr<BrowserThemePack> pack; |
596 BuildFromUnpackedExtension(hidpi_path, pack); | 609 BuildFromUnpackedExtension(hidpi_path, &pack); |
597 ASSERT_TRUE(pack->WriteToDisk(file)); | 610 ASSERT_TRUE(pack->WriteToDisk(file)); |
598 VerifyHiDpiTheme(pack.get()); | 611 VerifyHiDpiTheme(pack.get()); |
599 } | 612 } |
600 | 613 |
601 // Part 2: Try to read back the data pack that we just wrote to disk. | 614 // Part 2: Try to read back the data pack that we just wrote to disk. |
602 { | 615 { |
603 scoped_refptr<BrowserThemePack> pack = | 616 scoped_refptr<BrowserThemePack> pack = |
604 BrowserThemePack::BuildFromDataPack(file, "gllekhaobjnhgeag"); | 617 BrowserThemePack::BuildFromDataPack(file, "gllekhaobjnhgeag"); |
605 ASSERT_TRUE(pack.get()); | 618 ASSERT_TRUE(pack.get()); |
606 VerifyHiDpiTheme(pack.get()); | 619 VerifyHiDpiTheme(pack.get()); |
607 } | 620 } |
608 } | 621 } |
OLD | NEW |