forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverrideWidgetReader.java
More file actions
118 lines (98 loc) · 5.22 KB
/
OverrideWidgetReader.java
File metadata and controls
118 lines (98 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.appium.java_client.pagefactory;
import io.appium.java_client.pagefactory.bys.ContentType;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import static io.appium.java_client.pagefactory.WidgetConstructorUtil.findConvenientConstructor;
import static io.appium.java_client.remote.MobilePlatform.ANDROID;
import static io.appium.java_client.remote.MobilePlatform.IOS;
import static io.appium.java_client.remote.MobilePlatform.WINDOWS;
import static java.util.Locale.ROOT;
class OverrideWidgetReader {
private static final Class<? extends Widget> EMPTY = Widget.class;
private static final String HTML = "html";
private static final String ANDROID_UI_AUTOMATOR = "androidUIAutomator";
private static final String IOS_XCUIT_AUTOMATION = "iOSXCUITAutomation";
private static final String WINDOWS_AUTOMATION = "windowsAutomation";
private OverrideWidgetReader() {
}
@SuppressWarnings("unchecked")
private static Class<? extends Widget> getConvenientClass(Class<? extends Widget> declaredClass,
AnnotatedElement annotatedElement, String method) {
Class<? extends Widget> convenientClass;
OverrideWidget overrideWidget = annotatedElement.getAnnotation(OverrideWidget.class);
try {
if (overrideWidget == null || (convenientClass =
(Class<? extends Widget>) OverrideWidget.class
.getDeclaredMethod(method).invoke(overrideWidget))
.equals(EMPTY)) {
convenientClass = declaredClass;
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
if (!declaredClass.isAssignableFrom(convenientClass)) {
throw new IllegalArgumentException(
new InstantiationException(declaredClass.getName()
+ " is not assignable from "
+ convenientClass.getName()));
}
return convenientClass;
}
static Class<? extends Widget> getDefaultOrHTMLWidgetClass(
Class<? extends Widget> declaredClass, AnnotatedElement annotatedElement) {
return getConvenientClass(declaredClass, annotatedElement, HTML);
}
static Class<? extends Widget> getMobileNativeWidgetClass(Class<? extends Widget> declaredClass,
AnnotatedElement annotatedElement, String platform) {
String transformedPlatform = String.valueOf(platform).toUpperCase(ROOT).trim();
if (ANDROID.equalsIgnoreCase(transformedPlatform)) {
return getConvenientClass(declaredClass, annotatedElement, ANDROID_UI_AUTOMATOR);
}
if (IOS.equalsIgnoreCase(transformedPlatform)) {
return getConvenientClass(declaredClass, annotatedElement, IOS_XCUIT_AUTOMATION);
}
if (WINDOWS.equalsIgnoreCase(transformedPlatform)) {
return getConvenientClass(declaredClass, annotatedElement, WINDOWS_AUTOMATION);
}
return getDefaultOrHTMLWidgetClass(declaredClass, annotatedElement);
}
private static Constructor<? extends Widget> getConstructorOfADefaultOrHTMLWidget(
Class<? extends Widget> declaredClass, AnnotatedElement annotatedElement) {
Class<? extends Widget> clazz =
getDefaultOrHTMLWidgetClass(declaredClass, annotatedElement);
return findConvenientConstructor(clazz);
}
private static Constructor<? extends Widget> getConstructorOfAMobileNativeWidgets(
Class<? extends Widget> declaredClass, AnnotatedElement annotatedElement, String platform) {
Class<? extends Widget> clazz =
getMobileNativeWidgetClass(declaredClass, annotatedElement, platform);
return findConvenientConstructor(clazz);
}
protected static Map<ContentType, Constructor<? extends Widget>> read(
Class<? extends Widget> declaredClass, AnnotatedElement annotatedElement, String platform) {
Map<ContentType, Constructor<? extends Widget>> result = new HashMap<>();
result.put(ContentType.HTML_OR_DEFAULT,
getConstructorOfADefaultOrHTMLWidget(declaredClass, annotatedElement));
result.put(ContentType.NATIVE_MOBILE_SPECIFIC,
getConstructorOfAMobileNativeWidgets(declaredClass, annotatedElement, platform));
return result;
}
}