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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.base;
6
7 import static org.junit.Assert.assertEquals;
8
9 import org.chromium.testing.local.LocalRobolectricTestRunner;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.robolectric.annotation.Config;
13
14 /** Unit tests for {@link Promise}. */
15 @RunWith(LocalRobolectricTestRunner.class)
16 @Config(manifest = Config.NONE)
17 public class PromiseTest {
18 // We need a simple mutable reference type for testing.
19 private static class Value {
20 private int mValue;
21
22 public int get() {
23 return mValue;
24 }
25
26 public void set(int value) {
27 mValue = value;
28 }
29 }
30
31 /** Tests that the callback is called on fulfillment. */
32 @Test
33 public void callback() {
34 final Value value = new Value();
35
36 Promise<Integer> promise = new Promise<Integer>();
37 promise.then(new Callback<Integer>() {
38 @Override
39 public void onResult(Integer result) {
40 value.set(result);
41 }
42 });
43
44 assertEquals(value.get(), 0);
45
46 promise.fulfill(new Integer(1));
47 assertEquals(value.get(), 1);
48 }
49
50 /** Tests that multiple callbacks are called. */
51 @Test
52 public void multipleCallbacks() {
53 final Value value = new Value();
54
55 Promise<Integer> promise = new Promise<Integer>();
56 promise.then(new Callback<Integer>() {
57 @Override
58 public void onResult(Integer result) {
59 value.set(1);
60 }
61 });
62 promise.then(new Callback<Integer>() {
63 @Override
64 public void onResult(Integer result) {
65 value.set(2);
66 }
67 });
68
69 assertEquals(value.get(), 0);
70
71 promise.fulfill(new Integer(0));
72 assertEquals(value.get(), 2);
73 }
74
75 /** Tests that a callback is called immediately when given to a fulfilled Pr omise. */
76 @Test
77 public void callbackOnFulfilled() {
78 final Value value = new Value();
79
80 Promise<Integer> promise = Promise.fulfilled(new Integer(0));
81 assertEquals(value.get(), 0);
82
83 promise.then(new Callback<Integer>() {
84 @Override
85 public void onResult(Integer result) {
86 value.set(1);
87 }
88 });
89
90 assertEquals(value.get(), 1);
91 }
92 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698