1 /* 2 * Copyright (C) 2021 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 com.android.car.settings.common; 18 19 import static org.testng.Assert.assertThrows; 20 21 import android.content.Context; 22 23 import androidx.fragment.app.DialogFragment; 24 import androidx.fragment.app.FragmentManager; 25 import androidx.test.core.app.ApplicationProvider; 26 import androidx.test.ext.junit.runners.AndroidJUnit4; 27 import androidx.test.rule.ActivityTestRule; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.testutils.BaseCarSettingsTestActivity; 31 import com.android.car.ui.preference.PreferenceFragment; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 38 @RunWith(AndroidJUnit4.class) 39 abstract class BaseCarSettingsActivityTestCase<T extends BaseCarSettingsTestActivity> { 40 protected Context mContext = ApplicationProvider.getApplicationContext(); 41 protected BaseCarSettingsTestActivity mActivity; 42 protected TopLevelMenuFragment mTopLevelFragment; 43 protected FragmentManager mFragmentManager; 44 getActivityTestRule()45 abstract ActivityTestRule<T> getActivityTestRule(); 46 47 @Before setUp()48 public void setUp() throws Throwable { 49 mActivity = getActivityTestRule().getActivity(); 50 mFragmentManager = mActivity.getSupportFragmentManager(); 51 } 52 53 @Test launchFragment_dialogFragment_throwsError()54 public void launchFragment_dialogFragment_throwsError() { 55 DialogFragment dialogFragment = new DialogFragment(); 56 57 assertThrows(IllegalArgumentException.class, 58 () -> mActivity.launchFragment(dialogFragment)); 59 } 60 getCurrentFragment()61 protected PreferenceFragment getCurrentFragment() { 62 return (PreferenceFragment) mFragmentManager.findFragmentById(R.id.fragment_container); 63 } 64 } 65