-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Upload UI tests #2626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Upload UI tests #2626
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f1366bd
Fix SettingsActivityTest
domdomegg a078d44
Add test in SettingsActivity to set recent upload limit to 0
domdomegg a83ab17
Add tests to recent upload limit setting
domdomegg b0a0fe7
Simplify activity test rule
domdomegg af1dcf8
Add UploadTest
domdomegg 8da3ae4
Log the URL where the file should be uploaded
domdomegg 0073b5d
Merge branch 'master' into test-upload
domdomegg 0392666
Merge branch 'test-upload' of https://github.com/domdomegg/apps-andro…
maskaravivek d86daf1
Merge branch 'master' into test-upload
domdomegg a9b7c92
Merge branch 'test-upload' of https://github.com/domdomegg/apps-andro…
maskaravivek 58fb8c6
Generate an image file before testing uploads
maskaravivek 1f801be
With runtime permissions
maskaravivek f37405c
With automatic login
maskaravivek e0738d4
Merge pull request #1 from maskaravivek/uploadTest
domdomegg a1d8704
With automatic login
maskaravivek daaea6f
Get test credentials from travis
maskaravivek fbceb9a
Merge pull request #2 from maskaravivek/uploadTest
domdomegg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
app/src/androidTest/java/fr/free/nrw/commons/UploadTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package fr.free.nrw.commons; | ||
|
||
import android.Manifest; | ||
import android.app.Activity; | ||
import android.app.Instrumentation.ActivityResult; | ||
import android.content.Context; | ||
import android.content.ContextWrapper; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.net.Uri; | ||
import android.os.Environment; | ||
import android.view.autofill.AutofillManager; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Random; | ||
|
||
import androidx.test.espresso.NoMatchingViewException; | ||
import androidx.test.espresso.intent.rule.IntentsTestRule; | ||
import androidx.test.filters.LargeTest; | ||
import androidx.test.platform.app.InstrumentationRegistry; | ||
import androidx.test.rule.ActivityTestRule; | ||
import androidx.test.rule.GrantPermissionRule; | ||
import androidx.test.runner.AndroidJUnit4; | ||
import fr.free.nrw.commons.auth.LoginActivity; | ||
import fr.free.nrw.commons.utils.ConfigUtils; | ||
import timber.log.Timber; | ||
|
||
import static androidx.test.espresso.Espresso.onView; | ||
import static androidx.test.espresso.action.ViewActions.clearText; | ||
import static androidx.test.espresso.action.ViewActions.click; | ||
import static androidx.test.espresso.action.ViewActions.replaceText; | ||
import static androidx.test.espresso.action.ViewActions.typeText; | ||
import static androidx.test.espresso.assertion.ViewAssertions.matches; | ||
import static androidx.test.espresso.intent.Intents.intended; | ||
import static androidx.test.espresso.intent.Intents.intending; | ||
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction; | ||
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasType; | ||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withId; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withParent; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withText; | ||
import static org.hamcrest.core.AllOf.allOf; | ||
|
||
@LargeTest | ||
@RunWith(AndroidJUnit4.class) | ||
public class UploadTest { | ||
@Rule | ||
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE, | ||
Manifest.permission.ACCESS_FINE_LOCATION); | ||
|
||
@Rule | ||
public ActivityTestRule activityRule = new IntentsTestRule<>(LoginActivity.class); | ||
|
||
@Before | ||
public void setup() { | ||
|
||
saveToInternalStorage(); | ||
} | ||
|
||
private void saveToInternalStorage() { | ||
Bitmap bitmapImage = getRandomBitmap(); | ||
Context applicationContext = InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext(); | ||
ContextWrapper cw = new ContextWrapper(applicationContext); | ||
// path to /data/data/yourapp/app_data/imageDir | ||
File mypath = new File(Environment.getExternalStorageDirectory(), "image.jpg"); | ||
|
||
Timber.d("Filepath: %s", mypath.getPath()); | ||
|
||
Timber.d("Absolute Filepath: %s", mypath.getAbsolutePath()); | ||
|
||
FileOutputStream fos = null; | ||
try { | ||
fos = new FileOutputStream(mypath); | ||
// Use the compress method on the BitMap object to write image to the OutputStream | ||
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if (fos != null) { | ||
fos.close(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private Bitmap getRandomBitmap() { | ||
Random random = new Random(); | ||
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); | ||
bitmap.eraseColor(random.nextInt(255)); | ||
return bitmap; | ||
} | ||
|
||
private void getToMainActivity() { | ||
try { | ||
//Skip tutorial | ||
onView(withId(R.id.finishTutorialButton)) | ||
.perform(click()); | ||
|
||
//Perform Login | ||
onView(withId(R.id.loginUsername)) | ||
.perform(clearText(), typeText(BuildConfig.TEST_USERNAME)); | ||
onView(withId(R.id.loginPassword)) | ||
.perform(clearText(), typeText(BuildConfig.TEST_PASSWORD)); | ||
onView(withId(R.id.loginButton)) | ||
.perform(click()); | ||
} catch (NoMatchingViewException ignored) { | ||
} | ||
} | ||
|
||
@Test | ||
public void uploadTest() { | ||
if (!ConfigUtils.isBetaFlavour()) { | ||
throw new Error("This test should only be run in Beta!"); | ||
} | ||
|
||
getToMainActivity(); | ||
|
||
// Uri to return by our mock gallery selector | ||
// Requires file 'image.jpg' to be placed at root of file structure | ||
Uri imageUri = Uri.parse("file://mnt/sdcard/image.jpg"); | ||
|
||
// Build a result to return from the Camera app | ||
Intent intent = new Intent(); | ||
intent.setData(imageUri); | ||
ActivityResult result = new ActivityResult(Activity.RESULT_OK, intent); | ||
|
||
// Stub out the File picker. When an intent is sent to the File picker, this tells | ||
// Espresso to respond with the ActivityResult we just created | ||
intending(allOf(hasAction(Intent.ACTION_GET_CONTENT), hasType("image/*"))).respondWith(result); | ||
|
||
// Open FAB | ||
onView(allOf(withId(R.id.fab_plus), isDisplayed())) | ||
.perform(click()); | ||
|
||
// Click gallery | ||
onView(allOf(withId(R.id.fab_gallery), isDisplayed())) | ||
.perform(click()); | ||
|
||
// Validate that an intent to get an image is sent | ||
intended(allOf(hasAction(Intent.ACTION_GET_CONTENT), hasType("image/*"))); | ||
|
||
// Create filename with the current time (to prevent overwrites) | ||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd-hhmmss"); | ||
String commonsFileName = "MobileTest " + dateFormat.format(new Date()); | ||
|
||
// Try to dismiss the error, if there is one (probably about duplicate files on Commons) | ||
try { | ||
onView(withText("Yes")) | ||
.check(matches(isDisplayed())) | ||
.perform(click()); | ||
} catch (NoMatchingViewException ignored) {} | ||
|
||
onView(allOf(withId(R.id.description_item_edit_text), withParent(withParent(withId(R.id.image_title_container))))) | ||
.perform(replaceText(commonsFileName)); | ||
|
||
onView(withId(R.id.bottom_card_next)) | ||
.perform(click()); | ||
|
||
try { | ||
Thread.sleep(500); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
onView(withId(R.id.category_search)) | ||
.perform(replaceText("Uploaded with Mobile/Android Tests")); | ||
|
||
try { | ||
Thread.sleep(3000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
onView(withParent(withId(R.id.categories))) | ||
.perform(click()); | ||
|
||
onView(withId(R.id.category_next)) | ||
.perform(click()); | ||
|
||
try { | ||
Thread.sleep(500); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
onView(withId(R.id.submit)) | ||
.perform(click()); | ||
|
||
try { | ||
Thread.sleep(10000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
String fileUrl = "https://commons.wikimedia.beta.wmflabs.org/wiki/File:" + | ||
commonsFileName.replace(' ', '_') + ".jpg"; | ||
Timber.i("File should be uploaded to " + fileUrl); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we enhance this bit as well? We can probably call media wiki api to assert if the image was uploaded.