1 /* 2 * Copyright (C) 2023 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 17 package android.car.app; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.Activity; 22 23 import androidx.lifecycle.Lifecycle; 24 import androidx.test.ext.junit.rules.ActivityScenarioRule; 25 26 import org.junit.Before; 27 import org.junit.Rule; 28 import org.junit.Test; 29 30 public class CarTaskViewControllerHostLifecycleFactoryTest { 31 private final FakeCarTaskViewControllerHostLifecycleObserver mObserver = 32 new FakeCarTaskViewControllerHostLifecycleObserver(); 33 private TestActivity mActivity; 34 35 @Rule 36 public ActivityScenarioRule<TestActivity> activityRule = new ActivityScenarioRule<>( 37 TestActivity.class); 38 39 @Before setup()40 public void setup() { 41 activityRule.getScenario().onActivity(activity -> mActivity = activity); 42 } 43 44 @Test forActivity_activityResumed_callsObserver()45 public void forActivity_activityResumed_callsObserver() { 46 // Arrange 47 CarTaskViewControllerHostLifecycle lifecycle = 48 CarTaskViewControllerHostLifecycleFactory.forActivity(mActivity); 49 lifecycle.registerObserver(mObserver); 50 51 // Act 52 activityRule.getScenario().moveToState(Lifecycle.State.CREATED); 53 activityRule.getScenario().moveToState(Lifecycle.State.RESUMED); 54 55 // Assert 56 assertThat(mObserver.mHostAppearedCalled).isTrue(); 57 } 58 59 @Test forActivity_activityStarted_callsObserver()60 public void forActivity_activityStarted_callsObserver() { 61 // Arrange 62 CarTaskViewControllerHostLifecycle lifecycle = 63 CarTaskViewControllerHostLifecycleFactory.forActivity(mActivity); 64 lifecycle.registerObserver(mObserver); 65 66 // Act 67 activityRule.getScenario().moveToState(Lifecycle.State.CREATED); 68 activityRule.getScenario().moveToState(Lifecycle.State.STARTED); 69 70 // Assert 71 assertThat(mObserver.mHostAppearedCalled).isTrue(); 72 } 73 74 @Test forActivity_activityStopped_callsObserver()75 public void forActivity_activityStopped_callsObserver() { 76 // Arrange 77 CarTaskViewControllerHostLifecycle lifecycle = 78 CarTaskViewControllerHostLifecycleFactory.forActivity(mActivity); 79 lifecycle.registerObserver(mObserver); 80 81 // Act 82 activityRule.getScenario().moveToState(Lifecycle.State.RESUMED); 83 activityRule.getScenario().moveToState(Lifecycle.State.CREATED); 84 85 // Assert 86 assertThat(mObserver.mHostDisappearedCalled).isTrue(); 87 } 88 89 @Test forActivity_activityDestroyed_callsObserver()90 public void forActivity_activityDestroyed_callsObserver() { 91 // Arrange 92 CarTaskViewControllerHostLifecycle lifecycle = 93 CarTaskViewControllerHostLifecycleFactory.forActivity(mActivity); 94 lifecycle.registerObserver(mObserver); 95 96 // Act 97 activityRule.getScenario().moveToState(Lifecycle.State.CREATED); 98 activityRule.getScenario().moveToState(Lifecycle.State.DESTROYED); 99 100 // Assert 101 assertThat(mObserver.mHostDestroyedCalled).isTrue(); 102 } 103 104 public static class TestActivity extends Activity { 105 } 106 } 107