Skip to content

Commit

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

[Nullability Annotations to Java Classes] Add Missing Nullability Annotations to `Theme` Network Client Classes (`safe`)
  • Loading branch information
Antonis Lilis authored Nov 7, 2023
2 parents 82a701e + 950b8bf commit 7a028a4
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

@SuppressWarnings("NewClassNamingConvention")
public class ReleaseStack_ThemeTestJetpack extends ReleaseStack_Base {
enum TestEvents {
NONE,
Expand Down Expand Up @@ -82,7 +85,7 @@ public void testFetchInstalledThemes() throws InterruptedException {
final SiteModel jetpackSite = signIntoWpComAccountWithJetpackSite();

// verify that installed themes list is empty first
assertTrue(mThemeStore.getThemesForSite(jetpackSite).size() == 0);
assertEquals(0, mThemeStore.getThemesForSite(jetpackSite).size());

// fetch installed themes
fetchInstalledThemes(jetpackSite);
Expand Down Expand Up @@ -252,7 +255,7 @@ public void onCurrentThemeFetched(OnCurrentThemeFetched event) {
throw new AssertionError("Unexpected error occurred with type: " + event.error.type);
}

assertTrue(mNextEvent == TestEvents.FETCHED_CURRENT_THEME);
assertSame(mNextEvent, TestEvents.FETCHED_CURRENT_THEME);
assertNotNull(event.theme);
mCountDownLatch.countDown();
}
Expand All @@ -263,7 +266,7 @@ public void onThemeActivated(OnThemeActivated event) {
if (event.isError()) {
throw new AssertionError("Unexpected error occurred with type: " + event.error.type);
}
assertTrue(mNextEvent == TestEvents.ACTIVATED_THEME);
assertSame(mNextEvent, TestEvents.ACTIVATED_THEME);
mCountDownLatch.countDown();
}

Expand All @@ -273,7 +276,7 @@ public void onThemeInstalled(OnThemeInstalled event) {
if (event.isError()) {
throw new AssertionError("Unexpected error occurred with type: " + event.error.type);
}
assertTrue(mNextEvent == TestEvents.INSTALLED_THEME);
assertSame(mNextEvent, TestEvents.INSTALLED_THEME);
mCountDownLatch.countDown();
}

Expand All @@ -283,7 +286,7 @@ public void onThemeDeleted(OnThemeDeleted event) {
if (event.isError()) {
throw new AssertionError("Unexpected error occurred with type: " + event.error.type);
}
assertTrue(mNextEvent == TestEvents.DELETED_THEME);
assertSame(mNextEvent, TestEvents.DELETED_THEME);
mCountDownLatch.countDown();
}

Expand Down Expand Up @@ -336,18 +339,20 @@ public void onSiteRemoved(OnSiteRemoved event) {

private SiteModel signIntoWpComAccountWithJetpackSite() throws InterruptedException {
// sign into a WP.com account with a Jetpack site
authenticateWPComAndFetchSites(BuildConfig.TEST_WPCOM_USERNAME_SINGLE_JETPACK_ONLY,
BuildConfig.TEST_WPCOM_PASSWORD_SINGLE_JETPACK_ONLY);
authenticateWPComAndFetchSites();

// verify Jetpack site is available
final SiteModel jetpackSite = getJetpackSite();
assertNotNull(jetpackSite);
return jetpackSite;
}

private void authenticateWPComAndFetchSites(String username, String password) throws InterruptedException {
private void authenticateWPComAndFetchSites() throws InterruptedException {
// Authenticate a test user (actual credentials declared in gradle.properties)
AuthenticatePayload payload = new AuthenticatePayload(username, password);
AuthenticatePayload payload = new AuthenticatePayload(
BuildConfig.TEST_WPCOM_USERNAME_SINGLE_JETPACK_ONLY,
BuildConfig.TEST_WPCOM_PASSWORD_SINGLE_JETPACK_ONLY
);
mCountDownLatch = new CountDownLatch(1);

// Correct user we should get an OnAuthenticationChanged message
Expand Down Expand Up @@ -421,7 +426,7 @@ private void deactivateAndDeleteTheme(@NonNull SiteModel jetpackSite, @NonNull T
activateTheme(jetpackSite, otherThemeToActivate);

// Make sure another theme is activated
assertFalse(theme.getThemeId().equals(mThemeStore.getActiveThemeForSite(jetpackSite).getThemeId()));
assertNotEquals(theme.getThemeId(), mThemeStore.getActiveThemeForSite(jetpackSite).getThemeId());
}
// delete existing theme from site
deleteTheme(jetpackSite, theme);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

@SuppressWarnings("NewClassNamingConvention")
public class ReleaseStack_ThemeTestWPCom extends ReleaseStack_WPComBase {
enum TestEvents {
NONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.android.synthetic.main.fragment_themes.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.wordpress.android.fluxc.Dispatcher
import org.wordpress.android.fluxc.example.ui.common.showSiteSelectorDialog
import org.wordpress.android.fluxc.generated.ThemeActionBuilder
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.ThemeModel
Expand All @@ -31,6 +32,9 @@ class ThemeFragment : Fragment() {
@Inject internal lateinit var themeStore: ThemeStore
@Inject internal lateinit var dispatcher: Dispatcher

private var selectedSite: SiteModel? = null
private var selectedPos: Int = -1

override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
Expand All @@ -53,6 +57,20 @@ class ThemeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

theme_select_site.setOnClickListener {
showSiteSelectorDialog(selectedPos, object : SiteSelectorDialog.Listener {
override fun onSiteSelected(site: SiteModel, pos: Int) {
selectedSite = site
selectedPos = pos
val wpcomSite = if (site.isWPCom) "WP" else ""
val jetpackSite = if (site.isJetpackConnected) "JP" else ""
val siteName = site.name ?: site.displayName
val selectedSite = "[$wpcomSite$jetpackSite] $siteName"
theme_selected_site.text = selectedSite
}
})
}

activate_theme_wpcom.setOnClickListener {
val id = getThemeIdFromInput(view)
if (TextUtils.isEmpty(id)) {
Expand Down Expand Up @@ -194,7 +212,7 @@ class ThemeFragment : Fragment() {
if (event.isError) {
prependToLog("error: " + event.error.message)
} else {
prependToLog("success: theme = " + event.theme.name)
prependToLog("success: theme = " + event.theme?.name)
}
}

Expand Down Expand Up @@ -241,10 +259,18 @@ class ThemeFragment : Fragment() {
}

private fun getWpComSite(): SiteModel? {
return siteStore.sites.firstOrNull { it != null && it.isWPCom }
return if (selectedSite?.isWPCom == true) {
selectedSite
} else {
null
}
}

private fun getJetpackConnectedSite(): SiteModel? {
return siteStore.sites.firstOrNull { it != null && it.isJetpackConnected }
return if (selectedSite?.isJetpackConnected == true) {
selectedSite
} else {
null
}
}
}
28 changes: 27 additions & 1 deletion example/src/main/res/layout/fragment_themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@
android:orientation="vertical"
tools:ignore="HardcodedText">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/theme_select_site"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingStart="@dimen/activity_start_end_half_margin"
android:paddingEnd="@dimen/activity_start_end_half_margin"
android:text="Select Site" />

<TextView
android:id="@+id/theme_selected_site"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingStart="@dimen/activity_start_end_half_margin"
android:paddingEnd="@dimen/activity_start_end_half_margin"
android:textAppearance="@style/Base.TextAppearance.Widget.AppCompat.Toolbar.Subtitle"
android:textColor="@android:color/holo_blue_bright" />

</LinearLayout>

<Button
android:id="@+id/fetch_wpcom_themes"
android:layout_width="match_parent"
Expand Down Expand Up @@ -37,7 +63,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the theme's id here"
android:inputType="number" />
android:inputType="text" />

<Button
android:id="@+id/activate_theme_wpcom"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

@RunWith(RobolectricTestRunner.class)
public class ThemeStoreUnitTest {
private ThemeStore mThemeStore = new ThemeStore(new Dispatcher(), Mockito.mock(ThemeRestClient.class));
private final ThemeStore mThemeStore = new ThemeStore(new Dispatcher(), Mockito.mock(ThemeRestClient.class));

@Before
public void setUp() {
Context appContext = RuntimeEnvironment.application.getApplicationContext();
Context appContext = RuntimeEnvironment.getApplication().getApplicationContext();
WellSqlConfig config = new WellSqlConfig(appContext);
WellSql.init(config);
config.reset();
Expand All @@ -56,7 +55,7 @@ public void testActiveTheme() throws SiteSqlUtils.DuplicateSiteException {
mThemeStore.setActiveThemeForSite(site, firstTheme);
List<ThemeModel> activeThemes = ThemeSqlUtils.getActiveThemeForSite(site);
assertNotNull(activeThemes);
assertTrue(activeThemes.size() == 1);
assertEquals(1, activeThemes.size());
ThemeModel firstActiveTheme = activeThemes.get(0);
assertEquals(firstTheme.getThemeId(), firstActiveTheme.getThemeId());
assertEquals(firstTheme.getName(), firstActiveTheme.getName());
Expand All @@ -65,7 +64,7 @@ public void testActiveTheme() throws SiteSqlUtils.DuplicateSiteException {
mThemeStore.setActiveThemeForSite(site, secondTheme);
activeThemes = ThemeSqlUtils.getActiveThemeForSite(site);
assertNotNull(activeThemes);
assertTrue(activeThemes.size() == 1);
assertEquals(1, activeThemes.size());
ThemeModel secondActiveTheme = activeThemes.get(0);
assertEquals(secondTheme.getThemeId(), secondActiveTheme.getThemeId());
assertEquals(secondTheme.getName(), secondActiveTheme.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package org.wordpress.android.fluxc.network.rest.wpcom.theme;

import androidx.annotation.NonNull;

import java.util.List;

@SuppressWarnings("WeakerAccess")
@SuppressWarnings({"WeakerAccess", "NotNullFieldNotInitialized"})
public class JetpackThemeResponse {
public class JetpackThemeListResponse {
public List<JetpackThemeResponse> themes;
public static class JetpackThemeListResponse {
@NonNull public List<JetpackThemeResponse> themes;
public int count;
}

public String id;
public String screenshot;
public String name;
public String theme_uri;
public String description;
public String author;
public String author_uri;
public String version;
@NonNull public String id;
@NonNull public String screenshot;
@NonNull public String name;
@NonNull public String theme_uri;
@NonNull public String description;
@NonNull public String author;
@NonNull public String author_uri;
@NonNull public String version;
public boolean active;
public boolean autoupdate;
public boolean autoupdate_translation;
Expand Down
Loading

0 comments on commit 7a028a4

Please sign in to comment.