fixed temp file stream

This commit is contained in:
Mariotaku Lee 2017-08-25 15:58:58 +08:00
parent 9ecafb2447
commit 6fbfb13339
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
7 changed files with 112 additions and 60 deletions

View File

@ -20,7 +20,6 @@
package org.mariotaku.twidere.activity
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
@ -30,6 +29,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mariotaku.twidere.constant.IntentConstants.*
import org.mariotaku.twidere.extension.set
import org.mariotaku.twidere.model.ParcelableStatus
import org.mariotaku.twidere.model.ParcelableStatusUpdate
import org.mariotaku.twidere.test.R
@ -167,7 +167,12 @@ class ComposeActivityTest {
activity.finish()
}
private fun Activity.getStatusUpdateTest(checkLength: Boolean): ParcelableStatusUpdate {
private fun ComposeActivity.requestSkipDraft() {
val shouldSkipDraft = javaClass.getDeclaredField("shouldSkipDraft")
this[shouldSkipDraft] = true
}
private fun ComposeActivity.getStatusUpdateTest(checkLength: Boolean): ParcelableStatusUpdate {
val getStatusUpdate = javaClass.getDeclaredMethod("getStatusUpdate",
kotlin.Boolean::class.java).apply {
isAccessible = true

View File

@ -0,0 +1,46 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.extension
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*
/**
* Created by mariotaku on 2017/1/24.
*/
@RunWith(AndroidJUnit4::class)
class FileExtensionsTest {
@Test
fun testTempFileInputStream() {
val context = InstrumentationRegistry.getTargetContext()
val random = Random()
val testData = ByteArray(1024)
random.nextBytes(testData)
val compareData = context.cacheDir.tempInputStream { os ->
os.write(testData)
}.use { it.readBytes(1024) }
Assert.assertArrayEquals(testData, compareData)
}
}

View File

@ -1,27 +0,0 @@
package org.mariotaku.twidere.util
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*
/**
* Created by mariotaku on 2017/1/24.
*/
@RunWith(AndroidJUnit4::class)
class IOFunctionsKtTest {
@Test
fun testTempFileInputStream() {
val context = InstrumentationRegistry.getTargetContext()
val random = Random()
val testData = ByteArray(1024)
random.nextBytes(testData)
val compareData = tempFileInputStream(context) { os ->
os.write(testData)
}.readBytes(1024)
Assert.assertArrayEquals(testData, compareData)
}
}

View File

@ -756,10 +756,6 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
mediaPreviewAdapter.setAltText(position, altText)
}
internal fun requestSkipDraft() {
shouldSkipDraft = true
}
private fun locationMenuItemSelected(item: MenuItem) {
item.isChecked = true
var attachLocationChecked = false

View File

@ -0,0 +1,49 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.extension
import org.mariotaku.twidere.util.DebugLog
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.io.OutputStream
fun File.tempInputStream(write: (OutputStream) -> Unit): InputStream {
val file = File.createTempFile("twidere__temp_is_file", ".tmp", this)
file.outputStream().use { write(it) }
return TempFileInputStream(file)
}
internal class TempFileInputStream(val file: File) : FileInputStream(file) {
override fun close() {
try {
super.close()
} finally {
file.delete()
}
}
override fun finalize() {
if (file.exists()) {
DebugLog.w(msg = "Stream not properly closed, ${file.absolutePath} not deleted")
}
super.finalize()
}
}

View File

@ -33,4 +33,14 @@ operator fun Any.get(field: Field): Any? {
} finally {
field.isAccessible = accessible
}
}
operator fun Any.set(field: Field, any: Any?) {
val accessible = field.isAccessible
try {
field.isAccessible = true
field[this] = any
} finally {
field.isAccessible = accessible
}
}

View File

@ -1,27 +0,0 @@
package org.mariotaku.twidere.util
import android.content.Context
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.io.OutputStream
/**
* Created by mariotaku on 2017/1/24.
*/
fun tempFileInputStream(context: Context, write: (OutputStream) -> Unit): InputStream {
val file = File.createTempFile("twidere__temp_is_file", ".tmp", context.cacheDir)
file.outputStream().use { write(it) }
return TempFileInputStream(file)
}
internal class TempFileInputStream(val file: File) : FileInputStream(file) {
override fun close() {
try {
super.close()
} finally {
file.delete()
}
}
}