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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
// Copyright 2023 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/toolbar/pinned_toolbar/pinned_toolbar_actions_model.h"
#include <memory>
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/toolbar/pinned_toolbar/pinned_toolbar_actions_model_factory.h"
#include "chrome/browser/ui/toolbar/toolbar_pref_names.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/actions/action_id.h"
#include "ui/actions/actions.h"
#include "ui/base/ui_base_features.h"
namespace {
// A simple observer that tracks the number of times certain events occur.
class PinnedToolbarActionsModelTestObserver
: public PinnedToolbarActionsModel::Observer {
public:
explicit PinnedToolbarActionsModelTestObserver(
PinnedToolbarActionsModel* model)
: model_(model) {
model_->AddObserver(this);
}
PinnedToolbarActionsModelTestObserver(
const PinnedToolbarActionsModelTestObserver&) = delete;
PinnedToolbarActionsModelTestObserver& operator=(
const PinnedToolbarActionsModelTestObserver&) = delete;
~PinnedToolbarActionsModelTestObserver() override {
model_->RemoveObserver(this);
}
int inserted_count() const { return inserted_count_; }
int removed_count() const { return removed_count_; }
int moved_to_index() const { return moved_to_index_; }
actions::ActionId last_changed_action() const { return last_changed_action_; }
const std::vector<actions::ActionId>& last_changed_ids() const {
return last_changed_ids_;
}
private:
// PinnedToolbarActionsModel::Observer:
void OnActionAddedLocally(actions::ActionId action_id) override {
++inserted_count_;
last_changed_action_ = action_id;
}
void OnActionRemovedLocally(actions::ActionId action_id) override {
++removed_count_;
last_changed_action_ = action_id;
}
// Signals that the given action with `id` has been moved in the model.
void OnActionMovedLocally(actions::ActionId id,
int from_index,
int to_index) override {
moved_to_index_ = to_index;
last_changed_action_ = id;
}
void OnActionsChanged() override {
last_changed_ids_ = model_->PinnedActionIds();
}
const raw_ptr<PinnedToolbarActionsModel> model_;
int inserted_count_ = 0;
int removed_count_ = 0;
int moved_to_index_ = -1;
actions::ActionId last_changed_action_ = -1;
std::vector<actions::ActionId> last_changed_ids_;
};
} // namespace
class PinnedToolbarActionsModelBrowserTest : public InProcessBrowserTest {
public:
PinnedToolbarActionsModelBrowserTest() = default;
PinnedToolbarActionsModelBrowserTest(
const PinnedToolbarActionsModelBrowserTest&) = delete;
PinnedToolbarActionsModelBrowserTest& operator=(
const PinnedToolbarActionsModelBrowserTest&) = delete;
~PinnedToolbarActionsModelBrowserTest() override = default;
protected:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
model_ =
PinnedToolbarActionsModelFactory::GetForProfile(browser()->profile());
model_observer_ =
std::make_unique<PinnedToolbarActionsModelTestObserver>(model_);
}
void TearDownOnMainThread() override {
model_observer_.reset();
model_ = nullptr;
InProcessBrowserTest::TearDownOnMainThread();
}
PinnedToolbarActionsModel* model() { return model_; }
const PinnedToolbarActionsModelTestObserver* observer() const {
return model_observer_.get();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
raw_ptr<PinnedToolbarActionsModel> model_;
std::unique_ptr<PinnedToolbarActionsModelTestObserver> model_observer_;
};
// Verify that we are able to add new pinned actions to the model and that
// it updates the prefs object accordingly.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest, PinActions) {
// Pin 3 ActionIds.
model()->UpdatePinnedState(kActionSidePanelShowBookmarks,
/*should_pin=*/true);
EXPECT_EQ(kActionSidePanelShowBookmarks, observer()->last_changed_action());
model()->UpdatePinnedState(kActionSidePanelShowReadingList,
/*should_pin=*/true);
EXPECT_EQ(kActionSidePanelShowReadingList, observer()->last_changed_action());
model()->UpdatePinnedState(kActionSidePanelShowHistoryCluster,
/*should_pin=*/true);
EXPECT_EQ(kActionSidePanelShowHistoryCluster,
observer()->last_changed_action());
EXPECT_EQ(0, observer()->removed_count());
EXPECT_EQ(3, observer()->inserted_count());
// Verify all actions ids were added to the model and that the prefs object
// maintains insertion order.
const base::Value::List& list =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(5u, list.size());
ASSERT_EQ("kActionShowChromeLabs", list[0].GetString());
ASSERT_EQ("kActionTabSearch", list[1].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list[2].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list[3].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list[4].GetString());
} else {
ASSERT_EQ(4u, list.size());
ASSERT_EQ("kActionShowChromeLabs", list[0].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list[1].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list[2].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list[3].GetString());
}
}
// Verify that we are able to remove pinned actions from the model and that
// it updates the prefs object accordingly.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest, UnpinActions) {
// Pin 3 ActionIds.
model()->UpdatePinnedState(kActionSidePanelShowBookmarks,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowReadingList,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowHistoryCluster,
/*should_pin=*/true);
// Expect unpinning the reading list ActionId will remove it from the model
// and the prefs object.
model()->UpdatePinnedState(kActionSidePanelShowReadingList,
/*should_pin=*/false);
EXPECT_EQ(1, observer()->removed_count());
EXPECT_EQ(3, observer()->inserted_count());
// Verify only the reading list ActionId was removed.
const base::Value::List& list =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(4u, list.size());
ASSERT_EQ("kActionShowChromeLabs", list[0].GetString());
ASSERT_EQ("kActionTabSearch", list[1].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list[2].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list[3].GetString());
} else {
ASSERT_EQ(3u, list.size());
ASSERT_EQ("kActionShowChromeLabs", list[0].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list[1].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list[2].GetString());
}
}
// Verify that we are able to move pinned actions in the model and that
// it updates the prefs object accordingly.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
MovePinnedActions) {
// Pin 3 ActionIds.
model()->UpdatePinnedState(kActionSidePanelShowBookmarks,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowReadingList,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowHistoryCluster,
/*should_pin=*/true);
// Expect moving the second action will put it at the end of the list.
model()->MovePinnedAction(kActionSidePanelShowReadingList,
features::HasTabSearchToolbarButton() ? 4 : 3);
EXPECT_EQ(0, observer()->removed_count());
EXPECT_EQ(3, observer()->inserted_count());
EXPECT_EQ(features::HasTabSearchToolbarButton() ? 4 : 3,
observer()->moved_to_index());
// Verify kActionCopy was moved to the end of the list which should be
// index 2.
const base::Value::List& list_1 =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(5u, list_1.size());
ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
ASSERT_EQ("kActionTabSearch", list_1[1].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_1[2].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_1[3].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_1[4].GetString());
} else {
ASSERT_EQ(4u, list_1.size());
ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_1[1].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_1[2].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_1[3].GetString());
}
// Expect that we can move the first action after the second action correctly.
model()->MovePinnedAction(kActionSidePanelShowBookmarks,
features::HasTabSearchToolbarButton() ? 3 : 2);
EXPECT_EQ(0, observer()->removed_count());
EXPECT_EQ(3, observer()->inserted_count());
EXPECT_EQ(features::HasTabSearchToolbarButton() ? 3 : 2,
observer()->moved_to_index());
// kActionSidePanelShowBookmarks was move to the end.
const base::Value::List& list_2 =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(5u, list_2.size());
ASSERT_EQ("kActionShowChromeLabs", list_2[0].GetString());
ASSERT_EQ("kActionTabSearch", list_2[1].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_2[2].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_2[3].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_2[4].GetString());
} else {
ASSERT_EQ(4u, list_2.size());
ASSERT_EQ("kActionShowChromeLabs", list_2[0].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_2[1].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_2[2].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_2[3].GetString());
}
// Expect that moving the kActionCopy action to the beginning of the list will
// place it in front of the current first element.
model()->MovePinnedAction(kActionSidePanelShowReadingList,
features::HasTabSearchToolbarButton() ? 2 : 1);
EXPECT_EQ(0, observer()->removed_count());
EXPECT_EQ(3, observer()->inserted_count());
EXPECT_EQ(features::HasTabSearchToolbarButton() ? 2 : 1,
observer()->moved_to_index());
// Verify kActionCopy was moved to index 0.
const base::Value::List& list_3 =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(5u, list_3.size());
ASSERT_EQ("kActionShowChromeLabs", list_3[0].GetString());
ASSERT_EQ("kActionTabSearch", list_3[1].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_3[2].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_3[3].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_3[4].GetString());
} else {
ASSERT_EQ(4u, list_3.size());
ASSERT_EQ("kActionShowChromeLabs", list_3[0].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_3[1].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_3[2].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_3[3].GetString());
}
}
// Verify that trying to move a pinned action out of bounds will do nothing.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
MovePinnedActionsOutOfBoundsDoesNothing) {
// Pin 3 ActionIds.
model()->UpdatePinnedState(kActionSidePanelShowBookmarks,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowReadingList,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowHistoryCluster,
/*should_pin=*/true);
// Expect that moving an Action out of bounds at the end of the list does
// nothing.
model()->MovePinnedAction(kActionSidePanelShowReadingList,
features::HasTabSearchToolbarButton() ? 5 : 4);
EXPECT_EQ(0, observer()->removed_count());
EXPECT_EQ(3, observer()->inserted_count());
EXPECT_EQ(-1, observer()->moved_to_index());
// Verify the action did not move.
const base::Value::List& list_1 =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(5u, list_1.size());
ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
ASSERT_EQ("kActionTabSearch", list_1[1].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_1[2].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_1[3].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_1[4].GetString());
} else {
ASSERT_EQ(4u, list_1.size());
ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_1[1].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_1[2].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_1[3].GetString());
}
// Expect that moving an action out of bounds before the list does nothing.
model()->MovePinnedAction(kActionSidePanelShowBookmarks, -1);
EXPECT_EQ(0, observer()->removed_count());
EXPECT_EQ(3, observer()->inserted_count());
EXPECT_EQ(-1, observer()->moved_to_index());
// Verify the action did not move.
const base::Value::List& list_2 =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(5u, list_2.size());
ASSERT_EQ("kActionShowChromeLabs", list_2[0].GetString());
ASSERT_EQ("kActionTabSearch", list_2[1].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_2[2].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_2[3].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_2[4].GetString());
} else {
ASSERT_EQ(4u, list_2.size());
ASSERT_EQ("kActionShowChromeLabs", list_2[0].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_2[1].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_2[2].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_2[3].GetString());
}
}
// Verify that trying to move a pinned action out of bounds will do nothing.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
MoveUnpinnedActionDoesNothing) {
// Pin 3 ActionIds.
model()->UpdatePinnedState(kActionSidePanelShowBookmarks,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowReadingList,
/*should_pin=*/true);
// Expect that moving an action which is not added to the model does nothing.
model()->MovePinnedAction(kActionSidePanelShowHistoryCluster, 3);
EXPECT_EQ(0, observer()->removed_count());
EXPECT_EQ(2, observer()->inserted_count());
EXPECT_EQ(-1, observer()->moved_to_index());
// Verify nothing changed.
const base::Value::List& list_1 =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(4u, list_1.size());
ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
ASSERT_EQ("kActionTabSearch", list_1[1].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_1[2].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_1[3].GetString());
} else {
ASSERT_EQ(3u, list_1.size());
ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_1[1].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_1[2].GetString());
}
}
// Verify that trying to move a pinned action to its current index does nothing.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
MovePinnedActionToSameIndexDoesNothing) {
// Pin 3 ActionIds.
model()->UpdatePinnedState(kActionSidePanelShowBookmarks,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowReadingList,
/*should_pin=*/true);
model()->UpdatePinnedState(kActionSidePanelShowHistoryCluster,
/*should_pin=*/true);
// Expect that moving an action to the same index does nothing.
model()->MovePinnedAction(kActionSidePanelShowHistoryCluster,
features::HasTabSearchToolbarButton() ? 4 : 3);
EXPECT_EQ(0, observer()->removed_count());
EXPECT_EQ(3, observer()->inserted_count());
EXPECT_EQ(-1, observer()->moved_to_index());
// Verify no action moved.
const base::Value::List& list_1 =
browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
if (features::HasTabSearchToolbarButton()) {
ASSERT_EQ(5u, list_1.size());
ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
ASSERT_EQ("kActionTabSearch", list_1[1].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_1[2].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_1[3].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_1[4].GetString());
} else {
ASSERT_EQ(4u, list_1.size());
ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
EXPECT_EQ("kActionSidePanelShowBookmarks", list_1[1].GetString());
EXPECT_EQ("kActionSidePanelShowReadingList", list_1[2].GetString());
EXPECT_EQ("kActionSidePanelShowHistoryCluster", list_1[3].GetString());
}
}
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
ResetToDefaultResetsToDefault) {
EXPECT_TRUE(model()->IsDefault());
EXPECT_FALSE(model()->Contains(kActionSidePanelShowBookmarks));
EXPECT_TRUE(model()->Contains(kActionShowChromeLabs));
if (features::HasTabSearchToolbarButton()) {
EXPECT_TRUE(model()->Contains(kActionTabSearch));
model()->UpdatePinnedState(kActionTabSearch, false);
}
model()->UpdatePinnedState(kActionShowChromeLabs, false);
model()->UpdatePinnedState(kActionSidePanelShowBookmarks, true);
EXPECT_FALSE(model()->IsDefault());
EXPECT_TRUE(model()->Contains(kActionSidePanelShowBookmarks));
EXPECT_FALSE(model()->Contains(kActionShowChromeLabs));
if (features::HasTabSearchToolbarButton()) {
EXPECT_FALSE(model()->Contains(kActionTabSearch));
}
model()->ResetToDefault();
EXPECT_TRUE(model()->IsDefault());
EXPECT_FALSE(model()->Contains(kActionSidePanelShowBookmarks));
EXPECT_TRUE(model()->Contains(kActionShowChromeLabs));
if (features::HasTabSearchToolbarButton()) {
EXPECT_TRUE(model()->Contains(kActionTabSearch));
}
}
// TODO(dljames): Write tests for guest and incognito mode profile that check
// that we cannot modify the model at all.
|