1 // Copyright 2020 The Chromium Authors 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.assertTrue; 8 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 import org.robolectric.annotation.Config; 12 13 import org.chromium.base.test.BaseRobolectricTestRunner; 14 15 import java.util.concurrent.atomic.AtomicBoolean; 16 17 /** 18 * Test class for {@link UnownedUserDataHost}, which also describes typical usage. 19 * 20 * Most tests for this class is in {@link UnownedUserDataKeyTest}, since the public API is mostly 21 * available from {@link UnownedUserDataKey}. 22 */ 23 @RunWith(BaseRobolectricTestRunner.class) 24 @Config(manifest = Config.NONE) 25 public class UnownedUserDataHostTest { 26 @Test testDestruction()27 public void testDestruction() { 28 UnownedUserDataHost host = new UnownedUserDataHost(); 29 host.destroy(); 30 assertTrue(host.isDestroyed()); 31 } 32 33 @Test testUnpreparedLooper()34 public void testUnpreparedLooper() throws InterruptedException { 35 AtomicBoolean illegalStateExceptionThrown = new AtomicBoolean(); 36 Thread t = 37 new Thread() { 38 @Override 39 public void run() { 40 try { 41 // The Looper on this thread is still unprepared, so this should fail. 42 new UnownedUserDataHost(); 43 } catch (IllegalStateException e) { 44 illegalStateExceptionThrown.set(true); 45 } 46 } 47 }; 48 t.start(); 49 t.join(); 50 51 assertTrue(illegalStateExceptionThrown.get()); 52 } 53 } 54