|
| 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 | +} |
0 commit comments