Skip to content

Commit dbcd847

Browse files
hoonjicopybara-github
authored andcommitted
ADK changes
PiperOrigin-RevId: 843140933
1 parent ffd5e20 commit dbcd847

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

src/app/components/view-image-dialog/view-image-dialog.component.spec.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
1919
import {MAT_DIALOG_DATA, MatDialogModule, MatDialogRef} from '@angular/material/dialog';
2020
import {By} from '@angular/platform-browser';
2121

22-
22+
import {SAFE_VALUES_SERVICE} from '../../core/services/interfaces/safevalues';
23+
import {MockSafeValuesService} from '../../core/services/testing/mock-safevalues.service';
2324
import {ViewImageDialogComponent, ViewImageDialogData} from './view-image-dialog.component';
2425

2526
describe('ViewImageDialogComponent', () => {
@@ -28,23 +29,30 @@ describe('ViewImageDialogComponent', () => {
2829
let mockDialogRef: MatDialogRef<ViewImageDialogComponent>;
2930
let mockDialogData: ViewImageDialogData;
3031

32+
let mockSafeValuesService: MockSafeValuesService;
33+
3134
beforeEach(async () => {
3235
mockDialogRef = jasmine.createSpyObj('MatDialogRef', ['close']);
3336
mockDialogData = {imageData: null};
3437

3538
await TestBed.configureTestingModule({
36-
imports: [MatDialogModule, ViewImageDialogComponent],
39+
imports: [
40+
MatDialogModule, ViewImageDialogComponent
41+
],
3742
providers: [
3843
{provide: MatDialogRef, useValue: mockDialogRef},
3944
{provide: MAT_DIALOG_DATA, useValue: mockDialogData},
45+
{provide: SAFE_VALUES_SERVICE, useClass: MockSafeValuesService},
4046
],
4147
}).compileComponents();
4248
});
4349

4450
beforeEach(() => {
4551
fixture = TestBed.createComponent(ViewImageDialogComponent);
4652
component = fixture.componentInstance;
47-
fixture.detectChanges(); // Initial change detection
53+
mockSafeValuesService =
54+
TestBed.inject(SAFE_VALUES_SERVICE) as MockSafeValuesService;
55+
fixture.detectChanges(); // Initial change detection
4856
});
4957

5058
it('should create', () => {
@@ -56,14 +64,19 @@ describe('ViewImageDialogComponent', () => {
5664
const base64Image =
5765
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
5866
mockDialogData.imageData = base64Image;
59-
component.ngOnInit(); // Manually call ngOnInit as it's not triggered by
60-
// fixture.detectChanges for @Input changes
67+
mockSafeValuesService.bypassSecurityTrustUrl.and.returnValue(
68+
'data:image/png;base64,' + base64Image);
69+
70+
component.ngOnInit(); // Manually call ngOnInit as it's not triggered by
71+
// fixture.detectChanges for @Input changes
6172
fixture.detectChanges();
6273

6374
const imgElement = fixture.debugElement.query(By.css('.image-wrapper img'));
6475
expect(imgElement).not.toBeNull();
65-
expect(imgElement.nativeElement.src)
66-
.toContain('data:image/png;base64,' + base64Image);
76+
expect(mockSafeValuesService.bypassSecurityTrustUrl)
77+
.toHaveBeenCalledWith('data:image/png;base64,' + base64Image);
78+
expect(imgElement.nativeElement.src).toEqual(
79+
'data:image/png;base64,' + base64Image);
6780
expect(component.isSvgContent).toBeFalse();
6881
});
6982

src/app/components/view-image-dialog/view-image-dialog.component.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
19-
import {Component, Inject, OnInit} from '@angular/core';
18+
import {SAFE_VALUES_SERVICE} from '../../core/services/interfaces/safevalues';
19+
import {Component, inject, OnInit} from '@angular/core';
2020
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
21-
import {DomSanitizer, SafeHtml, SafeUrl} from '@angular/platform-browser';
21+
import {SafeHtml, SafeUrl} from '@angular/platform-browser';
2222

2323
export interface ViewImageDialogData {
2424
imageData: string|null;
@@ -35,10 +35,9 @@ export class ViewImageDialogComponent implements OnInit {
3535
// Flag to determine if the content is SVG
3636
isSvgContent: boolean = false;
3737

38-
constructor(
39-
public dialogRef: MatDialogRef<ViewImageDialogComponent>,
40-
@Inject(MAT_DIALOG_DATA) public data: ViewImageDialogData,
41-
private sanitizer: DomSanitizer) {}
38+
readonly dialogRef = inject(MatDialogRef<ViewImageDialogComponent>);
39+
readonly data = inject<ViewImageDialogData>(MAT_DIALOG_DATA);
40+
private readonly safeValuesService = inject(SAFE_VALUES_SERVICE);
4241

4342
/**
4443
* Lifecycle hook to initialize the component.
@@ -64,15 +63,15 @@ export class ViewImageDialogComponent implements OnInit {
6463
// Check if the data looks like SVG
6564
if (imageData.trim().includes('<svg')) {
6665
this.isSvgContent = true;
67-
this.displayContent = this.sanitizer.bypassSecurityTrustHtml(imageData);
66+
this.displayContent = this.safeValuesService.bypassSecurityTrustHtml(imageData);
6867
} else {
6968
// Assume it's base64 data if not SVG.
7069
// Ensure it has the correct data URI prefix.
7170
const prefix =
7271
imageData.startsWith('data:image/') ? '' : 'data:image/png;base64,';
7372
this.isSvgContent = false;
7473
this.displayContent =
75-
this.sanitizer.bypassSecurityTrustUrl(prefix + imageData);
74+
this.safeValuesService.bypassSecurityTrustUrl(prefix + imageData);
7675
}
7776
}
7877

@@ -82,4 +81,4 @@ export class ViewImageDialogComponent implements OnInit {
8281
close(): void {
8382
this.dialogRef.close();
8483
}
85-
}
84+
}

src/app/core/services/interfaces/safevalues.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
*/
1717

1818
import {InjectionToken} from '@angular/core';
19-
import {SafeHtml} from '@angular/platform-browser';
20-
// 1p-ONLY-IMPORT
19+
import {SafeHtml, SafeUrl} from '@angular/platform-browser';
2120

2221
export const SAFE_VALUES_SERVICE = new InjectionToken<SafeValuesService>(
2322
'SafeValuesService',
@@ -37,6 +36,7 @@ declare interface SafeValuesServiceInterface {
3736
openBlobUrl(blob: Blob): Window | null;
3837
setAnchorHref(a: HTMLAnchorElement, url: string): void;
3938
bypassSecurityTrustHtml(value: string): SafeHtml;
39+
bypassSecurityTrustUrl(url: string): SafeUrl;
4040
openBase64InNewTab(dataUrl: string, mimeType: string): void;
4141
}
4242

@@ -58,6 +58,8 @@ export abstract class SafeValuesService implements SafeValuesServiceInterface {
5858

5959
abstract bypassSecurityTrustHtml(value: string): SafeHtml;
6060

61+
abstract bypassSecurityTrustUrl(url: string): SafeUrl;
62+
6163
openBase64InNewTab(dataUrl: string, mimeType: string) {
6264
try {
6365
if (!dataUrl) {

src/app/core/services/safevalues.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import {inject, Injectable} from '@angular/core';
19-
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
19+
import {DomSanitizer, SafeHtml, SafeUrl} from '@angular/platform-browser';
2020

2121
import {SafeValuesService} from './interfaces/safevalues';
2222

@@ -54,4 +54,8 @@ export class SafeValuesServiceImpl extends SafeValuesService {
5454
bypassSecurityTrustHtml(content: string): SafeHtml {
5555
return this.sanitizer.bypassSecurityTrustHtml(content);
5656
}
57+
58+
bypassSecurityTrustUrl(url: string): SafeUrl {
59+
return this.sanitizer.bypassSecurityTrustUrl(url);
60+
}
5761
}

src/app/core/services/testing/mock-safevalues.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import {SafeValuesService} from '../interfaces/safevalues';
2020

2121
/** Mock implementation of SafeValuesService. */
2222
@Injectable()
23-
export class MockSafeValuesService implements Partial<SafeValuesService> {
23+
export class MockSafeValuesService implements SafeValuesService {
2424
windowOpen = jasmine.createSpy('windowOpen');
2525
createObjectUrl = jasmine.createSpy('createObjectUrl');
2626
openBlobUrl = jasmine.createSpy('openBlobUrl');
2727
setAnchorHref = jasmine.createSpy('setAnchorHref');
2828
openBase64InNewTab = jasmine.createSpy('openBase64InNewTab');
2929
bypassSecurityTrustHtml = jasmine.createSpy('bypassSecurityTrustHtml');
30+
bypassSecurityTrustUrl = jasmine.createSpy('bypassSecurityTrustUrl');
3031
}

0 commit comments

Comments
 (0)