-
Notifications
You must be signed in to change notification settings - Fork 202
/
WebViewTests.java
113 lines (100 loc) · 3.52 KB
/
WebViewTests.java
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
/*
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazonaws.devicefarm.android.referenceapp.Categories;
import android.support.test.espresso.Espresso;
import android.webkit.WebView;
import com.amazonaws.devicefarm.android.referenceapp.BaseADFTest;
import com.amazonaws.devicefarm.android.referenceapp.IdlingResources.WebViewIdlingResource;
import com.amazonaws.devicefarm.android.referenceapp.R;
import org.junit.Test;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
/**
* Tests for a web view
*/
public class WebViewTests extends BaseADFTest{
private WebViewIdlingResource webViewIdlingResource;
/**
* Registers the webview idling resources before the webview tests.
*
* @throws Exception
*/
@Override
protected void setUp() throws Exception {
super.setUp();
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
webViewIdlingResource = new WebViewIdlingResource((WebView) getActivity().findViewById(R.id.webView_browser));
Espresso.registerIdlingResources(webViewIdlingResource);
}
});
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
/**
* Unregister the idling resource
*
* @throws Exception
*/
@Override
protected void tearDown() throws Exception {
Espresso.unregisterIdlingResources(webViewIdlingResource);
super.tearDown();
}
@Override
protected String getClassName() {
return "Web";
}
@Override
public void testSanity() {
checkIfIdDisplayed(R.id.website_input);
checkIfIdDisplayed(R.id.webView_browser);
}
/**
* Tests if the error toast is displayed when a bad url is entered in the url bar
*/
@Test
public void testInputInvalidUrl(){
final String BAD_URL = "a bad url string";
typeInWebBar(BAD_URL);
verifyToastMessage(R.string.web_error_toast);
}
/**
* Tests to see if google is loaded when navigated to Google.com
*/
@Test
public void testNavigateToWebSite() {
final String URL = "http://www.google.com/";
typeInWebBar(URL);
checkIdWithContentDescription(R.id.webView_browser, "www.google.com");
}
/**
* Types and navigates to url
*
* @param url the inputted url
*/
private void typeInWebBar(String url) {
onView(withId(R.id.website_input)).perform(click()).perform(typeText(url + "\n"));
}
}