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.assist.common;
17 
18 import android.content.Context;
19 import android.os.LocaleList;
20 import android.util.AttributeSet;
21 import android.view.ViewStructure;
22 import android.webkit.WebView;
23 
24 /**
25  * Custom webview to emulate behavior that is required on test cases.
26  */
27 public final class MyWebView extends WebView {
28 
29     private String mUrl;
30     private LocaleList mLocaleList;
31 
MyWebView(Context context, AttributeSet attrs)32     public MyWebView(Context context, AttributeSet attrs) {
33         super(context, attrs);
34     }
35 
36     /**
37      * Same as {@link #loadData(String, String, String)}, but setting the URL in the view structure.
38      */
myLoadData(String url, String data, String mimeType, String encoding)39     public void myLoadData(String url, String data, String mimeType, String encoding) {
40         mUrl = url;
41         super.loadData(data, mimeType, encoding);
42     }
43 
setLocaleList(LocaleList localeList)44     public void setLocaleList(LocaleList localeList) {
45         this.mLocaleList = localeList;
46     }
47 
getLocaleList()48     public LocaleList getLocaleList() {
49         return mLocaleList;
50     }
51 
52     @Override
onProvideStructure(ViewStructure structure)53     public void onProvideStructure(ViewStructure structure) {
54         super.onProvideStructure(structure);
55 
56         onProvideStructureForAssistOrAutofill(structure);
57     }
58 
59     @Override
onProvideAutofillStructure(ViewStructure structure, int flags)60     public void onProvideAutofillStructure(ViewStructure structure, int flags) {
61         super.onProvideAutofillStructure(structure, flags);
62 
63         onProvideStructureForAssistOrAutofill(structure);
64     }
65 
onProvideStructureForAssistOrAutofill(ViewStructure structure)66     private void onProvideStructureForAssistOrAutofill(ViewStructure structure) {
67         if (mUrl != null) {
68             structure.setWebDomain(mUrl);
69         }
70         if (mLocaleList != null) {
71             structure.setLocaleList(mLocaleList);
72         }
73     }
74 }
75