From 96b4bf57b2fd3443289f46e58fd2105e46049747 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Fri, 2 Oct 2020 18:07:29 -0700 Subject: [PATCH] chore: use single definition of HTTPCredentials --- .../playwright/tools/ApiGenerator.java | 27 ++++++-- .../com/microsoft/playwright/tools/Types.java | 16 ++--- .../com/microsoft/playwright/Browser.java | 50 ++------------ .../microsoft/playwright/BrowserContext.java | 31 +++++---- .../com/microsoft/playwright/BrowserType.java | 25 +------ .../impl/BrowserNewContextOptions.java | 65 ------------------- .../impl/BrowserTypeLaunchOptions.java | 47 -------------- .../playwright/impl/NavigateOptions.java | 28 -------- 8 files changed, 50 insertions(+), 239 deletions(-) delete mode 100644 lib/src/main/java/com/microsoft/playwright/impl/BrowserNewContextOptions.java delete mode 100644 lib/src/main/java/com/microsoft/playwright/impl/BrowserTypeLaunchOptions.java delete mode 100644 lib/src/main/java/com/microsoft/playwright/impl/NavigateOptions.java diff --git a/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java b/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java index 5a58cd5c..9f891f32 100644 --- a/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java +++ b/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java @@ -300,7 +300,6 @@ class Method extends Element { static { customSignature.put("Page.setViewportSize", "void setViewportSize(int width, int height);"); customSignature.put("BrowserContext.setHTTPCredentials", "void setHTTPCredentials(String username, String password);"); - customSignature.put("ChromiumBrowserContext.setHTTPCredentials", "void setHTTPCredentials(String username, String password);"); } Method(TypeDefinition parent, JsonObject jsonElement) { @@ -419,11 +418,9 @@ class Field extends Element { } void writeBuilderMethod(List output, String offset, String parentClass) { - if (name.equals("httpCredentials") && type.toJava().equals("HttpCredentials")) { + if (name.equals("httpCredentials")) { output.add(offset + "public " + parentClass + " with" + toTitle(name) + "(String username, String password) {"); - output.add(offset + " this." + name + " = new " + type.toJava() + "();"); - output.add(offset + " this." + name + ".username = username;"); - output.add(offset + " this." + name + ".password = password;"); + output.add(offset + " this." + name + " = new " + type.toJava() + "(username, password);"); output.add(offset + " return this;"); } else if (type.isNestedClass) { output.add(offset + "public " + type.toJava() + " set" + toTitle(name) + "() {"); @@ -536,6 +533,26 @@ class Interface extends TypeDefinition { output.add(offset + "}"); break; } + case "BrowserContext": { + output.add(offset + "class HTTPCredentials {"); + output.add(offset + " private final String username;"); + output.add(offset + " private final String password;"); + output.add(""); + output.add(offset + " public HTTPCredentials(String username, String password) {"); + output.add(offset + " this.username = username;"); + output.add(offset + " this.password = password;"); + output.add(offset + " }"); + output.add(""); + output.add(offset + " public String username() {"); + output.add(offset + " return username;"); + output.add(offset + " }"); + output.add(""); + output.add(offset + " public String password() {"); + output.add(offset + " return password;"); + output.add(offset + " }"); + output.add(offset + "}"); + break; + } default: return; } output.add(""); diff --git a/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java b/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java index 512eb0db..2d0ecdaa 100644 --- a/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java +++ b/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java @@ -122,15 +122,10 @@ class Types { add("WebKitBrowser.newPage.options.viewport", "null|Object", "Page.Viewport", new Empty()); // HTTP credentials. - add("Browser.newContext.options.httpCredentials", "Object", "HttpCredentials"); - add("Browser.newPage.options.httpCredentials", "Object", "HttpCredentials"); - add("BrowserType.launchPersistentContext.options.httpCredentials", "Object", "HttpCredentials"); - add("ChromiumBrowser.newContext.options.httpCredentials", "Object", "HttpCredentials"); - add("ChromiumBrowser.newPage.options.httpCredentials", "Object", "HttpCredentials"); - add("FirefoxBrowser.newContext.options.httpCredentials", "Object", "HttpCredentials"); - add("FirefoxBrowser.newPage.options.httpCredentials", "Object", "HttpCredentials"); - add("WebKitBrowser.newContext.options.httpCredentials", "Object", "HttpCredentials"); - add("WebKitBrowser.newPage.options.httpCredentials", "Object", "HttpCredentials"); + add("Browser.newContext.options.httpCredentials", "Object", "BrowserContext.HTTPCredentials", new Empty()); + add("Browser.newPage.options.httpCredentials", "Object", "BrowserContext.HTTPCredentials", new Empty()); + add("BrowserType.launchPersistentContext.options.httpCredentials", "Object", "BrowserContext.HTTPCredentials", new Empty()); + add("BrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials", new Empty()); // EvaluationArgument add("Page.$eval.arg", "EvaluationArgument", "Object"); @@ -266,9 +261,6 @@ class Types { add("WebKitBrowser.newPage.options.geolocation.accuracy", "number", "double"); add("BrowserContext.setGeolocation.geolocation", "null|Object", "Geolocation"); - add("ChromiumBrowserContext.setGeolocation.geolocation", "null|Object", "Geolocation"); - add("BrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials"); - add("ChromiumBrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials"); // Single field options add("Keyboard.type.options", "Object", "int", new Empty()); diff --git a/lib/src/main/java/com/microsoft/playwright/Browser.java b/lib/src/main/java/com/microsoft/playwright/Browser.java index ad418343..f1900920 100644 --- a/lib/src/main/java/com/microsoft/playwright/Browser.java +++ b/lib/src/main/java/com/microsoft/playwright/Browser.java @@ -46,25 +46,6 @@ public interface Browser { return this; } } - public class HttpCredentials { - public String username; - public String password; - - HttpCredentials() { - } - public NewContextOptions done() { - return NewContextOptions.this; - } - - public HttpCredentials withUsername(String username) { - this.username = username; - return this; - } - public HttpCredentials withPassword(String password) { - this.password = password; - return this; - } - } public class VideoSize { public int width; public int height; @@ -99,7 +80,7 @@ public interface Browser { public List permissions; public Map extraHTTPHeaders; public Boolean offline; - public HttpCredentials httpCredentials; + public BrowserContext.HTTPCredentials httpCredentials; public ColorScheme colorScheme; public Logger logger; public String relativeArtifactsPath; @@ -168,9 +149,7 @@ public interface Browser { return this; } public NewContextOptions withHttpCredentials(String username, String password) { - this.httpCredentials = new HttpCredentials(); - this.httpCredentials.username = username; - this.httpCredentials.password = password; + this.httpCredentials = new BrowserContext.HTTPCredentials(username, password); return this; } public NewContextOptions withColorScheme(ColorScheme colorScheme) { @@ -224,25 +203,6 @@ public interface Browser { return this; } } - public class HttpCredentials { - public String username; - public String password; - - HttpCredentials() { - } - public NewPageOptions done() { - return NewPageOptions.this; - } - - public HttpCredentials withUsername(String username) { - this.username = username; - return this; - } - public HttpCredentials withPassword(String password) { - this.password = password; - return this; - } - } public class VideoSize { public int width; public int height; @@ -277,7 +237,7 @@ public interface Browser { public List permissions; public Map extraHTTPHeaders; public Boolean offline; - public HttpCredentials httpCredentials; + public BrowserContext.HTTPCredentials httpCredentials; public ColorScheme colorScheme; public Logger logger; public String relativeArtifactsPath; @@ -346,9 +306,7 @@ public interface Browser { return this; } public NewPageOptions withHttpCredentials(String username, String password) { - this.httpCredentials = new HttpCredentials(); - this.httpCredentials.username = username; - this.httpCredentials.password = password; + this.httpCredentials = new BrowserContext.HTTPCredentials(username, password); return this; } public NewPageOptions withColorScheme(ColorScheme colorScheme) { diff --git a/lib/src/main/java/com/microsoft/playwright/BrowserContext.java b/lib/src/main/java/com/microsoft/playwright/BrowserContext.java index 70005572..46560ef1 100644 --- a/lib/src/main/java/com/microsoft/playwright/BrowserContext.java +++ b/lib/src/main/java/com/microsoft/playwright/BrowserContext.java @@ -20,6 +20,24 @@ import java.util.*; import java.util.function.BiConsumer; public interface BrowserContext { + class HTTPCredentials { + private final String username; + private final String password; + + public HTTPCredentials(String username, String password) { + this.username = username; + this.password = password; + } + + public String username() { + return username; + } + + public String password() { + return password; + } + } + class GrantPermissionsOptions { public String origin; @@ -46,19 +64,6 @@ public interface BrowserContext { return this; } } - class HTTPCredentials { - public String username; - public String password; - - public HTTPCredentials withUsername(String username) { - this.username = username; - return this; - } - public HTTPCredentials withPassword(String password) { - this.password = password; - return this; - } - } Deferred waitForPage(); void close(); void addCookies(List cookies); diff --git a/lib/src/main/java/com/microsoft/playwright/BrowserType.java b/lib/src/main/java/com/microsoft/playwright/BrowserType.java index d30fdb66..76ad9264 100644 --- a/lib/src/main/java/com/microsoft/playwright/BrowserType.java +++ b/lib/src/main/java/com/microsoft/playwright/BrowserType.java @@ -215,25 +215,6 @@ public interface BrowserType { return this; } } - public class HttpCredentials { - public String username; - public String password; - - HttpCredentials() { - } - public LaunchPersistentContextOptions done() { - return LaunchPersistentContextOptions.this; - } - - public HttpCredentials withUsername(String username) { - this.username = username; - return this; - } - public HttpCredentials withPassword(String password) { - this.password = password; - return this; - } - } public class VideoSize { public int width; public int height; @@ -284,7 +265,7 @@ public interface BrowserType { public List permissions; public Map extraHTTPHeaders; public Boolean offline; - public HttpCredentials httpCredentials; + public BrowserContext.HTTPCredentials httpCredentials; public ColorScheme colorScheme; public String relativeArtifactsPath; public Boolean recordVideos; @@ -416,9 +397,7 @@ public interface BrowserType { return this; } public LaunchPersistentContextOptions withHttpCredentials(String username, String password) { - this.httpCredentials = new HttpCredentials(); - this.httpCredentials.username = username; - this.httpCredentials.password = password; + this.httpCredentials = new BrowserContext.HTTPCredentials(username, password); return this; } public LaunchPersistentContextOptions withColorScheme(ColorScheme colorScheme) { diff --git a/lib/src/main/java/com/microsoft/playwright/impl/BrowserNewContextOptions.java b/lib/src/main/java/com/microsoft/playwright/impl/BrowserNewContextOptions.java deleted file mode 100644 index cb1c5e95..00000000 --- a/lib/src/main/java/com/microsoft/playwright/impl/BrowserNewContextOptions.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. - * - * 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.microsoft.playwright.impl; - -import java.util.LinkedHashMap; - -public class BrowserNewContextOptions { - public Boolean noDefaultViewport; - public static class Viewport { - // TODO: int is preferred here - public int width; - public int height; - } - public Viewport viewport; - public Boolean ignoreHTTPSErrors; - public Boolean javaScriptEnabled; - public Boolean bypassCSP; - public String userAgent; - public String locale; - public String timezoneId; - public static class Geolocation { - // TODO: can we use int somehow? - public Double longitude; - public Double latitude; - public Double accuracy; - }; - public Geolocation geolocation; - public String[] permissions; - public LinkedHashMap extraHTTPHeaders; - public Boolean offline; - public static class HttpCredentials { - public String username; - public String password; - } - public HttpCredentials httpCredentials; - public Integer deviceScaleFactor; - public Boolean isMobile; - public Boolean hasTouch; - public enum ColorScheme { - // TODO: noPreference => no-preference - dark, light, noPreference - } - public ColorScheme colorScheme; - public Boolean acceptDownloads; - public Boolean _recordVideos; - public static class _VideoSize { - public int width; - public int height; - } - public _VideoSize _videoSize; -} diff --git a/lib/src/main/java/com/microsoft/playwright/impl/BrowserTypeLaunchOptions.java b/lib/src/main/java/com/microsoft/playwright/impl/BrowserTypeLaunchOptions.java deleted file mode 100644 index 7dc859ee..00000000 --- a/lib/src/main/java/com/microsoft/playwright/impl/BrowserTypeLaunchOptions.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. - * - * 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.microsoft.playwright.impl; - -import com.google.gson.JsonObject; - -import java.util.LinkedHashMap; - -public class BrowserTypeLaunchOptions { - public String executablePath; - public String[] args; - public Boolean ignoreAllDefaultArgs; - public String[] ignoreDefaultArgs; - public Boolean handleSIGINT; - public Boolean handleSIGTERM; - public Boolean handleSIGHUP; - public Integer timeout; - public LinkedHashMap env; - public Boolean headless; - public Boolean devtools; - public static class Proxy { - String server; - String bypass; - String username; - String password; - } - public Proxy proxy; - public String downloadsPath; - public String _videosPath; - public JsonObject firefoxUserPrefs; - public Boolean chromiumSandbox; - public Integer slowMo; -} diff --git a/lib/src/main/java/com/microsoft/playwright/impl/NavigateOptions.java b/lib/src/main/java/com/microsoft/playwright/impl/NavigateOptions.java deleted file mode 100644 index 97bf471e..00000000 --- a/lib/src/main/java/com/microsoft/playwright/impl/NavigateOptions.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. - * - * 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.microsoft.playwright.impl; - -public class NavigateOptions { - Integer timeout; - enum WaitUntil { - load, - domcontentloaded, - networkidle - } - WaitUntil waitUntil = WaitUntil.load; - String referer; -}