1/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import {CommonModule} from '@angular/common';
17import {NO_ERRORS_SCHEMA} from '@angular/core';
18import {ComponentFixture, TestBed} from '@angular/core/testing';
19import {MatCheckboxModule} from '@angular/material/checkbox';
20import {MatDividerModule} from '@angular/material/divider';
21import {MatFormFieldModule} from '@angular/material/form-field';
22import {MatInputModule} from '@angular/material/input';
23import {MatSelectModule} from '@angular/material/select';
24import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
25import {assertDefined} from 'common/assert_utils';
26import {TraceConfigComponent} from './trace_config_component';
27
28describe('TraceConfigComponent', () => {
29  let fixture: ComponentFixture<TraceConfigComponent>;
30  let component: TraceConfigComponent;
31  let htmlElement: HTMLElement;
32
33  beforeEach(async () => {
34    await TestBed.configureTestingModule({
35      imports: [
36        CommonModule,
37        MatCheckboxModule,
38        MatDividerModule,
39        MatFormFieldModule,
40        MatInputModule,
41        MatSelectModule,
42        BrowserAnimationsModule,
43      ],
44      declarations: [TraceConfigComponent],
45      schemas: [NO_ERRORS_SCHEMA],
46    }).compileComponents();
47    fixture = TestBed.createComponent(TraceConfigComponent);
48    component = fixture.componentInstance;
49    htmlElement = fixture.nativeElement;
50    component.traceConfig = {
51      layers_trace: {
52        name: 'layers_trace',
53        run: false,
54        config: {
55          enableConfigs: [
56            {
57              name: 'trace buffers',
58              key: 'tracebuffers',
59              enabled: true,
60            },
61          ],
62          selectionConfigs: [
63            {
64              key: 'tracinglevel',
65              name: 'tracing level',
66              options: ['verbose', 'debug', 'critical'],
67              value: 'debug',
68            },
69          ],
70        },
71      },
72    };
73    fixture.detectChanges();
74  });
75
76  it('can be created', () => {
77    expect(component).toBeTruthy();
78  });
79
80  it('check that trace checkbox ticked on default run', () => {
81    assertDefined(component.traceConfig)['layers_trace'].run = true;
82    fixture.detectChanges();
83    const box = assertDefined(htmlElement.querySelector('.trace-checkbox'));
84    expect(box.innerHTML).toContain('aria-checked="true"');
85    expect(box.innerHTML).toContain('layers_trace');
86  });
87
88  it('check that trace checkbox not ticked on default run', () => {
89    assertDefined(component.traceConfig)['layers_trace'].run = false;
90    fixture.detectChanges();
91    const box = assertDefined(htmlElement.querySelector('.trace-checkbox'));
92    expect(box.innerHTML).toContain('aria-checked="false"');
93  });
94
95  it('check that advanced configs show', () => {
96    const enable_config_opt = assertDefined(
97      htmlElement.querySelector('.enable-config-opt'),
98    );
99    expect(enable_config_opt.innerHTML).toContain('trace buffers');
100    expect(enable_config_opt.innerHTML).not.toContain('tracing level');
101
102    const selection_config_opt = assertDefined(
103      htmlElement.querySelector('.selection-config-opt'),
104    );
105    expect(selection_config_opt.innerHTML).not.toContain('trace buffers');
106    expect(selection_config_opt.innerHTML).toContain('tracing level');
107  });
108
109  it('check that changing enable config causes box to change', async () => {
110    assertDefined(component.traceConfig)[
111      'layers_trace'
112    ].config!.enableConfigs[0].enabled = false;
113    fixture.detectChanges();
114    await fixture.whenStable();
115    expect(htmlElement.querySelector('.enable-config')?.innerHTML).toContain(
116      'aria-checked="false"',
117    );
118  });
119
120  it('check that changing selected config causes select to change', async () => {
121    fixture.detectChanges();
122    expect(htmlElement.querySelector('.config-selection')?.innerHTML).toContain(
123      'value="debug"',
124    );
125    assertDefined(component.traceConfig)[
126      'layers_trace'
127    ].config!.selectionConfigs[0].value = 'verbose';
128    fixture.detectChanges();
129    await fixture.whenStable();
130    expect(htmlElement.querySelector('.config-selection')?.innerHTML).toContain(
131      'value="verbose"',
132    );
133  });
134});
135