| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
| 8 import static org.junit.Assert.assertFalse; | 8 import static org.junit.Assert.assertFalse; |
| 9 import static org.junit.Assert.assertTrue; | 9 import static org.junit.Assert.assertTrue; |
| 10 | 10 |
| 11 import org.chromium.base.Promise.UnhandledRejectionException; | 11 import org.chromium.base.Promise.UnhandledRejectionException; |
| 12 import org.chromium.testing.local.LocalRobolectricTestRunner; | 12 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 13 import org.junit.Test; | 13 import org.junit.Test; |
| 14 import org.junit.runner.RunWith; | 14 import org.junit.runner.RunWith; |
| 15 import org.robolectric.annotation.Config; | 15 import org.robolectric.annotation.Config; |
| 16 import org.robolectric.shadows.ShadowHandler; | 16 import org.robolectric.shadows.ShadowLooper; |
| 17 | 17 |
| 18 /** Unit tests for {@link Promise}. */ | 18 /** Unit tests for {@link Promise}. */ |
| 19 @RunWith(LocalRobolectricTestRunner.class) | 19 @RunWith(LocalRobolectricTestRunner.class) |
| 20 @Config(manifest = Config.NONE) | 20 @Config(manifest = Config.NONE) |
| 21 public class PromiseTest { | 21 public class PromiseTest { |
| 22 // We need a simple mutable reference type for testing. | 22 // We need a simple mutable reference type for testing. |
| 23 private static class Value { | 23 private static class Value { |
| 24 private int mValue; | 24 private int mValue; |
| 25 | 25 |
| 26 public int get() { | 26 public int get() { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 return arg + arg; | 97 return arg + arg; |
| 98 } | 98 } |
| 99 }).then(new Callback<String>() { | 99 }).then(new Callback<String>() { |
| 100 @Override | 100 @Override |
| 101 public void onResult(String result) { | 101 public void onResult(String result) { |
| 102 value.set(result.length()); | 102 value.set(result.length()); |
| 103 } | 103 } |
| 104 }); | 104 }); |
| 105 | 105 |
| 106 promise.fulfill(new Integer(123)); | 106 promise.fulfill(new Integer(123)); |
| 107 ShadowHandler.runMainLooperToEndOfTasks(); | 107 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 108 assertEquals(6, value.get()); | 108 assertEquals(6, value.get()); |
| 109 } | 109 } |
| 110 | 110 |
| 111 /** Tests that promises can chain asynchronous functions correctly. */ | 111 /** Tests that promises can chain asynchronous functions correctly. */ |
| 112 @Test | 112 @Test |
| 113 public void promiseChainingAsyncFunctions() { | 113 public void promiseChainingAsyncFunctions() { |
| 114 Promise<Integer> promise = new Promise<Integer>(); | 114 Promise<Integer> promise = new Promise<Integer>(); |
| 115 final Value value = new Value(); | 115 final Value value = new Value(); |
| 116 | 116 |
| 117 final Promise<String> innerPromise = new Promise<String>(); | 117 final Promise<String> innerPromise = new Promise<String>(); |
| 118 | 118 |
| 119 promise.then(new Promise.AsyncFunction<Integer, String>() { | 119 promise.then(new Promise.AsyncFunction<Integer, String>() { |
| 120 @Override | 120 @Override |
| 121 public Promise<String> apply(Integer arg) { | 121 public Promise<String> apply(Integer arg) { |
| 122 return innerPromise; | 122 return innerPromise; |
| 123 } | 123 } |
| 124 }).then(new Callback<String>(){ | 124 }).then(new Callback<String>(){ |
| 125 @Override | 125 @Override |
| 126 public void onResult(String result) { | 126 public void onResult(String result) { |
| 127 value.set(result.length()); | 127 value.set(result.length()); |
| 128 } | 128 } |
| 129 }); | 129 }); |
| 130 | 130 |
| 131 assertEquals(0, value.get()); | 131 assertEquals(0, value.get()); |
| 132 | 132 |
| 133 promise.fulfill(5); | 133 promise.fulfill(5); |
| 134 ShadowHandler.runMainLooperToEndOfTasks(); | 134 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 135 assertEquals(0, value.get()); | 135 assertEquals(0, value.get()); |
| 136 | 136 |
| 137 innerPromise.fulfill("abc"); | 137 innerPromise.fulfill("abc"); |
| 138 ShadowHandler.runMainLooperToEndOfTasks(); | 138 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 139 assertEquals(3, value.get()); | 139 assertEquals(3, value.get()); |
| 140 } | 140 } |
| 141 | 141 |
| 142 /** Tests that a Promise that does not use its result does not throw on reje
ction. */ | 142 /** Tests that a Promise that does not use its result does not throw on reje
ction. */ |
| 143 @Test | 143 @Test |
| 144 public void rejectPromiseNoCallbacks() { | 144 public void rejectPromiseNoCallbacks() { |
| 145 Promise<Integer> promise = new Promise<Integer>(); | 145 Promise<Integer> promise = new Promise<Integer>(); |
| 146 | 146 |
| 147 boolean caught = false; | 147 boolean caught = false; |
| 148 try { | 148 try { |
| 149 promise.reject(); | 149 promise.reject(); |
| 150 ShadowHandler.runMainLooperToEndOfTasks(); | 150 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 151 } catch (UnhandledRejectionException e) { | 151 } catch (UnhandledRejectionException e) { |
| 152 caught = true; | 152 caught = true; |
| 153 } | 153 } |
| 154 assertFalse(caught); | 154 assertFalse(caught); |
| 155 } | 155 } |
| 156 | 156 |
| 157 /** Tests that a Promise that uses its result throws on rejection if it has
no handler. */ | 157 /** Tests that a Promise that uses its result throws on rejection if it has
no handler. */ |
| 158 @Test | 158 @Test |
| 159 public void rejectPromiseNoHandler() { | 159 public void rejectPromiseNoHandler() { |
| 160 Promise<Integer> promise = new Promise<Integer>(); | 160 Promise<Integer> promise = new Promise<Integer>(); |
| 161 promise.then(this.<Integer>identity()).then(this.<Integer>pass()); | 161 promise.then(this.<Integer>identity()).then(this.<Integer>pass()); |
| 162 | 162 |
| 163 boolean caught = false; | 163 boolean caught = false; |
| 164 try { | 164 try { |
| 165 promise.reject(); | 165 promise.reject(); |
| 166 ShadowHandler.runMainLooperToEndOfTasks(); | 166 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 167 } catch (UnhandledRejectionException e) { | 167 } catch (UnhandledRejectionException e) { |
| 168 caught = true; | 168 caught = true; |
| 169 } | 169 } |
| 170 assertTrue(caught); | 170 assertTrue(caught); |
| 171 } | 171 } |
| 172 | 172 |
| 173 /** Tests that a Promise that handles rejection does not throw on rejection.
*/ | 173 /** Tests that a Promise that handles rejection does not throw on rejection.
*/ |
| 174 @Test | 174 @Test |
| 175 public void rejectPromiseHandled() { | 175 public void rejectPromiseHandled() { |
| 176 Promise<Integer> promise = new Promise<Integer>(); | 176 Promise<Integer> promise = new Promise<Integer>(); |
| 177 promise.then(this.<Integer>identity()).then(this.<Integer>pass(), this.<
Exception>pass()); | 177 promise.then(this.<Integer>identity()).then(this.<Integer>pass(), this.<
Exception>pass()); |
| 178 | 178 |
| 179 boolean caught = false; | 179 boolean caught = false; |
| 180 try { | 180 try { |
| 181 promise.reject(); | 181 promise.reject(); |
| 182 ShadowHandler.runMainLooperToEndOfTasks(); | 182 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 183 } catch (UnhandledRejectionException e) { | 183 } catch (UnhandledRejectionException e) { |
| 184 caught = true; | 184 caught = true; |
| 185 } | 185 } |
| 186 assertFalse(caught); | 186 assertFalse(caught); |
| 187 } | 187 } |
| 188 | 188 |
| 189 /** Tests that rejections carry the exception information. */ | 189 /** Tests that rejections carry the exception information. */ |
| 190 @Test | 190 @Test |
| 191 public void rejectionInformation() { | 191 public void rejectionInformation() { |
| 192 Promise<Integer> promise = new Promise<Integer>(); | 192 Promise<Integer> promise = new Promise<Integer>(); |
| 193 promise.then(this.<Integer>pass()); | 193 promise.then(this.<Integer>pass()); |
| 194 | 194 |
| 195 String message = "Promise Test"; | 195 String message = "Promise Test"; |
| 196 try { | 196 try { |
| 197 promise.reject(new NegativeArraySizeException(message)); | 197 promise.reject(new NegativeArraySizeException(message)); |
| 198 ShadowHandler.runMainLooperToEndOfTasks(); | 198 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 199 } catch (UnhandledRejectionException e) { | 199 } catch (UnhandledRejectionException e) { |
| 200 assertTrue(e.getCause() instanceof NegativeArraySizeException); | 200 assertTrue(e.getCause() instanceof NegativeArraySizeException); |
| 201 assertEquals(e.getCause().getMessage(), message); | 201 assertEquals(e.getCause().getMessage(), message); |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 | 204 |
| 205 /** Tests that rejections propagate. */ | 205 /** Tests that rejections propagate. */ |
| 206 @Test | 206 @Test |
| 207 public void rejectionChaining() { | 207 public void rejectionChaining() { |
| 208 final Value value = new Value(); | 208 final Value value = new Value(); |
| 209 Promise<Integer> promise = new Promise<Integer>(); | 209 Promise<Integer> promise = new Promise<Integer>(); |
| 210 | 210 |
| 211 Promise<Integer> result = | 211 Promise<Integer> result = |
| 212 promise.then(this.<Integer>identity()).then(this.<Integer>identi
ty()); | 212 promise.then(this.<Integer>identity()).then(this.<Integer>identi
ty()); |
| 213 | 213 |
| 214 result.then(this.<Integer>pass(), this.<Exception>setValue(value, 5)); | 214 result.then(this.<Integer>pass(), this.<Exception>setValue(value, 5)); |
| 215 | 215 |
| 216 promise.reject(new Exception()); | 216 promise.reject(new Exception()); |
| 217 ShadowHandler.runMainLooperToEndOfTasks(); | 217 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 218 | 218 |
| 219 assertEquals(value.get(), 5); | 219 assertEquals(value.get(), 5); |
| 220 assertTrue(result.isRejected()); | 220 assertTrue(result.isRejected()); |
| 221 } | 221 } |
| 222 | 222 |
| 223 /** Tests that Promises get rejected if a Function throws. */ | 223 /** Tests that Promises get rejected if a Function throws. */ |
| 224 @Test | 224 @Test |
| 225 public void rejectOnThrow() { | 225 public void rejectOnThrow() { |
| 226 Value value = new Value(); | 226 Value value = new Value(); |
| 227 Promise<Integer> promise = new Promise<Integer>(); | 227 Promise<Integer> promise = new Promise<Integer>(); |
| 228 promise.then(new Promise.Function<Integer, Integer>() { | 228 promise.then(new Promise.Function<Integer, Integer>() { |
| 229 @Override | 229 @Override |
| 230 public Integer apply(Integer argument) { | 230 public Integer apply(Integer argument) { |
| 231 throw new IllegalArgumentException(); | 231 throw new IllegalArgumentException(); |
| 232 } | 232 } |
| 233 }).then(this.<Integer>pass(), this.<Exception>setValue(value, 5)); | 233 }).then(this.<Integer>pass(), this.<Exception>setValue(value, 5)); |
| 234 | 234 |
| 235 promise.fulfill(0); | 235 promise.fulfill(0); |
| 236 | 236 |
| 237 ShadowHandler.runMainLooperToEndOfTasks(); | 237 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 238 assertEquals(value.get(), 5); | 238 assertEquals(value.get(), 5); |
| 239 } | 239 } |
| 240 | 240 |
| 241 /** Tests that Promises get rejected if an AsyncFunction throws. */ | 241 /** Tests that Promises get rejected if an AsyncFunction throws. */ |
| 242 @Test | 242 @Test |
| 243 public void rejectOnAsyncThrow() { | 243 public void rejectOnAsyncThrow() { |
| 244 Value value = new Value(); | 244 Value value = new Value(); |
| 245 Promise<Integer> promise = new Promise<Integer>(); | 245 Promise<Integer> promise = new Promise<Integer>(); |
| 246 | 246 |
| 247 promise.then(new Promise.AsyncFunction<Integer, Integer>() { | 247 promise.then(new Promise.AsyncFunction<Integer, Integer>() { |
| 248 @Override | 248 @Override |
| 249 public Promise<Integer> apply(Integer argument) { | 249 public Promise<Integer> apply(Integer argument) { |
| 250 throw new IllegalArgumentException(); | 250 throw new IllegalArgumentException(); |
| 251 } | 251 } |
| 252 }).then(this.<Integer>pass(), this.<Exception>setValue(value, 5)); | 252 }).then(this.<Integer>pass(), this.<Exception>setValue(value, 5)); |
| 253 | 253 |
| 254 promise.fulfill(0); | 254 promise.fulfill(0); |
| 255 | 255 |
| 256 ShadowHandler.runMainLooperToEndOfTasks(); | 256 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 257 assertEquals(value.get(), 5); | 257 assertEquals(value.get(), 5); |
| 258 } | 258 } |
| 259 | 259 |
| 260 /** Tests that Promises get rejected if an AsyncFunction rejects. */ | 260 /** Tests that Promises get rejected if an AsyncFunction rejects. */ |
| 261 @Test | 261 @Test |
| 262 public void rejectOnAsyncReject() { | 262 public void rejectOnAsyncReject() { |
| 263 Value value = new Value(); | 263 Value value = new Value(); |
| 264 Promise<Integer> promise = new Promise<Integer>(); | 264 Promise<Integer> promise = new Promise<Integer>(); |
| 265 final Promise<Integer> inner = new Promise<Integer>(); | 265 final Promise<Integer> inner = new Promise<Integer>(); |
| 266 | 266 |
| 267 promise.then(new Promise.AsyncFunction<Integer, Integer>() { | 267 promise.then(new Promise.AsyncFunction<Integer, Integer>() { |
| 268 @Override | 268 @Override |
| 269 public Promise<Integer> apply(Integer argument) { | 269 public Promise<Integer> apply(Integer argument) { |
| 270 return inner; | 270 return inner; |
| 271 } | 271 } |
| 272 }).then(this.<Integer>pass(), this.<Exception>setValue(value, 5)); | 272 }).then(this.<Integer>pass(), this.<Exception>setValue(value, 5)); |
| 273 | 273 |
| 274 promise.fulfill(0); | 274 promise.fulfill(0); |
| 275 | 275 |
| 276 ShadowHandler.runMainLooperToEndOfTasks(); | 276 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 277 assertEquals(value.get(), 0); | 277 assertEquals(value.get(), 0); |
| 278 | 278 |
| 279 inner.reject(); | 279 inner.reject(); |
| 280 | 280 |
| 281 ShadowHandler.runMainLooperToEndOfTasks(); | 281 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 282 assertEquals(value.get(), 5); | 282 assertEquals(value.get(), 5); |
| 283 } | 283 } |
| 284 | 284 |
| 285 /** Convenience method that returns a Callback that does nothing with its re
sult. */ | 285 /** Convenience method that returns a Callback that does nothing with its re
sult. */ |
| 286 private static <T> Callback<T> pass() { | 286 private static <T> Callback<T> pass() { |
| 287 return new Callback<T>() { | 287 return new Callback<T>() { |
| 288 @Override | 288 @Override |
| 289 public void onResult(T result) {} | 289 public void onResult(T result) {} |
| 290 }; | 290 }; |
| 291 } | 291 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 303 /** Convenience method that returns a Callback that sets the given Value on
execution. */ | 303 /** Convenience method that returns a Callback that sets the given Value on
execution. */ |
| 304 private static <T> Callback<T> setValue(final Value toSet, final int value)
{ | 304 private static <T> Callback<T> setValue(final Value toSet, final int value)
{ |
| 305 return new Callback<T>() { | 305 return new Callback<T>() { |
| 306 @Override | 306 @Override |
| 307 public void onResult(T result) { | 307 public void onResult(T result) { |
| 308 toSet.set(value); | 308 toSet.set(value); |
| 309 } | 309 } |
| 310 }; | 310 }; |
| 311 } | 311 } |
| 312 } | 312 } |
| OLD | NEW |