/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.exoplayer2.testutil;

import com.google.android.exoplayer2.BaseRenderer;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.util.MimeTypes;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import junit.framework.Assert;

/**
 * Fake {@link Renderer} that supports any format with the matching MIME type. The renderer
 * verifies that it reads one of the given {@link Format}s.
 */
public class FakeRenderer extends BaseRenderer {

  private final List<Format> expectedFormats;

  public int positionResetCount;
  public int formatReadCount;
  public int bufferReadCount;
  public boolean isEnded;

  public FakeRenderer(Format... expectedFormats) {
    super(expectedFormats.length == 0 ? C.TRACK_TYPE_UNKNOWN
        : MimeTypes.getTrackType(expectedFormats[0].sampleMimeType));
    this.expectedFormats = Collections.unmodifiableList(Arrays.asList(expectedFormats));
  }

  @Override
  protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException {
    positionResetCount++;
    isEnded = false;
  }

  @Override
  public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
    if (isEnded) {
      return;
    }

    // Verify the format matches the expected format.
    FormatHolder formatHolder = new FormatHolder();
    DecoderInputBuffer buffer =
        new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
    int result = readSource(formatHolder, buffer, false);
    if (result == C.RESULT_FORMAT_READ) {
      formatReadCount++;
      Assert.assertTrue(expectedFormats.contains(formatHolder.format));
    } else if (result == C.RESULT_BUFFER_READ) {
      bufferReadCount++;
      if (buffer.isEndOfStream()) {
        isEnded = true;
      }
    }
  }

  @Override
  public boolean isReady() {
    return isSourceReady();
  }

  @Override
  public boolean isEnded() {
    return isEnded;
  }

  @Override
  public int supportsFormat(Format format) throws ExoPlaybackException {
    return getTrackType() == MimeTypes.getTrackType(format.sampleMimeType) ? FORMAT_HANDLED
        : FORMAT_UNSUPPORTED_TYPE;
  }

}

@@ -1438,16 +1438,59 @@
 Formats;
+%0A  private final DecoderInputBuffer buffer;
 %0A%0A  publ
@@ -1601,16 +1601,42 @@
 isEnded;
+%0A  public boolean isReady;
 %0A%0A  publ
@@ -1900,16 +1900,109 @@
 mats));%0A
+    this.buffer = new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);%0A
   %7D%0A%0A  @
@@ -2271,16 +2271,17 @@
     if (
+!
 isEnded)
@@ -2289,27 +2289,8 @@
 %7B%0A  
-    return;%0A    %7D%0A%0A
     
@@ -2343,16 +2343,18 @@
 at.%0A    
+  
 FormatHo
@@ -2399,121 +2399,8 @@
 ;%0A  
-  DecoderInputBuffer buffer =%0A        new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);%0A
     
@@ -2453,16 +2453,18 @@
 false);%0A
+  
     if (
@@ -2503,16 +2503,18 @@
 %7B%0A      
+  
 formatRe
@@ -2530,16 +2530,18 @@
 ;%0A      
+ 
+ 
 Assert.a
@@ -2594,24 +2594,26 @@
 r.format));%0A
+  
     %7D else i
@@ -2655,16 +2655,18 @@
 %7B%0A      
+  
 bufferRe
@@ -2678,24 +2678,26 @@
 nt++;%0A      
+ 
+ 
 if (buffer.i
@@ -2718,24 +2718,26 @@
 ) %7B%0A        
+  
 isEnded = tr
@@ -2750,15 +2750,68 @@
     
+  
 %7D%0A    
+ 
-%7D
+ %7D%0A    %7D%0A    isReady = buffer.timeUs %3E= positionUs;
 %0A  %7D
@@ -2863,16 +2863,27 @@
   return
+ isReady %7C%7C
  isSourc
@@ -3122,19 +3122,28 @@
 imeType)
+%0A       
  ? 
+(
 FORMAT_H
@@ -3148,24 +3148,37 @@
 _HANDLED
-%0A       
+ %7C ADAPTIVE_SEAMLESS)
  : FORMA

Make isReady of FakeRenderer more realistic.