Skip to content

Commit

Permalink
Merge pull request #5358 from XiangRongLin/testable_prettytime
Browse files Browse the repository at this point in the history
Make Localization.relativeTime testable
  • Loading branch information
Redirion authored Jan 13, 2021
2 parents e2ea217 + 031585b commit 0264383
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 46 deletions.
37 changes: 16 additions & 21 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDexApplication;
import androidx.preference.PreferenceManager;

import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.CompositeException;
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException;
import io.reactivex.rxjava3.exceptions.UndeliverableException;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.acra.ACRA;
import org.acra.config.ACRAConfigurationException;
import org.acra.config.CoreConfiguration;
Expand All @@ -31,21 +41,6 @@
import org.schabi.newpipe.util.ServiceHelper;
import org.schabi.newpipe.util.StateSaver;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.CompositeException;
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException;
import io.reactivex.rxjava3.exceptions.UndeliverableException;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;

/*
* Copyright (C) Hans-Christoph Steiner 2016 <hans@eds.org>
* App.java is part of NewPipe.
Expand Down Expand Up @@ -93,9 +88,9 @@ public void onCreate() {
SettingsActivity.initSettings(this);

NewPipe.init(getDownloader(),
Localization.getPreferredLocalization(this),
Localization.getPreferredContentCountry(this));
Localization.init(getApplicationContext());
Localization.getPreferredLocalization(this),
Localization.getPreferredContentCountry(this));
Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext()));

StateSaver.init(this);
initNotificationChannels();
Expand Down
16 changes: 6 additions & 10 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package org.schabi.newpipe;

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -41,7 +43,6 @@
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
Expand All @@ -52,9 +53,10 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager;

import com.google.android.material.bottomsheet.BottomSheetBehavior;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.schabi.newpipe.databinding.ActivityMainBinding;
import org.schabi.newpipe.databinding.DrawerHeaderBinding;
import org.schabi.newpipe.databinding.DrawerLayoutBinding;
Expand Down Expand Up @@ -87,12 +89,6 @@
import org.schabi.newpipe.util.ThemeHelper;
import org.schabi.newpipe.views.FocusOverlayView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release");
Expand Down Expand Up @@ -468,7 +464,7 @@ protected void onDestroy() {
protected void onResume() {
assureCorrectAppLanguage(this);
// Change the date format to match the selected language on resume
Localization.init(getApplicationContext());
Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext()));
super.onResume();

// Close drawer on return, and don't show animation,
Expand Down
27 changes: 12 additions & 15 deletions app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import androidx.annotation.NonNull;
import androidx.annotation.PluralsRes;
import androidx.annotation.StringRes;
import androidx.preference.PreferenceManager;

import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.units.Decade;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.ktx.OffsetDateTimeKt;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
Expand All @@ -33,6 +24,12 @@
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.units.Decade;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.ktx.OffsetDateTimeKt;


/*
Expand Down Expand Up @@ -62,10 +59,6 @@ public final class Localization {

private Localization() { }

public static void init(final Context context) {
initPrettyTime(context);
}

@NonNull
public static String concatenateStrings(final String... strings) {
return concatenateStrings(Arrays.asList(strings));
Expand Down Expand Up @@ -307,12 +300,16 @@ public static String localizeDuration(final Context context, final int durationI
// Pretty Time
//////////////////////////////////////////////////////////////////////////*/

private static void initPrettyTime(final Context context) {
prettyTime = new PrettyTime(getAppLocale(context));
public static void initPrettyTime(final PrettyTime time) {
prettyTime = time;
// Do not use decades as YouTube doesn't either.
prettyTime.removeUnit(Decade.class);
}

public static PrettyTime resolvePrettyTime(final Context context) {
return new PrettyTime(getAppLocale(context));
}

public static String relativeTime(final OffsetDateTime offsetDateTime) {
return relativeTime(OffsetDateTimeKt.toCalendar(offsetDateTime));
}
Expand Down
40 changes: 40 additions & 0 deletions app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.schabi.newpipe.util

import org.junit.Assert.assertEquals
import org.junit.Test
import org.ocpsoft.prettytime.PrettyTime
import java.text.SimpleDateFormat
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.GregorianCalendar
import java.util.Locale

class LocalizationTest {

@Test
fun `After initializing pretty time relativeTime() with a Calendar must work`() {
val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1")
Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH))

val actual = Localization.relativeTime(GregorianCalendar(2021, 1, 6))

// yes this assertion is true, even if it should be 5 days, it works as it is. Future research required.
assertEquals("1 month from now", actual)
}

@Test(expected = NullPointerException::class)
fun `relativeTime() must fail without initializing pretty time`() {
Localization.relativeTime(GregorianCalendar(2021, 1, 6))
}

@Test
fun `relativeTime() with a OffsetDateTime must work`() {
val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1")
Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH))

val offset = OffsetDateTime.of(2021, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC)
val actual = Localization.relativeTime(offset)

assertEquals("5 days from now", actual)
}
}

0 comments on commit 0264383

Please sign in to comment.