WebDriver: several tests in imported/w3c/webdriver/tests/sessions/new_session/merge.py are failing
https://bugs.webkit.org/show_bug.cgi?id=180407

Reviewed by Carlos Alberto Lopez Perez.

Source/WebDriver:

This is actually a bug in the spec, see https://github.com/w3c/webdriver/issues/1203. It seems the idea in any
case is that we should check all the firstMatch capabilities before matching them, and return if there's any
error.

* WebDriverService.cpp:
(WebDriver::WebDriverService::mergeCapabilities const):
(WebDriver::WebDriverService::processCapabilities const):

WebDriverTests:

Remove expectations for tests that are passing now and update the bug number of the other ones referencing
#180407.

* TestExpectations.json:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@227414 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebDriver/ChangeLog b/Source/WebDriver/ChangeLog
index d4d4c5b..4823dce 100644
--- a/Source/WebDriver/ChangeLog
+++ b/Source/WebDriver/ChangeLog
@@ -1,5 +1,20 @@
 2018-01-23  Carlos Garcia Campos  <cgarcia@igalia.com>
 
+        WebDriver: several tests in imported/w3c/webdriver/tests/sessions/new_session/merge.py are failing
+        https://bugs.webkit.org/show_bug.cgi?id=180407
+
+        Reviewed by Carlos Alberto Lopez Perez.
+
+        This is actually a bug in the spec, see https://github.com/w3c/webdriver/issues/1203. It seems the idea in any
+        case is that we should check all the firstMatch capabilities before matching them, and return if there's any
+        error.
+
+        * WebDriverService.cpp:
+        (WebDriver::WebDriverService::mergeCapabilities const):
+        (WebDriver::WebDriverService::processCapabilities const):
+
+2018-01-23  Carlos Garcia Campos  <cgarcia@igalia.com>
+
         WebDriver: test imported/w3c/webdriver/tests/sessions/new_session/response.py::test_timeouts fails
         https://bugs.webkit.org/show_bug.cgi?id=180412
 
diff --git a/Source/WebDriver/WebDriverService.cpp b/Source/WebDriver/WebDriverService.cpp
index 04368ef..68ef921 100644
--- a/Source/WebDriver/WebDriverService.cpp
+++ b/Source/WebDriver/WebDriverService.cpp
@@ -442,12 +442,8 @@
         result->setValue(it->key, RefPtr<JSON::Value>(it->value));
 
     auto firstMatchEnd = firstMatchCapabilities.end();
-    for (auto it = firstMatchCapabilities.begin(); it != firstMatchEnd; ++it) {
-        if (requiredCapabilities.find(it->key) != requiredEnd)
-            return nullptr;
-
+    for (auto it = firstMatchCapabilities.begin(); it != firstMatchEnd; ++it)
         result->setValue(it->key, RefPtr<JSON::Value>(it->value));
-    }
 
     return result;
 }
@@ -575,6 +571,18 @@
             completionHandler(CommandResult::fail(CommandResult::ErrorCode::InvalidArgument, String("Invalid firstMatch capabilities")));
             return nullptr;
         }
+
+        // Validate here that firstMatchCapabilities don't shadow alwaysMatchCapabilities.
+        auto requiredEnd = requiredCapabilities->end();
+        auto firstMatchEnd = firstMatchCapabilities->end();
+        for (auto it = firstMatchCapabilities->begin(); it != firstMatchEnd; ++it) {
+            if (requiredCapabilities->find(it->key) != requiredEnd) {
+                completionHandler(CommandResult::fail(CommandResult::ErrorCode::InvalidArgument,
+                    makeString("Invalid firstMatch capabilities: key ", it->key, " is present in alwaysMatch")));
+                return nullptr;
+            }
+        }
+
         // 5.2. Append validated capabilities to validated first match capabilities.
         validatedFirstMatchCapabilitiesList.uncheckedAppend(WTFMove(firstMatchCapabilities));
     }
diff --git a/WebDriverTests/ChangeLog b/WebDriverTests/ChangeLog
index d19e15a..37c5ff0 100644
--- a/WebDriverTests/ChangeLog
+++ b/WebDriverTests/ChangeLog
@@ -1,5 +1,17 @@
 2018-01-23  Carlos Garcia Campos  <cgarcia@igalia.com>
 
+        WebDriver: several tests in imported/w3c/webdriver/tests/sessions/new_session/merge.py are failing
+        https://bugs.webkit.org/show_bug.cgi?id=180407
+
+        Reviewed by Carlos Alberto Lopez Perez.
+
+        Remove expectations for tests that are passing now and update the bug number of the other ones referencing
+        #180407.
+
+        * TestExpectations.json:
+
+2018-01-23  Carlos Garcia Campos  <cgarcia@igalia.com>
+
         WebDriver: test imported/w3c/webdriver/tests/sessions/new_session/response.py::test_timeouts fails
         https://bugs.webkit.org/show_bug.cgi?id=180412
 
diff --git a/WebDriverTests/TestExpectations.json b/WebDriverTests/TestExpectations.json
index 9036bb9..1bfa71c 100644
--- a/WebDriverTests/TestExpectations.json
+++ b/WebDriverTests/TestExpectations.json
@@ -193,17 +193,11 @@
     },
     "imported/w3c/webdriver/tests/sessions/new_session/merge.py": {
         "subtests": {
-            "test_merge_invalid[unhandledPromptBehavior-value1]": {
-                "expected": {"all": {"status": ["FAIL"], "bug": "webkit.org/b/180407"}}
-            },
-            "test_merge_invalid[unhandledPromptBehavior-value2]": {
-                "expected": {"all": {"status": ["FAIL"], "bug": "webkit.org/b/180407"}}
-            },
             "test_merge_platformName": {
-                "expected": {"all": {"status": ["FAIL"], "bug": "webkit.org/b/180407"}}
+                "expected": {"all": {"status": ["FAIL"], "bug": "webkit.org/b/181984"}}
             },
             "test_merge_browserName": {
-                "expected": {"all": {"status": ["FAIL"], "bug": "webkit.org/b/180407"}}
+                "expected": {"all": {"status": ["FAIL"], "bug": "webkit.org/b/181985"}}
             }
         }
     },