Chromium Code Reviews| Index: ash/common/shelf/shelf_background_animator_unittest.cc |
| diff --git a/ash/common/shelf/shelf_background_animator_unittest.cc b/ash/common/shelf/shelf_background_animator_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d174bbc83fa30c8edb0c55c3161cd447b98991cf |
| --- /dev/null |
| +++ b/ash/common/shelf/shelf_background_animator_unittest.cc |
| @@ -0,0 +1,287 @@ |
| +// Copyright 2016 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/common/shelf/shelf_background_animator.h" |
| + |
| +#include "ash/common/material_design/test/material_design_controller_test_api.h" |
| +#include "ash/common/shelf/shelf_background_animator_observer.h" |
| +#include "ash/common/shelf/shelf_constants.h" |
| +#include "base/bind.h" |
| +#include "base/macros.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace ash { |
| +namespace test { |
| + |
| +using test::MaterialDesignControllerTestAPI; |
| + |
| +namespace { |
| + |
| +const int kMaxAlpha = 255; |
| + |
| +// Observer that caches alpha values for the last observation. |
| +class TestShelfBackgroundObserver : public ShelfBackgroundAnimatorObserver { |
| + public: |
| + TestShelfBackgroundObserver(); |
| + ~TestShelfBackgroundObserver() override; |
| + |
| + int opaque_background_alpha() const { return opaque_background_alpha_; } |
| + |
| + int item_background_alpha() const { return item_background_alpha_; } |
| + |
| + int asset_background_alpha() const { return asset_background_alpha_; } |
| + |
| + // ShelfBackgroundObserver: |
| + void UpdateShelfOpaqueBackground(int alpha) override; |
| + void UpdateShelfAssetBackground(int alpha) override; |
| + void UpdateShelfItemBackground(int alpha) override; |
| + |
| + private: |
| + int opaque_background_alpha_; |
| + |
| + int item_background_alpha_; |
| + |
|
James Cook
2016/06/14 17:50:02
nit: no blank lines needed
bruthig
2016/07/26 19:50:02
Done.
|
| + int asset_background_alpha_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestShelfBackgroundObserver); |
| +}; |
| + |
| +TestShelfBackgroundObserver::TestShelfBackgroundObserver() |
| + : opaque_background_alpha_(0), |
|
James Cook
2016/06/14 17:50:01
optional: initialize in class declaration
bruthig
2016/07/26 19:50:02
Done.
|
| + item_background_alpha_(0), |
| + asset_background_alpha_(0) {} |
| + |
| +TestShelfBackgroundObserver::~TestShelfBackgroundObserver() {} |
| + |
| +void TestShelfBackgroundObserver::UpdateShelfOpaqueBackground(int alpha) { |
| + opaque_background_alpha_ = alpha; |
| +} |
| + |
| +void TestShelfBackgroundObserver::UpdateShelfAssetBackground(int alpha) { |
| + asset_background_alpha_ = alpha; |
| +} |
| + |
| +void TestShelfBackgroundObserver::UpdateShelfItemBackground(int alpha) { |
| + item_background_alpha_ = alpha; |
| +} |
| + |
| +} // namespace |
| + |
| +// Provides internal access to a ShelfBackgroundAnimator instance. |
| +class ShelfBackgroundAnimatorTestApi { |
| + public: |
| + explicit ShelfBackgroundAnimatorTestApi(ShelfBackgroundAnimator* animator); |
| + |
| + virtual ~ShelfBackgroundAnimatorTestApi(); |
|
James Cook
2016/06/14 17:50:01
doesn't need virtual
bruthig
2016/07/26 19:50:02
Done.
|
| + |
| + ShelfBackgroundType previous_background_type() const { |
| + return animator_->previous_background_type_; |
| + } |
| + |
| + BackgroundAnimator* opaque_background_animator() const { |
| + return animator_->opaque_background_animator_.get(); |
| + } |
| + |
| + BackgroundAnimator* asset_background_animator() const { |
| + return animator_->asset_background_animator_.get(); |
| + } |
| + |
| + BackgroundAnimator* item_background_animator() const { |
| + return animator_->item_background_animator_.get(); |
| + } |
| + |
| + private: |
| + // The instance to provide internal access to. |
| + ShelfBackgroundAnimator* animator_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestApi); |
| +}; |
| + |
| +ShelfBackgroundAnimatorTestApi::ShelfBackgroundAnimatorTestApi( |
| + ShelfBackgroundAnimator* animator) |
| + : animator_(animator) {} |
| + |
| +ShelfBackgroundAnimatorTestApi::~ShelfBackgroundAnimatorTestApi() {} |
|
James Cook
2016/06/14 17:50:01
optional: feel free to inline little methods like
bruthig
2016/07/26 19:50:02
Done.
|
| + |
| +class ShelfBackgroundAnimatorTestBase : public testing::Test { |
| + public: |
| + ShelfBackgroundAnimatorTestBase(); |
| + ~ShelfBackgroundAnimatorTestBase() override; |
| + |
| + virtual MaterialDesignController::Mode GetMaterialDesignMode() = 0; |
| + |
| + int expected_translucent_alpha() const { return expected_translucent_alpha_; } |
| + |
| + // testing::Test: |
| + void SetUp() override; |
| + |
| + protected: |
| + // Convenience wrapper for |animator_|'s PaintBackground() that always uses |
| + // BACKGROUND_CHANGE_IMMEDIATE. |
| + void PaintBackground(ShelfBackgroundType background_type); |
| + |
| + TestShelfBackgroundObserver observer_; |
| + |
| + // Test target. |
| + std::unique_ptr<ShelfBackgroundAnimator> animator_; |
| + |
| + // Provides internal access to |animator_|. |
| + std::unique_ptr<ShelfBackgroundAnimatorTestApi> test_api_; |
| + |
| + private: |
| + std::unique_ptr<MaterialDesignControllerTestAPI> material_mode_test_api_; |
| + |
| + // The expected alpha value for translucent items. Cannot be a constant |
| + // because it is different for material design and non-material. |
| + int expected_translucent_alpha_; |
|
James Cook
2016/06/14 17:50:01
other reviewers may disagree, but I'm generally OK
bruthig
2016/07/26 19:50:02
Normally I would use protected members as well but
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestBase); |
| +}; |
| + |
| +ShelfBackgroundAnimatorTestBase::ShelfBackgroundAnimatorTestBase() |
| + : expected_translucent_alpha_(0) {} |
|
James Cook
2016/06/14 17:50:01
optional: init in class decl above
bruthig
2016/07/26 19:50:02
Done.
|
| + |
| +ShelfBackgroundAnimatorTestBase::~ShelfBackgroundAnimatorTestBase() {} |
| + |
| +void ShelfBackgroundAnimatorTestBase::SetUp() { |
| + material_mode_test_api_.reset( |
| + new MaterialDesignControllerTestAPI(GetMaterialDesignMode())); |
| + animator_.reset(new ShelfBackgroundAnimator()); |
| + animator_->AddObserver(&observer_); |
| + |
| + test_api_.reset(new ShelfBackgroundAnimatorTestApi(animator_.get())); |
| + |
| + // Initialized after the Material Design mode because GetShelfConstant() |
| + // depends on the mode. |
| + expected_translucent_alpha_ = GetShelfConstant(SHELF_BACKGROUND_ALPHA); |
| +} |
| + |
| +void ShelfBackgroundAnimatorTestBase::PaintBackground( |
| + ShelfBackgroundType background_type) { |
| + animator_->PaintBackground(background_type, BACKGROUND_CHANGE_IMMEDIATE); |
| +} |
| + |
| +class ShelfBackgroundAnimatorNonMDTest |
| + : public ShelfBackgroundAnimatorTestBase { |
| + public: |
| + ShelfBackgroundAnimatorNonMDTest() {} |
| + ~ShelfBackgroundAnimatorNonMDTest() override {} |
| + |
| + MaterialDesignController::Mode GetMaterialDesignMode() override { |
| + return MaterialDesignController::NON_MATERIAL; |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorNonMDTest); |
| +}; |
| + |
| +// Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state. |
| +TEST_F(ShelfBackgroundAnimatorNonMDTest, DefaultBackground) { |
| + PaintBackground(SHELF_BACKGROUND_DEFAULT); |
| + |
| + EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); |
| + EXPECT_EQ(0, observer_.opaque_background_alpha()); |
| + EXPECT_EQ(0, observer_.asset_background_alpha()); |
| + EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); |
| +} |
| + |
| +// Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state. |
| +TEST_F(ShelfBackgroundAnimatorNonMDTest, OverlapBackground) { |
| + PaintBackground(SHELF_BACKGROUND_OVERLAP); |
| + |
| + EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type()); |
| + EXPECT_EQ(0, observer_.opaque_background_alpha()); |
| + EXPECT_EQ(expected_translucent_alpha(), observer_.asset_background_alpha()); |
| + EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); |
| +} |
| + |
| +// Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state. |
| +TEST_F(ShelfBackgroundAnimatorNonMDTest, MaximizedBackground) { |
| + PaintBackground(SHELF_BACKGROUND_MAXIMIZED); |
| + |
| + EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); |
| + EXPECT_EQ(kMaxAlpha, observer_.opaque_background_alpha()); |
| + EXPECT_EQ(expected_translucent_alpha(), observer_.asset_background_alpha()); |
| + EXPECT_EQ(kMaxAlpha, observer_.item_background_alpha()); |
| +} |
| + |
| +class ShelfBackgroundAnimatorMDTest : public ShelfBackgroundAnimatorTestBase { |
| + public: |
| + ShelfBackgroundAnimatorMDTest() {} |
| + ~ShelfBackgroundAnimatorMDTest() override {} |
| + |
| + MaterialDesignController::Mode GetMaterialDesignMode() override { |
| + return MaterialDesignController::MATERIAL_EXPERIMENTAL; |
|
James Cook
2016/06/14 17:50:02
So the changes in this CL are tied to MATERIAL_EXP
bruthig
2016/07/26 19:50:02
Correct, but more accurately this CL is tied to th
|
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorMDTest); |
| +}; |
| + |
| +// Verify the |previous_background_type_| and |target_background_type_| values |
| +// when animating to the same target type multiple times. |
| +TEST_F(ShelfBackgroundAnimatorMDTest, |
| + BackgroundTypesWhenAnimatingToSameTarget) { |
| + PaintBackground(SHELF_BACKGROUND_MAXIMIZED); |
| + EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); |
| + |
| + PaintBackground(SHELF_BACKGROUND_DEFAULT); |
| + EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); |
| + EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type()); |
| + |
| + PaintBackground(SHELF_BACKGROUND_DEFAULT); |
| + EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); |
| + EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type()); |
| +} |
| + |
| +// Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state. |
| +TEST_F(ShelfBackgroundAnimatorMDTest, DefaultBackground) { |
| + PaintBackground(SHELF_BACKGROUND_DEFAULT); |
| + |
| + EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); |
| + EXPECT_EQ(0, observer_.opaque_background_alpha()); |
| + EXPECT_EQ(0, observer_.asset_background_alpha()); |
| + EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); |
| +} |
| + |
| +// Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state. |
| +TEST_F(ShelfBackgroundAnimatorMDTest, OverlapBackground) { |
| + PaintBackground(SHELF_BACKGROUND_OVERLAP); |
| + |
| + EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type()); |
| + EXPECT_EQ(expected_translucent_alpha(), observer_.opaque_background_alpha()); |
| + EXPECT_EQ(0, observer_.asset_background_alpha()); |
| + EXPECT_EQ(0, observer_.item_background_alpha()); |
| +} |
| + |
| +// Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state. |
| +TEST_F(ShelfBackgroundAnimatorMDTest, MaximizedBackground) { |
| + PaintBackground(SHELF_BACKGROUND_MAXIMIZED); |
| + |
| + EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); |
| + EXPECT_EQ(kMaxAlpha, observer_.opaque_background_alpha()); |
| + EXPECT_EQ(0, observer_.asset_background_alpha()); |
| + EXPECT_EQ(0, observer_.item_background_alpha()); |
| +} |
| + |
| +// Verify that existing animators are used when animating to the previous state. |
| +TEST_F(ShelfBackgroundAnimatorMDTest, VerifyExistingAnimatorsAreReused) { |
| + PaintBackground(SHELF_BACKGROUND_DEFAULT); |
| + PaintBackground(SHELF_BACKGROUND_MAXIMIZED); |
| + const BackgroundAnimator* opaque_animator = |
| + test_api_->opaque_background_animator(); |
| + const BackgroundAnimator* asset_animator = |
| + test_api_->asset_background_animator(); |
| + const BackgroundAnimator* item_animator = |
| + test_api_->item_background_animator(); |
| + |
| + PaintBackground(SHELF_BACKGROUND_DEFAULT); |
| + |
| + EXPECT_EQ(opaque_animator, test_api_->opaque_background_animator()); |
| + EXPECT_EQ(asset_animator, test_api_->asset_background_animator()); |
| + EXPECT_EQ(item_animator, test_api_->item_background_animator()); |
| +} |
| + |
| +} // namespace test |
| +} // namespace ash |
|
James Cook
2016/06/14 17:50:01
Nice test suite.
bruthig
2016/07/26 19:50:02
:D
|