Skip to content

Commit

Permalink
#32 Added unit tests for OldNovelContentParser and UserInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
MewX committed Jul 24, 2018
1 parent 465ccb9 commit c99adfc
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -35,5 +35,5 @@ before_install:
- android-wait-for-emulator
- adb shell input keyevent 82 &

script: ./gradlew build connectedAlphaDebugAndroidTest coveralls
script: ./gradlew assembleAlpha connectedAlphaDebugAndroidTest coveralls

@@ -0,0 +1,57 @@
package org.mewx.wenku8.global.api;

import org.junit.Test;

import java.util.List;

import static org.mewx.wenku8.global.api.OldNovelContentParser.NovelContentType.*;

import static org.junit.Assert.assertEquals;

public class OldNovelContentParserTest {
private static final String NOVEL_CONTENT = "line 1\r\n" +
" <!--image-->http://bbbbb.com/pictures/1/1339/90903/107724.jpg<!--image--> \r\n" +
" line 2 \r\n\r\n" +
"<!--image-->http://bbbbb.com/pictures/1/1339/90903/107725.jpg<!--image-->\r\n" +
"line 3\r\n";

@Test
public void parseNovelContent() {
List<OldNovelContentParser.NovelContent> contents = OldNovelContentParser.parseNovelContent(NOVEL_CONTENT, null);
assertEquals(5, contents.size());

OldNovelContentParser.NovelContent tempContent = contents.get(0);
assertEquals(TEXT, tempContent.type);
assertEquals("line 1", tempContent.content);

tempContent = contents.get(1);
assertEquals(IMAGE, tempContent.type);
assertEquals("http://bbbbb.com/pictures/1/1339/90903/107724.jpg", tempContent.content);

tempContent = contents.get(2);
assertEquals(TEXT, tempContent.type);
assertEquals("line 2", tempContent.content);

tempContent = contents.get(3);
assertEquals(IMAGE, tempContent.type);
assertEquals("http://bbbbb.com/pictures/1/1339/90903/107725.jpg", tempContent.content);

tempContent = contents.get(4);
assertEquals(TEXT, tempContent.type);
assertEquals("line 3", tempContent.content);
}

@Test
public void novelContentParser_onlyImage() {
List<OldNovelContentParser.NovelContent> contents = OldNovelContentParser.NovelContentParser_onlyImage(NOVEL_CONTENT);
assertEquals(2, contents.size());

OldNovelContentParser.NovelContent tempContent = contents.get(0);
assertEquals(IMAGE, tempContent.type);
assertEquals("http://bbbbb.com/pictures/1/1339/90903/107724.jpg", tempContent.content);

tempContent = contents.get(1);
assertEquals(IMAGE, tempContent.type);
assertEquals("http://bbbbb.com/pictures/1/1339/90903/107725.jpg", tempContent.content);
}
}
@@ -0,0 +1,36 @@
package org.mewx.wenku8.global.api;

import android.support.test.filters.SmallTest;

import org.junit.Test;

import static org.junit.Assert.*;

@SmallTest
public class UserInfoTest {
private static final String USER_INFO_XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<metadata>\n" +
"<item name=\"uname\"><![CDATA[apptest]]></item>\n" +
"<item name=\"nickname\"><![CDATA[apptest nick]]></item>\n" +
"<item name=\"score\">100</item>\n" +
"<item name=\"experience\">10</item>\n" +
"<item name=\"rank\"><![CDATA[新手上路]]></item>\n" +
"</metadata>";

@Test
public void parseUserInfo() {
UserInfo ui = UserInfo.parseUserInfo(USER_INFO_XML);
assertNotNull(ui);
assertEquals("apptest", ui.username);
assertEquals("apptest nick", ui.nickyname);
assertEquals(10, ui.experience);
assertEquals(100, ui.score);
assertEquals("新手上路", ui.rank);
}

@Test
public void parseInvalidUserInfo() {
UserInfo ui = UserInfo.parseUserInfo("adfsdfasdfasdf");
assertNull(ui);
}
}
Expand Up @@ -37,6 +37,7 @@
import org.mewx.wenku8.global.api.ChapterInfo;
import org.mewx.wenku8.global.api.NovelItemMeta;
import org.mewx.wenku8.global.api.OldNovelContentParser;
import org.mewx.wenku8.global.api.OldNovelContentParser.NovelContentType;
import org.mewx.wenku8.global.api.VolumeList;
import org.mewx.wenku8.global.api.Wenku8API;
import org.mewx.wenku8.global.api.Wenku8Error;
Expand Down Expand Up @@ -861,10 +862,9 @@ protected Wenku8Error.ErrorCode doInBackground(Integer... params) {
// cache image
if (GlobalConfig.doCacheImage()) {
List<OldNovelContentParser.NovelContent> nc = OldNovelContentParser.NovelContentParser_onlyImage(xml);
if (nc == null) return Wenku8Error.ErrorCode.NETWORK_ERROR;

for (int i = 0; i < nc.size(); i++) {
if (nc.get(i).type == 'i') {
if (nc.get(i).type == NovelContentType.IMAGE) {
pDialog.setMaxProgress(++size_a);

// save this images, judge exist first
Expand Down Expand Up @@ -972,7 +972,7 @@ protected Wenku8Error.ErrorCode doInBackground(Integer... params) {
String result;
try {
result = new String(bytes, "UTF-8");
Log.e("MewX", result);
Log.d("MewX", result);
if (!LightTool.isInteger(result))
return Wenku8Error.ErrorCode.RETURNED_VALUE_EXCEPTION;
if(Wenku8Error.getSystemDefinedErrorCode(Integer.parseInt(result)) != Wenku8Error.ErrorCode.SYSTEM_1_SUCCEEDED
Expand Down Expand Up @@ -1075,10 +1075,9 @@ protected Wenku8Error.ErrorCode doInBackground(Integer[]... params) {
// cache image
if (GlobalConfig.doCacheImage()) {
List<OldNovelContentParser.NovelContent> nc = OldNovelContentParser.NovelContentParser_onlyImage(xml);
if (nc == null) return Wenku8Error.ErrorCode.NETWORK_ERROR;

for (int i = 0; i < nc.size(); i++) {
if (nc.get(i).type == 'i') {
if (nc.get(i).type == NovelContentType.IMAGE) {
size_a ++;

// save this images, judge exist first
Expand Down
Expand Up @@ -55,7 +55,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.layout_account_info);

// set indicator enable
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
Toolbar mToolbar = findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Expand Down Expand Up @@ -85,13 +85,13 @@ protected void onCreate(Bundle savedInstanceState) {
}

// get views
rivAvatar = (RoundedImageView)findViewById(R.id.user_avatar);
tvUserName = (TextView)findViewById(R.id.username);
tvNickyName = (TextView)findViewById(R.id.nickname);
tvScore = (TextView)findViewById(R.id.score);
tvExperience = (TextView)findViewById(R.id.experience);
tvRank = (TextView)findViewById(R.id.rank);
tvLogout = (TextView)findViewById(R.id.btn_logout);
rivAvatar = findViewById(R.id.user_avatar);
tvUserName = findViewById(R.id.username);
tvNickyName = findViewById(R.id.nickname);
tvScore = findViewById(R.id.score);
tvExperience = findViewById(R.id.experience);
tvRank = findViewById(R.id.rank);
tvLogout = findViewById(R.id.btn_logout);

// sync get info
agui = new AsyncGetUserInfo();
Expand Down Expand Up @@ -154,7 +154,7 @@ else if(Wenku8Error.getSystemDefinedErrorCode(Integer.valueOf(new String(b))) ==
}

Log.e("MewX", xml);
ui = UserInfo.parseUserInfo(new UserInfo(), xml);
ui = UserInfo.parseUserInfo(xml);
if(ui == null) return Wenku8Error.ErrorCode.XML_PARSE_FAILED;

return Wenku8Error.ErrorCode.SYSTEM_1_SUCCEEDED;
Expand Down
Expand Up @@ -242,7 +242,7 @@ protected void onPostExecute(Integer result) {
pDialog.setProgress(i);

switch (nc.get(i).type) {
case 't':
case TEXT:
TextView tempTV = new TextView(VerticalReaderActivity.this);
if (i == 0) {
tempTV.setTextSize(TypedValue.COMPLEX_UNIT_SP,
Expand All @@ -264,7 +264,7 @@ protected void onPostExecute(Integer result) {
TextListLayout.addView(tempTV);
break;

case 'i':
case IMAGE:
final ImageView tempIV = new ImageView(VerticalReaderActivity.this);
tempIV.setClickable(true);
tempIV.setAdjustViewBounds(true);
Expand Down
@@ -1,12 +1,11 @@
package org.mewx.wenku8.global.api;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;

import org.mewx.wenku8.MyApp;

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

Expand All @@ -15,12 +14,19 @@
* Old Novel Content Parser.
*/
public class OldNovelContentParser {
private static final String TAG = OldNovelContentParser.class.getSimpleName();

public enum NovelContentType {
TEXT, IMAGE
}

public static class NovelContent {
public char type = 't'; // 't' - text (default); 'i' - img
public NovelContentType type = NovelContentType.TEXT;
public String content = "";
}

public static List<NovelContent> parseNovelContent(String raw, MaterialDialog pDialog) {
@NonNull
public static List<NovelContent> parseNovelContent(@NonNull String raw, @Nullable MaterialDialog pDialog) {
List<NovelContent> result = new ArrayList<>();

// use split
Expand All @@ -42,15 +48,15 @@ public static List<NovelContent> parseNovelContent(String raw, MaterialDialog pD
temp = t.indexOf("<!--image-->", 0);
if (temp == -1) {
NovelContent nc = new NovelContent();
nc.type = 't';
nc.type = NovelContentType.TEXT;
nc.content = t.trim(); //.replaceAll("[ | ]", " ").trim();
result.add(nc);

// update progress
if (pDialog != null)
pDialog.setMaxProgress(result.size());
} else {
Log.v("MewX", "img index = " + temp);
Log.d(TAG, "img index = " + temp);
// nc.content = nc.content.substring(temp + 12,
// nc.content.indexOf("<!--image-->", temp + 12));

Expand All @@ -64,16 +70,15 @@ public static List<NovelContent> parseNovelContent(String raw, MaterialDialog pD
NovelContent nc2 = new NovelContent();
int t2 = t.indexOf("<!--image-->", temp + 12);
if (t2 < 0) {
Log.v("MewX", "Breaked in parseNovelContent, t2 = "
+ t2);
Log.d(TAG, "Broke in parseNovelContent, t2 = " + t2);
NovelContent nc = new NovelContent();
nc.type = 't';
nc.type = NovelContentType.TEXT;
nc.content = t;
result.add(nc);
break;
}
nc2.content = t.substring(temp + 12, t2);
nc2.type = 'i';
nc2.type = NovelContentType.IMAGE;
result.add(nc2);
temp = t2 + 12;

Expand All @@ -89,7 +94,8 @@ public static List<NovelContent> parseNovelContent(String raw, MaterialDialog pD

}

public static List<NovelContent> NovelContentParser_onlyImage(String raw) {
@NonNull
public static List<NovelContent> NovelContentParser_onlyImage(@NonNull String raw) {
List<NovelContent> result = new ArrayList<>();

// use split
Expand All @@ -99,7 +105,7 @@ public static List<NovelContent> NovelContentParser_onlyImage(String raw) {
// test
temp = t.indexOf("<!--image-->", 0);
if (temp != -1) {
Log.v("MewX", "img index = " + temp);
Log.d(TAG, "img index = " + temp);
// nc.content = nc.content.substring(temp + 12,
// nc.content.indexOf("<!--image-->", temp + 12));

Expand All @@ -113,13 +119,11 @@ public static List<NovelContent> NovelContentParser_onlyImage(String raw) {
NovelContent nc2 = new NovelContent();
int t2 = t.indexOf("<!--image-->", temp + 12);
if (t2 < 0) {
Log.v("MewX",
"Breaked in NovelContentParser_onlyImage, t2 = "
+ t2);
Log.d(TAG, "Breaked in NovelContentParser_onlyImage, t2 = " + t2);
break;
}
nc2.content = t.substring(temp + 12, t2);
nc2.type = 'i';
nc2.type = NovelContentType.IMAGE;
result.add(nc2);
temp = t2 + 12;

Expand Down
@@ -1,5 +1,7 @@
package org.mewx.wenku8.global.api;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import org.mewx.wenku8.global.GlobalConfig;
Expand All @@ -13,7 +15,7 @@
* User Info.
*/
public class UserInfo {
/**
/*
* <?xml version="1.0" encoding="utf-8"?>
* <metadata>
* <item name="uname"><![CDATA[apptest]]></item>
Expand All @@ -30,9 +32,10 @@ public class UserInfo {
public int experience; // 经验值
public String rank;

public static UserInfo parseUserInfo(UserInfo ui, String xml) {

@Nullable
public static UserInfo parseUserInfo(@NonNull String xml) {
try {
UserInfo ui = new UserInfo();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xml));
Expand All @@ -48,23 +51,19 @@ public static UserInfo parseUserInfo(UserInfo ui, String xml) {
// root tag
break;
} else if ("item".equals(xmlPullParser.getName())) {
if("uname".equals(xmlPullParser.getAttributeValue(0))) {
if ("uname".equals(xmlPullParser.getAttributeValue(0))) {
ui.username = xmlPullParser.nextText();
Log.e("MewX", ui.username.length() == 0 ? GlobalConfig.UNKNOWN : ui.username);
}
else if("nickname".equals(xmlPullParser.getAttributeValue(0))) {
} else if ("nickname".equals(xmlPullParser.getAttributeValue(0))) {
ui.nickyname = xmlPullParser.nextText();
Log.e("MewX", ui.nickyname.length() == 0 ? GlobalConfig.UNKNOWN : ui.nickyname);
}
else if("score".equals(xmlPullParser.getAttributeValue(0))) {
} else if ("score".equals(xmlPullParser.getAttributeValue(0))) {
ui.score = Integer.valueOf(xmlPullParser.nextText());
Log.e("MewX", "score:" + ui.score);
}
else if("experience".equals(xmlPullParser.getAttributeValue(0))) {
} else if ("experience".equals(xmlPullParser.getAttributeValue(0))) {
ui.experience = Integer.valueOf(xmlPullParser.nextText());
Log.e("MewX", "experience:" + ui.experience);
}
else if("rank".equals(xmlPullParser.getAttributeValue(0))) {
} else if ("rank".equals(xmlPullParser.getAttributeValue(0))) {
ui.rank = xmlPullParser.nextText();
Log.e("MewX", ui.rank.length() == 0 ? GlobalConfig.UNKNOWN : ui.rank);
}
Expand Down

0 comments on commit c99adfc

Please sign in to comment.