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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
// Copyright 2024 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/mahi/mahi_menu_controller.h"
#include <memory>
#include <string>
#include "ash/constants/ash_switches.h"
#include "ash/system/mahi/fake_mahi_manager.h"
#include "ash/system/mahi/test/mock_mahi_media_app_events_proxy.h"
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/ash/mahi/web_contents/test_support/fake_mahi_web_contents_manager.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/ui/ash/editor_menu/utils/utils.h"
#include "chrome/browser/ui/ash/read_write_cards/read_write_cards_ui_controller.h"
#include "chrome/browser/ui/views/mahi/mahi_condensed_menu_view.h"
#include "chrome/browser/ui/views/mahi/mahi_menu_constants.h"
#include "chrome/browser/ui/views/mahi/mahi_menu_view.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "chromeos/components/mahi/public/cpp/mahi_media_app_events_proxy.h"
#include "chromeos/components/mahi/public/cpp/mahi_web_contents_manager.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/application_locale_storage/application_locale_storage.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/view_utils.h"
namespace chromeos::mahi {
namespace {
const std::string& GetApplicationLocale() {
return TestingBrowserProcess::GetGlobal()
->GetFeatures()
->application_locale_storage()
->Get();
}
} // namespace
using ::testing::IsNull;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::Return;
class MahiMenuControllerTest : public ChromeViewsTestBase {
public:
MahiMenuControllerTest() {
menu_controller_ =
std::make_unique<MahiMenuController>(TestingBrowserProcess::GetGlobal()
->GetFeatures()
->application_locale_storage(),
read_write_cards_ui_controller_);
scoped_mahi_web_contents_manager_ =
std::make_unique<chromeos::ScopedMahiWebContentsManagerOverride>(
&fake_mahi_web_contents_manager_);
fake_mahi_manager_ = std::make_unique<ash::FakeMahiManager>();
// Sets the focused page's distillability to true so that it does not block
// the menu widget's display.
ChangePageDistillability(true);
}
MahiMenuControllerTest(const MahiMenuControllerTest&) = delete;
MahiMenuControllerTest& operator=(const MahiMenuControllerTest&) = delete;
~MahiMenuControllerTest() override = default;
void SetUp() override {
feature_list_.InitWithFeatures(
/*enabled_features=*/{chromeos::features::kMahi,
chromeos::features::kFeatureManagementMahi},
/*disabled_features=*/{});
ChromeViewsTestBase::SetUp();
}
void TearDown() override {
// Manually reset `menu_controller_` here because it requires the existence
// of `mock_mahi_media_app_events_proxy_` to destroy.
menu_controller_.reset();
ChromeViewsTestBase::TearDown();
}
MahiMenuController* menu_controller() { return menu_controller_.get(); }
void ChangePageDistillability(bool value) {
fake_mahi_web_contents_manager_.set_focused_web_content_is_distillable(
value);
}
protected:
ReadWriteCardsUiController read_write_cards_ui_controller_;
std::unique_ptr<ash::FakeMahiManager> fake_mahi_manager_;
::mahi::FakeMahiWebContentsManager fake_mahi_web_contents_manager_;
private:
base::test::ScopedFeatureList feature_list_;
// Providing a mock MahiMediaAppEvnetsProxy and a fake mahi manager to satisfy
// MahiMenuController.
testing::NiceMock<::ash::MockMahiMediaAppEventsProxy>
mock_mahi_media_app_events_proxy_;
chromeos::ScopedMahiMediaAppEventsProxySetter
scoped_mahi_media_app_events_proxy_{&mock_mahi_media_app_events_proxy_};
std::unique_ptr<MahiMenuController> menu_controller_;
std::unique_ptr<chromeos::ScopedMahiWebContentsManagerOverride>
scoped_mahi_web_contents_manager_;
};
// Tests the behavior of the controller when there's no text selected when
// `OnTextAvailable()` is triggered.
TEST_F(MahiMenuControllerTest, TextNotSelected) {
EXPECT_FALSE(menu_controller()->menu_widget_for_test());
// Menu widget should show when text is displayed.
menu_controller()->OnTextAvailable(/*anchor_bounds=*/gfx::Rect(),
/*selected_text=*/"",
/*surrounding_text=*/"");
EXPECT_TRUE(menu_controller()->menu_widget_for_test());
EXPECT_TRUE(menu_controller()->menu_widget_for_test()->IsVisible());
EXPECT_TRUE(views::IsViewClass<MahiMenuView>(
menu_controller()->menu_widget_for_test()->GetContentsView()));
EXPECT_EQ(u"", fake_mahi_web_contents_manager_.GetSelectedText());
// Menu widget should hide when dismissed.
menu_controller()->OnDismiss(/*is_other_command_executed=*/false);
EXPECT_FALSE(menu_controller()->menu_widget_for_test());
// If page is not distillable, then menu widget should not be triggered.
ChangePageDistillability(false);
menu_controller()->OnTextAvailable(/*anchor_bounds=*/gfx::Rect(),
/*selected_text=*/"",
/*surrounding_text=*/"");
EXPECT_FALSE(menu_controller()->menu_widget_for_test());
}
// Tests the behavior of the controller when `OnAnchorBoundsChanged()` is
// triggered.
TEST_F(MahiMenuControllerTest, BoundsChanged) {
EXPECT_FALSE(menu_controller()->menu_widget_for_test());
gfx::Rect anchor_bounds = gfx::Rect(50, 50, 25, 100);
menu_controller()->OnTextAvailable(anchor_bounds,
/*selected_text=*/"",
/*surrounding_text=*/"");
auto* widget = menu_controller()->menu_widget_for_test();
EXPECT_TRUE(widget);
EXPECT_EQ(
editor_menu::GetEditorMenuBounds(anchor_bounds, widget->GetContentsView(),
GetApplicationLocale(),
editor_menu::CardType::kMahiDefaultMenu),
widget->GetRestoredBounds());
anchor_bounds = gfx::Rect(0, 50, 55, 80);
// Widget should change bounds accordingly.
menu_controller()->OnAnchorBoundsChanged(anchor_bounds);
EXPECT_EQ(
editor_menu::GetEditorMenuBounds(anchor_bounds, widget->GetContentsView(),
GetApplicationLocale(),
editor_menu::CardType::kMahiDefaultMenu),
widget->GetRestoredBounds());
}
// Tests the behavior of the controller when there's text selected when
// `OnTextAvailable()` is triggered.
TEST_F(MahiMenuControllerTest, TextSelected) {
EXPECT_FALSE(read_write_cards_ui_controller_.widget_for_test());
// Menu widget should show when text is displayed.
menu_controller()->OnTextAvailable(/*anchor_bounds=*/gfx::Rect(),
/*selected_text=*/"test selected text",
/*surrounding_text=*/"");
EXPECT_TRUE(read_write_cards_ui_controller_.widget_for_test());
EXPECT_TRUE(read_write_cards_ui_controller_.widget_for_test()->IsVisible());
EXPECT_TRUE(read_write_cards_ui_controller_.GetMahiUiForTest());
EXPECT_TRUE(views::IsViewClass<MahiCondensedMenuView>(
read_write_cards_ui_controller_.GetMahiUiForTest()));
EXPECT_EQ(u"test selected text",
fake_mahi_web_contents_manager_.GetSelectedText());
// Menu widget should hide when dismissed.
menu_controller()->OnDismiss(/*is_other_command_executed=*/false);
EXPECT_FALSE(read_write_cards_ui_controller_.widget_for_test());
EXPECT_FALSE(read_write_cards_ui_controller_.GetMahiUiForTest());
}
// Tests the behavior of the controller when feature enable state changed.
TEST_F(MahiMenuControllerTest, FeatureEnableStatusChange) {
EXPECT_FALSE(menu_controller()->menu_widget_for_test());
// Menu widget should show when text is displayed as the default is that Mahi
// is enabled.
menu_controller()->OnTextAvailable(/*anchor_bounds=*/gfx::Rect(),
/*selected_text=*/"",
/*surrounding_text=*/"");
EXPECT_TRUE(menu_controller()->menu_widget_for_test());
EXPECT_TRUE(menu_controller()->menu_widget_for_test()->IsVisible());
EXPECT_TRUE(views::IsViewClass<MahiMenuView>(
menu_controller()->menu_widget_for_test()->GetContentsView()));
// Menu widget should hide when dismissed.
menu_controller()->OnDismiss(/*is_other_command_executed=*/false);
EXPECT_FALSE(menu_controller()->menu_widget_for_test());
// If MahiManager says the feature is not enabled, then menu widget should not
// be triggered.
fake_mahi_manager_->set_mahi_enabled(false);
menu_controller()->OnTextAvailable(/*anchor_bounds=*/gfx::Rect(),
/*selected_text=*/"",
/*surrounding_text=*/"");
EXPECT_FALSE(menu_controller()->menu_widget_for_test());
// Set mahi_enabled to true should show the widget again.
fake_mahi_manager_->set_mahi_enabled(true);
menu_controller()->OnTextAvailable(/*anchor_bounds=*/gfx::Rect(),
/*selected_text=*/"",
/*surrounding_text=*/"");
EXPECT_TRUE(menu_controller()->menu_widget_for_test());
EXPECT_TRUE(menu_controller()->menu_widget_for_test()->IsVisible());
EXPECT_TRUE(views::IsViewClass<MahiMenuView>(
menu_controller()->menu_widget_for_test()->GetContentsView()));
}
TEST_F(MahiMenuControllerTest, DistillableMetrics) {
base::HistogramTester histogram_tester;
histogram_tester.ExpectBucketCount(kMahiContextMenuDistillableHistogram, true,
0);
histogram_tester.ExpectBucketCount(kMahiContextMenuDistillableHistogram,
false, 0);
ChangePageDistillability(false);
menu_controller()->RecordPageDistillable();
histogram_tester.ExpectBucketCount(kMahiContextMenuDistillableHistogram, true,
0);
histogram_tester.ExpectBucketCount(kMahiContextMenuDistillableHistogram,
false, 1);
// If page is not distillable, then menu widget should not be triggered.
ChangePageDistillability(true);
menu_controller()->RecordPageDistillable();
histogram_tester.ExpectBucketCount(kMahiContextMenuDistillableHistogram, true,
1);
histogram_tester.ExpectBucketCount(kMahiContextMenuDistillableHistogram,
false, 1);
}
} // namespace chromeos::mahi
|