1 /*
2  * Copyright (C) 2020 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 android.view.autofill;
18 
19 import android.app.assist.AssistStructure;
20 import android.app.assist.AssistStructure.ViewNode;
21 import android.app.assist.AssistStructure.WindowNode;
22 import android.service.autofill.FillContext;
23 import android.util.Log;
24 
25 import java.util.List;
26 
27 /**
28  * Helper for common funcionalities.
29  */
30 public class AutofillTestHelper {
31     private static final String TAG = "AutofillTestHelper";
32 
33     /**
34      * Gets a node given its Android resource id, or {@code null} if not found.
35      */
findNodeByResourceId(List<FillContext> contexts, String resourceId)36     public static ViewNode findNodeByResourceId(List<FillContext> contexts, String resourceId) {
37         for (FillContext context : contexts) {
38             ViewNode node = findNodeByResourceId(context.getStructure(), resourceId);
39             if (node != null) {
40                 return node;
41             }
42         }
43         return null;
44     }
45 
46     /**
47      * Gets a node if it matches the filter criteria for the given id.
48      */
findNodeByResourceId(AssistStructure structure, String id)49     private static ViewNode findNodeByResourceId(AssistStructure structure, String id) {
50         Log.v(TAG, "Parsing request for activity " + structure.getActivityComponent());
51         final int nodes = structure.getWindowNodeCount();
52         for (int i = 0; i < nodes; i++) {
53             final WindowNode windowNode = structure.getWindowNodeAt(i);
54             final ViewNode rootNode = windowNode.getRootViewNode();
55             final ViewNode node = findNodeByResourceId(rootNode, id);
56             if (node != null) {
57                 return node;
58             }
59         }
60         return null;
61     }
62 
63     /**
64      * Gets a node if it matches the filter criteria for the given id.
65      */
findNodeByResourceId(ViewNode node, String id)66     private static ViewNode findNodeByResourceId(ViewNode node, String id) {
67         if (id.equals(node.getIdEntry())) {
68             return node;
69         }
70         final int childrenSize = node.getChildCount();
71         if (childrenSize > 0) {
72             for (int i = 0; i < childrenSize; i++) {
73                 final ViewNode found = findNodeByResourceId(node.getChildAt(i), id);
74                 if (found != null) {
75                     return found;
76                 }
77             }
78         }
79         return null;
80     }
81 }
82