Skip to content

Commit af7ca6c

Browse files
authored
Merge pull request #59 from Fleker/master
Adds instrumentation tests to prevent regressions
2 parents b0abb65 + eb83530 commit af7ca6c

8 files changed

Lines changed: 294 additions & 57 deletions

File tree

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ android {
88
applicationId "news.androidtv.tvapprepo"
99
minSdkVersion 21
1010
targetSdkVersion 25
11-
versionCode 16
12-
versionName "1.1.3"
11+
versionCode 17
12+
versionName "1.1.4"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1314
}
1415
buildTypes {
1516
release {
@@ -73,6 +74,11 @@ dependencies {
7374
compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
7475
transitive = true;
7576
}
77+
78+
androidTestCompile 'com.android.support:support-annotations:25.3.1'
79+
androidTestCompile 'com.android.support.test:runner:0.5'
80+
androidTestCompile 'com.android.support.test:rules:0.5'
81+
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
7682
}
7783

7884
apply plugin: 'com.google.gms.google-services'

app/src/androidTest/java/news/androidtv/tvapprepo/ApplicationTest.java renamed to app/src/androidTest/java/news/androidtv/tvapprepo/IntentsInstrumentationTest.java

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,51 @@
1-
package news.androidtv.tvapprepo;
2-
3-
import android.app.Application;
4-
import android.content.ComponentName;
5-
import android.content.Intent;
6-
import android.test.ApplicationTestCase;
7-
import android.util.Log;
8-
9-
import java.io.File;
10-
import java.net.URISyntaxException;
11-
12-
import dalvik.annotation.TestTargetClass;
13-
import news.androidtv.tvapprepo.intents.IntentUriGenerator;
14-
15-
/**
16-
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
17-
*/
18-
public class ApplicationTest extends ApplicationTestCase<Application> {
19-
public static final String TAG = ApplicationTest.class.getSimpleName();
20-
21-
public ApplicationTest() {
22-
super(Application.class);
23-
}
24-
25-
public void testWebBookmarks() {
26-
final String expected = "intent://google.com#Intent;scheme=http;end";
27-
String actual = IntentUriGenerator.generateWebBookmark("http://google.com");
28-
Log.d(TAG, actual);
29-
assertEquals(expected, actual);
30-
}
31-
32-
public void testActivityShortcut() {
33-
final String expected = "intent:#Intent;component=news.androidtv.tvapprepo/.activities.SettingsActivity;end";
34-
String actual = IntentUriGenerator.generateActivityShortcut(new ComponentName("news.androidtv.tvapprepo", ".activities.SettingsActivity"));
35-
Log.d(TAG, actual);
36-
assertEquals(expected, actual);
37-
}
38-
39-
public void testFileOpening() {
40-
// Note: This can be flaky if your device doesn't have this file. Future versions of this
41-
// test should create and delete a temporary file.
42-
final String expected = "intent:///storage/emulated/0/Download/com.felkertech.n.cumulustv.test.apk#Intent;scheme=file;launchFlags=0x10000000;end";
43-
String actual = IntentUriGenerator.generateVideoPlayback(new File("/storage/emulated/0/Download/com.felkertech.n.cumulustv.test.apk"));
44-
Log.d(TAG, actual);
45-
assertEquals(expected, actual);
46-
}
47-
48-
public void testOpenGoogle() throws URISyntaxException {
49-
String string = "intent:#Intent;component=news.androidtv.tvapprepo/.activities.SettingsActivity;end";
50-
getContext().startActivity(Intent.parseUri(string, Intent.URI_INTENT_SCHEME));
51-
}
1+
package news.androidtv.tvapprepo;
2+
3+
import android.app.Application;
4+
import android.content.ComponentName;
5+
import android.content.Intent;
6+
import android.test.ApplicationTestCase;
7+
import android.util.Log;
8+
9+
import java.io.File;
10+
import java.net.URISyntaxException;
11+
12+
import news.androidtv.tvapprepo.intents.IntentUriGenerator;
13+
14+
/**
15+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
16+
*/
17+
public class IntentsInstrumentationTest extends ApplicationTestCase<Application> {
18+
public static final String TAG = IntentsInstrumentationTest.class.getSimpleName();
19+
20+
public IntentsInstrumentationTest() {
21+
super(Application.class);
22+
}
23+
24+
public void testWebBookmarks() {
25+
final String expected = "intent://google.com#Intent;scheme=http;end";
26+
String actual = IntentUriGenerator.generateWebBookmark("http://google.com");
27+
Log.d(TAG, actual);
28+
assertEquals(expected, actual);
29+
}
30+
31+
public void testActivityShortcut() {
32+
final String expected = "intent:#Intent;component=news.androidtv.tvapprepo/.activities.SettingsActivity;end";
33+
String actual = IntentUriGenerator.generateActivityShortcut(new ComponentName("news.androidtv.tvapprepo", ".activities.SettingsActivity"));
34+
Log.d(TAG, actual);
35+
assertEquals(expected, actual);
36+
}
37+
38+
public void testFileOpening() {
39+
// Note: This can be flaky if your device doesn't have this file. Future versions of this
40+
// test should create and delete a temporary file.
41+
final String expected = "intent:///storage/emulated/0/Download/com.felkertech.n.cumulustv.test.apk#Intent;scheme=file;launchFlags=0x10000000;end";
42+
String actual = IntentUriGenerator.generateVideoPlayback(new File("/storage/emulated/0/Download/com.felkertech.n.cumulustv.test.apk"));
43+
Log.d(TAG, actual);
44+
assertEquals(expected, actual);
45+
}
46+
47+
public void testOpenGoogle() throws URISyntaxException {
48+
String string = "intent:#Intent;component=news.androidtv.tvapprepo/.activities.SettingsActivity;end";
49+
getContext().startActivity(Intent.parseUri(string, Intent.URI_INTENT_SCHEME));
50+
}
5251
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package news.androidtv.tvapprepo;
2+
3+
import android.content.pm.ActivityInfo;
4+
import android.content.pm.ApplicationInfo;
5+
import android.content.pm.ResolveInfo;
6+
import android.graphics.Bitmap;
7+
import android.support.test.rule.ActivityTestRule;
8+
import android.support.test.runner.AndroidJUnit4;
9+
import android.util.Log;
10+
11+
import com.android.volley.VolleyError;
12+
import com.bumptech.glide.Glide;
13+
14+
import org.junit.Assert;
15+
import org.junit.Rule;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.ExecutionException;
21+
import java.util.concurrent.TimeUnit;
22+
23+
import news.androidtv.tvapprepo.activities.MainActivity;
24+
import news.androidtv.tvapprepo.intents.IntentUriGenerator;
25+
import news.androidtv.tvapprepo.model.AdvancedOptions;
26+
import news.androidtv.tvapprepo.utils.GenerateShortcutHelper;
27+
28+
/**
29+
* A series of tests relating to generating shortcut apks.
30+
* Right now all tests must be verified manually.
31+
*/
32+
@RunWith(AndroidJUnit4.class)
33+
public class ShortcutGenerationInstrumentationTest {
34+
@Rule
35+
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
36+
MainActivity.class);
37+
38+
private static final String TAG = ShortcutGenerationInstrumentationTest.class.getSimpleName();
39+
private static final int TIMEOUT_LENGTH = 90;
40+
private static final TimeUnit TIMEOUT_UNITS = TimeUnit.SECONDS;
41+
private static final ResolveInfo SAMPLE_APK;
42+
private static final String SAMPLE_BANNER = "http://via.placeholder.com/320x180";
43+
44+
static {
45+
SAMPLE_APK = new ResolveInfo();
46+
SAMPLE_APK.activityInfo = new ActivityInfo();
47+
SAMPLE_APK.activityInfo.applicationInfo = new ApplicationInfo();
48+
SAMPLE_APK.activityInfo.applicationInfo.packageName = "com.test";
49+
SAMPLE_APK.activityInfo.nonLocalizedLabel = "Test Activity Label";
50+
SAMPLE_APK.activityInfo.name = "Test Activity Name";
51+
SAMPLE_APK.icon = R.drawable.ic_launcher;
52+
}
53+
54+
private void doOnMainThread(Runnable r) {
55+
mActivityRule.getActivity().runOnUiThread(r);
56+
}
57+
58+
/**
59+
* Generates an APK shortcut from an on-device APK without advanced options.
60+
*/
61+
@Test
62+
public void testSimpleApkGeneration() throws InterruptedException {
63+
// Set up a CountdownLatch to accommodate for network events
64+
final CountDownLatch latch = new CountDownLatch(1);
65+
66+
doOnMainThread(new Runnable() {
67+
@Override
68+
public void run() {
69+
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
70+
new AdvancedOptions(mActivityRule.getActivity()), new GenerateShortcutHelper.Callback() {
71+
@Override
72+
public void onResponseComplete(String response) {
73+
latch.countDown();
74+
}
75+
76+
@Override
77+
public void onResponseFailed(VolleyError error) {
78+
throw new RuntimeException(error.getMessage());
79+
}
80+
});
81+
}
82+
});
83+
84+
Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
85+
}
86+
87+
@Test
88+
public void testCustomBannerApkGeneration() throws InterruptedException {
89+
// Set up a CountdownLatch to accommodate for network events
90+
final CountDownLatch latch = new CountDownLatch(1);
91+
final AdvancedOptions advancedOptions = new AdvancedOptions(mActivityRule.getActivity());
92+
advancedOptions.setBannerUrl(SAMPLE_BANNER);
93+
94+
doOnMainThread(new Runnable() {
95+
@Override
96+
public void run() {
97+
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
98+
advancedOptions, new GenerateShortcutHelper.Callback() {
99+
@Override
100+
public void onResponseComplete(String response) {
101+
Log.d(TAG, response);
102+
latch.countDown();
103+
}
104+
105+
@Override
106+
public void onResponseFailed(VolleyError error) {
107+
throw new RuntimeException(error.getMessage());
108+
}
109+
});
110+
}
111+
});
112+
113+
Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
114+
}
115+
116+
@Test
117+
public void testGameApkGeneration() throws InterruptedException {
118+
// Set up a CountdownLatch to accommodate for network events
119+
final CountDownLatch latch = new CountDownLatch(1);
120+
final AdvancedOptions advancedOptions = new AdvancedOptions(mActivityRule.getActivity());
121+
advancedOptions.setIsGame(true);
122+
123+
doOnMainThread(new Runnable() {
124+
@Override
125+
public void run() {
126+
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
127+
advancedOptions, new GenerateShortcutHelper.Callback() {
128+
@Override
129+
public void onResponseComplete(String response) {
130+
latch.countDown();
131+
}
132+
133+
@Override
134+
public void onResponseFailed(VolleyError error) {
135+
throw new RuntimeException(error.getMessage());
136+
}
137+
});
138+
}
139+
});
140+
141+
Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
142+
}
143+
144+
@Test
145+
public void testBannerBitmapApk() throws InterruptedException, ExecutionException {
146+
// Set up a CountdownLatch to accommodate for network events
147+
final CountDownLatch latch = new CountDownLatch(1);
148+
final AdvancedOptions advancedOptions = new AdvancedOptions(mActivityRule.getActivity());
149+
Bitmap bitmap = Glide.with(mActivityRule.getActivity()).load(SAMPLE_BANNER).asBitmap()
150+
.into(320, 180).get();
151+
advancedOptions.setBannerBitmap(bitmap);
152+
153+
doOnMainThread(new Runnable() {
154+
@Override
155+
public void run() {
156+
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
157+
advancedOptions, new GenerateShortcutHelper.Callback() {
158+
@Override
159+
public void onResponseComplete(String response) {
160+
latch.countDown();
161+
}
162+
163+
@Override
164+
public void onResponseFailed(VolleyError error) {
165+
throw new RuntimeException(error.getMessage());
166+
}
167+
});
168+
}
169+
});
170+
171+
Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
172+
}
173+
174+
@Test
175+
public void testWebShortcut() throws InterruptedException, ExecutionException {
176+
// Set up a CountdownLatch to accommodate for network events
177+
final CountDownLatch latch = new CountDownLatch(1);
178+
String url = "http://example.com";
179+
String label = "Example.Com";
180+
final AdvancedOptions advancedOptions = new AdvancedOptions(mActivityRule.getActivity())
181+
.setIntentUri(IntentUriGenerator.generateWebBookmark(url))
182+
.setIconUrl("https://raw.githubusercontent.com/ITVlab/TvAppRepo/master/promo/graphics/icon.png")
183+
.setCustomLabel(label);
184+
185+
doOnMainThread(new Runnable() {
186+
@Override
187+
public void run() {
188+
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
189+
advancedOptions, new GenerateShortcutHelper.Callback() {
190+
@Override
191+
public void onResponseComplete(String response) {
192+
latch.countDown();
193+
}
194+
195+
@Override
196+
public void onResponseFailed(VolleyError error) {
197+
throw new RuntimeException(error.getMessage());
198+
}
199+
});
200+
}
201+
});
202+
203+
Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
204+
}
205+
}

app/src/main/java/news/androidtv/tvapprepo/fragments/MainFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public void onDestroy() {
145145
mBackgroundTimer.cancel();
146146
}
147147
mApkDownloadHelper.removeListener(mDownloadListener);
148+
mApkDownloadHelper.destroy();
148149
}
149150

150151
@Override

0 commit comments

Comments
 (0)