Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: base/android/junit/src/org/chromium/base/PromiseTest.java

Issue 1885463002: Create a Promise class to simplify dealing with async results. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/android/junit/src/org/chromium/base/PromiseTest.java
diff --git a/base/android/junit/src/org/chromium/base/PromiseTest.java b/base/android/junit/src/org/chromium/base/PromiseTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..0b5d4c7ceefef1c1dd24f46c62a3b5fa1df6a091
--- /dev/null
+++ b/base/android/junit/src/org/chromium/base/PromiseTest.java
@@ -0,0 +1,92 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.base;
+
+import static org.junit.Assert.assertEquals;
+
+import org.chromium.testing.local.LocalRobolectricTestRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.annotation.Config;
+
+/** Unit tests for {@link Promise}. */
+@RunWith(LocalRobolectricTestRunner.class)
+@Config(manifest = Config.NONE)
+public class PromiseTest {
+ // We need a simple mutable reference type for testing.
+ private static class Value {
+ private int mValue;
+
+ public int get() {
+ return mValue;
+ }
+
+ public void set(int value) {
+ mValue = value;
+ }
+ }
+
+ /** Tests that the callback is called on fulfillment. */
+ @Test
+ public void callback() {
+ final Value value = new Value();
+
+ Promise<Integer> promise = new Promise<Integer>();
+ promise.then(new Callback<Integer>() {
+ @Override
+ public void onResult(Integer result) {
+ value.set(result);
+ }
+ });
+
+ assertEquals(value.get(), 0);
+
+ promise.fulfill(new Integer(1));
+ assertEquals(value.get(), 1);
+ }
+
+ /** Tests that multiple callbacks are called. */
+ @Test
+ public void multipleCallbacks() {
+ final Value value = new Value();
+
+ Promise<Integer> promise = new Promise<Integer>();
+ promise.then(new Callback<Integer>() {
+ @Override
+ public void onResult(Integer result) {
+ value.set(1);
+ }
+ });
+ promise.then(new Callback<Integer>() {
+ @Override
+ public void onResult(Integer result) {
+ value.set(2);
+ }
+ });
+
+ assertEquals(value.get(), 0);
+
+ promise.fulfill(new Integer(0));
+ assertEquals(value.get(), 2);
+ }
+
+ /** Tests that a callback is called immediately when given to a fulfilled Promise. */
+ @Test
+ public void callbackOnFulfilled() {
+ final Value value = new Value();
+
+ Promise<Integer> promise = Promise.fulfilled(new Integer(0));
+ assertEquals(value.get(), 0);
+
+ promise.then(new Callback<Integer>() {
+ @Override
+ public void onResult(Integer result) {
+ value.set(1);
+ }
+ });
+
+ assertEquals(value.get(), 1);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698