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 package android.autofillservice.cts.activities;
17 
18 import static com.google.common.truth.Truth.assertWithMessage;
19 
20 import android.app.PendingIntent;
21 import android.autofillservice.cts.R;
22 import android.autofillservice.cts.testcore.UiBot;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentSender;
26 import android.os.Bundle;
27 import android.support.test.uiautomator.UiObject2;
28 import android.text.TextUtils;
29 import android.util.Log;
30 import android.widget.TextView;
31 
32 import androidx.annotation.Nullable;
33 
34 /**
35  * Activity that displays a "Welcome USER" message after login.
36  */
37 public class WelcomeActivity extends AbstractAutoFillActivity {
38 
39     private static WelcomeActivity sInstance;
40 
41     private static final String TAG = "WelcomeActivity";
42 
43     public static final String EXTRA_MESSAGE = "message";
44     public static final String ID_WELCOME = "welcome";
45 
46     private static int sPendingIntentId;
47     private static PendingIntent sPendingIntent;
48 
49     private TextView mWelcome;
50 
WelcomeActivity()51     public WelcomeActivity() {
52         sInstance = this;
53     }
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         setContentView(R.layout.welcome_activity);
60 
61         mWelcome = (TextView) findViewById(R.id.welcome);
62 
63         final Intent intent = getIntent();
64         final String message = intent.getStringExtra(EXTRA_MESSAGE);
65 
66         if (!TextUtils.isEmpty(message)) {
67             mWelcome.setText(message);
68         }
69 
70         Log.d(TAG, "Message: " + message);
71     }
72 
73     @Override
onDestroy()74     protected void onDestroy() {
75         super.onDestroy();
76 
77         Log.v(TAG, "Setting sInstance to null onDestroy()");
78         sInstance = null;
79     }
80 
81     @Override
finish()82     public void finish() {
83         super.finish();
84         Log.d(TAG, "So long and thanks for all the finish!");
85 
86         if (sPendingIntent != null) {
87             Log.v(TAG, " canceling pending intent on finish(): " + sPendingIntent);
88             sPendingIntent.cancel();
89         }
90     }
91 
finishIt()92     public static void finishIt() {
93         if (sInstance != null) {
94             sInstance.finish();
95         }
96     }
97 
98     // TODO: reuse in other places
assertShowingDefaultMessage(UiBot uiBot)99     public static void assertShowingDefaultMessage(UiBot uiBot) throws Exception {
100         assertShowing(uiBot, null);
101     }
102 
103     // TODO: reuse in other places
assertShowing(UiBot uiBot, @Nullable String expectedMessage)104     public static void assertShowing(UiBot uiBot, @Nullable String expectedMessage)
105             throws Exception {
106         final UiObject2 activity = uiBot.assertShownByRelativeId(ID_WELCOME);
107         if (expectedMessage == null) {
108             expectedMessage = "Welcome to the jungle!";
109         }
110         assertWithMessage("wrong text on '%s'", activity).that(activity.getText())
111                 .isEqualTo(expectedMessage);
112     }
113 
createSender(Context context, String message)114     public static IntentSender createSender(Context context, String message) {
115         if (sPendingIntent != null) {
116             throw new IllegalArgumentException("Already have pending intent (id="
117                     + sPendingIntentId + "): " + sPendingIntent);
118         }
119         ++sPendingIntentId;
120         Log.v(TAG, "createSender: id=" + sPendingIntentId + " message=" + message);
121         final Intent intent = new Intent(context, WelcomeActivity.class)
122                 .putExtra(EXTRA_MESSAGE, message)
123                 .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
124         sPendingIntent = PendingIntent.getActivity(context, sPendingIntentId, intent,
125                 PendingIntent.FLAG_IMMUTABLE);
126         return sPendingIntent.getIntentSender();
127     }
128 }
129