1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package androidx.emoji.text; 17 18 import static org.junit.Assert.fail; 19 20 import android.content.res.AssetManager; 21 import android.support.test.InstrumentationRegistry; 22 23 import androidx.annotation.GuardedBy; 24 import androidx.annotation.NonNull; 25 26 import java.util.concurrent.CountDownLatch; 27 28 public class TestConfigBuilder { 29 public static EmojiCompat.Config config() { 30 return new TestConfig().setReplaceAll(true); 31 } 32 33 public static class TestConfig extends EmojiCompat.Config { 34 TestConfig() { 35 super(new TestEmojiDataLoader()); 36 } 37 38 TestConfig(final EmojiCompat.MetadataRepoLoader metadataLoader) { 39 super(metadataLoader); 40 } 41 } 42 43 public static class WaitingDataLoader implements EmojiCompat.MetadataRepoLoader { 44 private final CountDownLatch mLoaderLatch; 45 private final CountDownLatch mTestLatch; 46 private final boolean mSuccess; 47 48 public WaitingDataLoader(boolean success) { 49 mLoaderLatch = new CountDownLatch(1); 50 mTestLatch = new CountDownLatch(1); 51 mSuccess = success; 52 } 53 54 public WaitingDataLoader() { 55 this(true); 56 } 57 58 public CountDownLatch getLoaderLatch() { 59 return mLoaderLatch; 60 } 61 62 public CountDownLatch getTestLatch() { 63 return mTestLatch; 64 } 65 66 @Override 67 public void load(@NonNull final EmojiCompat.MetadataRepoLoaderCallback loaderCallback) { 68 new Thread(new Runnable() { 69 @Override 70 public void run() { 71 try { 72 mLoaderLatch.await(); 73 if (mSuccess) { 74 loaderCallback.onLoaded(new MetadataRepo()); 75 } else { 76 loaderCallback.onFailed(null); 77 } 78 79 mTestLatch.countDown(); 80 } catch (Throwable e) { 81 fail(); 82 } 83 } 84 }).start(); 85 } 86 } 87 88 public static class TestEmojiDataLoader implements EmojiCompat.MetadataRepoLoader { 89 static final Object sMetadataRepoLock = new Object(); 90 // keep a static instance to in order not to slow down the tests 91 @GuardedBy("sMetadataRepoLock") 92 static volatile MetadataRepo sMetadataRepo; 93 94 TestEmojiDataLoader() { 95 } 96 97 @Override 98 public void load(@NonNull EmojiCompat.MetadataRepoLoaderCallback loaderCallback) { 99 if (sMetadataRepo == null) { 100 synchronized (sMetadataRepoLock) { 101 if (sMetadataRepo == null) { 102 try { 103 final AssetManager assetManager = 104 InstrumentationRegistry.getContext().getAssets(); 105 sMetadataRepo = MetadataRepo.create(assetManager, 106 "NotoColorEmojiCompat.ttf"); 107 } catch (Throwable e) { 108 loaderCallback.onFailed(e); 109 throw new RuntimeException(e); 110 } 111 } 112 } 113 } 114 115 loaderCallback.onLoaded(sMetadataRepo); 116 } 117 } 118 119 } 120