From c99adfc29fb0afbef1d7131fb9641f0f59ee88f5 Mon Sep 17 00:00:00 2001 From: MewX Date: Tue, 24 Jul 2018 20:48:15 +0800 Subject: [PATCH] #32 Added unit tests for OldNovelContentParser and UserInfo --- .travis.yml | 2 +- .../global/api/OldNovelContentParserTest.java | 57 +++++++++++++++++++ .../mewx/wenku8/global/api/UserInfoTest.java | 36 ++++++++++++ .../wenku8/activity/NovelInfoActivity.java | 9 ++- .../wenku8/activity/UserInfoActivity.java | 18 +++--- .../activity/VerticalReaderActivity.java | 4 +- .../global/api/OldNovelContentParser.java | 38 +++++++------ .../org/mewx/wenku8/global/api/UserInfo.java | 23 ++++---- .../mewx/wenku8/global/api/VolumeList.java | 11 ++-- .../reader/loader/WenkuReaderLoaderXML.java | 17 +++--- .../org/mewx/wenku8/util/LightNetwork.java | 8 --- 11 files changed, 154 insertions(+), 69 deletions(-) create mode 100644 studio-android/LightNovelLibrary/app/src/androidTest/java/org/mewx/wenku8/global/api/OldNovelContentParserTest.java create mode 100644 studio-android/LightNovelLibrary/app/src/androidTest/java/org/mewx/wenku8/global/api/UserInfoTest.java diff --git a/.travis.yml b/.travis.yml index 4be9eecf..6211b21f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/studio-android/LightNovelLibrary/app/src/androidTest/java/org/mewx/wenku8/global/api/OldNovelContentParserTest.java b/studio-android/LightNovelLibrary/app/src/androidTest/java/org/mewx/wenku8/global/api/OldNovelContentParserTest.java new file mode 100644 index 00000000..a41e7a29 --- /dev/null +++ b/studio-android/LightNovelLibrary/app/src/androidTest/java/org/mewx/wenku8/global/api/OldNovelContentParserTest.java @@ -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" + + " http://bbbbb.com/pictures/1/1339/90903/107724.jpg \r\n" + + " line 2 \r\n\r\n" + + "http://bbbbb.com/pictures/1/1339/90903/107725.jpg\r\n" + + "line 3\r\n"; + + @Test + public void parseNovelContent() { + List 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 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); + } +} \ No newline at end of file diff --git a/studio-android/LightNovelLibrary/app/src/androidTest/java/org/mewx/wenku8/global/api/UserInfoTest.java b/studio-android/LightNovelLibrary/app/src/androidTest/java/org/mewx/wenku8/global/api/UserInfoTest.java new file mode 100644 index 00000000..e1d2d058 --- /dev/null +++ b/studio-android/LightNovelLibrary/app/src/androidTest/java/org/mewx/wenku8/global/api/UserInfoTest.java @@ -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 = "\n" + + "\n" + + "\n" + + "\n" + + "100\n" + + "10\n" + + "\n" + + ""; + + @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); + } +} \ No newline at end of file diff --git a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/NovelInfoActivity.java b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/NovelInfoActivity.java index 9bd4ef27..2a04ded0 100644 --- a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/NovelInfoActivity.java +++ b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/NovelInfoActivity.java @@ -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; @@ -861,10 +862,9 @@ protected Wenku8Error.ErrorCode doInBackground(Integer... params) { // cache image if (GlobalConfig.doCacheImage()) { List 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 @@ -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 @@ -1075,10 +1075,9 @@ protected Wenku8Error.ErrorCode doInBackground(Integer[]... params) { // cache image if (GlobalConfig.doCacheImage()) { List 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 diff --git a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/UserInfoActivity.java b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/UserInfoActivity.java index 482b1b0c..aa1c4a1a 100644 --- a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/UserInfoActivity.java +++ b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/UserInfoActivity.java @@ -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); @@ -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(); @@ -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; diff --git a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/VerticalReaderActivity.java b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/VerticalReaderActivity.java index 1fd66c6d..716a90e9 100644 --- a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/VerticalReaderActivity.java +++ b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/activity/VerticalReaderActivity.java @@ -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, @@ -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); diff --git a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/OldNovelContentParser.java b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/OldNovelContentParser.java index 9b07f378..eaba5bcf 100644 --- a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/OldNovelContentParser.java +++ b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/OldNovelContentParser.java @@ -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; @@ -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 parseNovelContent(String raw, MaterialDialog pDialog) { + @NonNull + public static List parseNovelContent(@NonNull String raw, @Nullable MaterialDialog pDialog) { List result = new ArrayList<>(); // use split @@ -42,7 +48,7 @@ public static List parseNovelContent(String raw, MaterialDialog pD temp = t.indexOf("", 0); if (temp == -1) { NovelContent nc = new NovelContent(); - nc.type = 't'; + nc.type = NovelContentType.TEXT; nc.content = t.trim(); //.replaceAll("[ | ]", " ").trim(); result.add(nc); @@ -50,7 +56,7 @@ public static List parseNovelContent(String raw, MaterialDialog pD 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("", temp + 12)); @@ -64,16 +70,15 @@ public static List parseNovelContent(String raw, MaterialDialog pD NovelContent nc2 = new NovelContent(); int t2 = t.indexOf("", 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; @@ -89,7 +94,8 @@ public static List parseNovelContent(String raw, MaterialDialog pD } - public static List NovelContentParser_onlyImage(String raw) { + @NonNull + public static List NovelContentParser_onlyImage(@NonNull String raw) { List result = new ArrayList<>(); // use split @@ -99,7 +105,7 @@ public static List NovelContentParser_onlyImage(String raw) { // test temp = t.indexOf("", 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("", temp + 12)); @@ -113,13 +119,11 @@ public static List NovelContentParser_onlyImage(String raw) { NovelContent nc2 = new NovelContent(); int t2 = t.indexOf("", 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; diff --git a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/UserInfo.java b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/UserInfo.java index 174e384a..dfd32b23 100644 --- a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/UserInfo.java +++ b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/UserInfo.java @@ -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; @@ -13,7 +15,7 @@ * User Info. */ public class UserInfo { - /** + /* * * * @@ -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)); @@ -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); } diff --git a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/VolumeList.java b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/VolumeList.java index 231a1c34..48fb4892 100644 --- a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/VolumeList.java +++ b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/global/api/VolumeList.java @@ -2,11 +2,9 @@ import org.mewx.wenku8.global.GlobalConfig; import org.mewx.wenku8.util.LightCache; -import org.mewx.wenku8.util.LightNetwork; import java.io.File; import java.io.Serializable; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; @@ -23,19 +21,18 @@ public class VolumeList implements Serializable { public void cleanLocalCache() { for (ChapterInfo tempCi : this.chapterList) { String xml = GlobalConfig.loadFullFileFromSaveFolder("novel", tempCi.cid + ".xml"); - if (xml == null || xml.length() == 0) { + if (xml.length() == 0) { return; } List nc = OldNovelContentParser.NovelContentParser_onlyImage(xml); - if (nc == null) return; for (int i = 0; i < nc.size(); i++) { - if (nc.get(i).type == 'i'){ + if (nc.get(i).type == OldNovelContentParser.NovelContentType.IMAGE) { String imgFileName = GlobalConfig.generateImageFileNameByURL(nc.get(i).content); LightCache.deleteFile( - GlobalConfig.getFirstFullSaveFilePath()+ + GlobalConfig.getFirstFullSaveFilePath() + GlobalConfig.imgsSaveFolderName + File.separator + imgFileName); LightCache.deleteFile( - GlobalConfig.getSecondFullSaveFilePath()+ + GlobalConfig.getSecondFullSaveFilePath() + GlobalConfig.imgsSaveFolderName + File.separator + imgFileName); } } diff --git a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/reader/loader/WenkuReaderLoaderXML.java b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/reader/loader/WenkuReaderLoaderXML.java index 14701455..164513c3 100644 --- a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/reader/loader/WenkuReaderLoaderXML.java +++ b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/reader/loader/WenkuReaderLoaderXML.java @@ -5,6 +5,7 @@ import org.mewx.wenku8.global.GlobalConfig; import org.mewx.wenku8.global.api.OldNovelContentParser; +import org.mewx.wenku8.global.api.OldNovelContentParser.NovelContentType; import java.util.List; @@ -43,11 +44,11 @@ public boolean hasNext(int wordIndex) { } else { // last one - if(nc.get(currentIndex).type == 't' && wordIndex + 1 < nc.get(currentIndex).content.length()) { + if(nc.get(currentIndex).type == NovelContentType.TEXT && wordIndex + 1 < nc.get(currentIndex).content.length()) { // text but not last word return true; } - else if(nc.get(currentIndex).type != 't' && wordIndex == 0) { + else if(nc.get(currentIndex).type != NovelContentType.TEXT && wordIndex == 0) { // image return true; } @@ -66,11 +67,11 @@ public boolean hasPrevious(int wordIndex) { } else { // first one - if(nc.get(currentIndex).type == 't' && wordIndex - 1 >= 0) { + if(nc.get(currentIndex).type == NovelContentType.TEXT && wordIndex - 1 >= 0) { // one more word ahead return true; } - else if(nc.get(currentIndex).type != 't' && wordIndex == nc.get(currentIndex).content.length() - 1) + else if(nc.get(currentIndex).type != NovelContentType.TEXT && wordIndex == nc.get(currentIndex).content.length() - 1) // image previous use index last return true; } @@ -232,10 +233,10 @@ public void closeLoader() { nc = null; } - private ElementType intepreteOldSign(char s) { - switch (s) { - case 't': return ElementType.TEXT; - case 'i': return ElementType.IMAGE_DEPENDENT; + private ElementType intepreteOldSign(NovelContentType type) { + switch (type) { + case TEXT: return ElementType.TEXT; + case IMAGE: return ElementType.IMAGE_DEPENDENT; default: return ElementType.TEXT; } } diff --git a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/util/LightNetwork.java b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/util/LightNetwork.java index 786b41d4..d16d60b7 100644 --- a/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/util/LightNetwork.java +++ b/studio-android/LightNovelLibrary/app/src/main/java/org/mewx/wenku8/util/LightNetwork.java @@ -55,11 +55,6 @@ public static String encodeToHttp(String str) { */ @Nullable public static byte[] LightHttpPostConnection(String u, ContentValues values) { - - // a replacer -// u = u.replace(fromEle, toEle); -// long start = System.currentTimeMillis(); - // new API, initial URL url; HttpURLConnection http; @@ -116,9 +111,6 @@ public static byte[] LightHttpPostConnection(String u, ContentValues values) { outStream.close(); inStream.close(); -// long elapsed = System.currentTimeMillis() - start; -// Log.e("MewX-Net", "page fetched in " + elapsed + "ms"); - return data; // return value } catch (IOException e) {