• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.Instrumentation;
20 import android.app.UiAutomation;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ResolveInfo;
24 import android.graphics.Bitmap;
25 import android.support.test.InstrumentationRegistry;
26 import android.support.test.filters.SmallTest;
27 import android.support.test.runner.AndroidJUnit4;
28 import android.util.Log;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.io.File;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 
38 import static android.support.test.espresso.Espresso.onView;
39 import static android.support.test.espresso.assertion.ViewAssertions.matches;
40 import static android.support.test.espresso.matcher.RootMatchers.isDialog;
41 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
42 import static android.support.test.espresso.matcher.ViewMatchers.withId;
43 import static junit.framework.Assert.fail;
44 
45 @RunWith(AndroidJUnit4.class)
46 @SmallTest
47 public class RegulatoryInfoDisplayActivityTest {
48     private static final String TAG = "RegulatoryInfoTest";
49 
50     private Instrumentation mInstrumentation;
51     private Intent mRegulatoryInfoIntent;
52     private UiAutomation mUiAutomation;
53 
54     @Before
setUp()55     public void setUp() {
56         mInstrumentation = InstrumentationRegistry.getInstrumentation();
57         mUiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
58         mRegulatoryInfoIntent = new Intent("android.settings.SHOW_REGULATORY_INFO")
59                 .addCategory(Intent.CATEGORY_DEFAULT)
60                 .setPackage(mInstrumentation.getTargetContext().getPackageName());
61     }
62 
63     @Test
resolveRegulatoryInfoIntent_intentShouldMatchConfig()64     public void resolveRegulatoryInfoIntent_intentShouldMatchConfig() {
65         // Load intent from PackageManager and load config from Settings app
66         final Context context = mInstrumentation.getTargetContext();
67 
68         final boolean hasRegulatoryInfo = context.getResources()
69                 .getBoolean(R.bool.config_show_regulatory_info);
70         final ResolveInfo resolveInfo = mInstrumentation.getTargetContext().getPackageManager()
71                 .resolveActivity(mRegulatoryInfoIntent, 0 /* flags */);
72 
73         // Check config and intent both enable or both disabled.
74         if (hasRegulatoryInfo && resolveInfo == null) {
75             fail("Config enables regulatory info but there is no handling intent");
76             return;
77         }
78         if (!hasRegulatoryInfo && resolveInfo != null) {
79             fail("Config disables regulatory info but there is at least one handling intent");
80             return;
81         }
82     }
83 
84     @Test
launchRegulatoryInfo_shouldNotCrash()85     public void launchRegulatoryInfo_shouldNotCrash() {
86         final Context context = mInstrumentation.getTargetContext();
87         final boolean hasRegulatoryInfo = context.getResources()
88                 .getBoolean(R.bool.config_show_regulatory_info);
89 
90         if (!hasRegulatoryInfo) {
91             return;
92         }
93         // Launch intent
94         mInstrumentation.startActivitySync(mRegulatoryInfoIntent);
95 
96         onView(withId(R.id.regulatoryInfo))
97                 .inRoot(isDialog())
98                 .check(matches(isDisplayed()));
99     }
100 
101     @Test
launchRegulatoryInfo_withInfoImage_shouldDisplay()102     public void launchRegulatoryInfo_withInfoImage_shouldDisplay() throws IOException {
103         // TODO: Remove "setenforce 0" when selinux rules is updated to give read permission for
104         // regulatory info.
105         mUiAutomation.executeShellCommand("setenforce 0");
106 
107         final boolean tempFileCreated = ensureRegulatoryInfoImageExists();
108         try {
109             final Context context = mInstrumentation.getTargetContext();
110             final boolean hasRegulatoryInfo = context.getResources()
111                     .getBoolean(R.bool.config_show_regulatory_info);
112 
113             if (!hasRegulatoryInfo) {
114                 return;
115             }
116             // Launch intent
117             mInstrumentation.startActivitySync(mRegulatoryInfoIntent);
118 
119             onView(withId(R.id.regulatoryInfo))
120                     .inRoot(isDialog())
121                     .check(matches(isDisplayed()));
122         } finally {
123             if (tempFileCreated) {
124                 final String filename =
125                         RegulatoryInfoDisplayActivity.getRegulatoryInfoImageFileName();
126                 new File(filename).delete();
127                 Log.d(TAG, "Deleting temp file " + filename);
128             }
129         }
130     }
131 
132     /**
133      * Ensures regulatory label image exists on disk.
134      *
135      * @return true if a test image is created.
136      */
ensureRegulatoryInfoImageExists()137     private boolean ensureRegulatoryInfoImageExists() throws IOException {
138         final String filename = RegulatoryInfoDisplayActivity.getRegulatoryInfoImageFileName();
139         if (new File(filename).exists()) {
140             return false;
141         }
142         Log.d(TAG, "Creating temp file " + filename);
143         final Bitmap bitmap = Bitmap.createBitmap(400 /* width */, 400 /* height */,
144                 Bitmap.Config.ARGB_8888);
145         final FileOutputStream out = new FileOutputStream(filename);
146         bitmap.compress(Bitmap.CompressFormat.PNG, 100 /* quality */, out);
147         out.close();
148         return true;
149     }
150 
151 
152 }
153