launchSettingFragment(initialFragmentName, isSubSettings, intent);
}
+ final boolean deviceProvisioned = Utils.isDeviceProvisioned(this);
if (mIsShowingDashboard) {
- findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
+ findViewById(R.id.search_bar).setVisibility(
+ deviceProvisioned ? View.VISIBLE : View.INVISIBLE);
findViewById(R.id.action_bar).setVisibility(View.GONE);
final Toolbar toolbar = findViewById(R.id.search_action_bar);
FeatureFactory.getFactory(this).getSearchFeatureProvider()
ActionBar actionBar = getActionBar();
if (actionBar != null) {
- boolean deviceProvisioned = Utils.isDeviceProvisioned(this);
actionBar.setDisplayHomeAsUpEnabled(deviceProvisioned);
actionBar.setHomeButtonEnabled(deviceProvisioned);
actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ final Activity activity = getActivity();
+ if (!Utils.isDeviceProvisioned(activity) && !canRunBeforeDeviceProvisioned()) {
+ activity.finish();
+ return;
+ }
String chooseLockAction = getActivity().getIntent().getAction();
mFingerprintManager = Utils.getFingerprintManagerOrNull(getActivity());
addHeaderView();
}
+ protected boolean canRunBeforeDeviceProvisioned() {
+ return false;
+ }
+
protected void addHeaderView() {
if (mForFingerprint) {
setHeaderView(R.layout.choose_lock_generic_fingerprint_header);
return layout.onCreateRecyclerView(inflater, parent, savedInstanceState);
}
+ @Override
+ protected boolean canRunBeforeDeviceProvisioned() {
+ return true;
+ }
+
/***
* Disables preferences that are less secure than required quality and shows only secure
* screen lock options here.
import android.view.MenuItem;
import com.android.settings.R;
+import com.android.settings.Utils;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.SearchFeatureProvider;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+ if (!Utils.isDeviceProvisioned(mHost.getContext())) {
+ return;
+ }
if (menu == null) {
return;
}
import static org.mockito.Mockito.when;
import android.app.ActivityManager;
+import android.content.Context;
import android.content.Intent;
-
+import android.os.Bundle;
+import android.provider.Settings.Global;
+import android.view.View;
import com.android.settings.core.OnActivityResultListener;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
+import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
@Mock
private ActivityManager.TaskDescription mTaskDescription;
private SettingsActivity mActivity;
+ private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.application;
mActivity = spy(new SettingsActivity());
}
@Test
+ public void onCreate_deviceNotProvisioned_shouldDisableSearch() {
+ Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 0);
+ final Intent intent = new Intent(mContext, Settings.class);
+ final SettingsActivity activity =
+ Robolectric.buildActivity(SettingsActivity.class, intent).create(Bundle.EMPTY).get();
+
+ assertThat(activity.findViewById(R.id.search_bar).getVisibility())
+ .isEqualTo(View.INVISIBLE);
+ }
+
+ @Test
+ public void onCreate_deviceProvisioned_shouldEnableSearch() {
+ Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
+ final Intent intent = new Intent(mContext, Settings.class);
+ final SettingsActivity activity =
+ Robolectric.buildActivity(SettingsActivity.class, intent).create(Bundle.EMPTY).get();
+
+ assertThat(activity.findViewById(R.id.search_bar).getVisibility()).isEqualTo(View.VISIBLE);
+ }
+
+ @Test
public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() {
when(mActivity.getSupportFragmentManager()).thenReturn(mFragmentManager);
when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class));
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import android.app.Activity;
+import android.content.Context;
+import android.os.Bundle;
+import android.provider.Settings.Global;
import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.SettingsShadowResources;
+import androidx.fragment.app.FragmentActivity;
+
+import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
public class ChooseLockGenericTest {
+ @After
+ public void tearDown() {
+ Global.putInt(RuntimeEnvironment.application.getContentResolver(),
+ Global.DEVICE_PROVISIONED, 1);
+ }
+
+ @Test
+ @Config(shadows = SettingsShadowResources.SettingsShadowTheme.class)
+ public void onCreate_deviceNotProvisioned_shouldFinishActivity() {
+ final Context context = RuntimeEnvironment.application;
+ Global.putInt(context.getContentResolver(), Global.DEVICE_PROVISIONED, 0);
+ final FragmentActivity activity = mock(FragmentActivity.class);
+ when(activity.getContentResolver()).thenReturn(context.getContentResolver());
+ when(activity.getTheme()).thenReturn(context.getTheme());
+
+ final ChooseLockGenericFragment fragment = spy(new ChooseLockGenericFragment());
+ when(fragment.getActivity()).thenReturn(activity);
+ when(fragment.getArguments()).thenReturn(Bundle.EMPTY);
+
+ fragment.onCreate(Bundle.EMPTY);
+ verify(activity).finish();
+ }
+
@Test
public void onActivityResult_nullIntentData_shouldNotCrash() {
ChooseLockGenericFragment fragment = spy(new ChooseLockGenericFragment());
package com.android.settings.search.actionbar;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
+import android.content.Context;
import android.os.Bundle;
+import android.provider.Settings.Global;
import android.view.Menu;
import android.view.MenuItem;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class SearchMenuControllerTest {
private Menu mMenu;
private TestPreferenceFragment mPreferenceHost;
private ObservableFragment mHost;
+ private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- mHost = new ObservableFragment();
+ mContext = RuntimeEnvironment.application;
+ mHost = spy(new ObservableFragment());
+ when(mHost.getContext()).thenReturn(mContext);
mPreferenceHost = new TestPreferenceFragment();
+ Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
when(mMenu.add(Menu.NONE, Menu.NONE, 0 /* order */, R.string.search_menu))
.thenReturn(mock(MenuItem.class));
verifyZeroInteractions(mMenu);
}
+ @Test
+ public void init_deviceNotProvisioned_shouldNotAddMenu() {
+ Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 0);
+ SearchMenuController.init(mHost);
+ mHost.getSettingsLifecycle().onCreateOptionsMenu(mMenu, null /* inflater */);
+
+ verifyZeroInteractions(mMenu);
+ }
+
private static class TestPreferenceFragment extends ObservablePreferenceFragment {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}
+
+ @Override
+ public Context getContext() {
+ return RuntimeEnvironment.application;
+ }
}
}