Skip to content

Commit

Permalink
thread test and callback test outline
Browse files Browse the repository at this point in the history
  • Loading branch information
grcoleman committed Jul 23, 2020
1 parent f263800 commit e9e7a72
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 27 deletions.
1 change: 1 addition & 0 deletions WebView/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation "androidx.webkit:webkit:$webkit_version"


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/

package com.android.samples.webviewdemo
import android.content.Context
import android.os.Looper
import android.webkit.WebView
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches
import androidx.test.espresso.web.model.Atoms.castOrDie
import androidx.test.espresso.web.model.Atoms.script
Expand All @@ -25,17 +29,24 @@ import androidx.test.espresso.web.webdriver.DriverAtoms.webClick
import androidx.test.espresso.web.webdriver.Locator
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertTrue
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.containsString
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


/**
* Launch, interact, and verify conditions in an activity that has a WebView instance.
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {


class MainActivityTest {

val context = ApplicationProvider.getApplicationContext<Context>()

@Rule @JvmField
val mainActivityRule = ActivityTestRule(MainActivity::class.java)
Expand Down Expand Up @@ -71,4 +82,78 @@ class ExampleInstrumentedTest {
)
)
}

@Test
fun valueInCallback_compareValueInput_returnsTrue(){
mainActivityRule.getActivity()

// Setup
val webView = WebView(context)
val jsObjName = "jsObject"
val allowedOriginRules = setOf<String>("https://example.com")

// Create HTML
val htmlPage = "<!DOCTYPE html><html><body>" + " <script>" + " myObject.postMessage('hello');" + " </script>" + "</body></html>"

// Create JsObject
MainActivity.createJsObject(
webView,
jsObjName,
allowedOriginRules
) { message -> MainActivity.invokeShareIntent(message) }

//Inject JsObject into Html
webView.loadData(htmlPage, "text/html", "UTF-8")


//Call js code to invoke callback (in script tag of htmlPage)

// evaluate what comes out -> it should be hello
// *Note: "response from callback" is a place holder here I am unsure what should be placed there
assertEquals("response from callback", "hello")

}

@Test
// Checks that postMessage runs on the UI thread
fun checkingThreadCallbackRunsOn() {
mainActivityRule.getActivity()

// Setup
val webView = WebView(context)
val jsObjName = "jsObject"
val allowedOriginRules = setOf<String>("https://example.com")

// Create HTML
val htmlPage =
"<!DOCTYPE html><html><body>" + " <script>" + " jsObject.postMessage('hello');" + " </script>" + "</body></html>"


// Create JsObject
MainActivity.createJsObject(
webView,
jsObjName,
allowedOriginRules
) { message -> MainActivity.invokeShareIntent(message) }


// Inject JsObject into Html by loading it in the webview
webView.loadData(htmlPage, "text/html", "UTF-8")


// Use coroutine to go onto UI thread here?
// Call js code to invoke callback (in script tag of htmlPage)


// check that method is running on the UI thread
assertTrue(isUiThread())
}

/**
* Returns true if the current thread is the UI thread based on the
* Looper.
*/
private fun isUiThread(): Boolean {
return Looper.myLooper() == Looper.getMainLooper()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,18 @@

package com.android.samples.webviewdemo

import org.junit.Assert.assertThat
import org.junit.Test
import java.util.Objects

import org.junit.Assert.*

/**
* Tests to ensure that callbacks are behaving as expected
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/


class EmailValidatorTest {
class ExampleUnitTest {
@Test
fun testConvertFahrenheitToCelsius() {
val actual: Float = ConverterUtil.convertCelsiusToFahrenheit(100)
// expected value is 212
val expected = 212f
// use this method because float is not precise
// assertEquals("Conversion from celsius to fahrenheit failed", expected, actual, 0.001)
}
@Test
fun postMessage_CorrectStringFormat_ReturnsTrue() {
// create object
// what object do we need to create?
// we need to create a js object to call postMessage on

val jsObject: Objects =

// create expected return
// call post message
// check that post message

// assertEquals("Hi", jsObject.postMessage("Hi"))
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}

0 comments on commit e9e7a72

Please sign in to comment.