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
17package com.android.settings;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.robolectric.RuntimeEnvironment.application;
22
23import android.app.Activity;
24import android.content.ComponentName;
25import android.content.pm.PackageManager;
26import android.os.UserHandle;
27
28import com.android.settings.password.ChooseLockPattern.ChooseLockPatternFragment;
29import com.android.settings.password.ChooseLockPattern.IntentBuilder;
30import com.android.settings.password.SetupChooseLockPattern;
31import com.android.settings.testutils.SettingsRobolectricTestRunner;
32import com.android.settings.testutils.shadow.SettingsShadowResources;
33import com.android.settings.testutils.shadow.ShadowEventLogWriter;
34import com.android.settings.testutils.shadow.ShadowUtils;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.robolectric.Robolectric;
40import org.robolectric.RuntimeEnvironment;
41import org.robolectric.annotation.Config;
42import org.robolectric.res.builder.RobolectricPackageManager.ComponentState;
43
44@RunWith(SettingsRobolectricTestRunner.class)
45@Config(
46        manifest = TestConfig.MANIFEST_PATH,
47        sdk = TestConfig.SDK_VERSION,
48        shadows = {
49                SettingsShadowResources.class,
50                SettingsShadowResources.SettingsShadowTheme.class,
51                ShadowEventLogWriter.class,
52                ShadowUtils.class
53        })
54public class SetupChooseLockPatternTest {
55
56    private SetupChooseLockPattern mActivity;
57
58    @Before
59    public void setUp() {
60        RuntimeEnvironment.getRobolectricPackageManager().setComponentEnabledSetting(
61                new ComponentName(application, SetupRedactionInterstitial.class),
62                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
63                PackageManager.DONT_KILL_APP);
64
65        mActivity =  Robolectric.buildActivity(
66                SetupChooseLockPattern.class,
67                SetupChooseLockPattern.modifyIntentForSetup(
68                        application,
69                        new IntentBuilder(application)
70                                .setUserId(UserHandle.myUserId())
71                                .build()))
72                .setup().get();
73    }
74
75    @Test
76    public void chooseLockSaved_shouldEnableRedactionInterstitial() {
77        findFragment(mActivity).onChosenLockSaveFinished(false, null);
78
79        ComponentState redactionComponentState =
80                RuntimeEnvironment.getRobolectricPackageManager().getComponentState(
81                        new ComponentName(application, SetupRedactionInterstitial.class));
82        assertThat(redactionComponentState.newState).named("Redaction component state")
83                .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
84    }
85
86    private ChooseLockPatternFragment findFragment(Activity activity) {
87        return (ChooseLockPatternFragment)
88                activity.getFragmentManager().findFragmentById(R.id.main_content);
89    }
90}
91