Skip to content
Merged
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
@@ -1,5 +1,10 @@
package org.wordpress.android.fluxc.theme;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import android.content.Context;

import com.yarolegovich.wellsql.WellSql;
Expand All @@ -24,10 +29,6 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

@RunWith(RobolectricTestRunner.class)
public class ThemeStoreUnitTest {
private final ThemeStore mThemeStore = new ThemeStore(new Dispatcher(), Mockito.mock(ThemeRestClient.class));
Expand All @@ -44,7 +45,7 @@ public void setUp() {
public void testActiveTheme() throws SiteSqlUtils.DuplicateSiteException {
final SiteModel site = SiteUtils.generateWPComSite();
TestSiteSqlUtils.INSTANCE.getSiteSqlUtils().insertOrUpdateSite(site);
assertNull(mThemeStore.getActiveThemeForSite(site));
assertThat(ThemeSqlUtils.getActiveThemeForSite(site)).isEmpty();

final ThemeModel firstTheme = generateTestTheme(site.getId(), "first-active", "First Active");
final ThemeModel secondTheme = generateTestTheme(site.getId(), "second-active", "Second Active");
Expand Down Expand Up @@ -105,67 +106,30 @@ public void testInsertOrReplaceWpThemes() {

// first add 20 themes and make sure the count is correct
ThemeSqlUtils.insertOrReplaceWpComThemes(firstTestThemes);
assertEquals(20, mThemeStore.getWpComThemes().size());
assertEquals(20, mThemeStore.getWpComThemes(ids(firstTestThemes)).size());

// next add a larger list of themes (with 20 being duplicates) and make sure the count is correct
ThemeSqlUtils.insertOrReplaceWpComThemes(secondTestThemes);
assertEquals(30, mThemeStore.getWpComThemes().size());
assertEquals(30, mThemeStore.getWpComThemes(ids(secondTestThemes)).size());

// lastly add a smaller list of themes (all duplicates) and make sure count is correct
ThemeSqlUtils.insertOrReplaceWpComThemes(thirdTestThemes);
assertEquals(10, mThemeStore.getWpComThemes().size());
}

@Test
public void testInsertOrReplaceInstalledThemes() throws SiteSqlUtils.DuplicateSiteException {
final SiteModel site = SiteUtils.generateJetpackSiteOverRestOnly();
TestSiteSqlUtils.INSTANCE.getSiteSqlUtils().insertOrUpdateSite(site);

final List<ThemeModel> firstTestThemes = generateThemesTestList(5);
final List<ThemeModel> secondTestThemes = generateThemesTestList(10);
final List<ThemeModel> thirdTestThemes = generateThemesTestList(1);

// first add 5 installed themes
ThemeSqlUtils.insertOrReplaceInstalledThemes(site, firstTestThemes);
assertEquals(firstTestThemes.size(), mThemeStore.getThemesForSite(site).size());

// then replace them all with a new list of 10
ThemeSqlUtils.insertOrReplaceInstalledThemes(site, secondTestThemes);
assertEquals(secondTestThemes.size(), mThemeStore.getThemesForSite(site).size());

// then replace them all with a single theme
ThemeSqlUtils.insertOrReplaceInstalledThemes(site, thirdTestThemes);
assertEquals(thirdTestThemes.size(), mThemeStore.getThemesForSite(site).size());
assertEquals(10, mThemeStore.getWpComThemes(ids(thirdTestThemes)).size());
}

@Test
public void testRemoveThemesWithNoSite() {
final List<ThemeModel> testThemes = generateThemesTestList(20);
final List<String> themeIds = ids(testThemes);

// insert and verify count
assertEquals(0, mThemeStore.getWpComThemes().size());
assertEquals(0, mThemeStore.getWpComThemes(themeIds).size());
ThemeSqlUtils.insertOrReplaceWpComThemes(testThemes);
assertEquals(testThemes.size(), mThemeStore.getWpComThemes().size());
assertEquals(testThemes.size(), mThemeStore.getWpComThemes(themeIds).size());

// remove and verify count
ThemeSqlUtils.removeWpComThemes();
assertEquals(0, mThemeStore.getWpComThemes().size());
}

@Test
public void testRemoveInstalledSiteThemes() throws SiteSqlUtils.DuplicateSiteException {
final SiteModel site = SiteUtils.generateJetpackSiteOverRestOnly();
TestSiteSqlUtils.INSTANCE.getSiteSqlUtils().insertOrUpdateSite(site);

final List<ThemeModel> testThemes = generateThemesTestList(5);

// add site themes and verify count
ThemeSqlUtils.insertOrReplaceInstalledThemes(site, testThemes);
assertEquals(testThemes.size(), mThemeStore.getThemesForSite(site).size());

// remove and verify count
ThemeSqlUtils.removeSiteThemes(site);
assertEquals(0, mThemeStore.getThemesForSite(site).size());
assertEquals(0, mThemeStore.getWpComThemes(themeIds).size());
}

private ThemeModel generateTestTheme(int siteId, String themeId, String themeName) {
Expand All @@ -183,4 +147,12 @@ private List<ThemeModel> generateThemesTestList(int num) {
}
return testThemes;
}

private List<String> ids(List<ThemeModel> themes) {
List<String> themeIds = new ArrayList<>();
for (ThemeModel theme : themes) {
themeIds.add(theme.getThemeId());
}
return themeIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import org.wordpress.android.fluxc.annotations.ActionEnum;
import org.wordpress.android.fluxc.annotations.action.IAction;
import org.wordpress.android.fluxc.model.SiteModel;
import org.wordpress.android.fluxc.store.ThemeStore.FetchStarterDesignsPayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchWPComThemesPayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchedCurrentThemePayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchedSiteThemesPayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchedStarterDesignsPayload;
import org.wordpress.android.fluxc.store.ThemeStore.FetchedWpComThemesPayload;
import org.wordpress.android.fluxc.store.ThemeStore.SiteThemePayload;

Expand All @@ -17,36 +14,20 @@ public enum ThemeAction implements IAction {
// Remote actions
@Action(payloadType = FetchWPComThemesPayload.class)
FETCH_WP_COM_THEMES,
@Action(payloadType = FetchStarterDesignsPayload.class)
FETCH_STARTER_DESIGNS,
@Action(payloadType = SiteModel.class)
FETCH_INSTALLED_THEMES, // Jetpack only
@Action(payloadType = SiteModel.class)
FETCH_CURRENT_THEME,
@Action(payloadType = SiteThemePayload.class)
ACTIVATE_THEME,
@Action(payloadType = SiteThemePayload.class)
INSTALL_THEME,
@Action(payloadType = SiteThemePayload.class)
DELETE_THEME,

// Remote responses
@Action(payloadType = FetchedWpComThemesPayload.class)
FETCHED_WP_COM_THEMES,
@Action(payloadType = FetchedStarterDesignsPayload.class)
FETCHED_STARTER_DESIGNS,
@Action(payloadType = FetchedSiteThemesPayload.class)
FETCHED_INSTALLED_THEMES,
@Action(payloadType = FetchedCurrentThemePayload.class)
FETCHED_CURRENT_THEME,
@Action(payloadType = SiteThemePayload.class)
ACTIVATED_THEME,
@Action(payloadType = SiteThemePayload.class)
INSTALLED_THEME,
@Action(payloadType = SiteThemePayload.class)
DELETED_THEME,

// Local actions
@Action(payloadType = SiteModel.class)
REMOVE_SITE_THEMES
INSTALLED_THEME
}
Loading