-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing #158
Draft
gcoleman799
wants to merge
58
commits into
android:webview
Choose a base branch
from
gcoleman799:testing
base: webview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Testing #158
Changes from 21 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
ee10b26
setup
grcoleman 6973b6b
adds night theme color scheme, adds setForceDarkStrategy, adds media …
grcoleman efcf673
setup
grcoleman d6836d6
adds night theme color scheme, adds setForceDarkStrategy, adds media …
grcoleman c2de65f
fix conflicts
grcoleman 761ecf0
changes path, adds icon choosing logic
grcoleman 6db9c65
changes initial image path, adds symbos values
grcoleman 55a9608
adds meta tag, corrects media query, adds setDefaultNightMode
grcoleman 3e2c009
adds media query
grcoleman d70a861
changes path, adds icon choosing logic
grcoleman 47f53af
changes initial image path, adds symbos values
grcoleman f429a26
changes fetch to correct path
grcoleman bc9ae5f
changes fetch path
grcoleman f0b47b9
changes background color, adds clarification on ForceDarkStrategy
grcoleman 5dc103e
add clarifying comments on location of data, change path to ensure co…
grcoleman 2c773f8
adds testing outline and dependencies
grcoleman 403446f
changes res path, fixes comment in main.js
grcoleman 77ff7e0
adds tests for createJsObject and drop down menu
grcoleman 7185787
add description to tests,add ids to menu
grcoleman 61c895e
splits light and dark themes into seperate files, adds logic to check…
grcoleman 66b3c49
removes static text color setting from style.css
grcoleman da570e4
Merge pull request #157 from gcoleman799/change-data-source
gcoleman799 4323afe
adds testing outline and dependencies
grcoleman aa3df09
adds tests for createJsObject and drop down menu
grcoleman 6d218b7
add description to tests,add ids to menu
grcoleman f263800
adds set up for unit tests
grcoleman 42111ec
setup
grcoleman d42ded6
adds night theme color scheme, adds setForceDarkStrategy, adds media …
grcoleman 0d4fdf8
setup
grcoleman 85d21b8
adds night theme color scheme, adds setForceDarkStrategy, adds media …
grcoleman 637fec3
adds meta tag, corrects media query, adds setDefaultNightMode
grcoleman 2c83302
adds media query
grcoleman 3a9d8fa
changes background color, adds clarification on ForceDarkStrategy
grcoleman f8b0c11
splits light and dark themes into seperate files, adds logic to check…
grcoleman be2899c
removes static text color setting from style.css
grcoleman 7c3b2c1
add comment about default reloading
grcoleman 60105b6
fixes conflicts
grcoleman e9e7a72
thread test and callback test outline
grcoleman 4934ed2
fixes merge conflicts
grcoleman d7806ed
revert to one css file
grcoleman 13d0b17
puts createJsObject in seperate file, adds handlers and runnables to …
grcoleman ce63069
converts runable to lambda, fixes message val, fixes loadDataWithBase…
grcoleman fa60356
adds coroutines to tests
grcoleman b4c6e58
changes dark theme css formatting
grcoleman 35b902d
Merge pull request #155 from gcoleman799/custom-dark-theme
gcoleman799 77350b7
changes initial image path, gets rid of unncessary logging, fixes all…
grcoleman 00b047b
Merge pull request #160 from gcoleman799/share-feature
gcoleman799 048851d
adds testing outline and dependencies
grcoleman 7f7152e
adds tests for createJsObject and drop down menu
grcoleman bbc334b
add description to tests,add ids to menu
grcoleman 4528470
adds set up for unit tests
grcoleman fa580f4
thread test and callback test outline
grcoleman 684f2ef
adds testing outline and dependencies
grcoleman 016ac01
adds tests for createJsObject and drop down menu
grcoleman c920025
puts createJsObject in seperate file, adds handlers and runnables to …
grcoleman e7bb953
uses CompletableDeffered in tests
grcoleman 84cea6b
uses Completable Deferredand run blocking
grcoleman 5c447de
Fixes merge conflicts
grcoleman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
WebView/app/src/androidTest/java/com/android/samples/webviewdemo/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
170 changes: 170 additions & 0 deletions
170
WebView/app/src/androidTest/java/com/android/samples/webviewdemo/MainActivityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.samples.webviewdemo | ||
|
||
import android.content.Context | ||
import android.os.Handler | ||
import android.os.Looper | ||
import android.util.Log | ||
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 | ||
import androidx.test.espresso.web.sugar.Web.onWebView | ||
import androidx.test.espresso.web.webdriver.DriverAtoms.findElement | ||
import androidx.test.espresso.web.webdriver.DriverAtoms.getText | ||
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 kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
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 MainActivityTest { | ||
|
||
val context = ApplicationProvider.getApplicationContext<Context>() | ||
|
||
@Rule | ||
@JvmField | ||
val mainActivityRule = ActivityTestRule(MainActivity::class.java) | ||
fun afterActivityLaunched() { | ||
// Technically we do not need to do this - MainActivity has javascript turned on. | ||
// Other WebViews in your app may have javascript turned off, however since the only way | ||
// to automate WebViews is through javascript, it must be enabled. | ||
onWebView().forceJavascriptEnabled() | ||
} | ||
|
||
// Test to check that the drop down menu behaves as expected | ||
@Test | ||
fun dropDownMenu_SanFran() { | ||
mainActivityRule.getActivity() | ||
onWebView() | ||
.withElement(findElement(Locator.ID, "location")) | ||
.perform(webClick()) // Similar to perform(click()) | ||
.withElement(findElement(Locator.ID, "SF")) | ||
.perform(webClick()) // Similar to perform(click()) | ||
.withElement(findElement(Locator.ID, "title")) | ||
.check(webMatches(getText(), containsString("San Francisco"))) | ||
} | ||
|
||
// Test for checking createJsObject | ||
@Test | ||
fun jsObjectIsInjectedAndContainsPostMessage() { | ||
mainActivityRule.getActivity() | ||
onWebView() | ||
.check( | ||
webMatches( | ||
script( | ||
"return jsObject && jsObject.postMessage != null;", | ||
castOrDie(Boolean::class.javaObjectType) | ||
), | ||
`is`(true) | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun valueInCallback_compareValueInput_returnsTrue() = runBlocking() { | ||
mainActivityRule.activity | ||
// Setup | ||
val jsObjName = "jsObject" | ||
val allowedOriginRules = setOf<String>("https://example.com") | ||
val expectedMessage = "hello" | ||
val callback = async { return@async "hello" } | ||
// Get a handler that can be used to post to the main thread | ||
val mainHandler = Handler(Looper.getMainLooper()); | ||
mainHandler.post { | ||
run { | ||
val webView = WebView(context) | ||
// Create JsObject | ||
createJsObject( | ||
webView, | ||
jsObjName, | ||
allowedOriginRules | ||
) { message -> callback | ||
} | ||
//Inject JsObject into Html | ||
webView.loadDataWithBaseURL( | ||
"https://example.com", "<html></html>", | ||
"text/html", null, null | ||
) | ||
//Call js code to invoke callback | ||
webView.evaluateJavascript("${jsObjName}.postMessage(`hello`)", null) | ||
} | ||
} | ||
// evaluate what comes out -> it should be hello | ||
assertEquals(expectedMessage, callback.await()) | ||
} | ||
|
||
@Test | ||
// Checks that postMessage runs on the UI thread | ||
fun checkingThreadCallbackRunsOn() = runBlocking { | ||
mainActivityRule.activity | ||
// Setup | ||
val jsObjName = "jsObject" | ||
val allowedOriginRules = setOf<String>("https://example.com") | ||
val callback = async { isUiThread() } | ||
// Get a handler that can be used to post to the main thread | ||
val mainHandler = Handler(Looper.getMainLooper()) | ||
// Start Interacting with webView on UI thread | ||
mainHandler.post { | ||
run { | ||
val webView = WebView(context) | ||
// Create JsObject | ||
createJsObject( | ||
webView, | ||
jsObjName, | ||
allowedOriginRules | ||
) { message -> callback } | ||
//Inject JsObject into Html | ||
webView.loadDataWithBaseURL( | ||
"https://example.com", "<html></html>", | ||
"text/html", "UTF-8", null | ||
) | ||
//Call js code to invoke callback | ||
webView.evaluateJavascript("${jsObjName}.postMessage(`hello`)", null) | ||
} | ||
} | ||
assertTrue(callback.await()) | ||
} | ||
|
||
/** | ||
* Returns true if the current thread is the UI thread based on the | ||
* Looper. | ||
*/ | ||
private fun isUiThread(): Boolean { | ||
return Looper.myLooper() == Looper.getMainLooper() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this again: how does this control when the coroutine executes? I wonder if the coroutine is executing on its own before the callback happens, which might explain test failures (the code is running on a non-UI thread, so the assertion would fail).