Skip to content

Commit

Permalink
[Pixel Diff Ash] Add a helper class to fetch icon colors
Browse files Browse the repository at this point in the history
This CL adds a test helper class that returns different colors in
several consecutive color fetches. This helper class is useful in
pixel tests where we would like to add several items of different
icon colors to verify the item order.

This CL should not bring any noticeable changes.

Bug: 1351444
Change-Id: I3971780f2e73908be31ea41d45d5b1898f7c9aae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3829656
Reviewed-by: Xiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Andrew Xu <andrewxu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1035146}
  • Loading branch information
Andrew Xu authored and Chromium LUCI CQ committed Aug 15, 2022
1 parent 0d34fe2 commit a27dc46
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
2 changes: 2 additions & 0 deletions ash/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3430,6 +3430,8 @@ static_library("test_support") {
"test/ash_pixel_test_init_params.h",
"test/ash_test_base.cc",
"test/ash_test_base.h",
"test/ash_test_color_generator.cc",
"test/ash_test_color_generator.h",
"test/ash_test_helper.cc",
"test/ash_test_helper.h",
"test/ash_test_suite.cc",
Expand Down
19 changes: 6 additions & 13 deletions ash/shelf/test/scrollable_shelf_test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
namespace ash {
namespace {

// The array of the candidate colors for app icons.
constexpr std::array<SkColor, 7> kColorArray = {
SK_ColorWHITE, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
SK_ColorYELLOW, SK_ColorCYAN, SK_ColorMAGENTA};

// Create a test 1x1 icon image with a given |color|.
gfx::ImageSkia CreateImageSkiaIcon(SkColor color) {
SkBitmap bitmap;
Expand Down Expand Up @@ -55,15 +50,19 @@ void ScrollableShelfTestBase::PopulateAppShortcut(int number,
bool use_alternative_color) {
for (int i = 0; i < number; i++)
AddAppShortcutWithIconColor(
TYPE_PINNED_APP, use_alternative_color ? GetNextColor() : SK_ColorRED);
TYPE_PINNED_APP, use_alternative_color
? icon_color_generator_.GetAlternativeColor()
: icon_color_generator_.default_color());
}

void ScrollableShelfTestBase::AddAppShortcutsUntilOverflow(
bool use_alternative_color) {
while (scrollable_shelf_view_->layout_strategy_for_test() ==
ScrollableShelfView::kNotShowArrowButtons) {
AddAppShortcutWithIconColor(
TYPE_PINNED_APP, use_alternative_color ? GetNextColor() : SK_ColorRED);
TYPE_PINNED_APP, use_alternative_color
? icon_color_generator_.GetAlternativeColor()
: icon_color_generator_.default_color());
}
}

Expand All @@ -80,10 +79,4 @@ ShelfID ScrollableShelfTestBase::AddAppShortcutWithIconColor(
return item.id;
}

SkColor ScrollableShelfTestBase::GetNextColor() {
const SkColor color = kColorArray[next_color_index_];
next_color_index_ = (next_color_index_ + 1) % kColorArray.size();
return color;
}

} // namespace ash
9 changes: 2 additions & 7 deletions ash/shelf/test/scrollable_shelf_test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ash/public/cpp/shelf_types.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_color_generator.h"

namespace ash {
class ScrollableShelfView;
Expand Down Expand Up @@ -38,20 +39,14 @@ class ScrollableShelfTestBase : public AshTestBase {
// Adds a shelf item of the specified type and color.
ShelfID AddAppShortcutWithIconColor(ShelfItemType item_type, SkColor color);

// Returns a color used to paint an icon. Called when neighboring pinned
// apps should have different icon colors.
SkColor GetNextColor();

ScrollableShelfView* scrollable_shelf_view_ = nullptr;
ShelfView* shelf_view_ = nullptr;
std::unique_ptr<ShelfViewTestAPI> test_api_;

// Used as the id of the next pinned app. Updates when an app is pinned.
int id_ = 0;

// Indicates the color to be returned by `GetNextColor()`. Updates when
// `GetNextColor()` is called.
size_t next_color_index_ = 0;
AshTestColorGenerator icon_color_generator_{/*default_color=*/SK_ColorRED};
};

} // namespace ash
Expand Down
29 changes: 29 additions & 0 deletions ash/test/ash_test_color_generator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/test/ash_test_color_generator.h"

namespace ash {

namespace {

// The array of the candidate colors for app icons.
constexpr std::array<SkColor, 7> kColorArray = {
SK_ColorWHITE, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
SK_ColorYELLOW, SK_ColorCYAN, SK_ColorMAGENTA};

} // namespace

AshTestColorGenerator::AshTestColorGenerator(SkColor default_color)
: default_color_(default_color) {}

AshTestColorGenerator::~AshTestColorGenerator() = default;

SkColor AshTestColorGenerator::GetAlternativeColor() {
const SkColor color = kColorArray[next_color_index_];
next_color_index_ = (next_color_index_ + 1) % kColorArray.size();
return color;
}

} // namespace ash
34 changes: 34 additions & 0 deletions ash/test/ash_test_color_generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_TEST_ASH_TEST_COLOR_GENERATOR_H_
#define ASH_TEST_ASH_TEST_COLOR_GENERATOR_H_

#include "third_party/skia/include/core/SkColor.h"

namespace ash {

// A helper class to return a color. Usually used for solid-colored icons.
class AshTestColorGenerator {
public:
explicit AshTestColorGenerator(SkColor default_color);
AshTestColorGenerator(const AshTestColorGenerator&) = delete;
AshTestColorGenerator& operator=(const AshTestColorGenerator&) = delete;
~AshTestColorGenerator();

SkColor default_color() const { return default_color_; }

// This method guarantees that two consequent callings get different colors.
SkColor GetAlternativeColor();

private:
const SkColor default_color_;

// The next color to be returned by `GetAlternativeColor()`.
size_t next_color_index_ = 0;
};

} // namespace ash

#endif // ASH_TEST_ASH_TEST_COLOR_GENERATOR_H_

0 comments on commit a27dc46

Please sign in to comment.