1// Copyright (C) 2021 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {createEmptyState} from '../common/empty_state'; 16import {AreaById} from '../common/state'; 17import {globals} from '../frontend/globals'; 18 19import {AreaSelectionHandler} from './area_selection_handler'; 20 21test('validAreaAfterUndefinedArea', () => { 22 const areaId = '0'; 23 const latestArea: AreaById = {start: 0n, end: 1n, tracks: [], id: areaId}; 24 globals.state = createEmptyState(); 25 globals.state.currentSelection = {kind: 'AREA', areaId}; 26 globals.state.areas[areaId] = latestArea; 27 28 const areaSelectionHandler = new AreaSelectionHandler(); 29 const [hasAreaChanged, selectedArea] = areaSelectionHandler.getAreaChange(); 30 31 expect(hasAreaChanged).toEqual(true); 32 expect(selectedArea).toEqual(latestArea); 33}); 34 35test('UndefinedAreaAfterValidArea', () => { 36 const previousAreaId = '0'; 37 const previous: 38 AreaById = {start: 0n, end: 1n, tracks: [], id: previousAreaId}; 39 globals.state = createEmptyState(); 40 globals.state.currentSelection = { 41 kind: 'AREA', 42 areaId: previousAreaId, 43 }; 44 globals.state.areas[previousAreaId] = previous; 45 const areaSelectionHandler = new AreaSelectionHandler(); 46 areaSelectionHandler.getAreaChange(); 47 48 const currentAreaId = '1'; 49 globals.state.currentSelection = { 50 kind: 'AREA', 51 areaId: currentAreaId, 52 }; 53 const [hasAreaChanged, selectedArea] = areaSelectionHandler.getAreaChange(); 54 55 expect(hasAreaChanged).toEqual(true); 56 expect(selectedArea).toEqual(undefined); 57}); 58 59test('UndefinedAreaAfterUndefinedArea', () => { 60 globals.state.currentSelection = {kind: 'AREA', areaId: '0'}; 61 const areaSelectionHandler = new AreaSelectionHandler(); 62 areaSelectionHandler.getAreaChange(); 63 64 globals.state.currentSelection = {kind: 'AREA', areaId: '1'}; 65 const [hasAreaChanged, selectedArea] = areaSelectionHandler.getAreaChange(); 66 67 expect(hasAreaChanged).toEqual(true); 68 expect(selectedArea).toEqual(undefined); 69}); 70 71test('validAreaAfterValidArea', () => { 72 const previousAreaId = '0'; 73 const previous: 74 AreaById = {start: 0n, end: 1n, tracks: [], id: previousAreaId}; 75 globals.state = createEmptyState(); 76 globals.state.currentSelection = { 77 kind: 'AREA', 78 areaId: previousAreaId, 79 }; 80 globals.state.areas[previousAreaId] = previous; 81 const areaSelectionHandler = new AreaSelectionHandler(); 82 areaSelectionHandler.getAreaChange(); 83 84 const currentAreaId = '1'; 85 const current: AreaById = {start: 1n, end: 2n, tracks: [], id: currentAreaId}; 86 globals.state.currentSelection = { 87 kind: 'AREA', 88 areaId: currentAreaId, 89 }; 90 globals.state.areas[currentAreaId] = current; 91 const [hasAreaChanged, selectedArea] = areaSelectionHandler.getAreaChange(); 92 93 expect(hasAreaChanged).toEqual(true); 94 expect(selectedArea).toEqual(current); 95}); 96 97test('sameAreaSelected', () => { 98 const previousAreaId = '0'; 99 const previous: 100 AreaById = {start: 0n, end: 1n, tracks: [], id: previousAreaId}; 101 globals.state = createEmptyState(); 102 globals.state.currentSelection = { 103 kind: 'AREA', 104 areaId: previousAreaId, 105 }; 106 globals.state.areas[previousAreaId] = previous; 107 const areaSelectionHandler = new AreaSelectionHandler(); 108 areaSelectionHandler.getAreaChange(); 109 110 const currentAreaId = '0'; 111 const current: AreaById = {start: 0n, end: 1n, tracks: [], id: currentAreaId}; 112 globals.state.currentSelection = { 113 kind: 'AREA', 114 areaId: currentAreaId, 115 }; 116 globals.state.areas[currentAreaId] = current; 117 const [hasAreaChanged, selectedArea] = areaSelectionHandler.getAreaChange(); 118 119 expect(hasAreaChanged).toEqual(false); 120 expect(selectedArea).toEqual(current); 121}); 122 123test('NonAreaSelectionAfterUndefinedArea', () => { 124 globals.state.currentSelection = {kind: 'AREA', areaId: '0'}; 125 const areaSelectionHandler = new AreaSelectionHandler(); 126 areaSelectionHandler.getAreaChange(); 127 128 globals.state 129 .currentSelection = {kind: 'COUNTER', leftTs: 0n, rightTs: 0n, id: 1}; 130 const [hasAreaChanged, selectedArea] = areaSelectionHandler.getAreaChange(); 131 132 expect(hasAreaChanged).toEqual(false); 133 expect(selectedArea).toEqual(undefined); 134}); 135