Search in sources :

Example 6 with BaseSyncProcessor

use of com.vodafone360.people.engine.contactsync.BaseSyncProcessor in project 360-Engine-for-Android by 360.

the class ContactSyncEngineTest method testCancelSync.

/**
     * Tests the sync is cancelled in case we remove user data.
     */
@Suppress
public // Breaks tests.
void testCancelSync() {
    Log.i(LOG_TAG, "**** testNativeSync_newEngineInstantiation() begin ****");
    final ArrayList<ProcessorLog> processorLogs = new ArrayList<ProcessorLog>();
    final UiEventCall uiEventCall = new UiEventCall();
    final ProcessorLog processorLog = new ProcessorLog();
    final IEngineEventCallback engineEventCallback = new HelperClasses.EngineCallbackBase() {

        @Override
        public void onUiEvent(ServiceUiRequest event, int request, int status, Object data) {
            Log.i(LOG_TAG, "onUiEvent: " + event + ", " + request + ", " + status + ", " + data);
            uiEventCall.event = event.ordinal();
            uiEventCall.request = request;
            uiEventCall.status = status;
            uiEventCall.data = data;
        }
    };
    final ProcessorFactory factory = new ProcessorFactory() {

        @Override
        public BaseSyncProcessor create(int type, IContactSyncCallback callback, DatabaseHelper dbHelper) {
            Log.i(LOG_TAG, "create(), type=" + type);
            ProcessorLog log = new ProcessorLog();
            log.type = type;
            log.time = System.currentTimeMillis();
            processorLogs.add(log);
            return new BaseSyncProcessor(mContactSyncEngine, null) {

                @Override
                protected void doCancel() {
                    // cancel the job
                    processorLog.type = 1;
                }

                @Override
                protected void doStart() {
                    // set a "timeout" to be called back immediately
                    setTimeout(0);
                    processorLog.type = 2;
                }

                @Override
                public void onTimeoutEvent() {
                    // set the job as completed
                    Log.i(LOG_TAG, "onTimeoutEvent()");
                    complete(ServiceStatus.SUCCESS);
                    processorLog.type = 3;
                }

                @Override
                public void processCommsResponse(DecodedResponse resp) {
                    // we don't need this case in this test
                    processorLog.type = 4;
                }
            };
        }
    };
    minimalEngineSetup(engineEventCallback, factory);
    // set the connection to be fine
    NetworkAgent.setAgentState(NetworkAgent.AgentState.CONNECTED);
    long nextRuntime = mContactSyncEngine.getNextRunTime();
    // should be equal to -1 because first time sync has not been yet
    // started
    assertEquals(-1, nextRuntime);
    // force a first time sync
    mContactSyncEngine.addUiStartFullSync();
    processorLog.type = 0;
    // start performing the sync
    mContactSyncEngine.run();
    // the first processor should have started
    assertTrue(processorLog.type == 2);
    // this will cancel any sync
    mContactSyncEngine.onReset();
    // get the engine to perform a cancel on the current processor
    mContactSyncEngine.run();
    assertTrue(processorLog.type == 1);
    // check that the engine cancelled the sync
    assertEquals(ServiceUiRequest.UI_REQUEST_COMPLETE.ordinal(), uiEventCall.event);
    assertEquals(uiEventCall.status, ServiceStatus.USER_CANCELLED.ordinal());
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) ArrayList(java.util.ArrayList) ServiceUiRequest(com.vodafone360.people.service.ServiceUiRequest) DatabaseHelper(com.vodafone360.people.database.DatabaseHelper) BaseSyncProcessor(com.vodafone360.people.engine.contactsync.BaseSyncProcessor) ProcessorFactory(com.vodafone360.people.engine.contactsync.ProcessorFactory) IContactSyncCallback(com.vodafone360.people.engine.contactsync.IContactSyncCallback) IEngineEventCallback(com.vodafone360.people.engine.IEngineEventCallback) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 7 with BaseSyncProcessor

use of com.vodafone360.people.engine.contactsync.BaseSyncProcessor in project 360-Engine-for-Android by 360.

the class UpdateNativeContacts method onTimeoutEvent.

/**
     * @see BaseSyncProcessor#onTimeoutEvent()
     */
@Override
public void onTimeoutEvent() {
    LogUtils.logD("UpdateNativeContacts.onTimeoutEvent()");
    // call the tick method of the NativeExporter
    final boolean isDone = mNativeExporter.tick();
    // get the index of the last processed id
    final int position = mNativeExporter.getPosition();
    // get the total count of ids to process
    final int total = mNativeExporter.getCount();
    // get the percentage out of position and total count
    final int percentage = (total != 0) ? ((position * 100) / total) : 100;
    LogUtils.logD("UpdateNativeContacts.onTimeoutEvent() - pos=" + position + ", total=" + total + ", percentage=" + percentage);
    // check the NativeExporter progress
    if (!isDone) {
        // yield some time to the other engines and request to be called back immediately
        setTimeout(0);
    } else {
        // FIXME: More useful error reporting beyond just ERROR_UNKNOWN
        final ServiceStatus status = mNativeExporter.getResult() == NativeImporter.RESULT_OK ? ServiceStatus.SUCCESS : ServiceStatus.ERROR_UNKNOWN;
        LogUtils.logD("FetchNativeContacts.onTimeoutEvent() - complete(" + status + ")");
        complete(status);
    }
}
Also used : ServiceStatus(com.vodafone360.people.service.ServiceStatus)

Example 8 with BaseSyncProcessor

use of com.vodafone360.people.engine.contactsync.BaseSyncProcessor in project 360-Engine-for-Android by 360.

the class ContactSyncEngineTest method testUiRequestCompleteEvent_fullSync.

/**
     * Verifies that events are fired after UI requests.
     */
@Suppress
public // Breaks tests.
void testUiRequestCompleteEvent_fullSync() {
    Log.i(LOG_TAG, "**** testUiRequestCompleteEvent_fullSync() begin ****");
    final UiEventCall uiEventCall = new UiEventCall();
    final IEngineEventCallback engineEventCallback = new HelperClasses.EngineCallbackBase() {

        @Override
        public void onUiEvent(ServiceUiRequest event, int request, int status, Object data) {
            Log.i(LOG_TAG, "onUiEvent: " + event + ", " + request + ", " + status + ", " + data);
            uiEventCall.event = event.ordinal();
            uiEventCall.request = request;
            uiEventCall.status = status;
            uiEventCall.data = data;
        }
    };
    final ProcessorFactory factory = new ProcessorFactory() {

        @Override
        public BaseSyncProcessor create(int type, IContactSyncCallback callback, DatabaseHelper dbHelper) {
            Log.i(LOG_TAG, "create(), type=" + type);
            return new DummySyncProcessor(mContactSyncEngine, null);
        }
    };
    minimalEngineSetup(engineEventCallback, factory);
    NetworkAgent.setAgentState(NetworkAgent.AgentState.CONNECTED);
    long nextRuntime = mContactSyncEngine.getNextRunTime();
    // should be equal to -1 because first time sync has not been yet
    // started
    assertEquals(-1, nextRuntime);
    // set the connection to be fine
    NetworkAgent.setAgentState(AgentState.CONNECTED);
    // ask for a full sync
    mContactSyncEngine.addUiStartFullSync();
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertEquals(0, nextRuntime);
    mContactSyncEngine.run();
    // check that first time sync is completed
    assertEquals(ServiceUiRequest.UI_REQUEST_COMPLETE.ordinal(), uiEventCall.event);
    assertEquals(uiEventCall.status, ServiceStatus.SUCCESS.ordinal());
    Log.i(LOG_TAG, "**** testUiRequestCompleteEvent_fullSync() end ****");
}
Also used : DatabaseHelper(com.vodafone360.people.database.DatabaseHelper) ServiceUiRequest(com.vodafone360.people.service.ServiceUiRequest) ProcessorFactory(com.vodafone360.people.engine.contactsync.ProcessorFactory) IContactSyncCallback(com.vodafone360.people.engine.contactsync.IContactSyncCallback) IEngineEventCallback(com.vodafone360.people.engine.IEngineEventCallback) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 9 with BaseSyncProcessor

use of com.vodafone360.people.engine.contactsync.BaseSyncProcessor in project 360-Engine-for-Android by 360.

the class DefaultProcessorFactoryTest method testProcessorTypeCreation.

/**
     * Tests the type of the created processor depending on the requested type.
     */
@Suppress
public void testProcessorTypeCreation() {
    DefaultProcessorFactory factory = new DefaultProcessorFactory();
    BaseSyncProcessor processor;
    processor = factory.create(ProcessorFactory.DOWNLOAD_SERVER_CONTACTS, null, null);
    assertTrue(processor instanceof DownloadServerContacts);
    processor = factory.create(ProcessorFactory.FETCH_NATIVE_CONTACTS, null, null);
    assertTrue(processor instanceof FetchNativeContacts);
    processor = factory.create(ProcessorFactory.UPDATE_NATIVE_CONTACTS, null, null);
    assertTrue(processor instanceof UpdateNativeContacts);
    processor = factory.create(ProcessorFactory.UPLOAD_SERVER_CONTACTS, null, null);
    assertTrue(processor instanceof UploadServerContacts);
}
Also used : UpdateNativeContacts(com.vodafone360.people.engine.contactsync.UpdateNativeContacts) FetchNativeContacts(com.vodafone360.people.engine.contactsync.FetchNativeContacts) DefaultProcessorFactory(com.vodafone360.people.engine.contactsync.DefaultProcessorFactory) BaseSyncProcessor(com.vodafone360.people.engine.contactsync.BaseSyncProcessor) UploadServerContacts(com.vodafone360.people.engine.contactsync.UploadServerContacts) DownloadServerContacts(com.vodafone360.people.engine.contactsync.DownloadServerContacts) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 10 with BaseSyncProcessor

use of com.vodafone360.people.engine.contactsync.BaseSyncProcessor in project 360-Engine-for-Android by 360.

the class ContactSyncEngineTest method testAllSyncs.

/**
     * Checks different sync request that come from: -first time sync -fake
     * database change event -full sync -server sync
     */
@Suppress
public // Breaks tests.
void testAllSyncs() {
    Log.i(LOG_TAG, "**** testAllSyncs() begin ****");
    final ArrayList<ProcessorLog> processorLogs = new ArrayList<ProcessorLog>();
    final UiEventCall uiEventCall = new UiEventCall();
    final IEngineEventCallback engineEventCallback = new HelperClasses.EngineCallbackBase() {

        @Override
        public void onUiEvent(ServiceUiRequest event, int request, int status, Object data) {
            Log.i(LOG_TAG, "onUiEvent: " + event + ", " + request + ", " + status + ", " + data);
            uiEventCall.event = event.ordinal();
            uiEventCall.request = request;
            uiEventCall.status = status;
            uiEventCall.data = data;
        }
    };
    final ProcessorFactory factory = new ProcessorFactory() {

        @Override
        public BaseSyncProcessor create(int type, IContactSyncCallback callback, DatabaseHelper dbHelper) {
            Log.i(LOG_TAG, "create(), type=" + type);
            ProcessorLog log = new ProcessorLog();
            log.type = type;
            log.time = System.currentTimeMillis();
            processorLogs.add(log);
            return new DummySyncProcessor(mContactSyncEngine, null);
        }
    };
    minimalEngineSetup(engineEventCallback, factory);
    // set the connection to be fine
    NetworkAgent.setAgentState(NetworkAgent.AgentState.CONNECTED);
    long nextRuntime = mContactSyncEngine.getNextRunTime();
    // should be equal to -1 because first time sync has not been yet
    // started
    assertEquals(-1, mContactSyncEngine.getNextRunTime());
    // force a first time sync
    mContactSyncEngine.addUiStartFullSync();
    nextRuntime = mContactSyncEngine.getNextRunTime();
    // next runtime should be now
    assertEquals(0, nextRuntime);
    // perform the first time sync
    mContactSyncEngine.run();
    // check that first time sync is completed
    assertEquals(ServiceUiRequest.UI_REQUEST_COMPLETE.ordinal(), uiEventCall.event);
    assertEquals(uiEventCall.status, ServiceStatus.SUCCESS.ordinal());
    // check that a thumbnail sync is scheduled for now
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertEquals(0, nextRuntime);
    // reset the processor logs
    processorLogs.clear();
    // get the thumbnail sync to be run
    mContactSyncEngine.run();
    // check processor calls
    ProcessorLog log;
    assertEquals(2, processorLogs.size());
    log = processorLogs.get(0);
    assertEquals(ProcessorFactory.DOWNLOAD_SERVER_THUMBNAILS, log.type);
    log = processorLogs.get(1);
    assertEquals(ProcessorFactory.UPLOAD_SERVER_THUMBNAILS, log.type);
    // check that native sync is scheduled for now
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertEquals(0, nextRuntime);
    // reset the processor logs
    processorLogs.clear();
    // get the native sync to be run
    mContactSyncEngine.run();
    // check processor calls
    assertEquals(1, processorLogs.size());
    log = processorLogs.get(0);
    assertEquals(ProcessorFactory.UPDATE_NATIVE_CONTACTS, log.type);
    // reset the processor logs
    processorLogs.clear();
    // request a full sync
    mContactSyncEngine.addUiStartFullSync();
    nextRuntime = mContactSyncEngine.getNextRunTime();
    // next runtime should be now
    assertEquals(0, nextRuntime);
    // perform the full sync
    mContactSyncEngine.run();
    // check that first time sync is completed
    assertEquals(ServiceUiRequest.UI_REQUEST_COMPLETE.ordinal(), uiEventCall.event);
    assertEquals(uiEventCall.status, ServiceStatus.SUCCESS.ordinal());
    // get the thumbnail sync to be run
    mContactSyncEngine.run();
    // get the native sync to be run
    mContactSyncEngine.run();
    // get the thumbnail sync to be run
    mContactSyncEngine.run();
    // check nothing to be done
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertEquals(-1, nextRuntime);
    // fake a database change notification
    Handler handler = getContactSyncEngineHandler(mContactSyncEngine);
    Message msg = new Message();
    msg.what = ServiceUiRequest.DATABASE_CHANGED_EVENT.ordinal();
    msg.arg1 = DatabaseHelper.DatabaseChangeType.CONTACTS.ordinal();
    msg.arg2 = 0;
    handler.sendMessage(msg);
    final Looper looper = Looper.myLooper();
    final MessageQueue queue = Looper.myQueue();
    queue.addIdleHandler(new MessageQueue.IdleHandler() {

        @Override
        public boolean queueIdle() {
            // message has been processed and the looper is now in idle
            // state
            // quit the loop() otherwise we would not be able to carry on
            looper.quit();
            return false;
        }
    });
    // get the message processed by the thread event loop
    Looper.loop();
    // check sync is scheduled within 30 seconds
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertTrue(isValueInsideErrorMargin(30000, nextRuntime - System.currentTimeMillis(), 5));
    final long timeBeforeWait = System.currentTimeMillis();
    // reset the processor logs
    processorLogs.clear();
    // call run() and check that nothing is performed
    mContactSyncEngine.run();
    assertEquals(0, processorLogs.size());
    // wait until we get the nextRuntime to now
    boolean isNextRuntimeNow = false;
    while (!isNextRuntimeNow) {
        try {
            long timeToWait = mContactSyncEngine.getNextRunTime();
            timeToWait = (timeToWait <= 0) ? 0 : timeToWait - System.currentTimeMillis();
            if (timeToWait > 0) {
                synchronized (this) {
                    Log.i(LOG_TAG, "timeToWait=" + timeToWait);
                    wait(timeToWait);
                }
            }
            if (mContactSyncEngine.getNextRunTime() < System.currentTimeMillis()) {
                isNextRuntimeNow = true;
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "testAllSyncs(): error while waiting for the runtime now");
        }
    }
    long timeAfterWait = System.currentTimeMillis();
    // check that we have waited about 30 seconds
    assertTrue(isValueInsideErrorMargin(timeAfterWait - timeBeforeWait, 30000, 5));
    // call run() until the sync is performed
    final long startTime = System.currentTimeMillis();
    while (processorLogs.size() < 7) {
        if (System.currentTimeMillis() - startTime > TEST_TIMEOUT) {
            fail("It seems that the engine is stuck, the processor logs should contain 7 objects by now!");
        }
        mContactSyncEngine.run();
    }
    // check processor calls
    assertEquals(6, processorLogs.size());
    log = processorLogs.get(0);
    assertEquals(ProcessorFactory.DOWNLOAD_SERVER_CONTACTS, log.type);
    log = processorLogs.get(1);
    assertEquals(ProcessorFactory.UPLOAD_SERVER_CONTACTS, log.type);
    log = processorLogs.get(2);
    assertEquals(ProcessorFactory.DOWNLOAD_SERVER_THUMBNAILS, log.type);
    log = processorLogs.get(3);
    assertEquals(ProcessorFactory.UPLOAD_SERVER_THUMBNAILS, log.type);
    log = processorLogs.get(4);
    assertEquals(ProcessorFactory.SYNC_ME_PROFILE, log.type);
    log = processorLogs.get(5);
    assertEquals(ProcessorFactory.UPDATE_NATIVE_CONTACTS, log.type);
    Log.i(LOG_TAG, "**** testAllSyncs() end ****");
}
Also used : Message(android.os.Message) ArrayList(java.util.ArrayList) Handler(android.os.Handler) ServiceUiRequest(com.vodafone360.people.service.ServiceUiRequest) Looper(android.os.Looper) MessageQueue(android.os.MessageQueue) DatabaseHelper(com.vodafone360.people.database.DatabaseHelper) ProcessorFactory(com.vodafone360.people.engine.contactsync.ProcessorFactory) IContactSyncCallback(com.vodafone360.people.engine.contactsync.IContactSyncCallback) IEngineEventCallback(com.vodafone360.people.engine.IEngineEventCallback) Suppress(android.test.suitebuilder.annotation.Suppress)

Aggregations

Suppress (android.test.suitebuilder.annotation.Suppress)12 DatabaseHelper (com.vodafone360.people.database.DatabaseHelper)11 IEngineEventCallback (com.vodafone360.people.engine.IEngineEventCallback)11 IContactSyncCallback (com.vodafone360.people.engine.contactsync.IContactSyncCallback)11 ProcessorFactory (com.vodafone360.people.engine.contactsync.ProcessorFactory)11 ServiceUiRequest (com.vodafone360.people.service.ServiceUiRequest)9 ArrayList (java.util.ArrayList)6 BaseSyncProcessor (com.vodafone360.people.engine.contactsync.BaseSyncProcessor)2 Handler (android.os.Handler)1 Looper (android.os.Looper)1 Message (android.os.Message)1 MessageQueue (android.os.MessageQueue)1 IContactSyncObserver (com.vodafone360.people.engine.contactsync.ContactSyncEngine.IContactSyncObserver)1 DefaultProcessorFactory (com.vodafone360.people.engine.contactsync.DefaultProcessorFactory)1 DownloadServerContacts (com.vodafone360.people.engine.contactsync.DownloadServerContacts)1 FetchNativeContacts (com.vodafone360.people.engine.contactsync.FetchNativeContacts)1 UpdateNativeContacts (com.vodafone360.people.engine.contactsync.UpdateNativeContacts)1 UploadServerContacts (com.vodafone360.people.engine.contactsync.UploadServerContacts)1 ServiceStatus (com.vodafone360.people.service.ServiceStatus)1 DecodedResponse (com.vodafone360.people.service.io.ResponseQueue.DecodedResponse)1