forked from jaredsburrows/android-gradle-java-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaceHolderFragmentTest.java
More file actions
104 lines (84 loc) · 3.66 KB
/
PlaceHolderFragmentTest.java
File metadata and controls
104 lines (84 loc) · 3.66 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
package burrows.apps.example.template.fragment;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import burrows.apps.example.template.R;
import burrows.apps.example.template.test.TestBase;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.robolectric.util.SupportFragmentTestUtil.startFragment;
import static org.robolectric.util.SupportFragmentTestUtil.startVisibleFragment;
/**
* @author <a href="mailto:jaredsburrows@gmail.com">Jared Burrows</a>
* @since 0.0.1
*/
@RunWith(RobolectricGradleTestRunner.class)
@SuppressWarnings({"ConstantConditions", "ResourceType"})
public class PlaceHolderFragmentTest extends TestBase {
PlaceholderFragment fragment;
@Before
public void setUp() {
fragment = new PlaceholderFragment();
startFragment(fragment, FragmentActivity.class);
}
// --------------------------------------------
// Testing the Fragment itself
// --------------------------------------------
@Test
public void test_startFragmentView() {
assertThat(fragment, not(nullValue()));
assertThat(fragment.getView(), not(nullValue()));
assertThat(fragment.getActivity(), not(nullValue()));
assertThat(fragment.getActivity(), instanceOf(FragmentActivity.class));
}
@Test
public void test_startFragmentUniqueView() {
assertThat(fragment.getView().findViewById(R.id.buttonStartInterstitial), not(nullValue()));
assertThat(fragment.getView().findViewById(R.id.adView), not(nullValue()));
}
@Test
public void test_startVisible() {
PlaceholderFragment fragment = new PlaceholderFragment();
assertThat(fragment.isVisible(), is(false));
assertThat(fragment.isAdded(), is(false));
startVisibleFragment(fragment);
assertThat(fragment, not(nullValue()));
assertThat(fragment.getView(), not(nullValue()));
assertThat(fragment.getView().getWindowToken(), not(nullValue()));
assertThat(fragment.getActivity(), not(nullValue()));
assertThat(fragment.isAdded(), is(true));
assertThat(fragment.isVisible(), is(true));
}
// --------------------------------------------
// Testing the button clicks
// --------------------------------------------
@Test
public void test_showInterstitialAd() throws Exception {
fragment.getView().findViewById(R.id.buttonStartInterstitial).performClick();
Thread.sleep(3000);
fragment.getView().findViewById(R.id.buttonStartInterstitial).performClick();
}
// --------------------------------------------
// Testing ad listeners
// --------------------------------------------
@Test
public void test_adViewListeners() {
AdListener adListener = ((AdView) fragment.getView().findViewById(R.id.adView)).getAdListener();
adListener.onAdClosed();
adListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR);
adListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST);
adListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR);
adListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL);
adListener.onAdLeftApplication();
adListener.onAdLoaded();
adListener.onAdOpened();
}
}