Skip to content

Commit

Permalink
Merge pull request #2878 from wordpress-mobile/analysis/add-missing-n…
Browse files Browse the repository at this point in the history
…ullability-annotations-to-media-client-classes

[Nullability Annotations to Java Classes] Add Missing Nullability Annotations to `Media` Network Client Classes (`safe`)
  • Loading branch information
ParaskP7 authored Oct 27, 2023
2 parents 09842f3 + b93600e commit b85a542
Show file tree
Hide file tree
Showing 20 changed files with 660 additions and 621 deletions.
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 @@ -82,7 +83,7 @@ class MockedStack_MediaTest : MockedStack_Base() {
}

// 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 Down Expand Up @@ -205,7 +206,7 @@ class MockedStack_MediaTest : MockedStack_Base() {
Assert.assertEquals(amountToCancel, mediaStore.getSiteMediaWithState(testSite, MediaUploadState.FAILED).size)
}

@Suppress("unused")
@Suppress("unused", "ThrowsCount")
@Subscribe
fun onMediaUploaded(event: OnMediaUploaded) {
if (event.isError) {
Expand Down Expand Up @@ -244,19 +245,19 @@ class MockedStack_MediaTest : MockedStack_Base() {
}

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

private fun newMediaModel(testTitle: String, mediaPath: String, mimeType: String): MediaModel {
private fun newMediaModel(testTitle: String, mediaPath: 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
this.mimeType = "image/jpeg"
fileName = mediaPath.substring(mediaPath.lastIndexOf("/"))
title = testTitle
description = testDescription
Expand All @@ -278,7 +279,7 @@ 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,7 +293,7 @@ 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)
Expand Down
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,21 +90,17 @@ 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) {
private MediaModel newMediaModel(String mediaPath) {
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.setFileExtension(mediaPath.substring(mediaPath.lastIndexOf(".") + 1));
testMedia.setMimeType("image/jpeg");
testMedia.setFileName(mediaPath.substring(mediaPath.lastIndexOf("/")));
testMedia.setTitle("Test Title");
testMedia.setDescription(testDescription);
testMedia.setCaption(testCaption);
testMedia.setAlt(testAlt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
/**
* Tests using a Mocked Network app component. Test the Store itself and not the underlying network component(s).
*/
@SuppressWarnings("NewClassNamingConvention")
public class MockedStack_UploadTest extends MockedStack_Base {
private static final String POST_DEFAULT_TITLE = "UploadTest base post";
private static final String POST_DEFAULT_DESCRIPTION = "Hi there, I'm a post from FluxC!";
Expand Down Expand Up @@ -88,7 +89,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 All @@ -101,7 +102,7 @@ public void testUploadMedia() throws InterruptedException {

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

Expand All @@ -120,7 +121,7 @@ public void testCancelImageUpload() throws InterruptedException {
mInterceptor.respondWithSticky("media-upload-response-success.json", 1000L, null);

// First, try canceling an image with the default behavior (canceled image is deleted from the store)
MediaModel testMedia = newMediaModel(getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(getSampleImagePath());
mCountDownLatch = new CountDownLatch(1);
mNextEvent = TestEvents.CANCELED_MEDIA;
UploadMediaPayload payload = new UploadMediaPayload(getTestSite(), testMedia, true);
Expand All @@ -141,7 +142,7 @@ public void testCancelImageUpload() throws InterruptedException {
assertNull(mediaUploadModel);

// Now, try canceling with delete=false (canceled image should be marked as failed and kept in the store)
testMedia = newMediaModel(getSampleImagePath(), "image/jpeg");
testMedia = newMediaModel(getSampleImagePath());
mCountDownLatch = new CountDownLatch(1);
mNextEvent = TestEvents.CANCELED_MEDIA;
payload = new UploadMediaPayload(getTestSite(), testMedia, true);
Expand Down Expand Up @@ -174,7 +175,7 @@ public void testRegisterPostAndUploadMediaWithError() throws InterruptedExceptio
setupPostAttributes();

// Start uploading media
MediaModel testMedia = newMediaModel(getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(getSampleImagePath());
testMedia.setLocalPostId(mPost.getId());
startFailingMediaUpload(testMedia, site);

Expand Down Expand Up @@ -271,7 +272,7 @@ public void testRegisterPostAndUploadMediaWithPostCancellation() throws Interrup
setupPostAttributes();

// Start uploading media
MediaModel testMedia = newMediaModel(getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(getSampleImagePath());
testMedia.setLocalPostId(mPost.getId());
startSuccessfulMediaUpload(testMedia, site);

Expand Down Expand Up @@ -341,7 +342,7 @@ public void testUploadMediaInCancelledPost() throws InterruptedException {
setupPostAttributes();

// Start uploading media
MediaModel testMedia = newMediaModel(getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(getSampleImagePath());
testMedia.setLocalPostId(mPost.getId());
startFailingMediaUpload(testMedia, site);

Expand Down Expand Up @@ -372,7 +373,7 @@ public void testUploadMediaInCancelledPost() throws InterruptedException {
clearMedia(mPost, mUploadStore.getFailedMediaForPost(mPost));

// Upload a new media item to the cancelled post
testMedia = newMediaModel(getSampleImagePath(), "image/jpeg");
testMedia = newMediaModel(getSampleImagePath());
testMedia.setLocalPostId(mPost.getId());
startFailingMediaUpload(testMedia, site);

Expand All @@ -398,7 +399,7 @@ public void testUpdateMediaModelState() throws InterruptedException {
setupPostAttributes();

// Start uploading media
MediaModel testMedia = newMediaModel(getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(getSampleImagePath());
testMedia.setLocalPostId(mPost.getId());
startSuccessfulMediaUpload(testMedia, site);

Expand Down Expand Up @@ -513,31 +514,26 @@ public void onUploadChanged(OnUploadChanged event) {
}
}

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

private MediaModel newMediaModel(String testTitle, String mediaPath, String mimeType) {
private MediaModel newMediaModel(String mediaPath) {
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.setFileExtension(mediaPath.substring(mediaPath.lastIndexOf(".") + 1));
testMedia.setMimeType("image/jpeg");
testMedia.setFileName(mediaPath.substring(mediaPath.lastIndexOf("/")));
testMedia.setTitle("Test Title");
testMedia.setDescription(testDescription);
testMedia.setCaption(testCaption);
testMedia.setAlt(testAlt);

return testMedia;
}

private PostModel createNewPost(SiteModel site) throws InterruptedException {
private void createNewPost(SiteModel site) {
mPost = mPostStore.instantiatePostModel(site, false);
return mPost;
}

private void setupPostAttributes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.wordpress.android.fluxc.generated.SiteActionBuilder;
import org.wordpress.android.fluxc.model.MediaModel;
import org.wordpress.android.fluxc.model.SiteModel;
import org.wordpress.android.fluxc.store.AccountStore;
import org.wordpress.android.fluxc.store.AccountStore.AuthenticatePayload;
import org.wordpress.android.fluxc.store.AccountStore.OnAccountChanged;
import org.wordpress.android.fluxc.store.AccountStore.OnAuthenticationChanged;
Expand All @@ -33,9 +32,9 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@SuppressWarnings("NewClassNamingConvention")
public class ReleaseStack_MediaTestJetpack extends ReleaseStack_Base {
@Inject SiteStore mSiteStore;
@Inject AccountStore mAccountStore;
@Inject MediaStore mMediaStore;

private enum TestEvents {
Expand Down Expand Up @@ -72,7 +71,7 @@ public void testUploadMediaLowFilesizeLimit() throws InterruptedException {
site = mSiteStore.getSites().get(0);

// Attempt to upload an image that exceeds the site's maximum upload_max_filesize or post_max_size
MediaModel testMedia = newMediaModel(site, getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(site, getSampleImagePath());
mNextEvent = TestEvents.ERROR_EXCEEDS_FILESIZE_LIMIT;
uploadMedia(site, testMedia);

Expand All @@ -92,7 +91,7 @@ public void testUploadMediaLowMemoryLimit() throws InterruptedException {
site.setMemoryLimit(1985); // Artificially set the site's memory limit, in bytes

// Attempt to upload an image that exceeds the site's memory limit
MediaModel testMedia = newMediaModel(site, getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(site, getSampleImagePath());
mNextEvent = TestEvents.ERROR_EXCEEDS_MEMORY_LIMIT;
uploadMedia(site, testMedia);

Expand All @@ -116,7 +115,7 @@ public void testUploadMediaLowQuotaAvailableLimit() throws InterruptedException
site.setSpaceUsed(50);

// Attempt to upload an image that exceeds the site's memory limit
MediaModel testMedia = newMediaModel(site, getSampleImagePath(), "image/jpeg");
MediaModel testMedia = newMediaModel(site, getSampleImagePath());
mNextEvent = TestEvents.ERROR_EXCEEDS_SITE_SPACE_QUOTA_LIMIT;
uploadMedia(site, testMedia);

Expand Down Expand Up @@ -148,17 +147,17 @@ public void onMediaUploaded(OnMediaUploaded event) {
}
}

private MediaModel newMediaModel(SiteModel site, String mediaPath, String mimeType) {
private MediaModel newMediaModel(SiteModel site, String mediaPath) {
final String testTitle = "Test Title";
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.setFileExtension(mediaPath.substring(mediaPath.lastIndexOf(".") + 1));
testMedia.setMimeType("image/jpeg");
testMedia.setFileName(mediaPath.substring(mediaPath.lastIndexOf("/")));
testMedia.setTitle(testTitle);
testMedia.setDescription(testDescription);
testMedia.setCaption(testCaption);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static org.junit.Assert.assertTrue;

@SuppressLint("UseSparseArrays")
@SuppressWarnings("NewClassNamingConvention")
public class ReleaseStack_MediaTestWPCom extends ReleaseStack_WPComBase {
@Inject MediaStore mMediaStore;

Expand Down Expand Up @@ -380,10 +381,6 @@ private boolean eventHasKnownImages(OnMediaChanged 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";
Expand All @@ -393,7 +390,7 @@ private MediaModel newMediaModel(String testTitle, String mediaPath, String mime
testMedia.setFileExtension(mediaPath.substring(mediaPath.lastIndexOf(".") + 1));
testMedia.setMimeType(mimeType);
testMedia.setFileName(mediaPath.substring(mediaPath.lastIndexOf("/")));
testMedia.setTitle(testTitle);
testMedia.setTitle("Test Title");
testMedia.setDescription(testDescription);
testMedia.setCaption(testCaption);
testMedia.setAlt(testAlt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.junit.Assert.assertTrue;

@SuppressLint("UseSparseArrays")
@SuppressWarnings("NewClassNamingConvention")
public class ReleaseStack_MediaTestXMLRPC extends ReleaseStack_XMLRPCBase {
@SuppressWarnings("unused")
@Inject AccountStore mAccountStore;
Expand Down Expand Up @@ -403,10 +404,6 @@ public void onMediaListFetched(OnMediaListFetched 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";
Expand All @@ -416,7 +413,7 @@ private MediaModel newMediaModel(String testTitle, String mediaPath, String mime
testMedia.setFileExtension(mediaPath.substring(mediaPath.lastIndexOf(".") + 1));
testMedia.setMimeType(mimeType);
testMedia.setFileName(mediaPath.substring(mediaPath.lastIndexOf("/")));
testMedia.setTitle(testTitle);
testMedia.setTitle("Test Title");
testMedia.setDescription(testDescription);
testMedia.setCaption(testCaption);
testMedia.setAlt(testAlt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.Subscribe;
Expand Down Expand Up @@ -266,8 +267,10 @@ public void onMediaUploaded(OnMediaUploaded event) {
}
}

private void prependToLog(final String s) {
((MainExampleActivity) getActivity()).prependToLog(s);
private void prependToLog(@Nullable final String s) {
if (s != null) {
((MainExampleActivity) getActivity()).prependToLog(s);
}
}

private void fetchMediaList(@NonNull SiteModel site) {
Expand Down
Loading

0 comments on commit b85a542

Please sign in to comment.