1 /* 2 * Copyright (C) 2018 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 18 package com.android.settings.panel; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 27 import android.app.settings.SettingsEnums; 28 import android.content.Context; 29 import android.net.Uri; 30 import android.os.Bundle; 31 import android.view.LayoutInflater; 32 import android.view.View; 33 import android.widget.LinearLayout; 34 35 import com.android.settings.R; 36 import com.android.settings.testutils.FakeFeatureFactory; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import org.robolectric.Robolectric; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class PanelFragmentTest { 48 49 private Context mContext; 50 private PanelFragment mPanelFragment; 51 private FakeSettingsPanelActivity mActivity; 52 private FakeFeatureFactory mFakeFeatureFactory; 53 private PanelFeatureProvider mPanelFeatureProvider; 54 private FakePanelContent mFakePanelContent; 55 56 private final String FAKE_EXTRA = "fake_extra"; 57 58 @Before setUp()59 public void setUp() { 60 mContext = RuntimeEnvironment.application; 61 62 mPanelFeatureProvider = spy(new PanelFeatureProviderImpl()); 63 mFakeFeatureFactory = FakeFeatureFactory.setupForTest(); 64 mFakeFeatureFactory.panelFeatureProvider = mPanelFeatureProvider; 65 mFakePanelContent = new FakePanelContent(); 66 doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any(), any()); 67 68 mActivity = spy(Robolectric.buildActivity(FakeSettingsPanelActivity.class).setup().get()); 69 70 mPanelFragment = 71 spy((PanelFragment) 72 mActivity.getSupportFragmentManager().findFragmentById(R.id.main_content)); 73 doReturn(mActivity).when(mPanelFragment).getActivity(); 74 75 final Bundle bundle = new Bundle(); 76 bundle.putString(SettingsPanelActivity.KEY_PANEL_TYPE_ARGUMENT, FAKE_EXTRA); 77 doReturn(bundle).when(mPanelFragment).getArguments(); 78 } 79 80 @Test onCreateView_countdownLatch_setup()81 public void onCreateView_countdownLatch_setup() { 82 mPanelFragment.onCreateView(LayoutInflater.from(mContext), 83 new LinearLayout(mContext), null); 84 PanelSlicesLoaderCountdownLatch countdownLatch = 85 mPanelFragment.mPanelSlicesLoaderCountdownLatch; 86 for (Uri sliecUri: mFakePanelContent.getSlices()) { 87 countdownLatch.markSliceLoaded(sliecUri); 88 } 89 90 assertThat(countdownLatch.isPanelReadyToLoad()).isTrue(); 91 } 92 93 @Test onCreate_logsOpenEvent()94 public void onCreate_logsOpenEvent() { 95 verify(mFakeFeatureFactory.metricsFeatureProvider).action( 96 0, 97 SettingsEnums.PAGE_VISIBLE, 98 mFakePanelContent.getMetricsCategory(), 99 null, 100 0); 101 } 102 103 @Test onDestroy_logCloseEvent()104 public void onDestroy_logCloseEvent() { 105 mPanelFragment.onDestroyView(); 106 verify(mFakeFeatureFactory.metricsFeatureProvider).action( 107 0, 108 SettingsEnums.PAGE_HIDE, 109 mFakePanelContent.getMetricsCategory(), 110 PanelLoggingContract.PanelClosedKeys.KEY_OTHERS, 111 0); 112 } 113 114 @Test panelSeeMoreClick_logsCloseEvent()115 public void panelSeeMoreClick_logsCloseEvent() { 116 final View.OnClickListener listener = mPanelFragment.getSeeMoreListener(); 117 listener.onClick(null); 118 verify(mActivity).finish(); 119 120 mPanelFragment.onDestroyView(); 121 verify(mFakeFeatureFactory.metricsFeatureProvider).action( 122 0, 123 SettingsEnums.PAGE_HIDE, 124 mFakePanelContent.getMetricsCategory(), 125 PanelLoggingContract.PanelClosedKeys.KEY_SEE_MORE, 126 0 127 ); 128 } 129 130 @Test panelDoneClick_logsCloseEvent()131 public void panelDoneClick_logsCloseEvent() { 132 final View.OnClickListener listener = mPanelFragment.getCloseListener(); 133 listener.onClick(null); 134 verify(mActivity).finish(); 135 136 mPanelFragment.onDestroyView(); 137 verify(mFakeFeatureFactory.metricsFeatureProvider).action( 138 0, 139 SettingsEnums.PAGE_HIDE, 140 mFakePanelContent.getMetricsCategory(), 141 PanelLoggingContract.PanelClosedKeys.KEY_DONE, 142 0 143 ); 144 } 145 }