• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2025 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 {MatButtonModule} from '@angular/material/button';
20import {MatIconModule} from '@angular/material/icon';
21import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
22import {assertDefined} from 'common/assert_utils';
23import {ConnectionState} from 'trace_collection/connection_state';
24import {WdpSetupComponent} from './wdp_setup_component';
25
26describe('WdpSetupComponent', () => {
27  let fixture: ComponentFixture<WdpSetupComponent>;
28  let component: WdpSetupComponent;
29  let htmlElement: HTMLElement;
30
31  beforeEach(async () => {
32    await TestBed.configureTestingModule({
33      imports: [
34        CommonModule,
35        MatIconModule,
36        BrowserAnimationsModule,
37        MatButtonModule,
38      ],
39      declarations: [WdpSetupComponent],
40      schemas: [NO_ERRORS_SCHEMA],
41    }).compileComponents();
42    fixture = TestBed.createComponent(WdpSetupComponent);
43    component = fixture.componentInstance;
44    htmlElement = fixture.nativeElement;
45    component.state = ConnectionState.CONNECTING;
46  });
47
48  it('can be created', () => {
49    expect(component).toBeTruthy();
50  });
51
52  it('correct connecting message', () => {
53    fixture.detectChanges();
54    expect(
55      htmlElement.querySelector('.connecting-message')?.textContent,
56    ).toContain('Connecting...');
57    expect(htmlElement.querySelector('.retry')).toBeNull();
58    expect(htmlElement.querySelector('.install')).toBeNull();
59  });
60
61  it('correct icon and message displays if no proxy', () => {
62    component.state = ConnectionState.NOT_FOUND;
63    fixture.detectChanges();
64    const text = htmlElement.querySelector(
65      '.further-adb-info-text',
66    )?.textContent;
67    expect(text).toContain(
68      "Failed to connect. Web Device Proxy doesn't seem to be running.",
69    );
70    expect(text).toContain('Please check you have Web Device Proxy installed.');
71    checkRetryButton();
72
73    const windowSpy = spyOn(window, 'open');
74    assertDefined(
75      htmlElement.querySelector<HTMLButtonElement>('.install'),
76    ).click();
77    fixture.detectChanges();
78    expect(windowSpy).toHaveBeenCalledOnceWith(
79      'https://tools.google.com/dlpage/android_web_device_proxy',
80      '_blank',
81    );
82  });
83
84  it('correct icon and message displays if unauthorized proxy', () => {
85    component.state = ConnectionState.UNAUTH;
86    fixture.detectChanges();
87    expect(htmlElement.querySelector('.adb-info')?.textContent).toEqual(
88      'Web Device Proxy not yet authorized. Enable popups and try again.',
89    );
90    expect(htmlElement.querySelector('.adb-icon')?.textContent).toEqual('lock');
91    checkRetryButton();
92    expect(htmlElement.querySelector('.install')).toBeNull();
93  });
94
95  function checkRetryButton() {
96    const spy = spyOn(assertDefined(component.retryConnection), 'emit');
97    assertDefined(
98      htmlElement.querySelector<HTMLButtonElement>('.retry'),
99    ).click();
100    fixture.detectChanges();
101    expect(spy).toHaveBeenCalled();
102  }
103});
104