Skip to content

Commit

Permalink
Merge pull request getodk#3827 from seadowg/fix-qr-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel authored May 13, 2020
2 parents 010a3ca + b0eef98 commit 28f33f9
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
import android.graphics.BitmapFactory;
import android.net.Uri;

import com.google.zxing.WriterException;

import androidx.core.content.FileProvider;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.espresso.intent.Intents;
import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.GrantPermissionRule;
import dagger.Provides;
import io.reactivex.Observable;

import com.google.zxing.WriterException;

import org.junit.Before;
import org.junit.Rule;
Expand All @@ -28,8 +25,6 @@
import org.odk.collect.android.R;
import org.odk.collect.android.activities.MainMenuActivity;
import org.odk.collect.android.injection.config.AppDependencyModule;
import org.odk.collect.android.storage.StoragePathProvider;
import org.odk.collect.android.storage.StorageSubdirectory;
import org.odk.collect.android.support.ResetStateRule;
import org.odk.collect.android.support.pages.MainMenuPage;
import org.odk.collect.android.utilities.FileUtils;
Expand All @@ -38,6 +33,10 @@
import java.io.IOException;
import java.util.Collection;

import dagger.Provides;
import io.reactivex.Observable;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.Intents.intending;
import static androidx.test.espresso.intent.matcher.BundleMatchers.hasEntry;
Expand All @@ -49,7 +48,6 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

import static org.junit.Assert.assertTrue;


Expand All @@ -65,7 +63,8 @@ public class ConfigureWithQRCodeTest {
.outerRule(GrantPermissionRule.grant(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CAMERA
))
.around(new ResetStateRule(new AppDependencyModule() {
@Override
Expand All @@ -78,9 +77,8 @@ public QRCodeGenerator providesQRCodeGenerator() {

@Before
public void stubAllExternalIntents() {
// By default Espresso Intents does not stub any Intents. Stubbing needs to be setup before
// every test run. In this case all external Intents will be blocked.
intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
// Pretend that the share/import actions are cancelled so we don't deal with actual import here
intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, null));
}

@Test
Expand All @@ -101,7 +99,7 @@ public void onMainMenu_clickConfigureQRCode_andClickingOnView_showsQRCode() {
.clickConfigureQR()
.clickViewQRFragment()
.assertImageViewShowsImage(R.id.ivQRcode, BitmapFactory.decodeResource(
ApplicationProvider.getApplicationContext().getResources(),
getApplicationContext().getResources(),
CHECKER_BACKGROUND_DRAWABLE_ID
));
}
Expand All @@ -122,7 +120,7 @@ public void onMainMenu_clickConfigureQRCode_andClickingOnImportQRCode_startsExte
@Test
public void onMainMenu_clickConfigureQRCode_andClickingOnShareQRCode_startsExternalShareIntent() {
String path = new StubQRCodeGenerator().getQrCodeFilepath();
Uri expected = FileProvider.getUriForFile(ApplicationProvider.getApplicationContext(),
Uri expected = FileProvider.getUriForFile(getApplicationContext(),
BuildConfig.APPLICATION_ID + ".provider",
new File(path));

Expand All @@ -132,14 +130,18 @@ public void onMainMenu_clickConfigureQRCode_andClickingOnShareQRCode_startsExter
.clickConfigureQR()
.clickOnId(R.id.menu_item_share);

// Check that we've actually generated a QR code to share
File qrcodeFile = new File(path);
assertTrue(qrcodeFile.exists());

// should be two Intents, 1. to QRCodeTabsActivity 2. Share QR Code Intent
assertThat(Intents.getIntents().size(), equalTo(2));
Intent receivedIntent = Intents.getIntents().get(1);
assertThat(receivedIntent, hasAction(Intent.ACTION_CHOOSER));

// test title
assertThat(receivedIntent, hasExtras(hasEntry(Intent.EXTRA_TITLE,
ApplicationProvider.getApplicationContext().getString(R.string.share_qrcode))));
getApplicationContext().getString(R.string.share_qrcode))));

// test SEND intent
assertThat(receivedIntent, hasExtraWithKey(Intent.EXTRA_INTENT));
Expand All @@ -148,15 +150,12 @@ public void onMainMenu_clickConfigureQRCode_andClickingOnShareQRCode_startsExter
assertThat(sendIntent, hasAction(Intent.ACTION_SEND));
assertThat(sendIntent, hasType("image/*"));
assertThat(sendIntent, hasExtras(hasEntry(Intent.EXTRA_STREAM, expected)));

// test that file stream is valid by checking that file exists
File qrcodeFile = new File(path);
assertTrue(qrcodeFile.exists());
}

// StubQRCodeGenerator is a class that is injected during this test
// to verify that the QRCode is generated and shown to user correctly
private static class StubQRCodeGenerator implements QRCodeGenerator {

@Override
public Bitmap generateQRBitMap(String data, int sideLength) throws IOException, WriterException {
// don't use this in this test, so okay to return null
Expand All @@ -168,19 +167,19 @@ public Observable<Bitmap> generateQRCode(Collection<String> selectedPasswordKeys
return Observable.create(emitter -> {
Bitmap bitmap =
BitmapFactory.decodeResource(
ApplicationProvider.getApplicationContext().getResources(),
getApplicationContext().getResources(),
CHECKER_BACKGROUND_DRAWABLE_ID);
emitter.onNext(bitmap);
FileUtils.saveBitmapToFile(bitmap, getQrCodeFilepath());

// save bitmap to test that shareQRCode generates bitmap if file not there
FileUtils.saveBitmapToFile(bitmap, getQrCodeFilepath());
emitter.onComplete();
});
}

@Override
public String getQrCodeFilepath() {
return new StoragePathProvider().getDirPath(StorageSubdirectory.SETTINGS) + File.separator + "test-collect-settings.png";
return getApplicationContext().getExternalFilesDir(null) + File.separator + "test-collect-settings.png";
}

@Override
Expand Down

0 comments on commit 28f33f9

Please sign in to comment.