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 {FlagsForTesting as Flags, FlagStore} from '../core/feature_flags'; 16 17class TestFlagStore implements FlagStore { 18 o: object = {}; 19 20 load(): object { 21 return this.o; 22 } 23 24 save(o: object): void { 25 this.o = o; 26 } 27} 28 29test('create flag', () => { 30 const flags = new Flags(new TestFlagStore()); 31 const myFlag = flags.register({ 32 id: 'myFlag', 33 defaultValue: false, 34 description: '', 35 }); 36 expect(myFlag.get()).toEqual(false); 37 expect(myFlag.isOverridden()).toEqual(false); 38}); 39 40test('registering the same flag twice is an error', () => { 41 const flags = new Flags(new TestFlagStore()); 42 flags.register({ 43 id: 'foo', 44 defaultValue: false, 45 description: '', 46 }); 47 expect(() => 48 flags.register({ 49 id: 'foo', 50 defaultValue: false, 51 description: '', 52 }), 53 ).toThrow('Flag with id "foo" is already registered.'); 54}); 55 56test('can override', () => { 57 const flags = new Flags(new TestFlagStore()); 58 const foo = flags.register({ 59 id: 'foo', 60 defaultValue: false, 61 description: '', 62 }); 63 foo.set(true); 64 expect(foo.isOverridden()).toEqual(true); 65 expect(foo.get()).toEqual(true); 66}); 67 68test('overrides are persisted', () => { 69 const store = new TestFlagStore(); 70 const flagsA = new Flags(store); 71 const fooA = flagsA.register({ 72 id: 'foo', 73 defaultValue: true, 74 description: 'some description', 75 }); 76 77 fooA.set(true); 78 79 const flagsB = new Flags(store); 80 const fooB = flagsB.register({ 81 id: 'foo', 82 defaultValue: false, 83 description: 'a new description', 84 }); 85 86 expect(fooB.get()).toEqual(true); 87 expect(fooB.isOverridden()).toEqual(true); 88}); 89 90test('flags can be reset', () => { 91 const flags = new Flags(new TestFlagStore()); 92 const foo = flags.register({ 93 id: 'foo', 94 defaultValue: false, 95 description: 'some description', 96 }); 97 98 foo.set(false); 99 foo.reset(); 100 expect(foo.get()).toEqual(false); 101 expect(foo.isOverridden()).toEqual(false); 102}); 103 104test('corrupt store is ignored', () => { 105 class Store { 106 load(): object { 107 return {foo: 'bad state'}; 108 } 109 110 save(_: object): void {} 111 } 112 const flags = new Flags(new Store()); 113 const foo = flags.register({ 114 id: 'foo', 115 defaultValue: false, 116 description: 'some description', 117 }); 118 119 expect(foo.isOverridden()).toEqual(false); 120}); 121