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
|
// Copyright 2013 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/sync/sync_startup_tracker.h"
#include <memory>
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "components/sync/test/test_sync_service.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Mock;
namespace {
class SyncStartupTrackerTest : public testing::Test {
public:
void SetupNonInitializedSyncService() {
sync_service_.SetMaxTransportState(
syncer::SyncService::TransportState::INITIALIZING);
}
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
syncer::TestSyncService sync_service_;
base::MockCallback<SyncStartupTracker::SyncStartupStateChangedCallback>
callback_;
};
TEST_F(SyncStartupTrackerTest, SyncAlreadyInitialized) {
sync_service_.SetMaxTransportState(
syncer::SyncService::TransportState::ACTIVE);
EXPECT_CALL(callback_,
Run(SyncStartupTracker::ServiceStartupState::kComplete));
SyncStartupTracker tracker(&sync_service_, callback_.Get());
}
TEST_F(SyncStartupTrackerTest, SyncNotSignedIn) {
// Make sure that we get a SyncStartupFailed() callback if sync is not logged
// in.
sync_service_.SetSignedOut();
EXPECT_CALL(callback_, Run(SyncStartupTracker::ServiceStartupState::kError));
SyncStartupTracker tracker(&sync_service_, callback_.Get());
}
TEST_F(SyncStartupTrackerTest, SyncAuthError) {
// Make sure that we get a SyncStartupFailed() callback if sync gets an auth
// error.
sync_service_.SetPersistentAuthError();
EXPECT_CALL(callback_, Run(SyncStartupTracker::ServiceStartupState::kError));
SyncStartupTracker tracker(&sync_service_, callback_.Get());
}
TEST_F(SyncStartupTrackerTest, SyncDelayedInitialization) {
// Non-initialized Sync Service should result in no callbacks to the observer.
SetupNonInitializedSyncService();
EXPECT_CALL(callback_, Run(_)).Times(0);
SyncStartupTracker tracker(&sync_service_, callback_.Get());
Mock::VerifyAndClearExpectations(&callback_);
// Now, mark the Sync Service as initialized.
sync_service_.SetMaxTransportState(
syncer::SyncService::TransportState::ACTIVE);
EXPECT_CALL(callback_,
Run(SyncStartupTracker::ServiceStartupState::kComplete));
tracker.OnStateChanged(&sync_service_);
}
TEST_F(SyncStartupTrackerTest, SyncDelayedAuthError) {
// Non-initialized Sync Service should result in no callbacks to the observer.
SetupNonInitializedSyncService();
EXPECT_CALL(callback_, Run(_)).Times(0);
SyncStartupTracker tracker(&sync_service_, callback_.Get());
Mock::VerifyAndClearExpectations(&callback_);
Mock::VerifyAndClearExpectations(&sync_service_);
// Now, mark the Sync Service as having an auth error.
sync_service_.SetPersistentAuthError();
EXPECT_CALL(callback_, Run(SyncStartupTracker::ServiceStartupState::kError));
tracker.OnStateChanged(&sync_service_);
}
TEST_F(SyncStartupTrackerTest, SyncDelayedUnrecoverableError) {
// Non-initialized Sync Service should result in no callbacks to the observer.
SetupNonInitializedSyncService();
EXPECT_CALL(callback_, Run(_)).Times(0);
SyncStartupTracker tracker(&sync_service_, callback_.Get());
Mock::VerifyAndClearExpectations(&callback_);
Mock::VerifyAndClearExpectations(&sync_service_);
// Now, mark the Sync Service as having an unrecoverable error.
sync_service_.SetHasUnrecoverableError(true);
EXPECT_CALL(callback_, Run(SyncStartupTracker::ServiceStartupState::kError));
tracker.OnStateChanged(&sync_service_);
}
TEST_F(SyncStartupTrackerTest, TrackingCancelled) {
// Non-initialized Sync Service should result in no callbacks to the observer.
SetupNonInitializedSyncService();
EXPECT_CALL(callback_, Run(_)).Times(0);
std::unique_ptr<SyncStartupTracker> tracker =
std::make_unique<SyncStartupTracker>(&sync_service_, callback_.Get());
Mock::VerifyAndClearExpectations(&callback_);
// Now, cancel the tracker.
EXPECT_CALL(callback_, Run(_)).Times(0);
tracker.reset();
}
TEST_F(SyncStartupTrackerTest, SyncTimedOut) {
// Non-initialized Sync Service should result in no callbacks to the observer.
SetupNonInitializedSyncService();
EXPECT_CALL(callback_, Run(_)).Times(0);
std::unique_ptr<SyncStartupTracker> tracker =
std::make_unique<SyncStartupTracker>(&sync_service_, callback_.Get());
Mock::VerifyAndClearExpectations(&callback_);
// Now, advance the timer.
EXPECT_CALL(callback_,
Run(SyncStartupTracker::ServiceStartupState::kTimeout));
task_environment_.FastForwardBy(base::Seconds(30));
}
} // namespace
|