Skip to content

react-native-get-random-values适配harmony #2

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

Merged
merged 2 commits into from
May 13, 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 @@ -28,7 +28,6 @@ public String getName() {
public String getRandomBase64(int byteLength) throws NoSuchAlgorithmException {
byte[] data = new byte[byteLength];
SecureRandom random = new SecureRandom();

random.nextBytes(data);

return Base64.encodeToString(data, Base64.NO_WRAP);
Expand Down
Binary file added harmony/get_random_values.har
Binary file not shown.
6 changes: 6 additions & 0 deletions harmony/get_random_values/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/node_modules
/oh_modules
/.preview
/build
/.cxx
/.test
6 changes: 6 additions & 0 deletions harmony/get_random_values/BuildProfile.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default class BuildProfile {
static readonly HAR_VERSION = '1.11.0-0.0.1';
static readonly BUILD_MODE_NAME = 'debug';
static readonly DEBUG = true;
static readonly TARGET_NAME = 'default';
}
1 change: 1 addition & 0 deletions harmony/get_random_values/Index.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./ts";
28 changes: 28 additions & 0 deletions harmony/get_random_values/build-profile.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"apiType": "stageMode",
"buildOption": {
},
"buildOptionSet": [
{
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": true,
"files": [
"./obfuscation-rules.txt"
]
}
}
}
},
],
"targets": [
{
"name": "default"
},
{
"name": "ohosTest",
}
]
}
1 change: 1 addition & 0 deletions harmony/get_random_values/hvigorfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { harTasks } from '@ohos/hvigor-ohos-plugin';
18 changes: 18 additions & 0 deletions harmony/get_random_values/obfuscation-rules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Define project specific obfuscation rules here.
# You can include the obfuscation configuration files in the current module's build-profile.json5.
#
# For more details, see
# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md

# Obfuscation options:
# -disable-obfuscation: disable all obfuscations
# -enable-property-obfuscation: obfuscate the property names
# -enable-toplevel-obfuscation: obfuscate the names in the global scope
# -compact: remove unnecessary blank spaces and all line feeds
# -remove-log: remove all console.* statements
# -print-namecache: print the name cache that contains the mapping from the old names to new names
# -apply-namecache: reuse the given cache file

# Keep options:
# -keep-property-name: specifies property names that you want to keep
# -keep-global-name: specifies names that you want to keep in the global scope
12 changes: 12 additions & 0 deletions harmony/get_random_values/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@react-native-oh-tpl/react-native-get-random-values",
"version": "1.11.0-0.0.1",
"description": "Please describe the basic information.",
"main": "Index.ets",
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@rnoh/react-native-openharmony": 'file:../react_native_openharmony'
}
}

23 changes: 23 additions & 0 deletions harmony/get_random_values/src/main/ets/RNGetRandomValuesPackage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/ts';
import type { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
import { TM } from '@rnoh/react-native-openharmony/generated/ts'
import { RNGetRandomValuesTurboModule } from './RNGetRandomValuesTurboModule';

class RNGetRandomValuesTurboModuleFactory extends TurboModulesFactory {
createTurboModule(name: string): TurboModule | null {
if (this.hasTurboModule(name)) {
return new RNGetRandomValuesTurboModule(this.ctx);
}
return null;
}

hasTurboModule(name: string): boolean {
return name === TM.GetRandomValuesNativeModule.NAME;
}
}

export class RNGetRandomValuesPackage extends RNPackage {
createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
return new RNGetRandomValuesTurboModuleFactory(ctx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TurboModule } from '@rnoh/react-native-openharmony/ts';
import { TM } from '@rnoh/react-native-openharmony/generated/ts';
import window from '@ohos.window';
import { util } from '@kit.ArkTS';


export class RNGetRandomValuesTurboModule extends TurboModule implements TM.GetRandomValuesNativeModule.Spec {
windowClass:window.Window| undefined = undefined;
isKeepScreenOn: boolean = true;
unisKeepScreenOn: boolean = false;

constructor(ctx) {
super(ctx);
}


getRandomBase64(byteLength:number){
const randomBytes = new Uint8Array(byteLength)
for(var i = 0 ; i<byteLength;i++){
randomBytes[i] = Math.floor(Math.random()*256)
}
const that = new util.Base64Helper()
const base64String = that.encodeToStringSync(randomBytes);
//将随机字节数组转换为Base64编码的字符串
return base64String
}



}



11 changes: 11 additions & 0 deletions harmony/get_random_values/src/main/module.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"module": {
"name": "get_random_values",
"type": "har",
"deviceTypes": [
"default",
"tablet",
"2in1"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"color": [
{
"name": "start_window_background",
"value": "#FFFFFF"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"string": [
{
"name": "module_desc",
"value": "module description"
},
{
"name": "Get_random_valuesAbility_desc",
"value": "description"
},
{
"name": "Get_random_valuesAbility_label",
"value": "label"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"layered-image":
{
"background" : "$media:background",
"foreground" : "$media:foreground"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"src": [
"pages/Index"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"string": [
{
"name": "module_desc",
"value": "module description"
},
{
"name": "Get_random_valuesAbility_desc",
"value": "description"
},
{
"name": "Get_random_valuesAbility_label",
"value": "label"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"string": [
{
"name": "module_desc",
"value": "模块描述"
},
{
"name": "Get_random_valuesAbility_desc",
"value": "description"
},
{
"name": "Get_random_valuesAbility_label",
"value": "label"
}
]
}
35 changes: 35 additions & 0 deletions harmony/get_random_values/src/ohosTest/ets/test/Ability.test.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { hilog } from '@kit.PerformanceAnalysisKit';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';

export default function abilityTest() {
describe('ActsAbilityTest', () => {
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
beforeAll(() => {
// Presets an action, which is performed only once before all test cases of the test suite start.
// This API supports only one parameter: preset action function.
})
beforeEach(() => {
// Presets an action, which is performed before each unit test case starts.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: preset action function.
})
afterEach(() => {
// Presets a clear action, which is performed after each unit test case ends.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: clear action function.
})
afterAll(() => {
// Presets a clear action, which is performed after all test cases of the test suite end.
// This API supports only one parameter: clear action function.
})
it('assertContain', 0, () => {
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
let a = 'abc';
let b = 'b';
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
expect(a).assertContain(b);
expect(a).assertEqual(a);
})
})
}
5 changes: 5 additions & 0 deletions harmony/get_random_values/src/ohosTest/ets/test/List.test.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import abilityTest from './Ability.test';

export default function testsuite() {
abilityTest();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { abilityDelegatorRegistry } from '@kit.TestKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { Hypium } from '@ohos/hypium';
import testsuite from '../test/List.test';

export default class TestAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate');
hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');
let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator;
abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator();
let abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs;
abilityDelegatorArguments = abilityDelegatorRegistry.getArguments();
hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!');
Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite);
}

onDestroy() {
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy');
}

onWindowStageCreate(windowStage: window.WindowStage) {
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate');
windowStage.loadContent('testability/pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
}

onWindowStageDestroy() {
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy');
}

onForeground() {
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground');
}

onBackground() {
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@Entry
@Component
struct Index {
@State message: string = 'Hello World';

build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}
Loading