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

#include "base/time/time.h"
#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
#include "chrome/browser/sync/test/integration/encryption_helper.h"
#include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h"
#include "chrome/browser/sync/test/integration/user_events_helper.h"
#include "chrome/browser/sync/user_event_service_factory.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/browser_sync/browser_sync_switches.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/sync/protocol/user_event_specifics.pb.h"
#include "components/sync_user_events/user_event_service.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

using bookmarks_helper::BookmarksMatchChecker;
using bookmarks_helper::CountBookmarksWithUrlsMatching;
using bookmarks_helper::StoreType;

const int kEncryptingClientId = 0;
const int kDecryptingClientId = 1;

const char kTestBookmarkURL[] = "https://google.com/synced-bookmark-1";

class TwoClientUserEventsSyncTest
    : public SyncTest,
      public testing::WithParamInterface<SyncTest::SetupSyncMode> {
 public:
  TwoClientUserEventsSyncTest() : SyncTest(TWO_CLIENT) {
    if (GetSetupSyncMode() == SetupSyncMode::kSyncTransportOnly) {
      scoped_feature_list_.InitWithFeatures(
          {syncer::kReplaceSyncPromosWithSignInPromos,
           switches::kSyncEnableBookmarksInTransportMode},
          {});
    }
  }

  ~TwoClientUserEventsSyncTest() override = default;

  bool ExpectNoUserEvent(int index) {
    return UserEventEqualityChecker(GetSyncService(index), GetFakeServer(),
                                    /*expected_specifics=*/{})
        .Wait();
  }

  void AddTestBookmarkToClient(int index) {
    StoreType store_type =
        GetSetupSyncMode() == SyncTest::SetupSyncMode::kSyncTransportOnly
            ? StoreType::kAccountStore
            : StoreType::kLocalOrSyncableStore;
    ASSERT_TRUE(bookmarks_helper::AddURL(index, 0,
                                         u"What are you syncing about?",
                                         GURL(kTestBookmarkURL), store_type));
  }

  SyncTest::SetupSyncMode GetSetupSyncMode() const override {
    return GetParam();
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(,
                         TwoClientUserEventsSyncTest,
                         GetSyncTestModes(),
                         testing::PrintToStringParamName());

IN_PROC_BROWSER_TEST_P(TwoClientUserEventsSyncTest,
                       SetPassphraseAndRecordEventAndThenSetupSync) {
  ASSERT_TRUE(SetupClients());
  if (GetSetupSyncMode() == SetupSyncMode::kSyncTheFeature) {
    ASSERT_TRUE(GetClient(kEncryptingClientId)->SetupSync());
  } else {
    ASSERT_TRUE(GetClient(kEncryptingClientId)->SignInNoWaitForCompletion());
    ASSERT_TRUE(GetClient(kEncryptingClientId)->AwaitSyncTransportActive());
  }

  // Set up a sync client with custom passphrase to get the data encrypted on
  // the server.
  GetSyncService(kEncryptingClientId)
      ->GetUserSettings()
      ->SetEncryptionPassphrase("hunter2");
  UpdatedProgressMarkerChecker update_checker(
      GetSyncService(kEncryptingClientId));
  ASSERT_TRUE(
      PassphraseAcceptedChecker(GetSyncService(kEncryptingClientId)).Wait());
  // Make sure the updates are committed before proceeding with the test.
  ASSERT_TRUE(update_checker.Wait());

  // Record a user event on the second client before setting up sync (before
  // knowing it will be encrypted). This event should not get recorded while
  // starting up sync because the user has custom passphrase setup.
  syncer::UserEventService* event_service =
      browser_sync::UserEventServiceFactory::GetForProfile(
          GetProfile(kDecryptingClientId));
  event_service->RecordUserEvent(std::make_unique<sync_pb::UserEventSpecifics>(
      user_events_helper::CreateTestEvent(base::Time() +
                                          base::Microseconds(1))));

  // Set up sync on the second client.
  if (GetSetupSyncMode() == SetupSyncMode::kSyncTheFeature) {
    ASSERT_TRUE(GetClient(kDecryptingClientId)->SetupSyncNoWaitForCompletion());
  } else {
    ASSERT_TRUE(GetClient(kDecryptingClientId)->SignInNoWaitForCompletion());
    ASSERT_TRUE(GetClient(kDecryptingClientId)->AwaitSyncTransportActive());
  }
  // The second client asks the user to provide a password for decryption.
  ASSERT_TRUE(
      PassphraseRequiredChecker(GetSyncService(kDecryptingClientId)).Wait());
  // Provide the password.
  ASSERT_TRUE(GetSyncService(kDecryptingClientId)
                  ->GetUserSettings()
                  ->SetDecryptionPassphrase("hunter2"));
  // Check it is accepted and finish the setup.
  ASSERT_TRUE(
      PassphraseAcceptedChecker(GetSyncService(kDecryptingClientId)).Wait());
  if (GetSetupSyncMode() == SetupSyncMode::kSyncTheFeature) {
    GetClient(kDecryptingClientId)->FinishSyncSetup();
  } else {
    ASSERT_TRUE(GetClient(kDecryptingClientId)->AwaitSyncTransportActive());
  }

  // Just checking that we don't see the recorded test event isn't very
  // convincing yet, because it may simply not have reached the server yet. So
  // let's send something else on the second client through the system that we
  // can wait on before checking. Bookmark data was picked fairly arbitrarily.
  AddTestBookmarkToClient(kDecryptingClientId);
  ASSERT_TRUE(BookmarksMatchChecker().Wait());

  // Double check that the encrypting client has the bookmark.
  ASSERT_EQ(1u, CountBookmarksWithUrlsMatching(kEncryptingClientId,
                                               GURL(kTestBookmarkURL)));

  // Finally, make sure no user event got sent to the server.
  EXPECT_TRUE(ExpectNoUserEvent(kDecryptingClientId));
}

}  // namespace
