|
| 1 | +// Copyright 2021 Google LLC |
| 2 | + |
| 3 | +#include "firestore/src/android/promise_factory_android.h" |
| 4 | + |
| 5 | +#include <utility> |
| 6 | + |
| 7 | +#include "android/firestore_integration_test_android.h" |
| 8 | +#include "android/task_completion_source.h" |
| 9 | +#include "firebase/future.h" |
| 10 | +#include "firestore/src/android/converter_android.h" |
| 11 | +#include "firestore/src/android/firestore_android.h" |
| 12 | +#include "firestore/src/jni/env.h" |
| 13 | +#include "firestore/src/jni/integer.h" |
| 14 | +#include "firestore/src/jni/ownership.h" |
| 15 | +#include "firestore/src/jni/task.h" |
| 16 | +#include "gmock/gmock.h" |
| 17 | +#include "gtest/gtest.h" |
| 18 | + |
| 19 | +namespace firebase { |
| 20 | +namespace firestore { |
| 21 | +namespace { |
| 22 | + |
| 23 | +using jni::Env; |
| 24 | +using jni::Integer; |
| 25 | +using jni::Local; |
| 26 | +using jni::Task; |
| 27 | + |
| 28 | +// Since `PromiseFactory` acts as a "constructor" of `Promise` objects, its |
| 29 | +// ability to create `Promise` objects is thoroughly tested in the unit tests |
| 30 | +// for `Promise` and therefore the tests here only cover the other aspects of |
| 31 | +// `PromiseFactory`, such as move semantics. |
| 32 | + |
| 33 | +class PromiseFactoryTest : public FirestoreAndroidIntegrationTest { |
| 34 | + public: |
| 35 | + // An enum of asynchronous functions to use in tests, as required by |
| 36 | + // `FutureManager`. |
| 37 | + enum class AsyncFn { |
| 38 | + kFn, |
| 39 | + kCount, // Must be the last enum value. |
| 40 | + }; |
| 41 | + |
| 42 | + protected: |
| 43 | + void AssertCreatesValidFutures(Env& env, |
| 44 | + PromiseFactory<AsyncFn>& promise_factory) { |
| 45 | + Local<TaskCompletionSource> tcs = TaskCompletionSource::Create(env); |
| 46 | + Local<Task> task = tcs.GetTask(env); |
| 47 | + Future<void> future = |
| 48 | + promise_factory.NewFuture<void>(env, AsyncFn::kFn, task); |
| 49 | + EXPECT_EQ(future.status(), FutureStatus::kFutureStatusPending); |
| 50 | + tcs.SetResult(env, jni::Integer::Create(env, 42)); |
| 51 | + Await(future); |
| 52 | + EXPECT_EQ(future.status(), FutureStatus::kFutureStatusComplete); |
| 53 | + } |
| 54 | + |
| 55 | + void AssertCreatesInvalidFutures(Env& env, |
| 56 | + PromiseFactory<AsyncFn>& promise_factory) { |
| 57 | + Local<TaskCompletionSource> tcs = TaskCompletionSource::Create(env); |
| 58 | + Local<Task> task = tcs.GetTask(env); |
| 59 | + Future<void> future = |
| 60 | + promise_factory.NewFuture<void>(env, AsyncFn::kFn, task); |
| 61 | + EXPECT_EQ(future.status(), FutureStatus::kFutureStatusInvalid); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +TEST_F(PromiseFactoryTest, CopyConstructor) { |
| 66 | + Firestore* firestore = TestFirestore(); |
| 67 | + PromiseFactory<AsyncFn> promise_factory1(GetFirestoreInternal(firestore)); |
| 68 | + |
| 69 | + PromiseFactory<AsyncFn> promise_factory2(promise_factory1); |
| 70 | + |
| 71 | + Env env; |
| 72 | + { |
| 73 | + SCOPED_TRACE("promise_factory1"); |
| 74 | + AssertCreatesValidFutures(env, promise_factory1); |
| 75 | + } |
| 76 | + { |
| 77 | + SCOPED_TRACE("promise_factory2"); |
| 78 | + AssertCreatesValidFutures(env, promise_factory2); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +TEST_F(PromiseFactoryTest, CopyConstructorWithDeletedFirestore) { |
| 83 | + Firestore* firestore = TestFirestore(); |
| 84 | + PromiseFactory<AsyncFn> promise_factory1(GetFirestoreInternal(firestore)); |
| 85 | + DeleteFirestore(firestore); |
| 86 | + |
| 87 | + PromiseFactory<AsyncFn> promise_factory2(promise_factory1); |
| 88 | + |
| 89 | + Env env; |
| 90 | + { |
| 91 | + SCOPED_TRACE("promise_factory1"); |
| 92 | + AssertCreatesInvalidFutures(env, promise_factory1); |
| 93 | + } |
| 94 | + { |
| 95 | + SCOPED_TRACE("promise_factory2"); |
| 96 | + AssertCreatesInvalidFutures(env, promise_factory2); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +TEST_F(PromiseFactoryTest, MoveConstructor) { |
| 101 | + Firestore* firestore = TestFirestore(); |
| 102 | + PromiseFactory<AsyncFn> promise_factory1(GetFirestoreInternal(firestore)); |
| 103 | + |
| 104 | + PromiseFactory<AsyncFn> promise_factory2(std::move(promise_factory1)); |
| 105 | + |
| 106 | + Env env; |
| 107 | + AssertCreatesValidFutures(env, promise_factory2); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_F(PromiseFactoryTest, MoveConstructorWithDeletedFirestore) { |
| 111 | + Firestore* firestore = TestFirestore(); |
| 112 | + PromiseFactory<AsyncFn> promise_factory1(GetFirestoreInternal(firestore)); |
| 113 | + DeleteFirestore(firestore); |
| 114 | + |
| 115 | + PromiseFactory<AsyncFn> promise_factory2(std::move(promise_factory1)); |
| 116 | + |
| 117 | + Env env; |
| 118 | + AssertCreatesInvalidFutures(env, promise_factory2); |
| 119 | +} |
| 120 | + |
| 121 | +TEST_F(PromiseFactoryTest, ShouldCreateInvalidPromisesIfFirestoreIsDeleted) { |
| 122 | + Firestore* firestore = TestFirestore(); |
| 123 | + PromiseFactory<AsyncFn> promise_factory(GetFirestoreInternal(firestore)); |
| 124 | + DeleteFirestore(firestore); |
| 125 | + Env env; |
| 126 | + |
| 127 | + AssertCreatesInvalidFutures(env, promise_factory); |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace |
| 131 | +} // namespace firestore |
| 132 | +} // namespace firebase |
0 commit comments