chromium/chrome/browser/ui/toolbar/pinned_toolbar/pinned_toolbar_actions_model_browsertest.cc

// 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 "build/chromeos_buildflags.h"
#include "chrome/browser/companion/core/features.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/views/side_panel/companion/companion_utils.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/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 {};
}  // namespace

class PinnedToolbarActionsModelBrowserTest : public InProcessBrowserTest {};

// 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) {}

// 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) {}

// 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) {}

// Verify that trying to move a pinned action out of bounds will do nothing.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
                       MovePinnedActionsOutOfBoundsDoesNothing) {}

// Verify that trying to move a pinned action out of bounds will do nothing.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
                       MoveUnpinnedActionDoesNothing) {}

// Verify that trying to move a pinned action to its current index does nothing.
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
                       MovePinnedActionToSameIndexDoesNothing) {}

IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
                       ResetToDefaultResetsToDefault) {}

// Verify that the search companion updates the model and prefs object
// appropriately.
#if BUILDFLAG(GOOGLE_CHROME_BRANDING) && !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(PinnedToolbarActionsModelBrowserTest,
                       MigratingSearchCompanionUpdatesModel) {
  // Verify nothing happens if the migration already happened.
  {
    browser()->profile()->GetPrefs()->SetBoolean(
        prefs::kPinnedSearchCompanionMigrationComplete, true);
    browser()->profile()->GetPrefs()->SetBoolean(
        prefs::kSidePanelCompanionEntryPinnedToToolbar, true);
    EXPECT_EQ(0, observer()->removed_count());
    EXPECT_EQ(0, observer()->inserted_count());
    EXPECT_EQ(-1, observer()->moved_to_index());

    companion::UpdateCompanionDefaultPinnedToToolbarState(browser()->profile());

    const base::Value::List& list_1 =
        browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
    EXPECT_EQ(1u, list_1.size());
  }

  // Verify nothing happens if the search companion is not pinned.
  {
    browser()->profile()->GetPrefs()->SetBoolean(
        prefs::kPinnedSearchCompanionMigrationComplete, false);
    browser()->profile()->GetPrefs()->SetBoolean(
        prefs::kSidePanelCompanionEntryPinnedToToolbar, false);
    EXPECT_EQ(0, observer()->removed_count());
    EXPECT_EQ(0, observer()->inserted_count());
    EXPECT_EQ(-1, observer()->moved_to_index());

    companion::UpdateCompanionDefaultPinnedToToolbarState(browser()->profile());

    const base::Value::List& list_1 =
        browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);
    EXPECT_EQ(1u, list_1.size());
  }

  // Verify the migration updates the model if search companion is pinned.
  {
    browser()->profile()->GetPrefs()->SetBoolean(
        prefs::kPinnedSearchCompanionMigrationComplete, false);
    browser()->profile()->GetPrefs()->SetBoolean(
        prefs::kSidePanelCompanionEntryPinnedToToolbar, true);
    EXPECT_EQ(0, observer()->removed_count());
    EXPECT_EQ(0, observer()->inserted_count());
    EXPECT_EQ(-1, observer()->moved_to_index());

    companion::UpdateCompanionDefaultPinnedToToolbarState(browser()->profile());

    const base::Value::List& list_1 =
        browser()->profile()->GetPrefs()->GetList(prefs::kPinnedActions);

    ASSERT_EQ(2u, list_1.size());
    EXPECT_EQ(1, observer()->inserted_count());

    const std::optional<std::string>& search_companion_string =
        actions::ActionIdMap::ActionIdToString(
            kActionSidePanelShowSearchCompanion);
    ASSERT_EQ("kActionShowChromeLabs", list_1[0].GetString());
    EXPECT_EQ(search_companion_string, list_1[1].GetString());
  }
}
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING) && !BUILDFLAG(IS_CHROMEOS)

// TODO(dljames): Write tests for guest and incognito mode profile that check
// that we cannot modify the model at all.