1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { LitChartScatter } from '../../../../src/base-ui/chart/scatter/LitChartScatter';
17fb726d48Sopenharmony_ciimport { LitChartScatterConfig } from '../../../../src/base-ui/chart/scatter/LitChartScatterConfig';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ci// @ts-ignore
20fb726d48Sopenharmony_ciwindow.ResizeObserver =
21fb726d48Sopenharmony_ci  window.ResizeObserver ||
22fb726d48Sopenharmony_ci  jest.fn().mockImplementation(() => ({
23fb726d48Sopenharmony_ci    disconnect: jest.fn(),
24fb726d48Sopenharmony_ci    observe: jest.fn(),
25fb726d48Sopenharmony_ci    unobserve: jest.fn(),
26fb726d48Sopenharmony_ci  }));
27fb726d48Sopenharmony_ci
28fb726d48Sopenharmony_cidescribe('LitChartScatter Test', () => {
29fb726d48Sopenharmony_ci  let litChartScatter = new LitChartScatter();
30fb726d48Sopenharmony_ci  litChartScatter.canvas = litChartScatter.shadowRoot!.querySelector<HTMLCanvasElement>('#canvas');
31fb726d48Sopenharmony_ci  litChartScatter.ctx = litChartScatter.canvas!.getContext('2d', { alpha: true });
32fb726d48Sopenharmony_ci  litChartScatter.connectedCallback();
33fb726d48Sopenharmony_ci  litChartScatter.options = {
34fb726d48Sopenharmony_ci    yAxisLabel: [20000, 40000, 60000, 80000, 100000],
35fb726d48Sopenharmony_ci    xAxisLabel: [20000, 40000, 60000, 80000, 100000, 120000],
36fb726d48Sopenharmony_ci    axisLabel: ['负载', '算力供给'],
37fb726d48Sopenharmony_ci    drawload: true,
38fb726d48Sopenharmony_ci    load: [100000, 1000],
39fb726d48Sopenharmony_ci    colorPool: () => ['#2f72f8', '#ffab67', '#a285d2'],
40fb726d48Sopenharmony_ci    colorPoolText: () => ['Total', 'CycleA', 'CycleB'],
41fb726d48Sopenharmony_ci    paintingData: [
42fb726d48Sopenharmony_ci      {
43fb726d48Sopenharmony_ci        x: 111.11939333333333,
44fb726d48Sopenharmony_ci        y: 173.71615585811537,
45fb726d48Sopenharmony_ci        r: 6,
46fb726d48Sopenharmony_ci        c: [16655.818, 2983.844141884629, 1, 5.582, 5.361],
47fb726d48Sopenharmony_ci        color: '#2f72f8',
48fb726d48Sopenharmony_ci      },
49fb726d48Sopenharmony_ci      {
50fb726d48Sopenharmony_ci        x: 77.52432666666667,
51fb726d48Sopenharmony_ci        y: 174.7453794947994,
52fb726d48Sopenharmony_ci        r: 6,
53fb726d48Sopenharmony_ci        c: [6577.298, 1954.6205052005942, 2, 3.365, 2.585],
54fb726d48Sopenharmony_ci        color: '#2f72f8',
55fb726d48Sopenharmony_ci      },
56fb726d48Sopenharmony_ci      {
57fb726d48Sopenharmony_ci        x: 100.43357333333333,
58fb726d48Sopenharmony_ci        y: 174.20508773882395,
59fb726d48Sopenharmony_ci        r: 6,
60fb726d48Sopenharmony_ci        c: [13450.072, 2494.912261176034, 3, 5.391, 5.287],
61fb726d48Sopenharmony_ci        color: '#2f72f8',
62fb726d48Sopenharmony_ci      },
63fb726d48Sopenharmony_ci    ],
64fb726d48Sopenharmony_ci    hoverData: {},
65fb726d48Sopenharmony_ci    globalGradient: {},
66fb726d48Sopenharmony_ci    data: [
67fb726d48Sopenharmony_ci      [
68fb726d48Sopenharmony_ci        [16655.818, 2983.844141884629, 1, 5.582, 5.361],
69fb726d48Sopenharmony_ci        [6577.298, 1954.6205052005942, 2, 3.365, 2.585],
70fb726d48Sopenharmony_ci        [13450.072, 2494.912261176034, 3, 5.391, 5.287],
71fb726d48Sopenharmony_ci      ],
72fb726d48Sopenharmony_ci      [],
73fb726d48Sopenharmony_ci      [],
74fb726d48Sopenharmony_ci    ],
75fb726d48Sopenharmony_ci    title: 'render_service 1155',
76fb726d48Sopenharmony_ci    tip: (data) => {
77fb726d48Sopenharmony_ci      return `
78fb726d48Sopenharmony_ci      <div>
79fb726d48Sopenharmony_ci          <span>Cycle: ${data.c[2]};</span></br>
80fb726d48Sopenharmony_ci          <span>Comsumption: ${data.c[0]};</span></br>
81fb726d48Sopenharmony_ci          <span>Cycle_dur: ${data.c[3]} ms;</span></br>
82fb726d48Sopenharmony_ci          <span>Running_dur: ${data.c[4]} ms;</span></br>
83fb726d48Sopenharmony_ci      </div>
84fb726d48Sopenharmony_ci      `;
85fb726d48Sopenharmony_ci    },
86fb726d48Sopenharmony_ci  };
87fb726d48Sopenharmony_ci  let options = litChartScatter.options!;
88fb726d48Sopenharmony_ci  it('LitChartScatter 01', function () {
89fb726d48Sopenharmony_ci    expect(litChartScatter.init()).toBeUndefined();
90fb726d48Sopenharmony_ci  });
91fb726d48Sopenharmony_ci
92fb726d48Sopenharmony_ci  it('LitChartScatter 02', function () {
93fb726d48Sopenharmony_ci    const drawBackgroundMock = jest.fn();
94fb726d48Sopenharmony_ci    litChartScatter.drawBackground = drawBackgroundMock;
95fb726d48Sopenharmony_ci    const drawScatterChartMock = jest.fn();
96fb726d48Sopenharmony_ci    litChartScatter.drawScatterChart = drawScatterChartMock;
97fb726d48Sopenharmony_ci    const setOffScreenMock = jest.fn();
98fb726d48Sopenharmony_ci    litChartScatter.setOffScreen = setOffScreenMock;
99fb726d48Sopenharmony_ci    const clearRectMock = jest.fn();
100fb726d48Sopenharmony_ci    litChartScatter.ctx!.clearRect = clearRectMock;
101fb726d48Sopenharmony_ci    litChartScatter.init();
102fb726d48Sopenharmony_ci    expect(drawBackgroundMock).toHaveBeenCalled();
103fb726d48Sopenharmony_ci    expect(drawScatterChartMock).toHaveBeenCalled();
104fb726d48Sopenharmony_ci    expect(setOffScreenMock).toHaveBeenCalled();
105fb726d48Sopenharmony_ci    expect(clearRectMock).toHaveBeenCalled();
106fb726d48Sopenharmony_ci  });
107fb726d48Sopenharmony_ci
108fb726d48Sopenharmony_ci  it('LitChartScatter 03', function () {
109fb726d48Sopenharmony_ci    expect(litChartScatter.setOffScreen()).toBeUndefined();
110fb726d48Sopenharmony_ci    litChartScatter.setOffScreen();
111fb726d48Sopenharmony_ci    expect(litChartScatter.canvas2?.height).toEqual(litChartScatter.clientHeight);
112fb726d48Sopenharmony_ci    expect(litChartScatter.canvas2?.width).toEqual(litChartScatter.clientWidth);
113fb726d48Sopenharmony_ci  });
114fb726d48Sopenharmony_ci
115fb726d48Sopenharmony_ci  it('LitChartScatter 04', function () {
116fb726d48Sopenharmony_ci    let hoverPoint = {
117fb726d48Sopenharmony_ci      x: 163.42456666666666,
118fb726d48Sopenharmony_ci      y: 160.69372623574142,
119fb726d48Sopenharmony_ci      r: 6,
120fb726d48Sopenharmony_ci      c: [3234.737, 559.0627376425856, 95, 5.786, 3.916],
121fb726d48Sopenharmony_ci      color: '#2f72f8',
122fb726d48Sopenharmony_ci    };
123fb726d48Sopenharmony_ci    expect(litChartScatter.drawBackground()).toBeUndefined();
124fb726d48Sopenharmony_ci    expect(litChartScatter.drawScatterChart(litChartScatter.options!)).toBeUndefined();
125fb726d48Sopenharmony_ci    expect(litChartScatter.drawAxis(litChartScatter.options!)).toBeUndefined();
126fb726d48Sopenharmony_ci    expect(litChartScatter.drawYLabels(litChartScatter.options!)).toBeUndefined();
127fb726d48Sopenharmony_ci    expect(litChartScatter.drawXLabels(litChartScatter.options!)).toBeUndefined();
128fb726d48Sopenharmony_ci    expect(litChartScatter.drawData(litChartScatter.options!)).toBeUndefined();
129fb726d48Sopenharmony_ci    expect(litChartScatter.drawCycle(2, 10, 1, 1, '#000000')).toBeUndefined();
130fb726d48Sopenharmony_ci    expect(litChartScatter.drawLoadLine(litChartScatter.options!.load)).toBeUndefined();
131fb726d48Sopenharmony_ci    expect(litChartScatter.drawBalanceLine(litChartScatter.options!.load)).toBeUndefined();
132fb726d48Sopenharmony_ci    expect(litChartScatter.resetHoverWithOffScreen()).toBeUndefined();
133fb726d48Sopenharmony_ci    expect(litChartScatter.paintHover()).toBeUndefined();
134fb726d48Sopenharmony_ci    expect(litChartScatter.connectedCallback()).toBeUndefined();
135fb726d48Sopenharmony_ci    expect(litChartScatter.initElements()).toBeUndefined();
136fb726d48Sopenharmony_ci  });
137fb726d48Sopenharmony_ci
138fb726d48Sopenharmony_ci  it('LitChartScatter 05', function () {
139fb726d48Sopenharmony_ci    litChartScatter.drawBackground();
140fb726d48Sopenharmony_ci    expect(litChartScatter.ctx?.save).toHaveBeenCalled();
141fb726d48Sopenharmony_ci    expect(litChartScatter.ctx?.fillRect).toHaveBeenCalled();
142fb726d48Sopenharmony_ci    expect(litChartScatter.ctx?.restore).toHaveBeenCalled();
143fb726d48Sopenharmony_ci  });
144fb726d48Sopenharmony_ci
145fb726d48Sopenharmony_ci  it('LitChartScatter 06', function () {
146fb726d48Sopenharmony_ci    litChartScatter.drawAxis(litChartScatter.options!);
147fb726d48Sopenharmony_ci    let ctx = litChartScatter.ctx!;
148fb726d48Sopenharmony_ci    expect(ctx.font).toEqual('10px KATTI');
149fb726d48Sopenharmony_ci    expect(ctx.fillStyle).toEqual('#000000');
150fb726d48Sopenharmony_ci    expect(ctx.strokeStyle).toEqual('#000000');
151fb726d48Sopenharmony_ci  });
152fb726d48Sopenharmony_ci
153fb726d48Sopenharmony_ci  it('LitChartScatter 07', function () {
154fb726d48Sopenharmony_ci    litChartScatter.drawYLabels(litChartScatter.options!);
155fb726d48Sopenharmony_ci    let ctx = litChartScatter.ctx!;
156fb726d48Sopenharmony_ci    expect(ctx.font).toEqual('12px KATTI');
157fb726d48Sopenharmony_ci    expect(ctx.fillStyle).toEqual('#000000');
158fb726d48Sopenharmony_ci    expect(ctx.strokeStyle).toEqual('#000000');
159fb726d48Sopenharmony_ci  });
160fb726d48Sopenharmony_ci
161fb726d48Sopenharmony_ci  it('LitChartScatter 08', function () {
162fb726d48Sopenharmony_ci    litChartScatter.drawXLabels(litChartScatter.options!);
163fb726d48Sopenharmony_ci    let ctx = litChartScatter.ctx!;
164fb726d48Sopenharmony_ci    expect(ctx.fillStyle).toEqual('#000000');
165fb726d48Sopenharmony_ci    expect(ctx.strokeStyle).toEqual('#000000');
166fb726d48Sopenharmony_ci    expect(ctx.beginPath).toHaveBeenCalled();
167fb726d48Sopenharmony_ci  });
168fb726d48Sopenharmony_ci
169fb726d48Sopenharmony_ci  it('LitChartScatter 9', function () {
170fb726d48Sopenharmony_ci    const mockInit = jest.fn();
171fb726d48Sopenharmony_ci    litChartScatter.init = mockInit;
172fb726d48Sopenharmony_ci    litChartScatter.config = options;
173fb726d48Sopenharmony_ci    expect(mockInit).toHaveBeenCalled();
174fb726d48Sopenharmony_ci    expect(litChartScatter.options).toEqual(options);
175fb726d48Sopenharmony_ci  });
176fb726d48Sopenharmony_ci});
177