Skip to content

Commit

Permalink
FuseTestContextFactory and customization for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed Oct 22, 2024
1 parent 50bf533 commit 0aeab5c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 26 deletions.
8 changes: 4 additions & 4 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"typescript": "5.5.4"
},
"dependencies": {
"tslib": "2.7.0",
"tslib": "2.8.0",
"uuid": "10.0.0"
}
}
13 changes: 12 additions & 1 deletion js/src/FuseContextBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ export class FuseContextBuilder {
private $platformResolver: PlatformResolver;
private $loggerFactory: AbstractFuseLoggerFactory | null;
private $apiFactory: AbstractFuseAPIFactory | null;
private $contextFactory: FuseContextFactory | null;

public constructor() {
this.$loggerFactory = null;
this.$apiFactory = null;
this.$platformResolver = new PlatformResolver();
this.$contextFactory = null;
}

public setPlatformResolver(resolver: PlatformResolver): FuseContextBuilder {
Expand All @@ -53,6 +55,11 @@ export class FuseContextBuilder {
return this;
}

public setContextFactory(factory: FuseContextFactory): FuseContextBuilder {
this.$contextFactory = factory;
return this;
}

protected async _isDebugMode(context: FuseContext): Promise<boolean> {
return await context.isDebugMode();
}
Expand All @@ -76,7 +83,11 @@ export class FuseContextBuilder {
loggerFactory = new FuseLoggerFactory(platform);
}

const contextFactory: FuseContextFactory = new FuseContextFactory();
let contextFactory: FuseContextFactory = this.$contextFactory;
if (contextFactory === null) {
contextFactory = new FuseContextFactory();
}

const context: FuseContext = contextFactory.create(platform, apiFactory, loggerFactory.create());

const isDebugMode: boolean = await this._isDebugMode(context);
Expand Down
2 changes: 1 addition & 1 deletion js/src/android/AndroidFuseContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { IInset } from '../IInset';
import { Platform } from '../Platform';

export class AndroidFuseContext extends FuseContext {
public constructor(apiFactory: AbstractFuseAPIFactory, logger: IFuseLogger,) {
public constructor(apiFactory: AbstractFuseAPIFactory, logger: IFuseLogger) {
super(Platform.ANDROID, apiFactory, logger);

this._getRuntime().registerInsetHandler((inset: IInset) => {
Expand Down
41 changes: 23 additions & 18 deletions js/src/test/FuseTestContext.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@

// /*
// Copyright 2023 Breautek
/*
Copyright 2024 Breautek
// 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
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
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.
// */
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.
*/

// import {AbstractFuseAPIFactory} from '../AbstractFuseAPIFactory';
// import {FuseContext} from '../FuseContext';
// import { PlatformResolver } from '../PlatformResolver';
// import { FuseTestAPIFactory } from './FuseTestAPIFactory';
// import { FuseTestPlataformResolver } from './FuseTestPlatformResolver';
import {AbstractFuseAPIFactory} from '../AbstractFuseAPIFactory';
import {FuseContext} from '../FuseContext';
import { Platform } from '../Platform';
import { IFuseLogger } from '../IFuseLogger';

// export class FuseTestContext extends FuseContext {}
export class FuseTestContext extends FuseContext {
public constructor(apiFactory: AbstractFuseAPIFactory, logger: IFuseLogger) {
super(Platform.TEST, apiFactory, logger);
}

public override async onWebviewReady(): Promise<void> {}
}
4 changes: 3 additions & 1 deletion js/src/test/FuseTestContextBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ limitations under the License.
import { FuseContext } from "../FuseContext";
import { FuseContextBuilder } from "../FuseContextBuilder";
import { FuseTestAPIFactory } from "./FuseTestAPIFactory";
import { FuseTestContextFactory } from './FuseTestContextFactory';
import { FuseTestPlataformResolver } from "./FuseTestPlatformResolver";

export class FuseTestContextBuilder extends FuseContextBuilder {
public constructor() {
super();
this.setPlatformResolver(new FuseTestPlataformResolver())
.setAPIFactory(new FuseTestAPIFactory());
.setAPIFactory(new FuseTestAPIFactory())
.setContextFactory(new FuseTestContextFactory());
}

protected override async _isDebugMode(context: FuseContext): Promise<boolean> {
Expand Down
34 changes: 34 additions & 0 deletions js/src/test/FuseTestContextFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/*
Copyright 2024 Breautek
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.
*/

import { AbstractFuseAPIFactory } from '../AbstractFuseAPIFactory';
import { FuseContext } from '../FuseContext';
import {FuseTestContext} from './FuseTestContext';
import {FuseContextFactory} from '../FuseContextFactory';
import { IFuseLogger } from '../IFuseLogger';
import { Platform } from '../Platform';

export class FuseTestContextFactory extends FuseContextFactory {
public override create(platform: Platform, apiFactory: AbstractFuseAPIFactory, logger: IFuseLogger): FuseContext {
if (platform === Platform.TEST) {
return new FuseTestContext(apiFactory, logger);
}
else {
return super.create(platform, apiFactory, logger);
}
}
}

0 comments on commit 0aeab5c

Please sign in to comment.