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 */ 16 17 package androidx.lifecycle; 18 19 import static org.mockito.Matchers.any; 20 import static org.mockito.Matchers.anyString; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.annotation.SuppressLint; 26 import android.app.Activity; 27 import android.app.Fragment; 28 import android.app.FragmentTransaction; 29 import android.os.Bundle; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.junit.runners.JUnit4; 34 35 @RunWith(JUnit4.class) 36 public class DispatcherActivityCallbackTest { 37 @Test 38 public void onCreateFrameworkActivity() { 39 LifecycleDispatcher.DispatcherActivityCallback callback = 40 new LifecycleDispatcher.DispatcherActivityCallback(); 41 Activity activity = mock(Activity.class); 42 checkReportFragment(callback, activity); 43 } 44 45 @SuppressLint("CommitTransaction") 46 private void checkReportFragment(LifecycleDispatcher.DispatcherActivityCallback callback, 47 Activity activity) { 48 android.app.FragmentManager fm = mock(android.app.FragmentManager.class); 49 FragmentTransaction transaction = mock(FragmentTransaction.class); 50 when(activity.getFragmentManager()).thenReturn(fm); 51 when(fm.beginTransaction()).thenReturn(transaction); 52 when(transaction.add(any(Fragment.class), anyString())).thenReturn(transaction); 53 callback.onActivityCreated(activity, mock(Bundle.class)); 54 verify(activity).getFragmentManager(); 55 verify(fm).beginTransaction(); 56 verify(transaction).add(any(ReportFragment.class), anyString()); 57 verify(transaction).commit(); 58 } 59 } 60