1 /*
2  * Copyright (C) 2019 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.autofillservice.cts.R;
21 import android.autofillservice.cts.testcore.UiBot;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.support.test.uiautomator.UiObject2;
26 import android.util.Log;
27 import android.widget.TextView;
28 
29 /**
30  * Activity that handles VIEW action.
31  */
32 public class ViewActionActivity extends AbstractAutoFillActivity {
33 
34     private static ViewActionActivity sInstance;
35 
36     private static final String TAG = "ViewActionHandleActivity";
37     static final String ID_WELCOME = "welcome";
38     static final String DEFAULT_MESSAGE = "Welcome VIEW action handle activity";
39     private boolean mHasCustomBackBehavior;
40 
41     public enum ActivityCustomAction {
42         NORMAL_ACTIVITY,
43         FAST_FORWARD_ANOTHER_ACTIVITY,
44         TAP_BACK_WITHOUT_FINISH
45     }
46 
ViewActionActivity()47     public ViewActionActivity() {
48         sInstance = this;
49     }
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54 
55         setContentView(R.layout.welcome_activity);
56 
57         final Uri data = getIntent().getData();
58         ActivityCustomAction type = ActivityCustomAction.valueOf(data.getSchemeSpecificPart());
59 
60         switch (type) {
61             case FAST_FORWARD_ANOTHER_ACTIVITY:
62                 startSecondActivity();
63                 break;
64             case TAP_BACK_WITHOUT_FINISH:
65                 mHasCustomBackBehavior = true;
66                 break;
67             case NORMAL_ACTIVITY:
68             default:
69                 // no-op
70         }
71 
72         TextView welcome = (TextView) findViewById(R.id.welcome);
73         welcome.setText(DEFAULT_MESSAGE);
74     }
75 
76     @Override
onDestroy()77     protected void onDestroy() {
78         super.onDestroy();
79 
80         Log.v(TAG, "Setting sInstance to null onDestroy()");
81         sInstance = null;
82     }
83 
84     @Override
finish()85     public void finish() {
86         super.finish();
87         mHasCustomBackBehavior = false;
88     }
89 
90     @Override
onBackPressed()91     public void onBackPressed() {
92         if (mHasCustomBackBehavior) {
93             moveTaskToBack(true);
94             return;
95         }
96         super.onBackPressed();
97     }
98 
finishIt()99     public static void finishIt() {
100         if (sInstance != null) {
101             sInstance.finish();
102         }
103     }
104 
startSecondActivity()105     private void startSecondActivity() {
106         final Intent intent = new Intent(this, SecondActivity.class)
107                 .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
108         startActivity(intent);
109         finish();
110     }
111 
assertShowingDefaultMessage(UiBot uiBot)112     public static void assertShowingDefaultMessage(UiBot uiBot) throws Exception {
113         final UiObject2 activity = uiBot.assertShownByRelativeId(ID_WELCOME);
114         assertWithMessage("wrong text on '%s'", activity).that(activity.getText())
115                 .isEqualTo(DEFAULT_MESSAGE);
116     }
117 }
118