1/*
2 * Copyright (C) 2017 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 */
16package android.support.text.emoji;
17
18import static org.junit.Assert.fail;
19
20import android.content.res.AssetManager;
21import android.support.annotation.GuardedBy;
22import android.support.annotation.NonNull;
23import android.support.test.InstrumentationRegistry;
24
25import java.util.concurrent.CountDownLatch;
26
27public class TestConfigBuilder {
28    public static EmojiCompat.Config config() {
29        return new TestConfig().setReplaceAll(true);
30    }
31
32    public static class TestConfig extends EmojiCompat.Config {
33        TestConfig() {
34            super(new TestEmojiDataLoader());
35        }
36
37        TestConfig(final EmojiCompat.MetadataLoader metadataLoader) {
38            super(metadataLoader);
39        }
40    }
41
42    public static class WaitingDataLoader implements EmojiCompat.MetadataLoader {
43        private final CountDownLatch mLoaderLatch;
44        private final CountDownLatch mTestLatch;
45        private final boolean mSuccess;
46
47        public WaitingDataLoader(boolean success) {
48            mLoaderLatch = new CountDownLatch(1);
49            mTestLatch = new CountDownLatch(1);
50            mSuccess = success;
51        }
52
53        public WaitingDataLoader() {
54            this(true);
55        }
56
57        public CountDownLatch getLoaderLatch() {
58            return mLoaderLatch;
59        }
60
61        public CountDownLatch getTestLatch() {
62            return mTestLatch;
63        }
64
65        @Override
66        public void load(@NonNull final EmojiCompat.LoaderCallback loaderCallback) {
67            new Thread(new Runnable() {
68                @Override
69                public void run() {
70                    try {
71                        mLoaderLatch.await();
72                        if (mSuccess) {
73                            loaderCallback.onLoaded(new MetadataRepo());
74                        } else {
75                            loaderCallback.onFailed(null);
76                        }
77
78                        mTestLatch.countDown();
79                    } catch (Throwable e) {
80                        fail();
81                    }
82                }
83            }).start();
84        }
85    }
86
87    public static class TestEmojiDataLoader implements EmojiCompat.MetadataLoader {
88        static final Object sMetadataRepoLock = new Object();
89        // keep a static instance to in order not to slow down the tests
90        @GuardedBy("sMetadataRepoLock")
91        static volatile MetadataRepo sMetadataRepo;
92
93        TestEmojiDataLoader() {
94        }
95
96        @Override
97        public void load(@NonNull EmojiCompat.LoaderCallback loaderCallback) {
98            if (sMetadataRepo == null) {
99                synchronized (sMetadataRepoLock) {
100                    if (sMetadataRepo == null) {
101                        try {
102                            final AssetManager assetManager =
103                                    InstrumentationRegistry.getContext().getAssets();
104                            sMetadataRepo = MetadataRepo.create(assetManager,
105                                    "NotoColorEmojiCompat.ttf");
106                        } catch (Throwable e) {
107                            loaderCallback.onFailed(e);
108                            throw new RuntimeException(e);
109                        }
110                    }
111                }
112            }
113
114            loaderCallback.onLoaded(sMetadataRepo);
115        }
116    }
117
118}
119