Commit ec2cd434 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Misc. cleanup to browser_theme_pack_unittest.cc; no functional change.

* Add GetDefaultColor() mapping function to make it easy to fix DCHECKs due to
  this code using raw _INCOGNITO color constants but
  ThemeProperties::GetDefaultColor() not expecting them.  This is the change
  that led me to get annoyed and do all the other stuff here.
* Convert typedefs to type aliases.
* Add TP = ThemeProperties type alias for brevity.
* Use =default.
* Mark functions that don't touch member variables "static".
* Fix wrapping issues.
* Because the class here is so long, split it into declaration and definition
  pieces so it's easier to see what's the API and what's helper functions.
* Make the API protected; make helpers private.
* Make data members private.  Add a const accessor for the one member tests use.

Bug: none
Change-Id: I4cc058bb29747cfa84835edbc5ce765401e14527
Reviewed-on: https://chromium-review.googlesource.com/1152108Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578775}
parent cc487f3e
...@@ -23,229 +23,267 @@ ...@@ -23,229 +23,267 @@
#include "ui/gfx/image/image_skia_rep.h" #include "ui/gfx/image/image_skia_rep.h"
using extensions::Extension; using extensions::Extension;
using TP = ThemeProperties;
// Maps scale factors (enum values) to file path. // Maps scale factors (enum values) to file path.
// A similar typedef in BrowserThemePack is private. // A similar typedef in BrowserThemePack is private.
typedef std::map<ui::ScaleFactor, base::FilePath> TestScaleFactorToFileMap; using TestScaleFactorToFileMap = std::map<ui::ScaleFactor, base::FilePath>;
// Maps image ids to maps of scale factors to file paths. // Maps image ids to maps of scale factors to file paths.
// A similar typedef in BrowserThemePack is private. // A similar typedef in BrowserThemePack is private.
typedef std::map<int, TestScaleFactorToFileMap> TestFilePathMap; using TestFilePathMap = std::map<int, TestScaleFactorToFileMap>;
// BrowserThemePackTest --------------------------------------------------------
class BrowserThemePackTest : public ::testing::Test { class BrowserThemePackTest : public ::testing::Test {
public: public:
BrowserThemePackTest() : theme_pack_(new BrowserThemePack()) { BrowserThemePackTest();
std::vector<ui::ScaleFactor> scale_factors; ~BrowserThemePackTest() override = default;
scale_factors.push_back(ui::SCALE_FACTOR_100P);
scale_factors.push_back(ui::SCALE_FACTOR_200P);
scoped_set_supported_scale_factors_.reset(
new ui::test::ScopedSetSupportedScaleFactors(scale_factors));
}
~BrowserThemePackTest() override {}
// Transformation for link underline colors.
SkColor BuildThirdOpacity(SkColor color_link) {
return SkColorSetA(color_link, SkColorGetA(color_link) / 3);
}
void GenerateDefaultFrameColor(std::map<int, SkColor>* colors,
int color, int tint, bool otr) {
(*colors)[color] = HSLShift(
ThemeProperties::GetDefaultColor(ThemeProperties::COLOR_FRAME, false),
ThemeProperties::GetDefaultTint(tint, otr));
}
protected:
// Returns a mapping from each COLOR_* constant to the default value for this // Returns a mapping from each COLOR_* constant to the default value for this
// constant. Callers get this map, and then modify expected values and then // constant. Callers get this map, and then modify expected values and then
// run the resulting thing through VerifyColorMap(). // run the resulting thing through VerifyColorMap().
std::map<int, SkColor> GetDefaultColorMap() { static std::map<int, SkColor> GetDefaultColorMap();
std::map<int, SkColor> colors;
GenerateDefaultFrameColor(&colors, ThemeProperties::COLOR_FRAME, void VerifyColorMap(const std::map<int, SkColor>& color_map);
ThemeProperties::TINT_FRAME, false); void LoadColorJSON(const std::string& json);
GenerateDefaultFrameColor(&colors, void LoadColorDictionary(base::DictionaryValue* value);
ThemeProperties::COLOR_FRAME_INACTIVE, void LoadTintJSON(const std::string& json);
ThemeProperties::TINT_FRAME_INACTIVE, false); void LoadTintDictionary(base::DictionaryValue* value);
GenerateDefaultFrameColor(&colors, void LoadDisplayPropertiesJSON(const std::string& json);
ThemeProperties::COLOR_FRAME_INCOGNITO, void LoadDisplayPropertiesDictionary(base::DictionaryValue* value);
ThemeProperties::TINT_FRAME, true); void ParseImageNamesJSON(const std::string& json,
GenerateDefaultFrameColor( TestFilePathMap* out_file_paths);
&colors, void ParseImageNamesDictionary(base::DictionaryValue* value,
ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE, TestFilePathMap* out_file_paths);
ThemeProperties::TINT_FRAME_INACTIVE, true); bool LoadRawBitmapsTo(const TestFilePathMap& out_file_paths);
// For the rest, use default colors.
for (int i = ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE + 1;
i <= ThemeProperties::COLOR_BUTTON_BACKGROUND; ++i) {
colors[i] = ThemeProperties::GetDefaultColor(i, false);
}
return colors; // This function returns void in order to be able use ASSERT_...
} // The BrowserThemePack is returned in |pack|.
static void BuildFromUnpackedExtension(const base::FilePath& extension_path,
scoped_refptr<BrowserThemePack>* pack);
void VerifyColorMap(const std::map<int, SkColor>& color_map) { static base::FilePath GetStarGazingPath();
for (std::map<int, SkColor>::const_iterator it = color_map.begin(); static base::FilePath GetHiDpiThemePath();
it != color_map.end(); ++it) {
SkColor color;
if (!theme_pack_->GetColor(it->first, &color))
color = ThemeProperties::GetDefaultColor(it->first, false);
EXPECT_EQ(it->second, color) << "Color id = " << it->first;
}
}
void LoadColorJSON(const std::string& json) { // Verifies the data in star gazing. We do this multiple times for different
std::unique_ptr<base::Value> value = base::JSONReader::Read(json); // BrowserThemePack objects to make sure it works in generated and mmapped
ASSERT_TRUE(value->is_dict()); // mode correctly.
LoadColorDictionary(static_cast<base::DictionaryValue*>(value.get())); static void VerifyStarGazing(BrowserThemePack* pack);
}
void LoadColorDictionary(base::DictionaryValue* value) { static void VerifyHiDpiTheme(BrowserThemePack* pack);
theme_pack_->BuildColorsFromJSON(value);
}
void LoadTintJSON(const std::string& json) { const BrowserThemePack& theme_pack() const { return *theme_pack_; }
std::unique_ptr<base::Value> value = base::JSONReader::Read(json);
ASSERT_TRUE(value->is_dict());
LoadTintDictionary(static_cast<base::DictionaryValue*>(value.get()));
}
void LoadTintDictionary(base::DictionaryValue* value) { private:
theme_pack_->BuildTintsFromJSON(value); using ScopedSetSupportedScaleFactors =
} std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors>;
void LoadDisplayPropertiesJSON(const std::string& json) { // Transformation for link underline colors.
std::unique_ptr<base::Value> value = base::JSONReader::Read(json); static SkColor BuildThirdOpacity(SkColor color_link);
ASSERT_TRUE(value->is_dict());
LoadDisplayPropertiesDictionary(
static_cast<base::DictionaryValue*>(value.get()));
}
void LoadDisplayPropertiesDictionary(base::DictionaryValue* value) { // Returns the appropriate default color for |id|.
theme_pack_->BuildDisplayPropertiesFromJSON(value); static SkColor GetDefaultColor(int id);
}
void ParseImageNamesJSON(const std::string& json, static void GenerateDefaultFrameColor(std::map<int, SkColor>* colors,
TestFilePathMap* out_file_paths) { int color,
std::unique_ptr<base::Value> value = base::JSONReader::Read(json); int tint,
ASSERT_TRUE(value->is_dict()); bool otr);
ParseImageNamesDictionary(static_cast<base::DictionaryValue*>(value.get()),
out_file_paths);
}
void ParseImageNamesDictionary( ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
base::DictionaryValue* value,
TestFilePathMap* out_file_paths) {
theme_pack_->ParseImageNamesFromJSON(value, base::FilePath(),
out_file_paths);
// Build the source image list for HasCustomImage(). content::TestBrowserThreadBundle thread_bundle_;
theme_pack_->BuildSourceImagesArray(*out_file_paths); scoped_refptr<BrowserThemePack> theme_pack_;
} };
bool LoadRawBitmapsTo(const TestFilePathMap& out_file_paths) { BrowserThemePackTest::BrowserThemePackTest()
return theme_pack_->LoadRawBitmapsTo(out_file_paths, &theme_pack_->images_); : theme_pack_(new BrowserThemePack()) {
} std::vector<ui::ScaleFactor> scale_factors;
scale_factors.push_back(ui::SCALE_FACTOR_100P);
scale_factors.push_back(ui::SCALE_FACTOR_200P);
scoped_set_supported_scale_factors_.reset(
new ui::test::ScopedSetSupportedScaleFactors(scale_factors));
}
// This function returns void in order to be able use ASSERT_... // static
// The BrowserThemePack is returned in |pack|. std::map<int, SkColor> BrowserThemePackTest::GetDefaultColorMap() {
void BuildFromUnpackedExtension(const base::FilePath& extension_path, std::map<int, SkColor> colors;
scoped_refptr<BrowserThemePack>* pack) { GenerateDefaultFrameColor(&colors, TP::COLOR_FRAME, TP::TINT_FRAME, false);
base::FilePath manifest_path = extension_path.AppendASCII("manifest.json"); GenerateDefaultFrameColor(&colors, TP::COLOR_FRAME_INACTIVE,
std::string error; TP::TINT_FRAME_INACTIVE, false);
JSONFileValueDeserializer deserializer(manifest_path); GenerateDefaultFrameColor(&colors, TP::COLOR_FRAME_INCOGNITO, TP::TINT_FRAME,
std::unique_ptr<base::DictionaryValue> valid_value = true);
base::DictionaryValue::From(deserializer.Deserialize(NULL, &error)); GenerateDefaultFrameColor(&colors, TP::COLOR_FRAME_INCOGNITO_INACTIVE,
EXPECT_EQ("", error); TP::TINT_FRAME_INACTIVE, true);
ASSERT_TRUE(valid_value.get());
scoped_refptr<Extension> extension(Extension::Create( // For the rest, use default colors.
extension_path, extensions::Manifest::INVALID_LOCATION, *valid_value, for (int i = TP::COLOR_FRAME_INCOGNITO_INACTIVE + 1;
Extension::REQUIRE_KEY, &error)); i <= TP::COLOR_BUTTON_BACKGROUND; ++i) {
ASSERT_TRUE(extension.get()); colors[i] = GetDefaultColor(i);
ASSERT_EQ("", error);
*pack = new BrowserThemePack;
BrowserThemePack::BuildFromExtension(extension.get(), *pack);
ASSERT_TRUE((*pack)->is_valid());
} }
base::FilePath GetStarGazingPath() { return colors;
base::FilePath test_path; }
if (!base::PathService::Get(chrome::DIR_TEST_DATA, &test_path)) {
NOTREACHED();
return test_path;
}
test_path = test_path.AppendASCII("profiles"); void BrowserThemePackTest::VerifyColorMap(
test_path = test_path.AppendASCII("profile_with_complex_theme"); const std::map<int, SkColor>& color_map) {
test_path = test_path.AppendASCII("Default"); for (std::map<int, SkColor>::const_iterator it = color_map.begin();
test_path = test_path.AppendASCII("Extensions"); it != color_map.end(); ++it) {
test_path = test_path.AppendASCII("mblmlcbknbnfebdfjnolmcapmdofhmme"); SkColor color;
test_path = test_path.AppendASCII("1.1"); if (!theme_pack_->GetColor(it->first, &color))
return base::FilePath(test_path); color = GetDefaultColor(it->first);
EXPECT_EQ(it->second, color) << "Color id = " << it->first;
} }
}
base::FilePath GetHiDpiThemePath() { void BrowserThemePackTest::LoadColorJSON(const std::string& json) {
base::FilePath test_path; std::unique_ptr<base::Value> value = base::JSONReader::Read(json);
if (!base::PathService::Get(chrome::DIR_TEST_DATA, &test_path)) { ASSERT_TRUE(value->is_dict());
NOTREACHED(); LoadColorDictionary(static_cast<base::DictionaryValue*>(value.get()));
return test_path; }
}
test_path = test_path.AppendASCII("extensions");
test_path = test_path.AppendASCII("theme_hidpi");
return base::FilePath(test_path);
}
// Verifies the data in star gazing. We do this multiple times for different void BrowserThemePackTest::LoadColorDictionary(base::DictionaryValue* value) {
// BrowserThemePack objects to make sure it works in generated and mmapped theme_pack_->BuildColorsFromJSON(value);
// mode correctly. }
void VerifyStarGazing(BrowserThemePack* pack) {
// First check that values we know exist, exist. void BrowserThemePackTest::LoadTintJSON(const std::string& json) {
SkColor color; std::unique_ptr<base::Value> value = base::JSONReader::Read(json);
EXPECT_TRUE(pack->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT, ASSERT_TRUE(value->is_dict());
&color)); LoadTintDictionary(static_cast<base::DictionaryValue*>(value.get()));
EXPECT_EQ(SK_ColorBLACK, color); }
EXPECT_TRUE(pack->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND, void BrowserThemePackTest::LoadTintDictionary(base::DictionaryValue* value) {
&color)); theme_pack_->BuildTintsFromJSON(value);
EXPECT_EQ(SkColorSetRGB(57, 137, 194), color); }
color_utils::HSL expected = { 0.6, 0.553, 0.5 }; void BrowserThemePackTest::LoadDisplayPropertiesJSON(const std::string& json) {
color_utils::HSL actual; std::unique_ptr<base::Value> value = base::JSONReader::Read(json);
EXPECT_TRUE(pack->GetTint(ThemeProperties::TINT_BUTTONS, &actual)); ASSERT_TRUE(value->is_dict());
EXPECT_DOUBLE_EQ(expected.h, actual.h); LoadDisplayPropertiesDictionary(
EXPECT_DOUBLE_EQ(expected.s, actual.s); static_cast<base::DictionaryValue*>(value.get()));
EXPECT_DOUBLE_EQ(expected.l, actual.l); }
int val; void BrowserThemePackTest::LoadDisplayPropertiesDictionary(
EXPECT_TRUE(pack->GetDisplayProperty( base::DictionaryValue* value) {
ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &val)); theme_pack_->BuildDisplayPropertiesFromJSON(value);
EXPECT_EQ(ThemeProperties::ALIGN_TOP, val); }
// The stargazing theme defines the following images: void BrowserThemePackTest::ParseImageNamesJSON(
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND)); const std::string& json,
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME)); TestFilePathMap* out_file_paths) {
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_NTP_BACKGROUND)); std::unique_ptr<base::Value> value = base::JSONReader::Read(json);
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND)); ASSERT_TRUE(value->is_dict());
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TOOLBAR)); ParseImageNamesDictionary(static_cast<base::DictionaryValue*>(value.get()),
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_WINDOW_CONTROL_BACKGROUND)); out_file_paths);
}
// Here are a few images that we shouldn't expect because even though
// they're included in the theme pack, they were autogenerated and void BrowserThemePackTest::ParseImageNamesDictionary(
// therefore shouldn't show up when calling HasCustomImage(). base::DictionaryValue* value,
// Verify they do appear when calling GetImageNamed(), though. TestFilePathMap* out_file_paths) {
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_INACTIVE)); theme_pack_->ParseImageNamesFromJSON(value, base::FilePath(), out_file_paths);
EXPECT_FALSE(pack->GetImageNamed(IDR_THEME_FRAME_INACTIVE).IsEmpty());
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO)); // Build the source image list for HasCustomImage().
EXPECT_FALSE(pack->GetImageNamed(IDR_THEME_FRAME_INCOGNITO).IsEmpty()); theme_pack_->BuildSourceImagesArray(*out_file_paths);
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO_INACTIVE)); }
EXPECT_FALSE(
pack->GetImageNamed(IDR_THEME_FRAME_INCOGNITO_INACTIVE).IsEmpty()); bool BrowserThemePackTest::LoadRawBitmapsTo(
const TestFilePathMap& out_file_paths) {
return theme_pack_->LoadRawBitmapsTo(out_file_paths, &theme_pack_->images_);
}
// static
void BrowserThemePackTest::BuildFromUnpackedExtension(
const base::FilePath& extension_path,
scoped_refptr<BrowserThemePack>* pack) {
base::FilePath manifest_path = extension_path.AppendASCII("manifest.json");
std::string error;
JSONFileValueDeserializer deserializer(manifest_path);
std::unique_ptr<base::DictionaryValue> valid_value =
base::DictionaryValue::From(deserializer.Deserialize(NULL, &error));
EXPECT_EQ("", error);
ASSERT_TRUE(valid_value.get());
scoped_refptr<Extension> extension(
Extension::Create(extension_path, extensions::Manifest::INVALID_LOCATION,
*valid_value, Extension::REQUIRE_KEY, &error));
ASSERT_TRUE(extension.get());
ASSERT_EQ("", error);
*pack = new BrowserThemePack;
BrowserThemePack::BuildFromExtension(extension.get(), *pack);
ASSERT_TRUE((*pack)->is_valid());
}
// static
base::FilePath BrowserThemePackTest::GetStarGazingPath() {
base::FilePath test_path;
DCHECK(base::PathService::Get(chrome::DIR_TEST_DATA, &test_path));
test_path = test_path.AppendASCII("profiles");
test_path = test_path.AppendASCII("profile_with_complex_theme");
test_path = test_path.AppendASCII("Default");
test_path = test_path.AppendASCII("Extensions");
test_path = test_path.AppendASCII("mblmlcbknbnfebdfjnolmcapmdofhmme");
test_path = test_path.AppendASCII("1.1");
return base::FilePath(test_path);
}
// static
base::FilePath BrowserThemePackTest::GetHiDpiThemePath() {
base::FilePath test_path;
DCHECK(base::PathService::Get(chrome::DIR_TEST_DATA, &test_path));
test_path = test_path.AppendASCII("extensions");
test_path = test_path.AppendASCII("theme_hidpi");
return base::FilePath(test_path);
}
// static
void BrowserThemePackTest::VerifyStarGazing(BrowserThemePack* pack) {
// First check that values we know exist, exist.
SkColor color;
EXPECT_TRUE(pack->GetColor(TP::COLOR_BOOKMARK_TEXT, &color));
EXPECT_EQ(SK_ColorBLACK, color);
// The overlay images are missing and they do not fall back to the active EXPECT_TRUE(pack->GetColor(TP::COLOR_NTP_BACKGROUND, &color));
// frame image. EXPECT_EQ(SkColorSetRGB(57, 137, 194), color);
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY));
EXPECT_TRUE(pack->GetImageNamed(IDR_THEME_FRAME_OVERLAY).IsEmpty()); color_utils::HSL expected = {0.6, 0.553, 0.5};
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY_INACTIVE)); color_utils::HSL actual;
EXPECT_TRUE( EXPECT_TRUE(pack->GetTint(TP::TINT_BUTTONS, &actual));
pack->GetImageNamed(IDR_THEME_FRAME_OVERLAY_INACTIVE).IsEmpty()); EXPECT_DOUBLE_EQ(expected.h, actual.h);
EXPECT_DOUBLE_EQ(expected.s, actual.s);
EXPECT_DOUBLE_EQ(expected.l, actual.l);
int val;
EXPECT_TRUE(pack->GetDisplayProperty(TP::NTP_BACKGROUND_ALIGNMENT, &val));
EXPECT_EQ(TP::ALIGN_TOP, val);
// The stargazing theme defines the following images:
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_NTP_BACKGROUND));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TOOLBAR));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_WINDOW_CONTROL_BACKGROUND));
// Here are a few images that we shouldn't expect because even though
// they're included in the theme pack, they were autogenerated and
// therefore shouldn't show up when calling HasCustomImage().
// Verify they do appear when calling GetImageNamed(), though.
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_INACTIVE));
EXPECT_FALSE(pack->GetImageNamed(IDR_THEME_FRAME_INACTIVE).IsEmpty());
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO));
EXPECT_FALSE(pack->GetImageNamed(IDR_THEME_FRAME_INCOGNITO).IsEmpty());
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO_INACTIVE));
EXPECT_FALSE(
pack->GetImageNamed(IDR_THEME_FRAME_INCOGNITO_INACTIVE).IsEmpty());
// The overlay images are missing and they do not fall back to the active
// frame image.
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY));
EXPECT_TRUE(pack->GetImageNamed(IDR_THEME_FRAME_OVERLAY).IsEmpty());
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY_INACTIVE));
EXPECT_TRUE(pack->GetImageNamed(IDR_THEME_FRAME_OVERLAY_INACTIVE).IsEmpty());
#if !defined(OS_MACOSX) #if !defined(OS_MACOSX)
// The tab background image is missing, but will still be generated in // The tab background image is missing, but will still be generated in
...@@ -256,131 +294,152 @@ class BrowserThemePackTest : public ::testing::Test { ...@@ -256,131 +294,152 @@ class BrowserThemePackTest : public ::testing::Test {
#endif #endif
// Make sure we don't have phantom data. // Make sure we don't have phantom data.
EXPECT_FALSE(pack->GetColor(ThemeProperties::COLOR_CONTROL_BACKGROUND, EXPECT_FALSE(pack->GetColor(TP::COLOR_CONTROL_BACKGROUND, &color));
&color)); EXPECT_FALSE(pack->GetTint(TP::TINT_FRAME, &actual));
EXPECT_FALSE(pack->GetTint(ThemeProperties::TINT_FRAME, &actual)); }
}
void VerifyHiDpiTheme(BrowserThemePack* pack) {
// The high DPI theme defines the following images:
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INACTIVE));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO_INACTIVE));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TOOLBAR));
// The high DPI theme does not define the following images: // static
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND)); void BrowserThemePackTest::VerifyHiDpiTheme(BrowserThemePack* pack) {
// The high DPI theme defines the following images:
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INACTIVE));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_FRAME_INCOGNITO_INACTIVE));
EXPECT_TRUE(pack->HasCustomImage(IDR_THEME_TOOLBAR));
// The high DPI theme does not define the following images:
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND));
#if !defined(OS_MACOSX) #if !defined(OS_MACOSX)
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_INCOGNITO)); EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_INCOGNITO));
#endif #endif
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_V)); EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_TAB_BACKGROUND_V));
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_NTP_BACKGROUND)); EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_NTP_BACKGROUND));
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY)); EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY));
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY_INACTIVE)); EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_FRAME_OVERLAY_INACTIVE));
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND)); EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND));
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION)); EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION));
EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_WINDOW_CONTROL_BACKGROUND)); EXPECT_FALSE(pack->HasCustomImage(IDR_THEME_WINDOW_CONTROL_BACKGROUND));
// Compare some known pixel colors at know locations for a theme // Compare some known pixel colors at know locations for a theme
// image where two different PNG files were specified for scales 100% // image where two different PNG files were specified for scales 100%
// and 200% respectively. // and 200% respectively.
int idr = IDR_THEME_FRAME; int idr = IDR_THEME_FRAME;
gfx::Image image = pack->GetImageNamed(idr); gfx::Image image = pack->GetImageNamed(idr);
EXPECT_FALSE(image.IsEmpty()); EXPECT_FALSE(image.IsEmpty());
const gfx::ImageSkia* image_skia = image.ToImageSkia(); const gfx::ImageSkia* image_skia = image.ToImageSkia();
ASSERT_TRUE(image_skia); ASSERT_TRUE(image_skia);
// Scale 100%. // Scale 100%.
const gfx::ImageSkiaRep& rep1 = image_skia->GetRepresentation(1.0f); const gfx::ImageSkiaRep& rep1 = image_skia->GetRepresentation(1.0f);
ASSERT_FALSE(rep1.is_null()); ASSERT_FALSE(rep1.is_null());
EXPECT_EQ(80, rep1.sk_bitmap().width()); EXPECT_EQ(80, rep1.sk_bitmap().width());
EXPECT_EQ(80, rep1.sk_bitmap().height()); EXPECT_EQ(80, rep1.sk_bitmap().height());
EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor(4, 4)); EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor(4, 4));
EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor(8, 8)); EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor(8, 8));
EXPECT_EQ(SkColorSetRGB(0, 241, 237), rep1.sk_bitmap().getColor(16, 16)); EXPECT_EQ(SkColorSetRGB(0, 241, 237), rep1.sk_bitmap().getColor(16, 16));
EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor(24, 24)); EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep1.sk_bitmap().getColor(24, 24));
EXPECT_EQ(SkColorSetRGB(0, 241, 237), rep1.sk_bitmap().getColor(32, 32)); EXPECT_EQ(SkColorSetRGB(0, 241, 237), rep1.sk_bitmap().getColor(32, 32));
// Scale 200%. // Scale 200%.
const gfx::ImageSkiaRep& rep2 = image_skia->GetRepresentation(2.0f); const gfx::ImageSkiaRep& rep2 = image_skia->GetRepresentation(2.0f);
ASSERT_FALSE(rep2.is_null()); ASSERT_FALSE(rep2.is_null());
EXPECT_EQ(160, rep2.sk_bitmap().width()); EXPECT_EQ(160, rep2.sk_bitmap().width());
EXPECT_EQ(160, rep2.sk_bitmap().height()); EXPECT_EQ(160, rep2.sk_bitmap().height());
EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep2.sk_bitmap().getColor(4, 4)); EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep2.sk_bitmap().getColor(4, 4));
EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(8, 8)); EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(8, 8));
EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(16, 16)); EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(16, 16));
EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(24, 24)); EXPECT_EQ(SkColorSetRGB(223, 42, 0), rep2.sk_bitmap().getColor(24, 24));
EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep2.sk_bitmap().getColor(32, 32)); EXPECT_EQ(SkColorSetRGB(255, 255, 255), rep2.sk_bitmap().getColor(32, 32));
// TODO(sschmitz): I plan to remove the following (to the end of the fct) // TODO(sschmitz): I plan to remove the following (to the end of the fct)
// Reason: this test may be brittle. It depends on details of how we scale // Reason: this test may be brittle. It depends on details of how we scale
// an 100% image to an 200% image. If there's filtering etc, this test would // an 100% image to an 200% image. If there's filtering etc, this test would
// break. Also High DPI is new, but scaling from 100% to 200% is not new // break. Also High DPI is new, but scaling from 100% to 200% is not new
// and need not be tested here. But in the interrim it is useful to verify // and need not be tested here. But in the interrim it is useful to verify
// that this image was scaled and did not appear in the input. // that this image was scaled and did not appear in the input.
// Compare pixel colors and locations for a theme image that had // Compare pixel colors and locations for a theme image that had
// only one PNG file specified (for scale 100%). The representation // only one PNG file specified (for scale 100%). The representation
// for scale of 200% was computed. // for scale of 200% was computed.
idr = IDR_THEME_FRAME_INCOGNITO_INACTIVE; idr = IDR_THEME_FRAME_INCOGNITO_INACTIVE;
image = pack->GetImageNamed(idr); image = pack->GetImageNamed(idr);
EXPECT_FALSE(image.IsEmpty()); EXPECT_FALSE(image.IsEmpty());
image_skia = image.ToImageSkia(); image_skia = image.ToImageSkia();
ASSERT_TRUE(image_skia); ASSERT_TRUE(image_skia);
// Scale 100%. // Scale 100%.
const gfx::ImageSkiaRep& rep3 = image_skia->GetRepresentation(1.0f); const gfx::ImageSkiaRep& rep3 = image_skia->GetRepresentation(1.0f);
ASSERT_FALSE(rep3.is_null()); ASSERT_FALSE(rep3.is_null());
EXPECT_EQ(80, rep3.sk_bitmap().width()); EXPECT_EQ(80, rep3.sk_bitmap().width());
EXPECT_EQ(80, rep3.sk_bitmap().height()); EXPECT_EQ(80, rep3.sk_bitmap().height());
// We take samples of colors and locations along the diagonal whenever // We take samples of colors and locations along the diagonal whenever
// the color changes. Note these colors are slightly different from // the color changes. Note these colors are slightly different from
// the input PNG file due to input processing. // the input PNG file due to input processing.
std::vector<std::pair<int, SkColor> > normal; std::vector<std::pair<int, SkColor>> normal;
int xy = 0; int xy = 0;
SkColor color = rep3.sk_bitmap().getColor(xy, xy); SkColor color = rep3.sk_bitmap().getColor(xy, xy);
normal.push_back(std::make_pair(xy, color)); normal.push_back(std::make_pair(xy, color));
for (int xy = 0; xy < 40; ++xy) { for (int xy = 0; xy < 40; ++xy) {
SkColor next_color = rep3.sk_bitmap().getColor(xy, xy); SkColor next_color = rep3.sk_bitmap().getColor(xy, xy);
if (next_color != color) { if (next_color != color) {
color = next_color; color = next_color;
normal.push_back(std::make_pair(xy, color)); normal.push_back(std::make_pair(xy, color));
}
}
EXPECT_EQ(static_cast<size_t>(9), normal.size());
// Scale 200%.
const gfx::ImageSkiaRep& rep4 = image_skia->GetRepresentation(2.0f);
ASSERT_FALSE(rep4.is_null());
EXPECT_EQ(160, rep4.sk_bitmap().width());
EXPECT_EQ(160, rep4.sk_bitmap().height());
// We expect the same colors and at locations scaled by 2
// since this bitmap was scaled by 2.
for (size_t i = 0; i < normal.size(); ++i) {
int xy = 2 * normal[i].first;
SkColor color = normal[i].second;
EXPECT_EQ(color, rep4.sk_bitmap().getColor(xy, xy));
} }
} }
EXPECT_EQ(static_cast<size_t>(9), normal.size());
// Scale 200%.
const gfx::ImageSkiaRep& rep4 = image_skia->GetRepresentation(2.0f);
ASSERT_FALSE(rep4.is_null());
EXPECT_EQ(160, rep4.sk_bitmap().width());
EXPECT_EQ(160, rep4.sk_bitmap().height());
// We expect the same colors and at locations scaled by 2
// since this bitmap was scaled by 2.
for (size_t i = 0; i < normal.size(); ++i) {
int xy = 2 * normal[i].first;
SkColor color = normal[i].second;
EXPECT_EQ(color, rep4.sk_bitmap().getColor(xy, xy));
}
}
protected: // static
typedef std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> SkColor BrowserThemePackTest::BuildThirdOpacity(SkColor color_link) {
ScopedSetSupportedScaleFactors; return SkColorSetA(color_link, SkColorGetA(color_link) / 3);
ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_; }
content::TestBrowserThreadBundle thread_bundle_; // static
scoped_refptr<BrowserThemePack> theme_pack_; SkColor BrowserThemePackTest::GetDefaultColor(int id) {
}; // Direct incognito IDs need to be mapped back to the non-incognito versions
// (plus passing "true" for |incognito|) to avoid DCHECK failures.
switch (id) {
case TP::COLOR_FRAME_INCOGNITO:
return TP::GetDefaultColor(TP::COLOR_FRAME, true);
case TP::COLOR_FRAME_INCOGNITO_INACTIVE:
return TP::GetDefaultColor(TP::COLOR_FRAME_INACTIVE, true);
default:
return TP::GetDefaultColor(id, false);
}
}
// static
void BrowserThemePackTest::GenerateDefaultFrameColor(
std::map<int, SkColor>* colors,
int color,
int tint,
bool otr) {
(*colors)[color] =
HSLShift(GetDefaultColor(TP::COLOR_FRAME), TP::GetDefaultTint(tint, otr));
}
// Actual tests ----------------------------------------------------------------
// 'ntp_section' used to correspond to ThemeProperties::COLOR_NTP_SECTION, // 'ntp_section' used to correspond to COLOR_NTP_SECTION, but COLOR_NTP_SECTION
// but COLOR_NTP_SECTION was since removed because it was never used. // was since removed because it was never used. While it was in use,
// While it was in use, COLOR_NTP_HEADER used 'ntp_section' as a fallback when // COLOR_NTP_HEADER used 'ntp_section' as a fallback when 'ntp_header' was
// 'ntp_header' was absent. We still preserve this fallback for themes that // absent. We still preserve this fallback for themes that relied on this.
// relied on this.
TEST_F(BrowserThemePackTest, UseSectionColorAsNTPHeader) { TEST_F(BrowserThemePackTest, UseSectionColorAsNTPHeader) {
std::string color_json = "{ \"ntp_section\": [190, 190, 190] }"; std::string color_json = "{ \"ntp_section\": [190, 190, 190] }";
LoadColorJSON(color_json); LoadColorJSON(color_json);
std::map<int, SkColor> colors = GetDefaultColorMap(); std::map<int, SkColor> colors = GetDefaultColorMap();
SkColor ntp_color = SkColorSetRGB(190, 190, 190); SkColor ntp_color = SkColorSetRGB(190, 190, 190);
colors[ThemeProperties::COLOR_NTP_HEADER] = ntp_color; colors[TP::COLOR_NTP_HEADER] = ntp_color;
VerifyColorMap(colors); VerifyColorMap(colors);
} }
...@@ -390,7 +449,7 @@ TEST_F(BrowserThemePackTest, ProvideNtpHeaderColor) { ...@@ -390,7 +449,7 @@ TEST_F(BrowserThemePackTest, ProvideNtpHeaderColor) {
LoadColorJSON(color_json); LoadColorJSON(color_json);
std::map<int, SkColor> colors = GetDefaultColorMap(); std::map<int, SkColor> colors = GetDefaultColorMap();
colors[ThemeProperties::COLOR_NTP_HEADER] = SkColorSetRGB(120, 120, 120); colors[TP::COLOR_NTP_HEADER] = SkColorSetRGB(120, 120, 120);
VerifyColorMap(colors); VerifyColorMap(colors);
} }
...@@ -406,13 +465,11 @@ TEST_F(BrowserThemePackTest, SupportsAlpha) { ...@@ -406,13 +465,11 @@ TEST_F(BrowserThemePackTest, SupportsAlpha) {
std::map<int, SkColor> colors = GetDefaultColorMap(); std::map<int, SkColor> colors = GetDefaultColorMap();
// Verify that valid alpha values are parsed correctly. // Verify that valid alpha values are parsed correctly.
// The toolbar color's alpha value is intentionally ignored by theme provider. // The toolbar color's alpha value is intentionally ignored by theme provider.
colors[ThemeProperties::COLOR_TOOLBAR] = SkColorSetARGB(255, 0, 20, 40); colors[TP::COLOR_TOOLBAR] = SkColorSetARGB(255, 0, 20, 40);
colors[ThemeProperties::COLOR_TAB_TEXT] = SkColorSetARGB(255, 60, 80, 100); colors[TP::COLOR_TAB_TEXT] = SkColorSetARGB(255, 60, 80, 100);
colors[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] = colors[TP::COLOR_BACKGROUND_TAB_TEXT] = SkColorSetARGB(0, 120, 140, 160);
SkColorSetARGB(0, 120, 140, 160); colors[TP::COLOR_BOOKMARK_TEXT] = SkColorSetARGB(255, 180, 200, 220);
colors[ThemeProperties::COLOR_BOOKMARK_TEXT] = colors[TP::COLOR_NTP_TEXT] = SkColorSetARGB(128, 240, 255, 0);
SkColorSetARGB(255, 180, 200, 220);
colors[ThemeProperties::COLOR_NTP_TEXT] = SkColorSetARGB(128, 240, 255, 0);
VerifyColorMap(colors); VerifyColorMap(colors);
} }
...@@ -434,8 +491,7 @@ TEST_F(BrowserThemePackTest, CanReadTints) { ...@@ -434,8 +491,7 @@ TEST_F(BrowserThemePackTest, CanReadTints) {
color_utils::HSL expected = { 0.5, 0.5, 0.5 }; color_utils::HSL expected = { 0.5, 0.5, 0.5 };
color_utils::HSL actual = { -1, -1, -1 }; color_utils::HSL actual = { -1, -1, -1 };
EXPECT_TRUE(theme_pack_->GetTint( EXPECT_TRUE(theme_pack().GetTint(TP::TINT_BUTTONS, &actual));
ThemeProperties::TINT_BUTTONS, &actual));
EXPECT_DOUBLE_EQ(expected.h, actual.h); EXPECT_DOUBLE_EQ(expected.h, actual.h);
EXPECT_DOUBLE_EQ(expected.s, actual.s); EXPECT_DOUBLE_EQ(expected.s, actual.s);
EXPECT_DOUBLE_EQ(expected.l, actual.l); EXPECT_DOUBLE_EQ(expected.l, actual.l);
...@@ -448,16 +504,16 @@ TEST_F(BrowserThemePackTest, CanReadDisplayProperties) { ...@@ -448,16 +504,16 @@ TEST_F(BrowserThemePackTest, CanReadDisplayProperties) {
LoadDisplayPropertiesJSON(json); LoadDisplayPropertiesJSON(json);
int out_val; int out_val;
EXPECT_TRUE(theme_pack_->GetDisplayProperty( EXPECT_TRUE(
ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &out_val)); theme_pack().GetDisplayProperty(TP::NTP_BACKGROUND_ALIGNMENT, &out_val));
EXPECT_EQ(ThemeProperties::ALIGN_BOTTOM, out_val); EXPECT_EQ(TP::ALIGN_BOTTOM, out_val);
EXPECT_TRUE(theme_pack_->GetDisplayProperty( EXPECT_TRUE(
ThemeProperties::NTP_BACKGROUND_TILING, &out_val)); theme_pack().GetDisplayProperty(TP::NTP_BACKGROUND_TILING, &out_val));
EXPECT_EQ(ThemeProperties::REPEAT_X, out_val); EXPECT_EQ(TP::REPEAT_X, out_val);
EXPECT_TRUE(theme_pack_->GetDisplayProperty( EXPECT_TRUE(
ThemeProperties::NTP_LOGO_ALTERNATE, &out_val)); theme_pack().GetDisplayProperty(TP::NTP_LOGO_ALTERNATE, &out_val));
EXPECT_EQ(0, out_val); EXPECT_EQ(0, out_val);
} }
...@@ -507,18 +563,17 @@ TEST_F(BrowserThemePackTest, InvalidTints) { ...@@ -507,18 +563,17 @@ TEST_F(BrowserThemePackTest, InvalidTints) {
// We should ignore completely invalid (non-numeric) tints. // We should ignore completely invalid (non-numeric) tints.
color_utils::HSL actual = { -1, -1, -1 }; color_utils::HSL actual = { -1, -1, -1 };
EXPECT_FALSE(theme_pack_->GetTint(ThemeProperties::TINT_BUTTONS, &actual)); EXPECT_FALSE(theme_pack().GetTint(TP::TINT_BUTTONS, &actual));
// We should change invalid numeric HSL tint components to the special -1 "no // We should change invalid numeric HSL tint components to the special -1 "no
// change" value. // change" value.
EXPECT_TRUE(theme_pack_->GetTint(ThemeProperties::TINT_FRAME, &actual)); EXPECT_TRUE(theme_pack().GetTint(TP::TINT_FRAME, &actual));
EXPECT_EQ(-1, actual.h); EXPECT_EQ(-1, actual.h);
EXPECT_EQ(-1, actual.s); EXPECT_EQ(-1, actual.s);
EXPECT_EQ(-1, actual.l); EXPECT_EQ(-1, actual.l);
// We should correct partially incorrect inputs as well. // We should correct partially incorrect inputs as well.
EXPECT_TRUE(theme_pack_->GetTint( EXPECT_TRUE(theme_pack().GetTint(TP::TINT_FRAME_INCOGNITO_INACTIVE, &actual));
ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE, &actual));
EXPECT_EQ(-1, actual.h); EXPECT_EQ(-1, actual.h);
EXPECT_EQ(-1, actual.s); EXPECT_EQ(-1, actual.s);
EXPECT_EQ(0.6, actual.l); EXPECT_EQ(0.6, actual.l);
...@@ -530,8 +585,8 @@ TEST_F(BrowserThemePackTest, InvalidDisplayProperties) { ...@@ -530,8 +585,8 @@ TEST_F(BrowserThemePackTest, InvalidDisplayProperties) {
LoadDisplayPropertiesJSON(invalid_properties); LoadDisplayPropertiesJSON(invalid_properties);
int out_val; int out_val;
EXPECT_FALSE(theme_pack_->GetDisplayProperty( EXPECT_FALSE(
ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &out_val)); theme_pack().GetDisplayProperty(TP::NTP_BACKGROUND_ALIGNMENT, &out_val));
} }
// These three tests should just not cause a segmentation fault. // These three tests should just not cause a segmentation fault.
...@@ -559,8 +614,8 @@ TEST_F(BrowserThemePackTest, TestHasCustomImage) { ...@@ -559,8 +614,8 @@ TEST_F(BrowserThemePackTest, TestHasCustomImage) {
TestFilePathMap out_file_paths; TestFilePathMap out_file_paths;
ParseImageNamesJSON(images, &out_file_paths); ParseImageNamesJSON(images, &out_file_paths);
EXPECT_TRUE(theme_pack_->HasCustomImage(IDR_THEME_FRAME)); EXPECT_TRUE(theme_pack().HasCustomImage(IDR_THEME_FRAME));
EXPECT_FALSE(theme_pack_->HasCustomImage(IDR_THEME_FRAME_INCOGNITO)); EXPECT_FALSE(theme_pack().HasCustomImage(IDR_THEME_FRAME_INCOGNITO));
} }
TEST_F(BrowserThemePackTest, TestNonExistantImages) { TEST_F(BrowserThemePackTest, TestNonExistantImages) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment