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