Task TestAssertTaskFaults()

in firestore/testapp/Assets/Firebase/Sample/Firestore/UIHandlerAutomated.cs [3017:3198]


    Task TestAssertTaskFaults() {
      return Async(() => {
        testRunner.DisablePostTestIgnoredFailureCheck();

        // Verify that AssertTaskFaults() passes if the Task faults with a FirestoreException with
        // the expected error code.
        {
          var tcs = new TaskCompletionSource<object>();
          tcs.SetException(new FirestoreException(FirestoreError.Unavailable));
          AssertTaskFaults(tcs.Task, FirestoreError.Unavailable);
        }

        // Verify that AssertTaskFaults() fails if the Task faults with an AggregateException that,
        // when flattened, resolves to a FirestoreException with the expected error code.
        {
          var tcs = new TaskCompletionSource<object>();
          var firestoreException = new FirestoreException(FirestoreError.Unavailable);
          var aggregateException1 = new AggregateException(new[] { firestoreException });
          var aggregateException2 = new AggregateException(new[] { aggregateException1 });
          tcs.SetException(aggregateException2);
          Exception thrownException = null;
          try {
            AssertTaskFaults(tcs.Task, FirestoreError.Unavailable);
          } catch (Exception e) {
            thrownException = e;
          }
          if (thrownException == null) {
            FailTest(
                "AssertTaskFaults() should have thrown an exception because the AggregateException" +
                "has multiple nested AggregateException instances.");
          }
        }

        // Verify that AssertTaskFaults() fails if the Task faults with a FirestoreException with
        // an unexpected error code.
        {
          var tcs = new TaskCompletionSource<object>();
          tcs.SetResult(new FirestoreException(FirestoreError.Unavailable));
          Exception thrownException = null;
          try {
            AssertTaskFaults(tcs.Task, FirestoreError.Ok);
          } catch (Exception e) {
            thrownException = e;
          }
          if (thrownException == null) {
            FailTest(
                "AssertTaskFaults() should have thrown an exception because the task faulted " +
                "with an incorrect error code.");
          }
        }

        // Verify that AssertTaskFaults() fails if the Task completes successfully.
        {
          var tcs = new TaskCompletionSource<object>();
          tcs.SetResult(null);
          Exception thrownException = null;
          try {
            AssertTaskFaults(tcs.Task, FirestoreError.Ok);
          } catch (Exception e) {
            thrownException = e;
          }
          if (thrownException == null) {
            FailTest(
                "AssertTaskFaults() should have thrown an exception because the task completed " +
                "successfully.");
          }
        }

        // Verify that AssertTaskFaults() fails if the Task fails with an exception other than
        // FirestoreException.
        {
          var tcs = new TaskCompletionSource<object>();
          tcs.SetException(new InvalidOperationException());
          Exception thrownException = null;
          try {
            AssertTaskFaults(tcs.Task, FirestoreError.Ok);
          } catch (Exception e) {
            thrownException = e;
          }
          if (thrownException == null) {
            FailTest(
                "AssertTaskFaults() should have thrown an exception because the task faulted " +
                "with an incorrect exception type.");
          }
        }

        // Verify that AssertTaskFaults() fails if the Task fails with an AggregateException
        // that, when flattened, resolves to an unexpected exception.
        {
          var tcs = new TaskCompletionSource<object>();
          var exception1 = new InvalidOperationException();
          var exception2 = new AggregateException(new[] { exception1 });
          var exception3 = new AggregateException(new[] { exception2 });
          tcs.SetException(exception3);
          Exception thrownException = null;
          try {
            AssertTaskFaults(tcs.Task, FirestoreError.Ok);
          } catch (Exception e) {
            thrownException = e;
          }
          if (thrownException == null) {
            FailTest(
                "AssertTaskFaults() should have thrown an exception because the task faulted " +
                "with an AggregateException that flattened to an unexpected exception.");
          }
        }

        // Verify that AssertTaskFaults() fails if the Task fails with an AggregateException that
        // has more than one InnerException.
        {
          var tcs = new TaskCompletionSource<object>();
          var exception1 = new InvalidOperationException();
          var exception2 = new InvalidOperationException();
          var exception3 = new AggregateException(new[] { exception1, exception2 });
          tcs.SetException(exception3);
          Exception thrownException = null;
          try {
            AssertTaskFaults(tcs.Task, FirestoreError.Ok);
          } catch (Exception e) {
            thrownException = e;
          }
          if (thrownException == null) {
            FailTest(
                "AssertTaskFaults() should have thrown an exception because the task faulted " +
                "with an AggregateException that could not be fully flattened.");
          }
        }

        // Verify that AssertTaskFaults() fails if the Task faults with a FirestoreException with
        // no message but a regular expression for the message is specified.
        {
          var tcs = new TaskCompletionSource<object>();
          tcs.SetException(new FirestoreException(FirestoreError.Ok));
          Exception thrownException = null;
          try {
            AssertTaskFaults(tcs.Task, FirestoreError.Ok, "SomeMessageRegex");
          } catch (Exception e) {
            thrownException = e;
          }
          if (thrownException == null) {
            FailTest(
                "AssertTaskFaults() should have thrown an exception because the task faulted " +
                "with an exception that does not have a message.");
          } else if (!thrownException.Message.Contains("SomeMessageRegex")) {
            FailTest(
                "AssertTaskFaults() threw an exception (as expected); however, its message was " +
                "incorrect: " + thrownException.Message);
          }
        }

        // Verify that AssertTaskFaults() fails if the Task faults with a FirestoreException with
        // a message that does not match the given regular expression.
        {
          var tcs = new TaskCompletionSource<object>();
          tcs.SetException(new FirestoreException(FirestoreError.Ok, "TheActualMessage"));
          Exception thrownException = null;
          try {
            AssertTaskFaults(tcs.Task, FirestoreError.Ok, "The.*MeaningOfLife");
          } catch (Exception e) {
            thrownException = e;
          }
          if (thrownException == null) {
            FailTest(
                "AssertTaskFaults() should have thrown an exception because the task faulted " +
                "with an exception that does not have a message.");
          } else if (!(thrownException.Message.Contains("TheActualMessage") &&
                       thrownException.Message.Contains("The.*MeaningOfLife"))) {
            FailTest(
                "AssertTaskFaults() threw an exception (as expected); however, its message was " +
                "incorrect: " + thrownException.Message);
          }
        }

        // Verify that AssertTaskFaults() passes if the Task faults with a FirestoreException with
        // a message that matches the given regular expression.
        {
          var tcs = new TaskCompletionSource<object>();
          tcs.SetException(new FirestoreException(FirestoreError.Ok, "TheActualMessage"));
          AssertTaskFaults(tcs.Task, FirestoreError.Ok, "The.*Message");
        }
      });
    }