blob: 425d162b94f53c76b9d29cfdeaab3e723c3087c6 [file] [log] [blame]
Juan Lang5efe8f02017-05-04 16:59:46 -07001/*
2 * Copyright (C) 2016 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 com.android.settingslib.utils;
17
18
19import com.android.settingslib.TestConfig;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.robolectric.RobolectricTestRunner;
23import org.robolectric.annotation.Config;
24
25import static com.google.common.truth.Truth.assertThat;
26import static org.junit.Assert.fail;
27
28@RunWith(RobolectricTestRunner.class)
29@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
30public class ThreadUtilsTest {
31
32 @Test
33 public void testMainThread() throws InterruptedException {
34 assertThat(ThreadUtils.isMainThread()).isTrue();
35 Thread background = new Thread(new Runnable() {
36 public void run() {
37 assertThat(ThreadUtils.isMainThread()).isFalse();
38 }
39 });
40 background.start();
41 background.join();
42 }
43
44 @Test
45 public void testEnsureMainThread() throws InterruptedException {
46 ThreadUtils.ensureMainThread();
47 Thread background = new Thread(new Runnable() {
48 public void run() {
49 try {
50 ThreadUtils.ensureMainThread();
51 fail("Should not pass ensureMainThread in a background thread");
52 } catch (RuntimeException e) {
53 }
54 }
55 });
56 background.start();
57 background.join();
58 }
59}