content: /**
   * Copyright (c) 2016-present, RxJava Contributors.
   *
   * 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 io.reactivex.interop.internal.operators;
  
  import static io.reactivex.interop.RxJava3Interop.*;
  import static org.junit.Assert.*;
  import static org.mockito.ArgumentMatchers.any;
  import static org.mockito.Mockito.*;
  
  import java.util.concurrent.TimeUnit;
  
  import org.junit.*;
  import org.reactivestreams.Publisher;
  
  import io.reactivex.common.Disposable;
  import io.reactivex.common.functions.*;
  import io.reactivex.flowable.Flowable;
  import io.reactivex.interop.TestHelper;
  import io.reactivex.observable.*;
  import io.reactivex.observable.observers.TestObserver;
  
  public class FlowableAllTest {
  
      @Test
      public void testAll() {
          Flowable<String> obs = Flowable.just("one", "two", "six");
  
          SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
  
          all(obs, new Predicate<String>() {
              @Override
              public boolean test(String s) {
                  return s.length() == 3;
              }
          })
          .subscribe(observer);
  
          verify(observer).onSubscribe((Disposable)any());
          verify(observer).onSuccess(true);
          verifyNoMoreInteractions(observer);
      }
  
      @Test
      public void testNotAll() {
          Flowable<String> obs = Flowable.just("one", "two", "three", "six");
  
          SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
  
          all(obs, new Predicate<String>() {
              @Override
              public boolean test(String s) {
                  return s.length() == 3;
              }
          })
          .subscribe(observer);
  
          verify(observer).onSubscribe((Disposable)any());
          verify(observer).onSuccess(false);
          verifyNoMoreInteractions(observer);
      }
  
      @Test
      public void testEmpty() {
          Flowable<String> obs = Flowable.empty();
  
          SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
  
          all(obs, new Predicate<String>() {
              @Override
              public boolean test(String s) {
                  return s.length() == 3;
              }
          })
          .subscribe(observer);
  
          verify(observer).onSubscribe((Disposable)any());
          verify(observer).onSuccess(true);
          verifyNoMoreInteractions(observer);
      }
  
      @Test
      public void testError() {
          Throwable error = new Throwable();
          Flowable<String> obs = Flowable.error(error);
  
          SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
  
          all(obs, new Predicate<String>() {
              @Override
              public boolean test(String s) {
                  return s.length() == 3;
              }
          })
          .subscribe(observer);
  
          verify(observer).onSubscribe((Disposable)any());
          verify(observer).onError(error);
          verifyNoMoreInteractions(observer);
      }
  
      @Test
      public void testFollowingFirst() {
          Flowable<Integer> o = Flowable.fromArray(1, 3, 5, 6);
          Single<Boolean> allOdd = all(o, new Predicate<Integer>() {
              @Override
              public boolean test(Integer i) {
                  return i % 2 == 1;
              }
          });
  
          assertFalse(allOdd.blockingGet());
      }
      @Test(timeout = 5000)
      public void testIssue1935NoUnsubscribeDownstream() {
          Flowable<Integer> source =
              flatMapPublisher(all(Flowable.just(1), new Predicate<Integer>() {
                  @Override
                  public boolean test(Integer t1) {
                      return false;
                  }
              }), new Function<Boolean, Publisher<Integer>>() {
                  @Override
                  public Publisher<Integer> apply(Boolean t1) {
                      return Flowable.just(2).delay(500, TimeUnit.MILLISECONDS);
                  }
              });
  
          assertEquals((Object)2, source.blockingFirst());
      }
  
      @Test
      @Ignore("No backpressure in Single")
      public void testBackpressureIfNoneRequestedNoneShouldBeDelivered() {
          TestObserver<Boolean> ts = new TestObserver<Boolean>();
          all(Flowable.empty(), new Predicate<Object>() {
              @Override
              public boolean test(Object t1) {
                  return false;
              }
          }).subscribe(ts);
  
          ts.assertNoValues();
          ts.assertNoErrors();
          ts.assertNotComplete();
      }
  
      @Test
      public void testBackpressureIfOneRequestedOneShouldBeDelivered() {
          TestObserver<Boolean> ts = new TestObserver<Boolean>();
  
          all(Flowable.empty(), new Predicate<Object>() {
              @Override
              public boolean test(Object t) {
                  return false;
              }
          }).subscribe(ts);
  
          ts.assertTerminated();
          ts.assertNoErrors();
          ts.assertComplete();
  
          ts.assertValue(true);
      }
  
      @Test
      public void testPredicateThrowsExceptionAndValueInCauseMessage() {
          TestObserver<Boolean> ts = new TestObserver<Boolean>();
  
          final IllegalArgumentException ex = new IllegalArgumentException();
  
          all(Flowable.just("Boo!"), new Predicate<String>() {
              @Override
              public boolean test(String v) {
                  throw ex;
              }
          })
          .subscribe(ts);
  
          ts.assertTerminated();
          ts.assertNoValues();
          ts.assertNotComplete();
          ts.assertError(ex);
          // FIXME need to decide about adding the value that probably caused the crash in some way
  //        assertTrue(ex.getCause().getMessage().contains("Boo!"));
      }
  
  }
  