Home | History | Annotate | Download | only in utils
      1 /*
      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  */
     16 package com.android.settings.utils;
     17 
     18 
     19 import com.android.settings.TestConfig;
     20 import org.junit.Test;
     21 import org.junit.runner.RunWith;
     22 import org.robolectric.RobolectricTestRunner;
     23 import org.robolectric.annotation.Config;
     24 
     25 import static com.google.common.truth.Truth.assertThat;
     26 import static org.junit.Assert.fail;
     27 
     28 @RunWith(RobolectricTestRunner.class)
     29 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     30 public 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 }
     60