Skip to content
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

tests/android: Use adb reverse to access host webserver, allow running on !macos #66

Merged
merged 1 commit into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn delayed_response(req: HttpRequest) -> impl Responder {

pub async fn check_request_received_using<F>(uri: String, host: &str, op: F)
where
F: FnOnce(&str),
F: FnOnce(&str, u16),
{
// initialize env logger
let _ = env_logger::try_init();
Expand Down Expand Up @@ -78,7 +78,7 @@ where
tokio::spawn(server);

// invoke the op
op(&format!("http:https://{}:{}{}", host, port, &uri));
op(&format!("http:https://{}:{}{}", host, port, &uri), port);

// wait for the url to be hit
let timeout = 90;
Expand All @@ -93,7 +93,7 @@ where

#[allow(dead_code)]
pub async fn check_request_received(browser: Browser, uri: String) {
check_request_received_using(uri, "127.0.0.1", |url| {
check_request_received_using(uri, "127.0.0.1", |url, _port| {
open_browser(browser, url).expect("failed to open browser");
})
.await;
Expand All @@ -110,7 +110,7 @@ where
let id = rand::thread_rng().next_u32();
let pb = html_dir.join(format!("test.{}.html", id));
let img_uri = format!("{}?r={}", URI_PNG_1PX, id);
check_request_received_using(img_uri, "127.0.0.1", |uri| {
check_request_received_using(img_uri, "127.0.0.1", |uri, _port| {
let url = url_op(&pb);
let mut html_file = std::fs::File::create(&pb).expect("failed to create html file");
html_file
Expand Down
1 change: 0 additions & 1 deletion tests/test-android-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ crate-type = ["lib", "cdylib"]
jni = "0.19"
ndk-glue = "0.7"
webbrowser = { path = "../.." }

38 changes: 17 additions & 21 deletions tests/test_android.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[cfg(target_os = "macos")]
mod common;

#[cfg(target_os = "macos")]
mod tests {
const TEST_PLATFORM: &str = "android";

Expand All @@ -24,8 +22,7 @@ mod tests {
app_dir.push("tests/test-android-app");
let uri = format!("/{}", TEST_PLATFORM);

let ipv4 = get_ipv4_address();
check_request_received_using(uri, &ipv4, |url| {
check_request_received_using(uri, "127.0.0.1", |url, port| {
// modify android app code to use the correct url
let mut lib_rs = PathBuf::from(&app_dir);
lib_rs.push("src/lib.rs");
Expand Down Expand Up @@ -55,9 +52,21 @@ mod tests {
log::error!("adb uninstall failed");
}
} else {
log::error!("failed to invoke adb uninstall");
log::error!("failed to run {:?}", adb_cmd);
}

let adb_reverse_port = format!("tcp:{}", port);
let mut adb_cmd = Command::new("adb");
adb_cmd
.arg("reverse")
.arg(&adb_reverse_port)
.arg(&adb_reverse_port);
assert!(
adb_cmd.status().expect("Failed to invoke").success(),
"Failed to run {:?}",
adb_cmd
);

// invoke app in android
let mut apk_run_cmd = Command::new("cargo");
apk_run_cmd.arg("apk").arg("run");
Expand All @@ -73,27 +82,14 @@ mod tests {

// check for apk run status
assert!(
apk_run_status.expect("cargo apk failed").success(),
"failed to run: cargo apk run"
apk_run_status.expect("Failed to invoke").success(),
"failed to run {:?}",
apk_run_cmd
);
})
.await;
}

fn get_ipv4_address() -> String {
let output = Command::new("sh")
.arg("-c")
.arg("ifconfig | grep 'inet ' | awk '{ print $2 }' | grep -v ^127.0.0")
.output()
.expect("failed to get non-local ipv4 address");
std::str::from_utf8(&output.stdout)
.expect("unable to parse output into utf8")
.split('\n')
.next()
.expect("no ip address found")
.into()
}

#[test]
fn test_existence_default() {
assert!(Browser::is_available(), "should have found a browser");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod tests {
run_cmd(&glue_dir, "glue code build failed", &["./build"]);

// invoke server
check_request_received_using(uri, &ipv4, |url| {
check_request_received_using(uri, &ipv4, |url, _port| {
// modify ios app code to use the correct url
let mut swift_src = PathBuf::from(&app_dir);
swift_src.push("test-ios-app/ContentView.swift");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod tests {
async fn test_wasm32() {
let uri = &format!("/{}", TEST_PLATFORM);
let ipv4 = "127.0.0.1";
check_request_received_using(uri.into(), ipv4, |url| {
check_request_received_using(uri.into(), ipv4, |url, _port| {
// modify html to use the correct url
let mut app_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
app_dir.push("tests/test-wasm-app");
Expand Down