Home | History | Annotate | Download | only in settings
      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 
     17 package com.android.settings;
     18 
     19 import static android.support.test.espresso.Espresso.onView;
     20 import static android.support.test.espresso.action.ViewActions.click;
     21 import static android.support.test.espresso.matcher.ViewMatchers.withId;
     22 
     23 import static org.junit.Assert.assertEquals;
     24 import static org.junit.Assert.assertFalse;
     25 import static org.junit.Assert.assertTrue;
     26 
     27 import android.app.Activity;
     28 import android.app.Instrumentation;
     29 import android.app.Instrumentation.ActivityMonitor;
     30 import android.app.Instrumentation.ActivityResult;
     31 import android.content.Context;
     32 import android.content.Intent;
     33 import android.support.test.InstrumentationRegistry;
     34 import android.support.test.filters.MediumTest;
     35 import android.support.test.runner.AndroidJUnit4;
     36 
     37 import org.junit.After;
     38 import org.junit.Before;
     39 import org.junit.Test;
     40 import org.junit.runner.RunWith;
     41 
     42 @RunWith(AndroidJUnit4.class)
     43 @MediumTest
     44 public class EncryptionInterstitialTest {
     45 
     46     private Instrumentation mInstrumentation;
     47     private Context mContext;
     48     private TestActivityMonitor mActivityMonitor;
     49 
     50     @Before
     51     public void setUp() {
     52         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     53         mContext = mInstrumentation.getTargetContext();
     54         mActivityMonitor = new TestActivityMonitor();
     55         mInstrumentation.addMonitor(mActivityMonitor);
     56     }
     57 
     58     @After
     59     public void tearDown() {
     60         mInstrumentation.removeMonitor(mActivityMonitor);
     61     }
     62 
     63     @Test
     64     public void clickYes_shouldRequirePassword() {
     65         mInstrumentation.startActivitySync(
     66                 new Intent(mContext, EncryptionInterstitial.class)
     67                         .putExtra("extra_unlock_method_intent", new Intent("test.unlock.intent")));
     68         onView(withId(R.id.encrypt_require_password)).perform(click());
     69 
     70         mActivityMonitor.waitForActivityWithTimeout(1000);
     71         assertEquals(1, mActivityMonitor.getHits());
     72 
     73         assertTrue(mActivityMonitor.mMatchedIntent.getBooleanExtra(
     74                 EncryptionInterstitial.EXTRA_REQUIRE_PASSWORD, false));
     75     }
     76 
     77     @Test
     78     public void clickNo_shouldNotRequirePassword() {
     79         mInstrumentation.startActivitySync(
     80                 new Intent(mContext, EncryptionInterstitial.class)
     81                         .putExtra("extra_unlock_method_intent", new Intent("test.unlock.intent")));
     82         onView(withId(R.id.encrypt_dont_require_password)).perform(click());
     83 
     84         mActivityMonitor.waitForActivityWithTimeout(1000);
     85         assertEquals(1, mActivityMonitor.getHits());
     86 
     87         assertFalse(mActivityMonitor.mMatchedIntent.getBooleanExtra(
     88                 EncryptionInterstitial.EXTRA_REQUIRE_PASSWORD, true));
     89     }
     90 
     91     private static class TestActivityMonitor extends ActivityMonitor {
     92 
     93         Intent mMatchedIntent = null;
     94 
     95         @Override
     96         public ActivityResult onStartActivity(Intent intent) {
     97             if ("test.unlock.intent".equals(intent.getAction())) {
     98                 mMatchedIntent = intent;
     99                 return new ActivityResult(Activity.RESULT_OK, null);
    100             }
    101             return null;
    102         }
    103     }
    104 }
    105