@@ -753,7 +753,7 @@ public void test_LocationListener_LocationListener_ordered_changing () {
753753 }));
754754 shell .open ();
755755 browser .setText ("You should not see this message." );
756- String url = getValidUrl ();
756+ String url = getValidFileUrl ();
757757 browser .setUrl (url );
758758 assertTrue (waitForPassCondition (() -> locations .size () == 2 ));
759759 assertTrue (locations .get (0 ).equals ("about:blank" ) && locations .get (1 ).contains ("testWebsiteWithTitle.html" ));
@@ -762,8 +762,10 @@ public void test_LocationListener_LocationListener_ordered_changing () {
762762@ TempDir
763763static Path tempFolder ;
764764
765- private String getValidUrl () {
766- return SwtTestUtil .getPath ("testWebsiteWithTitle.html" , tempFolder ).toUri ().toString ();
765+ private String getValidFileUrl () {
766+ String url = SwtTestUtil .getPath ("testWebsiteWithTitle.html" , tempFolder ).toUri ().toString ();
767+ assertTrue (url .startsWith ("file://" ));
768+ return url ;
767769}
768770
769771@ Test
@@ -1183,7 +1185,27 @@ public void test_setUrl_local() {
11831185 assumeFalse (SwtTestUtil .isCocoa , "Test fails on Mac, see https://github.com/eclipse-platform/eclipse.platform.swt/issues/722" );
11841186 String expectedTitle = "Website Title" ;
11851187 Runnable browserSetFunc = () -> {
1186- String url = getValidUrl ();
1188+ String url = getValidFileUrl ();
1189+ testLogAppend ("URL: " + url );
1190+ boolean opSuccess = browser .setUrl (url );
1191+ assertTrue (opSuccess );
1192+ };
1193+ validateTitleChanged (expectedTitle , browserSetFunc );
1194+ }
1195+
1196+ /**
1197+ * Verifies that an invalid URL (missing file://) will be converted to a valid
1198+ * URL with the file:// added.
1199+ */
1200+ @ Test
1201+ public void test_setUrl_invalid_url_local () {
1202+ assumeTrue (SwtTestUtil .isLinux , "Handling of invalid URLs is platform dependent" );
1203+ String expectedTitle = "Website Title" ;
1204+ Runnable browserSetFunc = () -> {
1205+ String url = getValidFileUrl ();
1206+ assertTrue (url .startsWith ("file://" ));
1207+ url = url .replaceFirst ("^file://" , "" );
1208+ assertTrue (url .startsWith ("/" ));
11871209 testLogAppend ("URL: " + url );
11881210 boolean opSuccess = browser .setUrl (url );
11891211 assertTrue (opSuccess );
@@ -1209,6 +1231,30 @@ public void test_setUrl_remote() throws IOException {
12091231 }
12101232}
12111233
1234+ /**
1235+ * Verifies that an invalid URL (missing http://) will be converted to a valid
1236+ * URL with the http:// added.
1237+ */
1238+ @ Test
1239+ public void test_setUrl_invalid_url_remote () throws IOException {
1240+ assumeTrue (SwtTestUtil .isLinux , "Handling of invalid URLs is platform dependent" );
1241+
1242+ try (var server = new EchoHttpServer ()) {
1243+
1244+ String validUrl = server .getEchoUrl ("test_setUrl_remote" );
1245+ assertTrue (validUrl .startsWith ("http://" ));
1246+ String url = validUrl .replaceFirst ("^http://" , "" );
1247+
1248+ String expectedTitle = "test_setUrl_remote" ;
1249+ Runnable browserSetFunc = () -> {
1250+ testLog .append ("Setting Browser url to:" + url );
1251+ boolean opSuccess = browser .setUrl (url );
1252+ assertTrue (opSuccess );
1253+ };
1254+ validateTitleChanged (expectedTitle , browserSetFunc );
1255+ }
1256+ }
1257+
12121258@ Test
12131259public void test_setUrl_remote_with_post () throws IOException {
12141260 try (var server = new EchoHttpServer ()) {
@@ -1238,6 +1284,106 @@ public void test_setUrl_remote_with_post() throws IOException {
12381284 }
12391285}
12401286
1287+ @ Test
1288+ public void test_setUrl_post_invalid_url () {
1289+ assumeTrue (SwtTestUtil .isLinux , "Handling of invalid URLs is platform dependent" );
1290+ // Purposefully invalid URL (Note that URLs that become valid with
1291+ // http:// or file:// prefixed will get converted to their valid URLs
1292+ String url = "" ;
1293+ String postData = "test_setUrl_post_invalid_url" ;
1294+
1295+ Runnable browserSetFunc = () -> {
1296+ testLog .append ("Setting Browser url to:" + url );
1297+ boolean opSuccess = browser .setUrl (url , postData , null );
1298+ assertTrue (opSuccess );
1299+ };
1300+
1301+ final AtomicReference <Boolean > completed = new AtomicReference <>(false );
1302+ browser .addProgressListener (completedAdapter (event -> {
1303+ testLog .append ("ProgressListener fired" );
1304+ completed .set (true );
1305+ }));
1306+ browserSetFunc .run ();
1307+ shell .open ();
1308+
1309+ boolean hasFinished = waitForPassCondition (() -> completed .get ().booleanValue ());
1310+ assertTrue (hasFinished );
1311+
1312+ String lowerCase = browser .getText ().toLowerCase ();
1313+ assertTrue (lowerCase .contains ("URL is invalid" .toLowerCase ()), "Browser getText was: " + browser .getText ());
1314+ }
1315+
1316+ @ Test
1317+ public void test_setUrl_post_connection_closes_prematurely () throws IOException {
1318+ assumeTrue (SwtTestUtil .isLinux , """
1319+ Handling POST in Linux is handled by SWT, so we need extra testing for the SWT code.
1320+ This test can be adapted to win32/cocoa, but that would be testing the third-party
1321+ browser component. Therefore (for now) this test only runs on Linux.
1322+ """ );
1323+ try (var server = new EchoHttpServer () {
1324+ @ Override
1325+ protected void handlePostEcho (HttpExchange exchange ) {
1326+ // Immediately close the connection - this should generate a user
1327+ // visible error in the UI.
1328+
1329+ // For Webkit case where we handle post
1330+ exchange .close ();
1331+ }
1332+ }) {
1333+ String url = server .postEchoUrl ();
1334+ String postData = "test_setUrl_remote_with_post" ;
1335+
1336+ Runnable browserSetFunc = () -> {
1337+ testLog .append ("Setting Browser url to:" + url );
1338+ boolean opSuccess = browser .setUrl (url , postData , null );
1339+ assertTrue (opSuccess );
1340+ };
1341+
1342+ final AtomicReference <Boolean > completed = new AtomicReference <>(false );
1343+ browser .addProgressListener (completedAdapter (event -> {
1344+ testLog .append ("ProgressListener fired" );
1345+ completed .set (true );
1346+ }));
1347+ browserSetFunc .run ();
1348+ shell .open ();
1349+
1350+ boolean hasFinished = waitForPassCondition (() -> completed .get ().booleanValue ());
1351+ assertTrue (hasFinished );
1352+
1353+ String lowerCase = browser .getText ().toLowerCase ();
1354+ assertTrue (lowerCase .contains ("Unexpected end of file from server" .toLowerCase ()), "Browser getText was: " + browser .getText ());
1355+ }
1356+ }
1357+
1358+ @ Test
1359+ public void test_setUrl_post_file_url () {
1360+ assumeTrue (SwtTestUtil .isLinux , "Handling of invalid URLs is platform dependent" );
1361+ // Purposefully invalid URL (Note that URLs that become valid with
1362+ // http:// or file:// prefixed will get converted to their valid URLs
1363+ String url = getValidFileUrl ();
1364+ String postData = "test_setUrl_post_file_url" ;
1365+
1366+ Runnable browserSetFunc = () -> {
1367+ testLog .append ("Setting Browser url to:" + url );
1368+ boolean opSuccess = browser .setUrl (url , postData , null );
1369+ assertTrue (opSuccess );
1370+ };
1371+
1372+ final AtomicReference <Boolean > completed = new AtomicReference <>(false );
1373+ browser .addProgressListener (completedAdapter (event -> {
1374+ testLog .append ("ProgressListener fired" );
1375+ completed .set (true );
1376+ }));
1377+ browserSetFunc .run ();
1378+ shell .open ();
1379+
1380+ boolean hasFinished = waitForPassCondition (() -> completed .get ().booleanValue ());
1381+ assertTrue (hasFinished );
1382+
1383+ String lowerCase = browser .getText ().toLowerCase ();
1384+ assertTrue (lowerCase .contains ("Unsupported connection type" .toLowerCase ()), "Browser getText was: " + browser .getText ());
1385+ }
1386+
12411387private void validateTitleChanged (String expectedTitle , Runnable browserSetFunc ) {
12421388 final AtomicReference <String > actualTitle = new AtomicReference <>("" );
12431389 browser .addTitleListener (event -> {
0 commit comments