forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The PageActionModel started having complex logic. As a result, this CL add a basic test to validate it behavior. Bug: 388527536 Change-Id: Ib61f72d1b3967c3ebaa5985086f887183bc623c6 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6217184 Reviewed-by: Caroline Rising <[email protected]> Reviewed-by: Kaan Alsan <[email protected]> Commit-Queue: Foromo Daniel Soromou <[email protected]> Cr-Commit-Position: refs/heads/main@{#1414363}
- Loading branch information
Foromo Daniel Soromou
authored and
Chromium LUCI CQ
committed
Jan 31, 2025
1 parent
0e89bed
commit 9098400
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
chrome/browser/ui/views/page_action/page_action_model_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2025 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/ui/views/page_action/page_action_model.h" | ||
|
||
#include "base/types/pass_key.h" | ||
#include "chrome/browser/ui/views/page_action/page_action_controller.h" | ||
#include "chrome/browser/ui/views/page_action/page_action_model_observer.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
#include "ui/actions/actions.h" | ||
#include "ui/gfx/image/image_unittest_util.h" | ||
|
||
namespace page_actions { | ||
namespace { | ||
|
||
using ::actions::ActionItem; | ||
|
||
const std::u16string kOverrideText = u"Override"; | ||
const std::u16string kTestText = u"Test"; | ||
const std::u16string kTooltipText = u"Tooltip"; | ||
const ui::ImageModel kTestImage = | ||
ui::ImageModel::FromImageSkia(gfx::test::CreateImageSkia(/*size=*/16)); | ||
|
||
base::PassKey<PageActionController> PassKey() { | ||
return PageActionController::PassKeyForTesting(); | ||
} | ||
|
||
class MockPageActionModelObserver : public PageActionModelObserver { | ||
public: | ||
MOCK_METHOD(void, | ||
OnPageActionModelChanged, | ||
(const PageActionModelInterface& model), | ||
(override)); | ||
MOCK_METHOD(void, | ||
OnPageActionModelWillBeDeleted, | ||
(const PageActionModelInterface& model), | ||
(override)); | ||
}; | ||
|
||
class PageActionModelTest : public ::testing::Test { | ||
protected: | ||
void SetUp() override { model_.AddObserver(&observer_); } | ||
|
||
void TearDown() override { model_.RemoveObserver(&observer_); } | ||
|
||
PageActionModel model_; | ||
testing::NiceMock<MockPageActionModelObserver> observer_; | ||
}; | ||
|
||
TEST_F(PageActionModelTest, DefaultVisibility) { | ||
EXPECT_FALSE(model_.GetVisible()); | ||
} | ||
|
||
TEST_F(PageActionModelTest, VisibilityConditions) { | ||
EXPECT_CALL(observer_, OnPageActionModelChanged).Times(4); | ||
|
||
model_.SetShowRequested(PassKey(), true); | ||
EXPECT_FALSE(model_.GetVisible()); | ||
|
||
model_.SetActionItemProperties( | ||
PassKey(), | ||
ActionItem::Builder().SetEnabled(true).SetVisible(true).Build().get()); | ||
EXPECT_FALSE(model_.GetVisible()); | ||
|
||
model_.SetTabActive(PassKey(), true); | ||
EXPECT_TRUE(model_.GetVisible()); | ||
|
||
model_.SetHasPinnedIcon(PassKey(), true); | ||
EXPECT_FALSE(model_.GetVisible()); | ||
} | ||
|
||
TEST_F(PageActionModelTest, OverrideText) { | ||
EXPECT_CALL(observer_, OnPageActionModelChanged).Times(2); | ||
|
||
model_.SetOverrideText(PassKey(), kOverrideText); | ||
EXPECT_EQ(model_.GetText(), kOverrideText); | ||
|
||
model_.SetOverrideText(PassKey(), std::nullopt); | ||
EXPECT_EQ(model_.GetText(), std::u16string()); | ||
} | ||
|
||
TEST_F(PageActionModelTest, SetActionItemProperties) { | ||
// NOTE: The visibility is exercised by the test VisibilityConditions. | ||
EXPECT_CALL(observer_, OnPageActionModelChanged).Times(1); | ||
|
||
model_.SetActionItemProperties(PassKey(), ActionItem::Builder() | ||
.SetText(kTestText) | ||
.SetTooltipText(kTooltipText) | ||
.SetImage(kTestImage) | ||
.Build() | ||
.get()); | ||
|
||
EXPECT_EQ(model_.GetText(), kTestText); | ||
EXPECT_EQ(model_.GetImage(), kTestImage); | ||
EXPECT_EQ(model_.GetTooltipText(), kTooltipText); | ||
} | ||
|
||
} // namespace | ||
} // namespace page_actions |