Skip to content

Commit

Permalink
Merge pull request #59 from Fleker/master
Browse files Browse the repository at this point in the history
Adds instrumentation tests to prevent regressions
  • Loading branch information
Fleker committed Dec 17, 2017
2 parents b0abb65 + eb83530 commit af7ca6c
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 57 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions app/build.gradle
Expand Up @@ -8,8 +8,9 @@ android {
applicationId "news.androidtv.tvapprepo"
minSdkVersion 21
targetSdkVersion 25
versionCode 16
versionName "1.1.3"
versionCode 17
versionName "1.1.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand Down Expand Up @@ -73,6 +74,11 @@ dependencies {
compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
transitive = true;
}

androidTestCompile 'com.android.support:support-annotations:25.3.1'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

apply plugin: 'com.google.gms.google-services'
@@ -1,52 +1,51 @@
package news.androidtv.tvapprepo;

import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.test.ApplicationTestCase;
import android.util.Log;

import java.io.File;
import java.net.URISyntaxException;

import dalvik.annotation.TestTargetClass;
import news.androidtv.tvapprepo.intents.IntentUriGenerator;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public static final String TAG = ApplicationTest.class.getSimpleName();

public ApplicationTest() {
super(Application.class);
}

public void testWebBookmarks() {
final String expected = "intent://google.com#Intent;scheme=http;end";
String actual = IntentUriGenerator.generateWebBookmark("http://google.com");
Log.d(TAG, actual);
assertEquals(expected, actual);
}

public void testActivityShortcut() {
final String expected = "intent:#Intent;component=news.androidtv.tvapprepo/.activities.SettingsActivity;end";
String actual = IntentUriGenerator.generateActivityShortcut(new ComponentName("news.androidtv.tvapprepo", ".activities.SettingsActivity"));
Log.d(TAG, actual);
assertEquals(expected, actual);
}

public void testFileOpening() {
// Note: This can be flaky if your device doesn't have this file. Future versions of this
// test should create and delete a temporary file.
final String expected = "intent:///storage/emulated/0/Download/com.felkertech.n.cumulustv.test.apk#Intent;scheme=file;launchFlags=0x10000000;end";
String actual = IntentUriGenerator.generateVideoPlayback(new File("/storage/emulated/0/Download/com.felkertech.n.cumulustv.test.apk"));
Log.d(TAG, actual);
assertEquals(expected, actual);
}

public void testOpenGoogle() throws URISyntaxException {
String string = "intent:#Intent;component=news.androidtv.tvapprepo/.activities.SettingsActivity;end";
getContext().startActivity(Intent.parseUri(string, Intent.URI_INTENT_SCHEME));
}
package news.androidtv.tvapprepo;

import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.test.ApplicationTestCase;
import android.util.Log;

import java.io.File;
import java.net.URISyntaxException;

import news.androidtv.tvapprepo.intents.IntentUriGenerator;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class IntentsInstrumentationTest extends ApplicationTestCase<Application> {
public static final String TAG = IntentsInstrumentationTest.class.getSimpleName();

public IntentsInstrumentationTest() {
super(Application.class);
}

public void testWebBookmarks() {
final String expected = "intent://google.com#Intent;scheme=http;end";
String actual = IntentUriGenerator.generateWebBookmark("http://google.com");
Log.d(TAG, actual);
assertEquals(expected, actual);
}

public void testActivityShortcut() {
final String expected = "intent:#Intent;component=news.androidtv.tvapprepo/.activities.SettingsActivity;end";
String actual = IntentUriGenerator.generateActivityShortcut(new ComponentName("news.androidtv.tvapprepo", ".activities.SettingsActivity"));
Log.d(TAG, actual);
assertEquals(expected, actual);
}

public void testFileOpening() {
// Note: This can be flaky if your device doesn't have this file. Future versions of this
// test should create and delete a temporary file.
final String expected = "intent:///storage/emulated/0/Download/com.felkertech.n.cumulustv.test.apk#Intent;scheme=file;launchFlags=0x10000000;end";
String actual = IntentUriGenerator.generateVideoPlayback(new File("/storage/emulated/0/Download/com.felkertech.n.cumulustv.test.apk"));
Log.d(TAG, actual);
assertEquals(expected, actual);
}

public void testOpenGoogle() throws URISyntaxException {
String string = "intent:#Intent;component=news.androidtv.tvapprepo/.activities.SettingsActivity;end";
getContext().startActivity(Intent.parseUri(string, Intent.URI_INTENT_SCHEME));
}
}
@@ -0,0 +1,205 @@
package news.androidtv.tvapprepo;

import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import news.androidtv.tvapprepo.activities.MainActivity;
import news.androidtv.tvapprepo.intents.IntentUriGenerator;
import news.androidtv.tvapprepo.model.AdvancedOptions;
import news.androidtv.tvapprepo.utils.GenerateShortcutHelper;

/**
* A series of tests relating to generating shortcut apks.
* Right now all tests must be verified manually.
*/
@RunWith(AndroidJUnit4.class)
public class ShortcutGenerationInstrumentationTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
MainActivity.class);

private static final String TAG = ShortcutGenerationInstrumentationTest.class.getSimpleName();
private static final int TIMEOUT_LENGTH = 90;
private static final TimeUnit TIMEOUT_UNITS = TimeUnit.SECONDS;
private static final ResolveInfo SAMPLE_APK;
private static final String SAMPLE_BANNER = "http://via.placeholder.com/320x180";

static {
SAMPLE_APK = new ResolveInfo();
SAMPLE_APK.activityInfo = new ActivityInfo();
SAMPLE_APK.activityInfo.applicationInfo = new ApplicationInfo();
SAMPLE_APK.activityInfo.applicationInfo.packageName = "com.test";
SAMPLE_APK.activityInfo.nonLocalizedLabel = "Test Activity Label";
SAMPLE_APK.activityInfo.name = "Test Activity Name";
SAMPLE_APK.icon = R.drawable.ic_launcher;
}

private void doOnMainThread(Runnable r) {
mActivityRule.getActivity().runOnUiThread(r);
}

/**
* Generates an APK shortcut from an on-device APK without advanced options.
*/
@Test
public void testSimpleApkGeneration() throws InterruptedException {
// Set up a CountdownLatch to accommodate for network events
final CountDownLatch latch = new CountDownLatch(1);

doOnMainThread(new Runnable() {
@Override
public void run() {
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
new AdvancedOptions(mActivityRule.getActivity()), new GenerateShortcutHelper.Callback() {
@Override
public void onResponseComplete(String response) {
latch.countDown();
}

@Override
public void onResponseFailed(VolleyError error) {
throw new RuntimeException(error.getMessage());
}
});
}
});

Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
}

@Test
public void testCustomBannerApkGeneration() throws InterruptedException {
// Set up a CountdownLatch to accommodate for network events
final CountDownLatch latch = new CountDownLatch(1);
final AdvancedOptions advancedOptions = new AdvancedOptions(mActivityRule.getActivity());
advancedOptions.setBannerUrl(SAMPLE_BANNER);

doOnMainThread(new Runnable() {
@Override
public void run() {
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
advancedOptions, new GenerateShortcutHelper.Callback() {
@Override
public void onResponseComplete(String response) {
Log.d(TAG, response);
latch.countDown();
}

@Override
public void onResponseFailed(VolleyError error) {
throw new RuntimeException(error.getMessage());
}
});
}
});

Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
}

@Test
public void testGameApkGeneration() throws InterruptedException {
// Set up a CountdownLatch to accommodate for network events
final CountDownLatch latch = new CountDownLatch(1);
final AdvancedOptions advancedOptions = new AdvancedOptions(mActivityRule.getActivity());
advancedOptions.setIsGame(true);

doOnMainThread(new Runnable() {
@Override
public void run() {
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
advancedOptions, new GenerateShortcutHelper.Callback() {
@Override
public void onResponseComplete(String response) {
latch.countDown();
}

@Override
public void onResponseFailed(VolleyError error) {
throw new RuntimeException(error.getMessage());
}
});
}
});

Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
}

@Test
public void testBannerBitmapApk() throws InterruptedException, ExecutionException {
// Set up a CountdownLatch to accommodate for network events
final CountDownLatch latch = new CountDownLatch(1);
final AdvancedOptions advancedOptions = new AdvancedOptions(mActivityRule.getActivity());
Bitmap bitmap = Glide.with(mActivityRule.getActivity()).load(SAMPLE_BANNER).asBitmap()
.into(320, 180).get();
advancedOptions.setBannerBitmap(bitmap);

doOnMainThread(new Runnable() {
@Override
public void run() {
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
advancedOptions, new GenerateShortcutHelper.Callback() {
@Override
public void onResponseComplete(String response) {
latch.countDown();
}

@Override
public void onResponseFailed(VolleyError error) {
throw new RuntimeException(error.getMessage());
}
});
}
});

Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
}

@Test
public void testWebShortcut() throws InterruptedException, ExecutionException {
// Set up a CountdownLatch to accommodate for network events
final CountDownLatch latch = new CountDownLatch(1);
String url = "http://example.com";
String label = "Example.Com";
final AdvancedOptions advancedOptions = new AdvancedOptions(mActivityRule.getActivity())
.setIntentUri(IntentUriGenerator.generateWebBookmark(url))
.setIconUrl("https://raw.githubusercontent.com/ITVlab/TvAppRepo/master/promo/graphics/icon.png")
.setCustomLabel(label);

doOnMainThread(new Runnable() {
@Override
public void run() {
GenerateShortcutHelper.generateShortcut(mActivityRule.getActivity(), SAMPLE_APK,
advancedOptions, new GenerateShortcutHelper.Callback() {
@Override
public void onResponseComplete(String response) {
latch.countDown();
}

@Override
public void onResponseFailed(VolleyError error) {
throw new RuntimeException(error.getMessage());
}
});
}
});

Assert.assertTrue(latch.await(TIMEOUT_LENGTH, TIMEOUT_UNITS));
}
}
Expand Up @@ -145,6 +145,7 @@ public void onDestroy() {
mBackgroundTimer.cancel();
}
mApkDownloadHelper.removeListener(mDownloadListener);
mApkDownloadHelper.destroy();
}

@Override
Expand Down

0 comments on commit af7ca6c

Please sign in to comment.