Skip to content

Commit

Permalink
feat: add support for OpenHarmony in device OS detection (#7045)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind feature 

#### What this PR does / why we need it:
This PR adds support for detecting OpenHarmony as a device operating system.

#### Which issue(s) this PR fixes:
Fixes #7039

#### Special notes for your reviewer:
This PR introduces minor changes in the device OS detection logic.

#### Does this PR introduce a user-facing change?
```release-note
新设备登录通知的操作系统名支持展示鸿蒙替代 Unknown
```
  • Loading branch information
Anyexyz authored Nov 18, 2024
1 parent 06f3c28 commit 2c8f6f5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
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);
}
}

0 comments on commit 2c8f6f5

Please sign in to comment.