private IDataSource <CloseableReference <T> > SubmitFetchRequest <T>(
            IProducer <CloseableReference <T> > producerSequence,
            ImageRequest imageRequest,
            RequestLevel lowestPermittedRequestLevelOnSubmit,
            object callerContext)
        {
            IRequestListener requestListener = GetRequestListenerForRequest(imageRequest);

            try
            {
                RequestLevel lowestPermittedRequestLevel = RequestLevel.GetMax(
                    imageRequest.LowestPermittedRequestLevel,
                    lowestPermittedRequestLevelOnSubmit);

                SettableProducerContext settableProducerContext = new SettableProducerContext(
                    imageRequest,
                    GenerateUniqueFutureId(),
                    requestListener,
                    callerContext,
                    lowestPermittedRequestLevel.Value,
                    /* isPrefetch */ false,
                    imageRequest.IsProgressiveRenderingEnabled ||
                    !UriUtil.IsNetworkUri(imageRequest.SourceUri),
                    imageRequest.Priority);

                return(CloseableProducerToDataSourceAdapter <T> .Create(
                           producerSequence,
                           settableProducerContext,
                           requestListener));
            }
            catch (Exception exception)
            {
                return(DataSources.ImmediateFailedDataSource <CloseableReference <T> >(exception));
            }
        }
        private IDataSource <object> SubmitPrefetchRequest(
            IProducer <object> producerSequence,
            ImageRequest imageRequest,
            RequestLevel lowestPermittedRequestLevelOnSubmit,
            object callerContext,
            int priority)
        {
            IRequestListener requestListener = GetRequestListenerForRequest(imageRequest);

            try
            {
                RequestLevel lowestPermittedRequestLevel = RequestLevel.GetMax(
                    imageRequest.LowestPermittedRequestLevel,
                    lowestPermittedRequestLevelOnSubmit);

                SettableProducerContext settableProducerContext = new SettableProducerContext(
                    imageRequest,
                    GenerateUniqueFutureId(),
                    requestListener,
                    callerContext,
                    lowestPermittedRequestLevel.Value,
                    /* isPrefetch */ true,
                    /* isIntermediateResultExpected */ false,
                    priority);

                return(ProducerToDataSourceAdapter <object> .Create(
                           producerSequence,
                           settableProducerContext,
                           requestListener));
            }
            catch (Exception exception)
            {
                return(DataSources.ImmediateFailedDataSource <object>(exception));
            }
        }
Beispiel #3
0
        public void TestDownloadHandler()
        {
            _imageRequest = ImageRequestBuilder
                            .NewBuilderWithSource(IMAGE_URL)
                            .SetProgressiveRenderingEnabled(true)
                            .Build();

            _producerContext = new SettableProducerContext(
                _imageRequest,
                $"{ _imageRequest.SourceUri.ToString() }2",
                _producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);

            _networkFetchProducer.ProduceResults(_consumer, _producerContext);

            // Wait for callback
            _completion.WaitOne();

            Assert.IsTrue(_onNewResultImplCalls > 1);
            Assert.IsTrue(_intermediateResultProducerEventCalls > 1);
        }
Beispiel #4
0
        public void TestExceptionInFetchImage()
        {
            _imageRequest = ImageRequestBuilder
                            .NewBuilderWithSource(FAILURE_URL)
                            .SetProgressiveRenderingEnabled(true)
                            .Build();

            _producerContext = new SettableProducerContext(
                _imageRequest,
                _imageRequest.SourceUri.ToString(),
                _producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);

            _networkFetchProducer.ProduceResults(_consumer, _producerContext);

            // Wait for callback
            _completion.WaitOne();

            Assert.IsTrue(_onProducerFinishWithFailureFuncCalls == 1);
            Assert.AreEqual(_internalRequestId, _imageRequest.SourceUri.ToString());
            Assert.AreEqual(_internalProducerName, NetworkFetchProducer.PRODUCER_NAME);
            Assert.IsNotNull(_internalError);
            Assert.IsNull(_internalExtraMap);
        }
 /// <summary>
 /// CloseableProducerToDataSourceAdapter factory method
 /// </summary>
 public static IDataSource <CloseableReference <T> > Create(
     IProducer <CloseableReference <T> > producer,
     SettableProducerContext settableProducerContext,
     IRequestListener listener)
 {
     return(new CloseableProducerToDataSourceAdapter <T>(
                producer, settableProducerContext, listener));
 }
Beispiel #6
0
 private ProducerToDataSourceAdapter(
     IProducer <T> producer,
     SettableProducerContext settableProducerContext,
     IRequestListener listener) : base(
         producer,
         settableProducerContext,
         listener)
 {
 }
 private CloseableProducerToDataSourceAdapter(
     IProducer <CloseableReference <T> > producer,
     SettableProducerContext settableProducerContext,
     IRequestListener listener) : base(
         producer,
         settableProducerContext,
         listener)
 {
 }
        /// <summary>
        /// Instantiates the <see cref="AbstractProducerToDataSourceAdapter{T}"/>.
        /// </summary>
        protected AbstractProducerToDataSourceAdapter(
            IProducer <T> producer,
            SettableProducerContext settableProducerContext,
            IRequestListener requestListener)
        {
            _settableProducerContext = settableProducerContext;
            _requestListener         = requestListener;
            _requestListener.OnRequestStart(
                _settableProducerContext.ImageRequest,
                _settableProducerContext.CallerContext,
                _settableProducerContext.Id,
                _settableProducerContext.IsPrefetch);

            producer.ProduceResults(CreateConsumer(), settableProducerContext);
        }
Beispiel #9
0
        public void Initialize()
        {
            // Initializes the mock producer listener
            ProducerListenerImpl producerListener = new ProducerListenerImpl(
                (_, __) => { },
                (_, __, ___) => { },
                (_, __, ___) => { },
                (_, __, ___, ____) => { },
                (_, __, ___) => { },
                (_) =>
            {
                return(false);
            });

            // Initializes the mock producer context callbacks
            _callbacks1 = new BaseProducerContextCallbacks(
                () =>
            {
                ++_onCancellationRequestedCount1;
            },
                () =>
            {
                ++_onIsPrefetchChangedCount1;
            },
                () =>
            {
                ++_onIsIntermediateResultExpectedChangedCount1;
            },
                () =>
            {
                ++_onPriorityChangedCount1;
            });

            _callbacks2 = new BaseProducerContextCallbacks(
                () =>
            {
                ++_onCancellationRequestedCount2;
            },
                () =>
            {
                ++_onIsPrefetchChangedCount2;
            },
                () =>
            {
                ++_onIsIntermediateResultExpectedChangedCount2;
            },
                () =>
            {
                ++_onPriorityChangedCount2;
            });

            _settableProducerContext = new SettableProducerContext(
                IMAGE_REQUEST,
                REQUEST_ID,
                producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);
        }
Beispiel #10
0
        public void Initialize()
        {
            // Initializes the mock RequestListener
            ProducerListenerImpl producerListener = new ProducerListenerImpl(
                (_, __) => { },
                (_, __, ___) => { },
                (_, __, ___) => { },
                (_, __, ___, ____) => { },
                (_, __, ___) => { },
                (_) => { return(false); });

            _requestListener = new RequestListenerImpl(
                producerListener,
                (imageRequest, callerContext, requestId, isPrefetch) =>
            {
                _onRequestStartInvocation = true;
                _internalImageRequest     = imageRequest;
                _internalCallerContext    = callerContext;
                _internalRequestId        = requestId;
                _internalIsPrefetch       = isPrefetch;
            },
                (imageRequest, requestId, isPrefetch) =>
            {
                _onRequestSuccessInvocation = true;
                _internalImageRequest       = imageRequest;
                _internalRequestId          = requestId;
                _internalIsPrefetch         = isPrefetch;
            },
                (imageRequest, requestId, exception, isPrefetch) =>
            {
                _onRequestFailureInvocation = true;
                _internalImageRequest       = imageRequest;
                _internalRequestId          = requestId;
                _internalException          = exception;
                _internalIsPrefetch         = isPrefetch;
            },
                (requestId) =>
            {
                _onRequestCancellationInvocation = true;
                _internalRequestId = requestId;
            });
            _resourceReleaser = new ResourceReleaserImpl <object>(_ => { });
            _resultRef1       = CloseableReference <object> .of(new object(), _resourceReleaser);

            _resultRef2 = CloseableReference <object> .of(new object(), _resourceReleaser);

            _resultRef3 = CloseableReference <object> .of(new object(), _resourceReleaser);

            _dataSubscriber1 = new MockDataSubscriber <CloseableReference <object> >();
            _dataSubscriber2 = new MockDataSubscriber <CloseableReference <object> >();

            _internalIsPrefetch      = true;
            _settableProducerContext = new SettableProducerContext(
                IMAGE_REQUEST,
                REQUEST_ID,
                producerListener,
                CALLER_CONTEXT,
                RequestLevel.FULL_FETCH,
                IS_PREFETCH,
                true,
                Priority.HIGH);
            _producer = new ProducerImpl <CloseableReference <object> >(
                (consumer, _) =>
            {
                _internalConsumer = consumer;
            });
            _dataSource = CloseableProducerToDataSourceAdapter <object> .Create(
                _producer,
                _settableProducerContext,
                _requestListener);

            Assert.IsTrue(_onRequestStartInvocation);
            Assert.AreSame(_internalImageRequest, IMAGE_REQUEST);
            Assert.AreSame(_internalCallerContext, CALLER_CONTEXT);
            Assert.AreSame(_internalRequestId, REQUEST_ID);
            Assert.IsFalse(_internalIsPrefetch);
            Assert.IsNotNull(_internalConsumer);
            _onRequestStartInvocation = false;

            _dataSource.Subscribe(_dataSubscriber1, CallerThreadExecutor.Instance);
        }
Beispiel #11
0
        public void Initialize()
        {
            // Initializes mock data
            _inputProducer = new ProducerImpl <object>((consumer, producerContext) =>
            {
                _internalConsumer        = consumer;
                _internalProducerContext = (SettableProducerContext)producerContext;
                _completion.Set();
            });

            _consumer = new BaseConsumerImpl <object>(
                (_, __) => { },
                (_) => { },
                () =>
            {
                ++_consumerOnCancellationCount;
                _completion.Set();
            },
                (_) => { });

            _producerListener = new ProducerListenerImpl(
                (requestId, producerName) =>
            {
                ++_onProducerStartCount;
                _internalRequestId    = requestId;
                _internalProducerName = producerName;
            },
                (_, __, ___) =>
            {
                ++_onProducerEventCount;
            },
                (requestId, producerName, extraMap) =>
            {
                ++_onProducerFinishWithSuccessCount;
                _internalRequestId    = requestId;
                _internalProducerName = producerName;
                _internalExtraMap     = extraMap;
            },
                (_, __, ___, ____) =>
            {
                ++_onProducerFinishWithFailureCount;
                _completion.Set();
            },
                (requestId, producerName, extraMap) =>
            {
                ++_onProducerFinishWithCancellationCount;
                _internalRequestId    = requestId;
                _internalProducerName = producerName;
                _internalExtraMap     = extraMap;
            },
                (_) =>
            {
                ++_requiresExtraMapCount;
                return(false);
            });

            _producerContext = new SettableProducerContext(
                IMAGE_REQUEST,
                REQUEST_ID,
                _producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);

            _testExecutorService   = new MockSerialExecutorService();
            _threadHandoffProducer = new ThreadHandoffProducer <object>(
                _inputProducer,
                new ThreadHandoffProducerQueue(_testExecutorService));

            _completion = new ManualResetEvent(false);
        }