Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.store.SiteStore
import org.wordpress.android.ui.uploads.LocalDraftUploadStarter
import org.wordpress.android.ui.uploads.UploadStarter

@RunWith(AndroidJUnit4::class)
class UploadWorkerTest {
private val localDraftUploadStarter = mock<LocalDraftUploadStarter>()
private val uploadStarter = mock<UploadStarter>()
private val siteStore = mock<SiteStore>()

@Before
Expand All @@ -38,7 +38,7 @@ class UploadWorkerTest {
.setMinimumLoggingLevel(Log.DEBUG)
// Use a SynchronousExecutor here to make it easier to write tests
.setExecutor(SynchronousExecutor())
.setWorkerFactory(UploadWorker.Factory(localDraftUploadStarter, siteStore))
.setWorkerFactory(UploadWorker.Factory(uploadStarter, siteStore))
.build()

// Initialize WorkManager for instrumentation tests.
Expand Down Expand Up @@ -67,7 +67,7 @@ class UploadWorkerTest {
val workInfo = workManager.getWorkInfoById(request.id).get()

// Check the work was successful and the method was called with the right argument
verify(localDraftUploadStarter, times(1)).queueUploadFromSite(eq(site))
verify(uploadStarter, times(1)).queueUploadFromSite(eq(site))
assertThat(workInfo.state, `is`(WorkInfo.State.SUCCEEDED))
}

Expand All @@ -89,7 +89,7 @@ class UploadWorkerTest {
val workInfo = workManager.getWorkInfoById(request.id).get()

// We didn't call setAllConstraintsMet earlier, so the work won't be executed (can't be success or failure)
verifyZeroInteractions(localDraftUploadStarter)
verifyZeroInteractions(uploadStarter)
assertThat(workInfo.state, `is`(WorkInfo.State.ENQUEUED))
}

Expand All @@ -113,7 +113,7 @@ class UploadWorkerTest {
val workInfo = workManager.getWorkInfoById(request.id).get()

// Periodic upload worker will stay enqueued after success/failure: ENQUEUED -> RUNNING -> ENQUEUED
verify(localDraftUploadStarter, times(1)).queueUploadFromAllSites()
verify(uploadStarter, times(1)).queueUploadFromAllSites()
assertThat(workInfo.state, `is`(WorkInfo.State.ENQUEUED))
}

Expand Down Expand Up @@ -142,8 +142,8 @@ class UploadWorkerTest {
testDriver.setPeriodDelayMet(request.id)
operation.result.get()

// Check LocalDraftUploadStarter.queueUploadFromAllSites() was called twice
verify(localDraftUploadStarter, times(2)).queueUploadFromAllSites()
// Check UploadStarter.queueUploadFromAllSites() was called twice
verify(uploadStarter, times(2)).queueUploadFromAllSites()

// WorkRequest should still be queued
val workInfo2 = workManager.getWorkInfoById(request.id).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void onCreate() {
protected void initWorkManager() {
Configuration config = (new Configuration.Builder())
.setMinimumLoggingLevel(Log.DEBUG)
.setWorkerFactory(new UploadWorker.Factory(mLocalDraftUploadStarter, mSiteStore))
.setWorkerFactory(new UploadWorker.Factory(mUploadStarter, mSiteStore))
.build();
WorkManager.initialize(this, config);
}
Expand Down
8 changes: 4 additions & 4 deletions WordPress/src/main/java/org/wordpress/android/WordPress.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
import org.wordpress.android.ui.stats.datasets.StatsDatabaseHelper;
import org.wordpress.android.ui.stats.datasets.StatsTable;
import org.wordpress.android.ui.stats.refresh.lists.widget.WidgetUpdater.StatsWidgetUpdaters;
import org.wordpress.android.ui.uploads.LocalDraftUploadStarter;
import org.wordpress.android.ui.uploads.UploadStarter;
import org.wordpress.android.ui.uploads.UploadService;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.AppLogListener;
Expand Down Expand Up @@ -147,7 +147,7 @@ public class WordPress extends MultiDexApplication implements HasServiceInjector
@Inject SiteStore mSiteStore;
@Inject MediaStore mMediaStore;
@Inject ZendeskHelper mZendeskHelper;
@Inject LocalDraftUploadStarter mLocalDraftUploadStarter;
@Inject UploadStarter mUploadStarter;
@Inject StatsWidgetUpdaters mStatsWidgetUpdaters;

@Inject @Named("custom-ssl") RequestQueue mRequestQueue;
Expand Down Expand Up @@ -285,7 +285,7 @@ public void onLog(T tag, LogLevel logLevel, String message) {
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);

// Make the UploadStarter observe the app process so it can auto-start uploads
mLocalDraftUploadStarter.activateAutoUploading((ProcessLifecycleOwner) ProcessLifecycleOwner.get());
mUploadStarter.activateAutoUploading((ProcessLifecycleOwner) ProcessLifecycleOwner.get());

initAnalytics(SystemClock.elapsedRealtime() - startDate);

Expand Down Expand Up @@ -325,7 +325,7 @@ public void onConnectionSuspended(int i) {
}

protected void initWorkManager() {
UploadWorker.Factory factory = new UploadWorker.Factory(mLocalDraftUploadStarter, mSiteStore);
UploadWorker.Factory factory = new UploadWorker.Factory(mUploadStarter, mSiteStore);
androidx.work.Configuration config =
(new androidx.work.Configuration.Builder()).setWorkerFactory(factory).build();
WorkManager.initialize(this, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import org.wordpress.android.ui.posts.PostListViewLayoutType.STANDARD
import org.wordpress.android.ui.posts.PostListViewLayoutTypeMenuUiState.CompactViewLayoutTypeMenuUiState
import org.wordpress.android.ui.posts.PostListViewLayoutTypeMenuUiState.StandardViewLayoutTypeMenuUiState
import org.wordpress.android.ui.prefs.AppPrefsWrapper
import org.wordpress.android.ui.uploads.LocalDraftUploadStarter
import org.wordpress.android.ui.uploads.UploadStarter
import org.wordpress.android.util.NetworkUtilsWrapper
import org.wordpress.android.util.SiteUtils
import org.wordpress.android.util.ToastUtils.Duration
Expand Down Expand Up @@ -72,7 +72,7 @@ class PostListMainViewModel @Inject constructor(
private val postListEventListenerFactory: PostListEventListener.Factory,
@Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher,
@Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher,
private val localDraftUploadStarter: LocalDraftUploadStarter
private val uploadStarter: UploadStarter
) : ViewModel(), LifecycleOwner, CoroutineScope {
private val lifecycleRegistry = LifecycleRegistry(this)
override fun getLifecycle(): Lifecycle = lifecycleRegistry
Expand Down Expand Up @@ -231,7 +231,7 @@ class PostListMainViewModel @Inject constructor(
)
lifecycleRegistry.markState(Lifecycle.State.STARTED)

localDraftUploadStarter.queueUploadFromSite(site)
uploadStarter.queueUploadFromSite(site)
}

override fun onCleared() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ import javax.inject.Singleton
import kotlin.coroutines.CoroutineContext

/**
* Automatically uploads local drafts.
* Automatically uploads posts and pages with local changes.
*
* Auto-uploads happen when the app is placed in the foreground or when the internet connection is restored. In
* addition to this, call sites can also request an immediate execution by calling [upload].
*
* The method [activateAutoUploading] must be called once, preferably during app creation, for the auto-uploads to work.
*/
@Singleton
open class LocalDraftUploadStarter @Inject constructor(
open class UploadStarter @Inject constructor(
/**
* The Application context
*/
Expand Down Expand Up @@ -76,7 +76,7 @@ open class LocalDraftUploadStarter @Inject constructor(
* This must be called during [org.wordpress.android.WordPress]' creation like so:
*
* ```
* mLocalDraftUploadStarter.activateAutoUploading(ProcessLifecycleOwner.get())
* mUploadStarter.activateAutoUploading(ProcessLifecycleOwner.get())
* ```
*/
fun activateAutoUploading(processLifecycleOwner: ProcessLifecycleOwner) {
Expand Down Expand Up @@ -131,8 +131,8 @@ open class LocalDraftUploadStarter @Inject constructor(
* This is meant to be used by [checkConnectionAndUpload] only.
*/
private suspend fun upload(site: SiteModel) = coroutineScope {
val posts = async { postStore.getLocalDraftPosts(site) }
val pages = async { pageStore.getLocalDraftPages(site) }
val posts = async { postStore.getLocallyChangedPosts(site) }
val pages = async { pageStore.getLocallyChangedPages(site) }

val postsAndPages = posts.await() + pages.await()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import kotlinx.coroutines.runBlocking
import org.wordpress.android.WordPress
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.store.SiteStore
import org.wordpress.android.ui.uploads.LocalDraftUploadStarter
import org.wordpress.android.ui.uploads.UploadStarter
import java.util.concurrent.TimeUnit.HOURS

class UploadWorker(
appContext: Context,
workerParams: WorkerParameters,
private val localDraftUploadStarter: LocalDraftUploadStarter,
private val uploadStarter: UploadStarter,
private val siteStore: SiteStore
) : Worker(appContext, workerParams) {
companion object {
Expand All @@ -35,16 +35,16 @@ class UploadWorker(
override fun doWork(): Result {
runBlocking {
val job = when (val localSiteId = inputData.getInt(WordPress.LOCAL_SITE_ID, UPLOAD_FROM_ALL_SITES)) {
UPLOAD_FROM_ALL_SITES -> localDraftUploadStarter.queueUploadFromAllSites()
else -> siteStore.getSiteByLocalId(localSiteId)?.let { localDraftUploadStarter.queueUploadFromSite(it) }
UPLOAD_FROM_ALL_SITES -> uploadStarter.queueUploadFromAllSites()
else -> siteStore.getSiteByLocalId(localSiteId)?.let { uploadStarter.queueUploadFromSite(it) }
}
job?.join()
}
return Result.success()
}

class Factory(
private val localDraftUploadStarter: LocalDraftUploadStarter,
private val uploadStarter: UploadStarter,
private val siteStore: SiteStore
) : WorkerFactory() {
override fun createWorker(
Expand All @@ -53,7 +53,7 @@ class UploadWorker(
workerParameters: WorkerParameters
): ListenableWorker? {
// TODO This should use the [workerClassName] if there are other of Worker subclasses in the project
return UploadWorker(appContext, workerParameters, localDraftUploadStarter, siteStore)
return UploadWorker(appContext, workerParameters, uploadStarter, siteStore)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import org.wordpress.android.ui.pages.PageItem.Action.SET_PARENT
import org.wordpress.android.ui.pages.PageItem.Action.VIEW_PAGE
import org.wordpress.android.ui.pages.PageItem.Page
import org.wordpress.android.ui.pages.SnackbarMessageHolder
import org.wordpress.android.ui.uploads.LocalDraftUploadStarter
import org.wordpress.android.ui.uploads.UploadStarter
import org.wordpress.android.ui.uploads.PostEvents
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.EventBusWrapper
Expand Down Expand Up @@ -76,7 +76,7 @@ class PagesViewModel
private val dispatcher: Dispatcher,
private val actionPerfomer: ActionPerformer,
private val networkUtils: NetworkUtilsWrapper,
private val localDraftUploadStarter: LocalDraftUploadStarter,
private val uploadStarter: UploadStarter,
private val eventBusWrapper: EventBusWrapper,
@Named(UI_THREAD) private val uiDispatcher: CoroutineDispatcher,
@Named(BG_THREAD) private val defaultDispatcher: CoroutineDispatcher
Expand Down Expand Up @@ -155,7 +155,7 @@ class PagesViewModel

loadPagesAsync()

localDraftUploadStarter.queueUploadFromSite(site)
uploadStarter.queueUploadFromSite(site)
}
}

Expand Down Expand Up @@ -421,7 +421,7 @@ class PagesViewModel
}

fun onPullToRefresh() {
localDraftUploadStarter.queueUploadFromSite(site)
uploadStarter.queueUploadFromSite(site)

launch {
reloadPages(FETCHING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.wordpress.android.ui.posts.AuthorFilterSelection.ME
import org.wordpress.android.ui.posts.PostListType.SEARCH
import org.wordpress.android.ui.posts.PostUtils
import org.wordpress.android.ui.posts.trackPostListAction
import org.wordpress.android.ui.uploads.LocalDraftUploadStarter
import org.wordpress.android.ui.uploads.UploadStarter
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.NetworkUtilsWrapper
import org.wordpress.android.util.SiteUtils
Expand Down Expand Up @@ -62,7 +62,7 @@ class PostListViewModel @Inject constructor(
private val accountStore: AccountStore,
private val listItemUiStateHelper: PostListItemUiStateHelper,
private val networkUtilsWrapper: NetworkUtilsWrapper,
private val localDraftUploadStarter: LocalDraftUploadStarter,
private val uploadStarter: UploadStarter,
@Named(UI_THREAD) private val uiDispatcher: CoroutineDispatcher,
@Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher,
connectionStatus: LiveData<ConnectionStatus>
Expand Down Expand Up @@ -285,7 +285,7 @@ class PostListViewModel @Inject constructor(
// Public Methods

fun swipeToRefresh() {
localDraftUploadStarter.queueUploadFromSite(connector.site)
uploadStarter.queueUploadFromSite(connector.site)
fetchFirstPage()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.ui.posts.PostListViewLayoutType.COMPACT
import org.wordpress.android.ui.posts.PostListViewLayoutType.STANDARD
import org.wordpress.android.ui.prefs.AppPrefsWrapper
import org.wordpress.android.ui.uploads.LocalDraftUploadStarter
import org.wordpress.android.ui.uploads.UploadStarter

class PostListMainViewModelTest : BaseUnitTest() {
lateinit var site: SiteModel
@Mock lateinit var localDraftUploadStarter: LocalDraftUploadStarter
@Mock lateinit var uploadStarter: UploadStarter
private lateinit var viewModel: PostListMainViewModel

@UseExperimental(ExperimentalCoroutinesApi::class)
Expand All @@ -43,15 +43,15 @@ class PostListMainViewModelTest : BaseUnitTest() {
mainDispatcher = Dispatchers.Unconfined,
bgDispatcher = Dispatchers.Unconfined,
postListEventListenerFactory = mock(),
localDraftUploadStarter = localDraftUploadStarter
uploadStarter = uploadStarter
)
}

@Test
fun `when started, it uploads all local drafts`() {
viewModel.start(site)

verify(localDraftUploadStarter, times(1)).queueUploadFromSite(eq(site))
verify(uploadStarter, times(1)).queueUploadFromSite(eq(site))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import org.wordpress.android.ui.posts.PostUtilsWrapper
import org.wordpress.android.util.NetworkUtilsWrapper

/**
* Tests for structured concurrency in [LocalDraftUploadStarter].
* Tests for structured concurrency in [UploadStarter].
*
* This is intentionally a separate class from [LocalDraftUploadStarterTest] because this contains non-deterministic
* This is intentionally a separate class from [UploadStarterTest] because this contains non-deterministic
* tests.
*/
@RunWith(MockitoJUnitRunner::class)
class LocalDraftUploadStarterConcurrentTest {
class UploadStarterConcurrentTest {
@get:Rule val rule = InstantTaskExecutorRule()

private val site = SiteModel()
Expand All @@ -40,18 +40,18 @@ class LocalDraftUploadStarterConcurrentTest {
)

private val postStore = mock<PostStore> {
on { getLocalDraftPosts(eq(site)) } doReturn posts
on { getLocallyChangedPosts(eq(site)) } doReturn posts
}
private val pageStore = mock<PageStore> {
onBlocking { getLocalDraftPages(any()) } doReturn emptyList()
onBlocking { getLocallyChangedPages(any()) } doReturn emptyList()
}

@Test
fun `it uploads local drafts concurrently`() {
// Given
val uploadServiceFacade = createMockedUploadServiceFacade()

val starter = createLocalDraftUploadStarter(uploadServiceFacade)
val starter = createUploadStarter(uploadServiceFacade)

// When
runBlocking {
Expand All @@ -68,7 +68,7 @@ class LocalDraftUploadStarterConcurrentTest {
)
}

private fun createLocalDraftUploadStarter(uploadServiceFacade: UploadServiceFacade) = LocalDraftUploadStarter(
private fun createUploadStarter(uploadServiceFacade: UploadServiceFacade) = UploadStarter(
context = mock(),
postStore = postStore,
pageStore = pageStore,
Expand Down
Loading