chromium/components/sync_sessions/local_session_event_handler_impl_unittest.cc

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync_sessions/local_session_event_handler_impl.h"

#include <map>
#include <utility>
#include <vector>

#include "base/functional/callback_helpers.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "components/sessions/core/serialized_navigation_entry.h"
#include "components/sessions/core/serialized_navigation_entry_test_helper.h"
#include "components/sync/base/features.h"
#include "components/sync/base/time.h"
#include "components/sync/protocol/session_specifics.pb.h"
#include "components/sync/protocol/sync_enums.pb.h"
#include "components/sync_sessions/mock_sync_sessions_client.h"
#include "components/sync_sessions/synced_session_tracker.h"
#include "components/sync_sessions/test_matchers.h"
#include "components/sync_sessions/test_synced_window_delegates_getter.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace sync_sessions {
namespace {

SerializedNavigationEntry;
SerializedNavigationEntryTestHelper;
_;
ByMove;
IsEmpty;
NiceMock;
Pointee;
Return;
StrictMock;

const char kFoo1[] =;
const char kBar1[] =;
const char kBar2[] =;
const char kBaz1[] =;

const char kSessionTag[] =;
const char kSessionName[] =;

const base::Time kTime0 =;
const base::Time kTime1 =;
const base::Time kTime2 =;
const base::Time kTime3 =;

const int kWindowId1 =;
const int kWindowId2 =;
const int kWindowId3 =;
const int kTabId1 =;
const int kTabId2 =;
const int kTabId3 =;

sync_pb::SessionSpecifics MakeSessionTabSpecifics(int window_id,
                                                  int tab_id,
                                                  int tab_node_id) {}

sync_pb::SessionSpecifics MakeSessionHeaderSpecifics(
    const std::map<int, std::vector<int>>& window_id_to_tabs) {}

class MockWriteBatch : public LocalSessionEventHandlerImpl::WriteBatch {};

class MockDelegate : public LocalSessionEventHandlerImpl::Delegate {};

class LocalSessionEventHandlerImplTest : public testing::Test {};

// Populate the mock tab delegate with some data and navigation
// entries and make sure that populating a SessionTab contains analgous
// information.
TEST_F(LocalSessionEventHandlerImplTest, GetTabSpecificsFromDelegate) {}

// Verifies SessionTab.browser_type is set correctly.
TEST_F(LocalSessionEventHandlerImplTest, BrowserTypeInTabSpecifics) {}

// Ensure the current_navigation_index gets set properly when the navigation
// stack gets trucated to +/- 6 entries.
TEST_F(LocalSessionEventHandlerImplTest,
       SetSessionTabFromDelegateNavigationIndex) {}

// Ensure the current_navigation_index gets set to the end of the navigation
// stack if the current navigation is invalid.
TEST_F(LocalSessionEventHandlerImplTest,
       SetSessionTabFromDelegateCurrentInvalid) {}

// Tests that for child account users blocked navigations are recorded.
TEST_F(LocalSessionEventHandlerImplTest, BlockedNavigations) {}

// Tests that calling AssociateWindowsAndTabs() handles well the case with no
// open tabs or windows.
TEST_F(LocalSessionEventHandlerImplTest, AssociateWindowsAndTabsIfEmpty) {}

// Tests that calling AssociateWindowsAndTabs() reflects the open tabs in a) the
// SyncSessionTracker and b) the delegate.
TEST_F(LocalSessionEventHandlerImplTest, AssociateWindowsAndTabs) {}

// Tests that association does not refresh window IDs for placeholder tabs, even
// if the window ID changes across restarts.
TEST_F(LocalSessionEventHandlerImplTest, DontUpdateWindowIdForPlaceholderTab) {}

// Tests that association of windows and tabs gets deferred due to ongoing
// session restore during startup.
TEST_F(LocalSessionEventHandlerImplTest,
       DeferAssociationDueToInitialSessionRestore) {}

// Tests that association of windows and tabs gets deferred due to ongoing
// session restore happening at a late stage (e.g. CCT-only / no-tabbed-window
// to tabbed-window transition).
TEST_F(LocalSessionEventHandlerImplTest,
       DeferAssociationDueToLateSessionRestore) {}

// Tests that calling AssociateWindowsAndTabs() reflects the open tabs in a) the
// SyncSessionTracker and b) the delegate, for the case where a custom tab
// exists without native data (no tabbed window).
TEST_F(LocalSessionEventHandlerImplTest, AssociateCustomTab) {}

TEST_F(LocalSessionEventHandlerImplTest, PropagateNewNavigation) {}

TEST_F(LocalSessionEventHandlerImplTest, PropagateNewTab) {}

TEST_F(LocalSessionEventHandlerImplTest, PropagateClosedTab) {}

TEST_F(LocalSessionEventHandlerImplTest, PropagateNewCustomTab) {}

TEST_F(LocalSessionEventHandlerImplTest, PropagateNewWindow) {}

TEST_F(LocalSessionEventHandlerImplTest,
       PropagateNewNavigationWithoutTabbedWindows) {}

TEST_F(LocalSessionEventHandlerImplTest, ShouldRemoveAllTabsOnEmptyWindow) {}

#if BUILDFLAG(IS_ANDROID)
TEST_F(LocalSessionEventHandlerImplTest, LoadPlaceholderTabFromDisk) {
  // Mimic the user opening a tab that is initially a placeholder tab.
  TestSyncedWindowDelegate* window = AddWindow(kWindowId1);
  PlaceholderTabDelegate placeholder_tab(
      SessionID::FromSerializedValue(kTabId1));
  auto snapshot = std::make_unique<TestSyncedTabDelegate>(
      SessionID::FromSerializedValue(kWindowId1),
      SessionID::FromSerializedValue(kTabId1), base::DoNothing());
  snapshot->Navigate(kFoo1);
  placeholder_tab.SetPlaceholderTabSyncedTabDelegate(std::move(snapshot));
  window->OverrideTabAt(0, &placeholder_tab);

  // Add expectations for the invocations that are expected when the loading
  // completes.
  auto mock_batch = std::make_unique<StrictMock<MockWriteBatch>>();
  EXPECT_CALL(
      *mock_batch,
      Put(Pointee(MatchesHeader(kSessionTag, {kWindowId1}, {kTabId1}))));
  // Expect that the tab originating as a placeholder tab was included in
  // the write batch for resync.
  EXPECT_CALL(*mock_batch, Put(Pointee(MatchesTab(kSessionTag, kWindowId1,
                                                  kTabId1, /*tab_node_id=*/0,
                                                  /*urls=*/{kFoo1}))));
  EXPECT_CALL(*mock_batch, Commit());

  EXPECT_CALL(mock_delegate_, CreateLocalSessionWriteBatch())
      .WillOnce(Return(ByMove(std::move(mock_batch))));

  InitHandler();
}
#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace
}  // namespace sync_sessions