michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.sync.helpers; michael@0: michael@0: import static junit.framework.Assert.assertNotNull; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.background.testhelpers.WaitHelper; michael@0: import org.mozilla.gecko.sync.repositories.InvalidSessionTransitionException; michael@0: import org.mozilla.gecko.sync.repositories.Repository; michael@0: import org.mozilla.gecko.sync.repositories.RepositorySession; michael@0: michael@0: import android.content.Context; michael@0: michael@0: public class SessionTestHelper { michael@0: michael@0: protected static RepositorySession prepareRepositorySession( michael@0: final Context context, michael@0: final boolean begin, michael@0: final Repository repository) { michael@0: michael@0: final WaitHelper testWaiter = WaitHelper.getTestWaiter(); michael@0: michael@0: final String logTag = "prepareRepositorySession"; michael@0: class CreationDelegate extends DefaultSessionCreationDelegate { michael@0: private RepositorySession session; michael@0: synchronized void setSession(RepositorySession session) { michael@0: this.session = session; michael@0: } michael@0: synchronized RepositorySession getSession() { michael@0: return this.session; michael@0: } michael@0: michael@0: @Override michael@0: public void onSessionCreated(final RepositorySession session) { michael@0: assertNotNull(session); michael@0: Logger.info(logTag, "Setting session to " + session); michael@0: setSession(session); michael@0: if (begin) { michael@0: Logger.info(logTag, "Calling session.begin on new session."); michael@0: // The begin callbacks will notify. michael@0: try { michael@0: session.begin(new ExpectBeginDelegate()); michael@0: } catch (InvalidSessionTransitionException e) { michael@0: testWaiter.performNotify(e); michael@0: } michael@0: } else { michael@0: Logger.info(logTag, "Notifying after setting new session."); michael@0: testWaiter.performNotify(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: final CreationDelegate delegate = new CreationDelegate(); michael@0: try { michael@0: Runnable runnable = new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: repository.createSession(delegate, context); michael@0: } michael@0: }; michael@0: testWaiter.performWait(runnable); michael@0: } catch (IllegalArgumentException ex) { michael@0: Logger.warn(logTag, "Caught IllegalArgumentException."); michael@0: } michael@0: michael@0: Logger.info(logTag, "Retrieving new session."); michael@0: final RepositorySession session = delegate.getSession(); michael@0: assertNotNull(session); michael@0: michael@0: return session; michael@0: } michael@0: michael@0: public static RepositorySession createSession(final Context context, final Repository repository) { michael@0: return prepareRepositorySession(context, false, repository); michael@0: } michael@0: michael@0: public static RepositorySession createAndBeginSession(Context context, Repository repository) { michael@0: return prepareRepositorySession(context, true, repository); michael@0: } michael@0: }