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.core.instrumentation; 17 18 import android.os.Bundle; 19 import com.android.settings.testutils.SettingsRobolectricTestRunner; 20 import com.android.settings.TestConfig; 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.robolectric.annotation.Config; 24 import org.robolectric.shadows.ShadowApplication; 25 26 import static com.google.common.truth.Truth.assertThat; 27 28 @RunWith(SettingsRobolectricTestRunner.class) 29 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 30 public class InstrumentedDialogFragmentTest { 31 32 public static class TestDialogFragment extends InstrumentedDialogFragment { 33 static final int TEST_METRIC = 1234; 34 35 public MetricsFeatureProvider getMetricsFeatureProvider() { 36 return mMetricsFeatureProvider; 37 } 38 39 @Override 40 public void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 43 assertThat(mMetricsFeatureProvider).isNotNull(); 44 } 45 46 @Override 47 public int getMetricsCategory() { 48 return TEST_METRIC; 49 } 50 } 51 52 @Test 53 public void runThroughFragmentLifecycles_shouldHaveMetricsFeatureProviderOnAttach() { 54 final TestDialogFragment fragment = new TestDialogFragment(); 55 56 // Precondition: no metrics feature 57 assertThat(fragment.getMetricsFeatureProvider()).isNull(); 58 59 fragment.onAttach(ShadowApplication.getInstance().getApplicationContext()); 60 61 // Verify: has metrics feature 62 assertThat(fragment.getMetricsFeatureProvider()).isNotNull(); 63 } 64 } 65