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

feat: add support for OpenHarmony in device OS detection #7045

Merged
merged 5 commits into from
Nov 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ record DeviceInfo(String browser, String os) {
Pattern.CASE_INSENSITIVE);

static final Pattern OS_REGEX =
Pattern.compile("(Windows NT|Mac OS X|Android|Linux|iPhone|iPad|Windows Phone)");
Pattern.compile(
"(Windows NT|Mac OS X|Android|Linux|iPhone|iPad|Windows Phone|OpenHarmony)");
static final Pattern[] osRegexes = {
Pattern.compile("Windows NT (\\d+\\.\\d+)"),
Pattern.compile("Mac OS X (\\d+[\\._]\\d+([\\._]\\d+)?)"),
Pattern.compile("iPhone OS (\\d+_\\d+(_\\d+)?)"),
Pattern.compile("Android (\\d+\\.\\d+(\\.\\d+)?)")
Pattern.compile("Android (\\d+\\.\\d+(\\.\\d+)?)"),
Pattern.compile("OpenHarmony (\\d+\\.\\d+(\\.\\d+)?)")
};

public static DeviceInfo parse(String userAgent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Tests for {@link DeviceServiceImpl}.
Expand All @@ -12,13 +15,29 @@
*/
class DeviceServiceImplTest {

@Test
void deviceInfoParseTest() {
var info = DeviceServiceImpl.DeviceInfo.parse(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like "
+ "Gecko) Chrome/126.0.0.0 Safari/537.36");
assertThat(info.os()).isEqualTo("Mac OS X 10.15.7");
assertThat(info.browser()).isEqualTo("Chrome 126.0");
static Stream<Arguments> deviceInfoParseTest() {
return Stream.of(
Arguments.of(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like "
+ "Gecko) Chrome/126.0.0.0 Safari/537.36",
"Mac OS X 10.15.7",
"Chrome 126.0"
),
Arguments.of(
"Mozilla/5.0 (Phone; OpenHarmony 5.0) AppleWebKit/537.36 (KHTML, like Gecko) "
+ "Chrome/114.0.0.0 Safari/537.36 ArkWeb/4.1.6.1 Mobile HuaweiBrowser/5.0.4"
+ ".300",
"OpenHarmony 5.0",
"Chrome 114.0"
)
);
}

@ParameterizedTest
@MethodSource
void deviceInfoParseTest(String userAgent, String expectedOs, String expectedBrowser) {
var info = DeviceServiceImpl.DeviceInfo.parse(userAgent);
assertThat(info.os()).isEqualTo(expectedOs);
assertThat(info.browser()).isEqualTo(expectedBrowser);
}
}