1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_view.h"
#include "chrome/browser/ui/views/page_action/page_action_view.h"
#include "chrome/browser/ui/views/translate/partial_translate_bubble_view.h"
#include "chrome/browser/ui/views/translate/translate_bubble_controller.h"
#include "chrome/browser/ui/views/translate/translate_icon_view.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "components/translate/core/browser/translate_manager.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/test/ui_controls.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/test/views_test_utils.h"
namespace translate {
class TranslateIconViewTest : public InProcessBrowserTest,
public ::testing::WithParamInterface<bool> {
public:
TranslateIconViewTest() {
if (IsMigrationEnabled()) {
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kPageActionsMigration,
{{features::kPageActionsMigrationTranslate.name, "true"}});
} else {
scoped_feature_list_.InitWithFeatures(
{}, {::features::kPageActionsMigration});
}
}
TranslateIconViewTest(const TranslateIconViewTest&) = delete;
TranslateIconViewTest& operator=(const TranslateIconViewTest&) = delete;
~TranslateIconViewTest() override = default;
// Returns the page action view that should be enabled for the current
// feature flag state.
IconLabelBubbleView* GetTranslateIcon() {
ToolbarButtonProvider* provider =
BrowserView::GetBrowserViewForBrowser(browser())
->toolbar_button_provider();
if (IsMigrationEnabled()) {
return provider->GetPageActionView(kActionShowTranslate);
}
return provider->GetPageActionIconView(PageActionIconType::kTranslate);
}
views::BubbleDialogDelegate* GetBubble() const {
return browser()
->GetFeatures()
.translate_bubble_controller()
->GetTranslateBubble();
}
PartialTranslateBubbleView* GetPartialTranslateBubble() {
return browser()
->GetFeatures()
.translate_bubble_controller()
->GetPartialTranslateBubble();
}
std::unique_ptr<views::Widget> CreateTestWidget(
views::Widget::InitParams::Ownership ownership) {
auto widget = std::make_unique<views::Widget>();
views::Widget::InitParams params(ownership,
views::Widget::InitParams::TYPE_WINDOW);
widget->Init(std::move(params));
// TODO(https://crbug.com/329235190): The bubble child of a widget that is
// invisible will not be mapped through wayland and hence never shown so
// widget must be shown. However, showing widget causes
// RunAccessibilityPaintChecks() to fail when this feature is disabled due
// to node_data.GetNameFrom() == kContents.
if (views::test::IsOzoneBubblesUsingPlatformWidgets()) {
widget->Show();
}
return widget;
}
bool IsMigrationEnabled() const { return GetParam(); }
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(All,
TranslateIconViewTest,
::testing::Values(false, true),
[](const ::testing::TestParamInfo<bool>& info) {
return info.param ? "MigrationEnabled"
: "MigrationDisabled";
});
// Verifies that clicking the Translate icon closes the Partial Translate bubble
// and results in neither of the two Translate bubbles being shown.
IN_PROC_BROWSER_TEST_P(TranslateIconViewTest, ClosePartialTranslateBubble) {
// Show the Translate icon.
ChromeTranslateClient::FromWebContents(
browser()->tab_strip_model()->GetActiveWebContents())
->GetTranslateManager()
->GetLanguageState()
->SetTranslateEnabled(true);
auto* translate_icon = GetTranslateIcon();
EXPECT_THAT(translate_icon, ::testing::NotNull());
TranslateBubbleController* controller =
browser()->GetFeatures().translate_bubble_controller();
auto anchor_widget =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
views::View* anchor_view = anchor_widget->GetContentsView();
controller->StartPartialTranslate(
browser()->tab_strip_model()->GetActiveWebContents(), anchor_view,
nullptr, "fr", "en", std::u16string());
base::RunLoop().RunUntilIdle();
EXPECT_THAT(GetPartialTranslateBubble(), ::testing::NotNull());
// Clicking the icon should close the Partial Translate bubble and should not
// open the Full Page Translate bubble.
base::RunLoop loop;
ui_test_utils::MoveMouseToCenterAndClick(translate_icon, ui_controls::LEFT,
ui_controls::DOWN | ui_controls::UP,
loop.QuitClosure());
loop.Run();
EXPECT_THAT(GetPartialTranslateBubble(), ::testing::IsNull());
EXPECT_THAT(GetBubble(), ::testing::IsNull());
}
IN_PROC_BROWSER_TEST_P(TranslateIconViewTest, IconViewAccessibleName) {
if (IsMigrationEnabled()) {
EXPECT_EQ(GetTranslateIcon()->GetViewAccessibility().GetCachedName(),
BrowserActions::GetCleanTitleAndTooltipText(
l10n_util::GetStringUTF16(IDS_SHOW_TRANSLATE)));
} else {
EXPECT_EQ(GetTranslateIcon()->GetViewAccessibility().GetCachedName(),
l10n_util::GetStringUTF16(IDS_TOOLTIP_TRANSLATE));
}
EXPECT_EQ(GetTranslateIcon()->GetTooltipText(),
l10n_util::GetStringUTF16(IDS_TOOLTIP_TRANSLATE));
}
} // namespace translate
|