chromium/chrome/browser/sessions/session_service_unittest.cc

// Copyright 2012 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/sessions/session_service.h"

#include <stddef.h>

#include <memory>
#include <optional>
#include <vector>

#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/atomic_flag.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/buildflags.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sessions/session_service_base_test_helper.h"
#include "chrome/browser/sessions/session_service_log.h"
#include "chrome/browser/sessions/session_service_test_helper.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/sessions/content/content_serialized_navigation_builder.h"
#include "components/sessions/content/content_test_helper.h"
#include "components/sessions/content/session_tab_helper.h"
#include "components/sessions/core/serialized_navigation_entry_test_helper.h"
#include "components/sessions/core/session_command.h"
#include "components/sessions/core/session_types.h"
#include "components/tab_groups/tab_group_color.h"
#include "components/tab_groups/tab_group_id.h"
#include "components/tab_groups/tab_group_visual_data.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/page_state/page_state.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/skia/include/core/SkColor.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/components/kiosk/kiosk_test_utils.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

NavigationEntry;
ContentTestHelper;
SerializedNavigationEntry;
SerializedNavigationEntryTestHelper;

class SessionServiceTest : public BrowserWithTestWindowTest {};

TEST_F(SessionServiceTest, Basic) {}

// Make sure we persist post entries.
TEST_F(SessionServiceTest, PersistPostData) {}

TEST_F(SessionServiceTest, ClosingTabStaysClosed) {}

TEST_F(SessionServiceTest, CloseSingleTabClosesWindowAndTab) {}

TEST_F(SessionServiceTest, Pruning) {}

TEST_F(SessionServiceTest, TwoWindows) {}

TEST_F(SessionServiceTest, WindowWithNoTabsGetsPruned) {}

TEST_F(SessionServiceTest, ClosingLastWindowDoesntCloseTabs) {}

TEST_F(SessionServiceTest, ClosingSecondWindowClosesTabs) {}

TEST_F(SessionServiceTest, LockingWindowRemembersAll) {}

TEST_F(SessionServiceTest, WindowCloseCommittedAfterNavigate) {}

TEST_F(SessionServiceTest, RemoveUnusedRestoreWindowsTest) {}

// Tests pruning from the front.
TEST_F(SessionServiceTest, PruneFromFront) {}

// Tests pruning from the middle.
TEST_F(SessionServiceTest, PruneFromMiddle) {}

// Tests possible computations of available ranges.
TEST_F(SessionServiceTest, AvailableRanges) {}

// Prunes from front so that we have no entries.
TEST_F(SessionServiceTest, PruneToEmpty) {}

// Don't set the pinned state and make sure the pinned value is false.
TEST_F(SessionServiceTest, PinnedDefaultsToFalse) {}

// Explicitly set the pinned state to false and make sure we get back false.
TEST_F(SessionServiceTest, PinnedFalseWhenSetToFalse) {}

// Explicitly set the pinned state to true and make sure we get back true.
TEST_F(SessionServiceTest, PinnedTrue) {}

// Make sure application extension ids are persisted.
TEST_F(SessionServiceTest, PersistApplicationExtensionID) {}

// Check that user agent overrides are persisted.
TEST_F(SessionServiceTest, PersistUserAgentOverrides) {}

TEST_F(SessionServiceTest, PersistExtraData) {}

// Verifies SetWindowBounds maps SHOW_STATE_DEFAULT to SHOW_STATE_NORMAL.
TEST_F(SessionServiceTest, DontPersistDefault) {}

TEST_F(SessionServiceTest, KeepPostDataWithoutPasswords) {}

TEST_F(SessionServiceTest, RemovePostDataWithPasswords) {}

TEST_F(SessionServiceTest, ReplacePendingNavigation) {}

TEST_F(SessionServiceTest, ReplacePendingNavigationAndPrune) {}

TEST_F(SessionServiceTest, RestoreActivation1) {}

// It's easier to have two separate tests with setup/teardown than to manualy
// reset the state for the different flavors of the test.
TEST_F(SessionServiceTest, RestoreActivation2) {}

// Makes sure sessions doesn't track certain urls.
TEST_F(SessionServiceTest, IgnoreBlockedUrls) {}

TEST_F(SessionServiceTest, TabGroupDefaultsToNone) {}

TEST_F(SessionServiceTest, TabGroupsSaved) {}

TEST_F(SessionServiceTest, TabGroupMetadataSaved) {}

TEST_F(SessionServiceTest, Workspace) {}

// Tests that the workspace is saved in the browser session during
// `SessionService::WindowOpened(),` called in `Browser()` constructor to
// save the current workspace to newly created browser.
TEST_F(SessionServiceTest, WorkspaceSavedOnOpened) {}

// Tests that the visible on all workspaces state is saved during
// SessionService::BuildCommandsForBrowser.
TEST_F(SessionServiceTest, VisibleOnAllWorkspaces) {}

TEST_F(SessionServiceTest, PinnedAfterReset) {}

// Functions used by GetSessionsAndDestroy.
namespace {

void OnGotPreviousSession(
    std::vector<std::unique_ptr<sessions::SessionWindow>> windows,
    SessionID ignored_active_window,
    bool error_reading) {}

// Blocks until |keep_waiting| is false.
void SimulateWaitForTesting(const base::AtomicFlag* flag) {}

}  // namespace

// Verifies that SessionService::GetLastSession() works correctly if the
// SessionService is deleted during processing. To verify the problematic case
// does the following:
// 1. Sends a task to the background thread that blocks.
// 2. Asks SessionService for the last session commands. This is blocked by 1.
// 3. Posts another task to the background thread, this too is blocked by 1.
// 4. Deletes SessionService.
// 5. Signals the semaphore that 2 and 3 are waiting on, allowing
//    GetLastSession() to continue.
// 6. runs the message loop, this is quit when the task scheduled in 3 posts
//    back to the ui thread to quit the run loop.
// The call to get the previous session should never be invoked because the
// SessionService was destroyed before SessionService could process the results.
TEST_F(SessionServiceTest, GetSessionsAndDestroy) {}

TEST_F(SessionServiceTest, LogExit) {}

TEST_F(SessionServiceTest, OnErrorWritingSessionCommands) {}

TEST_F(SessionServiceTest, OnErrorWritingSessionCommandsUnrecoverable) {}

TEST_F(SessionServiceTest, DisableSaving) {}

#if BUILDFLAG(IS_CHROMEOS)
class SessionServiceKioskTest : public SessionServiceTest {
 public:
  void LogIn(const std::string& email) override {
    chromeos::SetUpFakeKioskSession(email);
#if BUILDFLAG(IS_CHROMEOS_ASH)
    ash_test_helper()->test_session_controller_client()->AddUserSession(
        email, user_manager::UserType::kKioskApp);
#endif
  }
};

TEST_F(SessionServiceTest, OpenedWindowNotRestored) {
  // These preparation is necessary for `ShouldRestore` function to return true
  // in the regular user session.
  helper_.SetHasOpenTrackableBrowsers(false);
  service()->WindowClosing(window_id);
  service()->WindowClosed(window_id);
  // Make sure `ShouldRestore` returns true for the regular user session.
  EXPECT_TRUE(session_service_->ShouldRestore(browser()));
}

TEST_F(SessionServiceKioskTest, OpenedWindowNotRestored) {
  helper_.SetHasOpenTrackableBrowsers(false);
  service()->WindowClosing(window_id);
  service()->WindowClosed(window_id);
  // Make sure `ShouldRestore` returns true for the kiosk user session.
  EXPECT_FALSE(session_service_->ShouldRestore(browser()));
}
#endif  //  BUILDFLAG(IS_CHROMEOS)