This source file includes following definitions.
- ACTION
- ACTION_P
- change_processor_
- SetUp
- TearDown
- SetStartExpectations
- SetActivateExpectations
- SetStopExpectations
- Start
- PumpLoop
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/sync/glue/ui_data_type_controller.h"
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/tracked_objects.h"
#include "chrome/browser/sync/glue/fake_generic_change_processor.h"
#include "chrome/browser/sync/profile_sync_components_factory_mock.h"
#include "chrome/browser/sync/profile_sync_service_mock.h"
#include "chrome/test/base/profile_mock.h"
#include "components/sync_driver/data_type_controller_mock.h"
#include "content/public/test/test_browser_thread.h"
#include "sync/api/fake_syncable_service.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
using testing::_;
using testing::InvokeWithoutArgs;
using testing::Return;
namespace browser_sync {
namespace {
ACTION(MakeSharedChangeProcessor) {
return new SharedChangeProcessor();
}
ACTION_P(ReturnAndRelease, change_processor) {
return change_processor->release();
}
class SyncUIDataTypeControllerTest : public testing::Test {
public:
SyncUIDataTypeControllerTest()
: ui_thread_(BrowserThread::UI, &message_loop_),
profile_sync_service_(&profile_),
type_(syncer::PREFERENCES),
change_processor_(new FakeGenericChangeProcessor()) {}
virtual void SetUp() {
profile_sync_factory_.reset(new ProfileSyncComponentsFactoryMock());
preference_dtc_ =
new UIDataTypeController(base::MessageLoopProxy::current(),
base::Closure(),
type_,
profile_sync_factory_.get(),
&profile_,
&profile_sync_service_);
SetStartExpectations();
}
virtual void TearDown() {
syncable_service_.StopSyncing(type_);
preference_dtc_ = NULL;
PumpLoop();
}
protected:
void SetStartExpectations() {
change_processor_.reset(new FakeGenericChangeProcessor());
EXPECT_CALL(model_load_callback_, Run(_, _));
EXPECT_CALL(*profile_sync_factory_, GetSyncableServiceForType(type_)).
WillOnce(Return(syncable_service_.AsWeakPtr()));
EXPECT_CALL(*profile_sync_factory_, CreateSharedChangeProcessor()).
WillOnce(MakeSharedChangeProcessor());
EXPECT_CALL(*profile_sync_factory_,
CreateGenericChangeProcessor(_, _, _, _)).
WillOnce(ReturnAndRelease(&change_processor_));
}
void SetActivateExpectations() {
EXPECT_CALL(profile_sync_service_, ActivateDataType(type_, _, _));
}
void SetStopExpectations() {
EXPECT_CALL(profile_sync_service_, DeactivateDataType(type_));
}
void Start() {
preference_dtc_->LoadModels(
base::Bind(&ModelLoadCallbackMock::Run,
base::Unretained(&model_load_callback_)));
preference_dtc_->StartAssociating(
base::Bind(&StartCallbackMock::Run,
base::Unretained(&start_callback_)));
}
void PumpLoop() {
message_loop_.RunUntilIdle();
}
base::MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
ProfileMock profile_;
scoped_ptr<ProfileSyncComponentsFactoryMock> profile_sync_factory_;
ProfileSyncServiceMock profile_sync_service_;
const syncer::ModelType type_;
StartCallbackMock start_callback_;
ModelLoadCallbackMock model_load_callback_;
scoped_refptr<UIDataTypeController> preference_dtc_;
scoped_ptr<FakeGenericChangeProcessor> change_processor_;
syncer::FakeSyncableService syncable_service_;
};
TEST_F(SyncUIDataTypeControllerTest, Start) {
SetActivateExpectations();
EXPECT_CALL(start_callback_, Run(DataTypeController::OK, _, _));
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
Start();
EXPECT_EQ(DataTypeController::RUNNING, preference_dtc_->state());
EXPECT_TRUE(syncable_service_.syncing());
}
TEST_F(SyncUIDataTypeControllerTest, StartStop) {
SetActivateExpectations();
SetStopExpectations();
EXPECT_CALL(start_callback_, Run(DataTypeController::OK, _, _));
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
Start();
EXPECT_EQ(DataTypeController::RUNNING, preference_dtc_->state());
EXPECT_TRUE(syncable_service_.syncing());
preference_dtc_->Stop();
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
}
TEST_F(SyncUIDataTypeControllerTest, StartStopFirstRun) {
SetActivateExpectations();
SetStopExpectations();
EXPECT_CALL(start_callback_, Run(DataTypeController::OK_FIRST_RUN, _, _));
change_processor_->set_sync_model_has_user_created_nodes(false);
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
Start();
EXPECT_EQ(DataTypeController::RUNNING, preference_dtc_->state());
EXPECT_TRUE(syncable_service_.syncing());
preference_dtc_->Stop();
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
}
TEST_F(SyncUIDataTypeControllerTest, StartAssociationFailed) {
SetStopExpectations();
EXPECT_CALL(start_callback_,
Run(DataTypeController::ASSOCIATION_FAILED, _, _));
syncable_service_.set_merge_data_and_start_syncing_error(
syncer::SyncError(FROM_HERE,
syncer::SyncError::DATATYPE_ERROR,
"Error",
type_));
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
Start();
EXPECT_EQ(DataTypeController::DISABLED, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
preference_dtc_->Stop();
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
}
TEST_F(SyncUIDataTypeControllerTest,
StartAssociationTriggersUnrecoverableError) {
EXPECT_CALL(start_callback_,
Run(DataTypeController::UNRECOVERABLE_ERROR, _, _));
change_processor_->set_sync_model_has_user_created_nodes_success(false);
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
Start();
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
}
TEST_F(SyncUIDataTypeControllerTest, OnSingleDatatypeUnrecoverableError) {
SetActivateExpectations();
EXPECT_CALL(profile_sync_service_, DisableBrokenDatatype(_,_,_)).
WillOnce(InvokeWithoutArgs(preference_dtc_.get(),
&UIDataTypeController::Stop));
SetStopExpectations();
EXPECT_CALL(start_callback_, Run(DataTypeController::OK, _, _));
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
Start();
EXPECT_TRUE(syncable_service_.syncing());
preference_dtc_->OnSingleDatatypeUnrecoverableError(FROM_HERE, "Test");
PumpLoop();
EXPECT_EQ(DataTypeController::NOT_RUNNING, preference_dtc_->state());
EXPECT_FALSE(syncable_service_.syncing());
}
}
}