Support some lbry hosts to deep links. Add unit tests #1008

Merged
kekkyojin merged 3 commits from more-deeplinks into master 2020-10-02 13:34:39 +02:00
3 changed files with 143 additions and 4 deletions

View file

@ -68,6 +68,10 @@
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
<data android:scheme="https" android:host="open.lbry.com"/> <data android:scheme="https" android:host="open.lbry.com"/>
<data android:scheme="https" android:host="lbry.tv"/>
<data android:scheme="https" android:host="lbry.lat"/>
<data android:scheme="https" android:host="lbry.fr"/>
<data android:scheme="https" android:host="lbry.in"/>
<category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>

View file

@ -18,7 +18,7 @@ public class LbryUri {
public static final int CLAIM_ID_MAX_LENGTH = 40; public static final int CLAIM_ID_MAX_LENGTH = 40;
private static final String REGEX_PART_PROTOCOL = "^((?:lbry://|https://)?)"; private static final String REGEX_PART_PROTOCOL = "^((?:lbry://|https://)?)";
private static final String REGEX_PART_HOST = "((?:open.lbry.com/)?)"; private static final String REGEX_PART_HOST = "((?:open.lbry.com/|lbry.tv/|lbry.lat/|lbry.fr/|lbry.in/)?)";
private static final String REGEX_PART_STREAM_OR_CHANNEL_NAME = "([^:$#/]*)"; private static final String REGEX_PART_STREAM_OR_CHANNEL_NAME = "([^:$#/]*)";
private static final String REGEX_PART_MODIFIER_SEPARATOR = "([:$#]?)([^/]*)"; private static final String REGEX_PART_MODIFIER_SEPARATOR = "([:$#]?)([^/]*)";
private static final String QUERY_STRING_BREAKER = "^([\\S]+)([?][\\S]*)"; private static final String QUERY_STRING_BREAKER = "^([\\S]+)([?][\\S]*)";
@ -128,9 +128,14 @@ public class LbryUri {
boolean isChannel = includesChannel && Helper.isNullOrEmpty(possibleStreamName); boolean isChannel = includesChannel && Helper.isNullOrEmpty(possibleStreamName);
String channelName = includesChannel && streamOrChannelName.length() > 1 ? streamOrChannelName.substring(1) : null; String channelName = includesChannel && streamOrChannelName.length() > 1 ? streamOrChannelName.substring(1) : null;
// It would have thrown already on the RegEx parser if protocol value was incorrect /*
// open.lbry.com uses ':' as ModSeparators while lbry:// expects '#' * It would have thrown already on the RegEx parser if protocol value was incorrect
if (components.get(1).equals("open.lbry.com/")) { * or HTTPS host was unknown to us.
*
* [https://] hosts use ':' as ModSeparators while [lbry://] protocol expects '#'
*/
if (!components.get(1).isEmpty()) {
if (primaryModSeparator.equals(":")) if (primaryModSeparator.equals(":"))
primaryModSeparator = "#"; primaryModSeparator = "#";
if (secondaryModSeparator.equals(":")) if (secondaryModSeparator.equals(":"))

View file

@ -0,0 +1,130 @@
package io.lbry.browser.utils;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Test;
import io.lbry.browser.exceptions.LbryUriException;
import static org.junit.Assert.assertEquals;
public class LbryUriTest {
private LbryUri expected;
/*
* Create an LbryUri object and assign fields manually using class methods. This object will be
* compared with LbryUri.parse() returned object on each test.
*/
@Before
public void createExpected() {
expected = new LbryUri();
expected.setChannelName("@lbry");
expected.setStreamName("lbryturns4");
try {
LbryUri.UriModifier primaryMod = LbryUri.UriModifier.parse("#", "3f");
LbryUri.UriModifier secondaryMod = LbryUri.UriModifier.parse("#", "6");
expected.setChannelClaimId(primaryMod.getClaimId());
expected.setStreamClaimId(secondaryMod.getClaimId());
} catch (LbryUriException e) {
e.printStackTrace();
}
}
@Test
public void parseOpenLbryComWithChannel() {
LbryUri obtained = new LbryUri();
try {
obtained = LbryUri.parse("https://open.lbry.com/@lbry:3f/lbryturns4:6",false);
} catch (LbryUriException e) {
e.printStackTrace();
}
assertEquals(expected, obtained);
}
@Test
public void parseLbryTvWithChannel() {
LbryUri obtained = new LbryUri();
try {
obtained = LbryUri.parse("https://lbry.tv/@lbry:3f/lbryturns4:6",false);
} catch (LbryUriException e) {
e.printStackTrace();
}
assertEquals(expected, obtained);
}
@Test
public void parseLbryAlterWithChannel() {
LbryUri obtained = new LbryUri();
try {
obtained = LbryUri.parse("https://lbry.lat/@lbry:3f/lbryturns4:6",false);
} catch (LbryUriException e) {
e.printStackTrace();
}
assertEquals(expected, obtained);
}
@Test
public void parseLbryProtocolWithChannel() {
LbryUri obtained = new LbryUri();
try {
obtained = LbryUri.parse("lbry://@lbry#3f/lbryturns4#6",false);
} catch (LbryUriException e) {
e.printStackTrace();
}
assertEquals(expected, obtained);
}
@Test
public void parseLbryProtocolOnlyChannel() {
LbryUri expectedForChannel = sinthesizeExpected();
LbryUri obtained = new LbryUri();
try {
obtained = LbryUri.parse("lbry://@UCBerkeley#d",false);
} catch (LbryUriException e) {
e.printStackTrace();
}
assertEquals(expectedForChannel, obtained);
}
@Test
public void parseLbryTvProtocolOnlyChannel() {
LbryUri expectedForChannel = sinthesizeExpected();
LbryUri obtained = new LbryUri();
try {
obtained = LbryUri.parse("https://lbry.tv/@UCBerkeley:d",false);
} catch (LbryUriException e) {
e.printStackTrace();
}
assertEquals(expectedForChannel, obtained);
}
@NotNull
private LbryUri sinthesizeExpected() {
LbryUri expectedForChannel = new LbryUri();
expectedForChannel.setChannelName("@UCBerkeley");
expectedForChannel.setChannel(true);
try {
LbryUri.UriModifier primaryMod = LbryUri.UriModifier.parse("#", "d");
expectedForChannel.setChannelClaimId(primaryMod.getClaimId());
} catch (LbryUriException e) {
e.printStackTrace();
}
return expectedForChannel;
}
}