Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: chrome/browser/themes/browser_theme_pack_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698