/
still-image.component.spec.ts
264 lines (211 loc) · 9.76 KB
/
still-image.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { CdkCopyToClipboard } from '@angular/cdk/clipboard';
import { OverlayContainer } from '@angular/cdk/overlay';
import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { HttpClientModule } from '@angular/common/http';
import { Component, OnInit, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatIconModule } from '@angular/material/icon';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { MatToolbarModule } from '@angular/material/toolbar';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { Constants, MockResource, ReadGeomValue, ReadResource, ReadValue } from '@dasch-swiss/dsp-js';
import { AppInitService } from 'src/app/app-init.service';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { FileRepresentation } from '../file-representation';
import { Region, StillImageComponent } from './still-image.component';
// --> TODO: get test data from dsp-js
// --> TODO: get this from dsp-js: https://dasch.myjetbrains.com/youtrack/issue/DSP-506
const stillImageFileValue = {
'type': 'http://api.knora.org/ontology/knora-api/v2#StillImageFileValue',
'id': 'http://rdfh.ch/0803/00014b43f902/values/18dc0912cd05',
'attachedToUser': 'http://rdfh.ch/users/91e19f1e01',
'arkUrl': 'http://0.0.0.0:3336/ark:/72163/1/0803/00014b43f902l/000000000018dc0912cd0wl',
'versionArkUrl': 'http://0.0.0.0:3336/ark:/72163/1/0803/00014b43f902l/000000000018dc0912cd0wl.20121121T165038Z',
'valueCreationDate': '2012-11-21T16:50:38Z',
'hasPermissions': 'CR knora-admin:Creator|M knora-admin:ProjectMember|V knora-admin:KnownUser|RV knora-admin:UnknownUser',
'userHasPermission': 'RV',
'uuid': '000000000018dc0912cd0w',
'filename': 'incunabula_0000003328.jp2',
'fileUrl': 'http://0.0.0.0:1024/0803/incunabula_0000003328.jp2/full/1312,1815/0/default.jpg',
'dimX': 1312,
'dimY': 1815,
'iiifBaseUrl': 'http://0.0.0.0:1024/0803',
'strval': 'http://0.0.0.0:1024/0803/incunabula_0000003328.jp2/full/1312,1815/0/default.jpg',
'property': 'http://api.knora.org/ontology/knora-api/v2#hasStillImageFileValue',
'propertyLabel': 'has image file',
'propertyComment': 'Connects a Representation to an image file'
};
// --> TODO: remove dummy regions: https://dasch.myjetbrains.com/youtrack/issue/DSP-506
const rectangleGeom = `{
"status": "active",
"lineColor": "#ff3333",
"lineWidth": 2,
"points": [{
"x": 0.0989010989010989,
"y": 0.18055555555555555
}, {
"x": 0.7252747252747253,
"y": 0.7245370370370371
}],
"type": "rectangle"
}`;
class Geom extends ReadValue {
geometryString: string;
}
function makeRegion(geomString: string[], iri: string): ReadResource {
const geomVals = geomString.map(geom => {
const parseReg = new Geom();
parseReg.geometryString = geom;
return new ReadGeomValue(parseReg);
});
const regionRes = new ReadResource();
regionRes.id = iri;
regionRes.properties[Constants.HasGeometry] = geomVals;
return regionRes;
}
@Component({
template: `
<app-still-image [images]="stillImageFileRepresentations"
[imageCaption]="caption"
[iiifUrl]="iiifUrl"
[activateRegion]="inputActivateRegion"
[currentTab]="'annotations'"
[parentResource]="readresource"
(regionClicked)="regHovered($event)">
</app-still-image>`
})
class TestHostComponent implements OnInit {
@ViewChild(StillImageComponent) osdViewerComp: StillImageComponent;
readResource: ReadResource;
stillImageFileRepresentations: FileRepresentation[] = [];
caption = 'test image';
iiifUrl = 'https://iiif.test.dasch.swiss:443/0803/incunabula_0000003840.jp2/full/3210,5144/0/default.jpg';
inputActivateRegion: string;
activeRegion: string;
ngOnInit() {
MockResource.getTestThing().subscribe(res => {
this.readResource = res;
});
this.stillImageFileRepresentations = [
new FileRepresentation(stillImageFileValue,
[new Region(makeRegion([rectangleGeom], 'first'))]
)
];
}
regHovered(regIri: string) {
this.activeRegion = regIri;
}
}
describe('StillImageComponent', () => {
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
let rootLoader: HarnessLoader;
let overlayContainer: OverlayContainer;
beforeEach(waitForAsync(() => {
const adminSpyObj = {
v2: {
res: jasmine.createSpyObj('res', ['createResource'])
}
};
TestBed.configureTestingModule({
declarations: [
StillImageComponent,
TestHostComponent,
CdkCopyToClipboard
],
imports: [
BrowserAnimationsModule,
HttpClientModule,
MatDialogModule,
MatIconModule,
MatSnackBarModule,
MatToolbarModule,
],
providers: [
AppInitService,
{
provide: DspApiConnectionToken,
useValue: adminSpyObj
},
{
provide: MAT_DIALOG_DATA,
useValue: {}
},
{
provide: MatDialogRef,
useValue: {}
},
]
})
.compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
overlayContainer = TestBed.inject(OverlayContainer);
rootLoader = TestbedHarnessEnvironment.documentRootLoader(testHostFixture);
});
it('should create', () => {
expect(testHostComponent).toBeTruthy();
expect(testHostComponent.osdViewerComp).toBeTruthy();
});
// atm StillImageOSDViewerComponent has not many public methods or members.
// to be able to still test state of StillImageOSDViewerComponent we use the following technique for the first couple of tests:
// test private methods, members with: component["method"](param), or component["member"]
// this prevents TS compiler from restricting access, while still checking type safety.
it('should have initialized viewer after resources change', () => {
expect(testHostComponent.osdViewerComp['_viewer']).toBeTruthy();
});
it('should have OpenSeadragon.Viewer.isVisible() == true after resources change', () => {
expect(testHostComponent.osdViewerComp['_viewer'].isVisible()).toBeTruthy();
});
it('should have 1 image loaded after resources change with 1 full size image', done => {
testHostComponent.osdViewerComp['_viewer'].addHandler('open', (args) => {
expect(testHostComponent.osdViewerComp['_viewer'].world.getItemCount()).toEqual(1);
done();
});
});
it('should display the image caption', () => {
const hostCompDe = testHostFixture.debugElement;
const stillImageComponentDe = hostCompDe.query(By.directive(StillImageComponent));
const captionDebugElement = stillImageComponentDe.query(By.css('.caption'));
const captionEle = captionDebugElement.nativeElement;
expect(captionEle.innerText).toEqual('test image');
});
it('should display the iiifUrl of the image', () => {
const hostCompDe = testHostFixture.debugElement;
const stillImageComponentDe = hostCompDe.query(By.directive(StillImageComponent));
const iiifUrlDebugElement = stillImageComponentDe.query(By.css('.iiif-url a'));
const iiifUrlEle = iiifUrlDebugElement.nativeElement;
expect(iiifUrlEle.innerText).toEqual('https://iiif.test.dasch.swiss:443/0803/incunabula_0000003840.jp2/full/3210,5144/0/default.jpg');
expect(iiifUrlEle.getAttribute('target')).toEqual('_blank');
expect(iiifUrlEle.getAttribute('href')).toEqual('https://iiif.test.dasch.swiss:443/0803/incunabula_0000003840.jp2/full/3210,5144/0/default.jpg');
});
it('should have 1 test region loaded (rectangle)', () => {
const osd = testHostComponent.osdViewerComp['_viewer'];
expect(osd.element.getElementsByClassName('region').length).toEqual(1);
});
it('should emit the region\'s Iri when a region is clicked', () => {
const osd = testHostComponent.osdViewerComp['_viewer'];
const overlayElement = osd.element.getElementsByClassName('region')[0];
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
overlayElement.dispatchEvent(event);
testHostFixture.detectChanges();
expect(testHostComponent.activeRegion).toEqual('first');
});
// it('should open the dialog box when the replace image button is clicked', async () => {
// const replaceImageButton = await rootLoader.getHarness(MatButtonHarness.with({ selector: '.replace-image' }));
// console.log('replaceImageButton: ', replaceImageButton);
// await replaceImageButton.click();
// const dialogHarnesses = await rootLoader.getAllHarnesses(MatDialogHarness);
// expect(dialogHarnesses.length).toEqual(1);
// });
});