Skip to content
This repository has been archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
Merge branch 'issue/9541-configuration-key' into issue/9541-bundle-qu…
Browse files Browse the repository at this point in the history
…antity-rules-support
  • Loading branch information
Alejo committed Nov 10, 2023
2 parents ad47d7c + bbe86a9 commit ceb00dd
Show file tree
Hide file tree
Showing 111 changed files with 9,672 additions and 2,024 deletions.
2 changes: 2 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ dependencies {
// Coroutines
implementation sharedLibs.kotlinx.coroutines.core
implementation sharedLibs.kotlinx.coroutines.android

lintChecks sharedLibs.wordpress.lint
}

def loadPropertiesOrUseExampleProperties(fileName, warningDetail) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.properties.Delegates.notNull

@Suppress("ClassNaming")
@SuppressLint("UseSparseArrays")
class MockedStack_MediaTest : MockedStack_Base() {
@Inject lateinit var dispatcher: Dispatcher
Expand Down Expand Up @@ -66,7 +67,7 @@ class MockedStack_MediaTest : MockedStack_Base() {
interceptor.respondWithSticky("media-upload-response-success.json")

// First, try canceling an image with the default behavior (canceled image is deleted from the store)
newMediaModel("Test Title", sampleImagePath, "image/jpeg").let { testMedia ->
newMediaModel("Test Title", sampleImagePath)?.let { testMedia ->
countDownLatch = CountDownLatch(1)
nextEvent = TestEvents.CANCELED_MEDIA
val payload = UploadMediaPayload(testSite, testMedia, true)
Expand All @@ -77,12 +78,17 @@ class MockedStack_MediaTest : MockedStack_Base() {
val cancelPayload = CancelMediaPayload(testSite, testMedia)
dispatcher.dispatch(MediaActionBuilder.newCancelMediaUploadAction(cancelPayload))

Assert.assertTrue(countDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS))
Assert.assertTrue(
countDownLatch.await(
TestUtils.DEFAULT_TIMEOUT_MS.toLong(),
TimeUnit.MILLISECONDS
)
)
Assert.assertEquals(0, mediaStore.getSiteMediaCount(testSite))
}

// Now, try canceling with delete=false (canceled image should be marked as failed and kept in the store)
newMediaModel("Test Title", sampleImagePath, "image/jpeg").let { testMedia ->
newMediaModel("Test Title", sampleImagePath)?.let { testMedia ->
countDownLatch = CountDownLatch(1)
nextEvent = TestEvents.CANCELED_MEDIA
val payload = UploadMediaPayload(testSite, testMedia, true)
Expand All @@ -93,11 +99,16 @@ class MockedStack_MediaTest : MockedStack_Base() {
val cancelPayload = CancelMediaPayload(testSite, testMedia, false)
dispatcher.dispatch(MediaActionBuilder.newCancelMediaUploadAction(cancelPayload))

Assert.assertTrue(countDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS))
Assert.assertTrue(
countDownLatch.await(
TestUtils.DEFAULT_TIMEOUT_MS.toLong(),
TimeUnit.MILLISECONDS
)
)
Assert.assertEquals(1, mediaStore.getSiteMediaCount(testSite))

val canceledMedia = mediaStore.getMediaWithLocalId(testMedia.id)
Assert.assertEquals(MediaUploadState.FAILED.toString(), canceledMedia.uploadState)
Assert.assertEquals(MediaUploadState.FAILED.toString(), canceledMedia?.uploadState)
}
}

Expand All @@ -119,15 +130,17 @@ class MockedStack_MediaTest : MockedStack_Base() {
// Verify all have been uploaded
Assert.assertEquals(uploadedMediaModels.size, uploadedIds.size)
Assert.assertEquals(
uploadedMediaModels.size,
mediaStore.getSiteMediaWithState(testSite, MediaUploadState.UPLOADED).size
uploadedMediaModels.size,
mediaStore.getSiteMediaWithState(testSite, MediaUploadState.UPLOADED).size
)

// Verify they exist in the MediaStore
val iterator = uploadedMediaModels.values.iterator()
while (iterator.hasNext()) {
iterator.next().let { uploadedMediaModel ->
Assert.assertNotNull(mediaStore.getSiteMediaWithId(testSite, uploadedMediaModel.mediaId))
Assert.assertNotNull(
mediaStore.getSiteMediaWithId(testSite, uploadedMediaModel.mediaId)
)
}
}
}
Expand Down Expand Up @@ -163,8 +176,10 @@ class MockedStack_MediaTest : MockedStack_Base() {
Assert.assertEquals(uploadedIds.size, mediaStore.getSiteMediaCount(testSite))

// The number of uploaded media in the store should match our records of how many were not cancelled
Assert.assertEquals(uploadedIds.size,
mediaStore.getSiteMediaWithState(testSite, MediaUploadState.UPLOADED).size)
Assert.assertEquals(
uploadedIds.size,
mediaStore.getSiteMediaWithState(testSite, MediaUploadState.UPLOADED).size
)
}

@Test
Expand Down Expand Up @@ -198,72 +213,82 @@ class MockedStack_MediaTest : MockedStack_Base() {
Assert.assertEquals(uploadedMediaModels.size, mediaStore.getSiteMediaCount(testSite))

// The number of uploaded media in the store should match our records of how many were not cancelled
Assert.assertEquals(uploadedIds.size, mediaStore.getSiteMediaWithState(testSite,
MediaUploadState.UPLOADED).size)
Assert.assertEquals(
uploadedIds.size,
mediaStore.getSiteMediaWithState(testSite, MediaUploadState.UPLOADED).size
)

// All cancelled media should have a FAILED state
Assert.assertEquals(amountToCancel, mediaStore.getSiteMediaWithState(testSite, MediaUploadState.FAILED).size)
Assert.assertEquals(
amountToCancel,
mediaStore.getSiteMediaWithState(testSite, MediaUploadState.FAILED).size
)
}

@Suppress("unused")
@Subscribe
@Suppress("unused", "ThrowsCount", "CyclomaticComplexMethod", "NestedBlockDepth")
fun onMediaUploaded(event: OnMediaUploaded) {
if (event.isError) {
if (event.isError || event.media == null) {
throw AssertionError("Unexpected error occurred with type: " + event.error.type)
}
if (event.canceled) {
if (nextEvent == TestEvents.CANCELED_MEDIA || nextEvent == TestEvents.UPLOADED_MULTIPLE_MEDIA_WITH_CANCEL) {
countDownLatch.countDown()
} else {
throw AssertionError("Unexpected cancellation for media: " + event.media.id)
}
} else if (event.completed) {
when (nextEvent) {
TestEvents.UPLOADED_MULTIPLE_MEDIA_WITH_CANCEL -> {
uploadedIds.add(event.media.mediaId)
// Update our own map object with the new media id
val media = uploadedMediaModels[event.media.id]?.apply {
mediaId = event.media.mediaId
} ?: AppLog.e(T.MEDIA, "MediaModel not found: " + event.media.id)
Assert.assertNotNull(media)
}
TestEvents.UPLOADED_MULTIPLE_MEDIA -> {
uploadedIds.add(event.media.mediaId)
// Update our own map object with the new media id
val media = uploadedMediaModels[event.media.id]?.apply {
mediaId = event.media.mediaId
} ?: AppLog.e(T.MEDIA, "MediaModel not found: " + event.media.id)
Assert.assertNotNull(media)
event.media?.let {
if (event.canceled) {
if (nextEvent == TestEvents.CANCELED_MEDIA ||
nextEvent == TestEvents.UPLOADED_MULTIPLE_MEDIA_WITH_CANCEL) {
countDownLatch.countDown()
} else {
throw AssertionError("Unexpected cancellation for media: " + it.id)
}
else -> {
throw AssertionError("Unexpected completion for media: " + event.media.id)
} else if (event.completed) {
when (nextEvent) {
TestEvents.UPLOADED_MULTIPLE_MEDIA_WITH_CANCEL -> {
uploadedIds.add(it.mediaId)
// Update our own map object with the new media id
val media = uploadedMediaModels[it.id]?.apply {
mediaId = it.mediaId
} ?: AppLog.e(T.MEDIA, "MediaModel not found: " + it.id)
Assert.assertNotNull(media)
}

TestEvents.UPLOADED_MULTIPLE_MEDIA -> {
uploadedIds.add(it.mediaId)
// Update our own map object with the new media id
val media = uploadedMediaModels[it.id]?.apply {
mediaId = it.mediaId
} ?: AppLog.e(T.MEDIA, "MediaModel not found: " + it.id)
Assert.assertNotNull(media)
}

else -> {
throw AssertionError("Unexpected completion for media: " + it.id)
}
}
countDownLatch.countDown()
}
countDownLatch.countDown()
}
}

private fun addMediaModelToUploadArray(title: String) {
val mediaModel = newMediaModel(title, sampleImagePath, "image/jpeg")
uploadedMediaModels[mediaModel.id] = mediaModel
val mediaModel = newMediaModel(title, sampleImagePath)
mediaModel?.let { uploadedMediaModels[mediaModel.id] = it }
}

private fun newMediaModel(testTitle: String, mediaPath: String, mimeType: String): MediaModel {
val testDescription = "Test Description"
val testCaption = "Test Caption"
val testAlt = "Test Alt"

return mediaStore.instantiateMediaModel().apply {
filePath = mediaPath
fileExtension = mediaPath.substring(mediaPath.lastIndexOf(".") + 1)
this.mimeType = mimeType
fileName = mediaPath.substring(mediaPath.lastIndexOf("/"))
title = testTitle
description = testDescription
caption = testCaption
alt = testAlt
localSiteId = testSite.id
}
private fun newMediaModel(testTitle: String, mediaPath: String): MediaModel? {
val testMedia = MediaModel(
testSite.id,
null,
mediaPath.substring(mediaPath.lastIndexOf("/")),
mediaPath,
mediaPath.substring(mediaPath.lastIndexOf(".") + 1),
"image/jpeg",
testTitle,
null
)
testMedia.description = "Test Description"
testMedia.caption = "Test Caption"
testMedia.alt = "Test Alt"

return mediaStore.instantiateMediaModel(testMedia)
}

private fun uploadMultipleMedia(
Expand All @@ -278,7 +303,8 @@ class MockedStack_MediaTest : MockedStack_Base() {
// To imitate a real set of media upload requests as much as possible, each one should return a unique
// remote media id. This also makes sure the MediaModel table doesn't treat these as duplicate entries and
// deletes them, failing the test.
defaultId: String -> defaultId.replace("9999", remoteIdQueue.poll().toString())
defaultId: String ->
defaultId.replace("9999", remoteIdQueue.poll()?.toString() ?: "")
}

countDownLatch = CountDownLatch(mediaList.size)
Expand All @@ -292,14 +318,19 @@ class MockedStack_MediaTest : MockedStack_Base() {
// Wait a bit and issue the cancel command
TestUtils.waitFor(300)

// We'e only cancelling the first n=howManyFirstToCancel uploads
// We're only cancelling the first n=howManyFirstToCancel uploads
for (i in 0 until howManyFirstToCancel) {
val media = mediaList[i]
val payload = CancelMediaPayload(testSite, media, delete)
dispatcher.dispatch(MediaActionBuilder.newCancelMediaUploadAction(payload))
}
}

Assert.assertTrue(countDownLatch.await(TestUtils.MULTIPLE_UPLOADS_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS))
Assert.assertTrue(
countDownLatch.await(
TestUtils.MULTIPLE_UPLOADS_TIMEOUT_MS.toLong(),
TimeUnit.MILLISECONDS
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@

/**
* Tests using a Mocked Network app component. Test the Store itself and not the underlying network component(s).
*
* <p>
* Tests the interactions between the MediaStore/PostStore and the UploadStore, without directly injecting the
* UploadStore in the test class.
*/
@SuppressWarnings("NewClassNamingConvention")
public class MockedStack_UploadStoreTest extends MockedStack_Base {
@Inject Dispatcher mDispatcher;
@Inject MediaStore mMediaStore;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void setUp() throws Exception {

@Test
public void testUploadMedia() throws InterruptedException {
MediaModel testMedia = newMediaModel(getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(getSampleImagePath());
startSuccessfulMediaUpload(testMedia, getTestSite());
assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS));

Expand Down Expand Up @@ -89,26 +90,22 @@ public void onMediaUploaded(OnMediaUploaded event) {
}
}

private MediaModel newMediaModel(String mediaPath, String mimeType) {
return newMediaModel("Test Title", mediaPath, mimeType);
}

private MediaModel newMediaModel(String testTitle, String mediaPath, String mimeType) {
final String testDescription = "Test Description";
final String testCaption = "Test Caption";
final String testAlt = "Test Alt";

MediaModel testMedia = mMediaStore.instantiateMediaModel();
testMedia.setFilePath(mediaPath);
testMedia.setFileExtension(mediaPath.substring(mediaPath.lastIndexOf(".") + 1, mediaPath.length()));
testMedia.setMimeType(mimeType);
testMedia.setFileName(mediaPath.substring(mediaPath.lastIndexOf("/"), mediaPath.length()));
testMedia.setTitle(testTitle);
testMedia.setDescription(testDescription);
testMedia.setCaption(testCaption);
testMedia.setAlt(testAlt);

return testMedia;
private MediaModel newMediaModel(String mediaPath) {
MediaModel testMedia = new MediaModel(
0,
null,
mediaPath.substring(mediaPath.lastIndexOf("/")),
mediaPath,
mediaPath.substring(mediaPath.lastIndexOf(".") + 1),
"image/jpeg",
"Test Title",
null
);
testMedia.setDescription("Test Description");
testMedia.setCaption("Test Caption");
testMedia.setAlt("Test Alt");

return mMediaStore.instantiateMediaModel(testMedia);
}

private SiteModel getTestSite() {
Expand Down
Loading

0 comments on commit ceb00dd

Please sign in to comment.