• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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 */
15
16const testViewAppStorage = tsuite("View AppStorge", () => {
17
18  // @Entry
19  class Player extends View {
20
21    // declare vars:
22
23    /* @StorageLink("isPlaying") playCount : number = 0;  */
24    private __playCount: SubscribedAbstractProperty<number>
25      = as.setAndLink<number>("playCount", /* default value */ 0, /* subscriber */ this);
26
27    public get playCount(): number {
28      return this.__playCount.get();
29    }
30    public set playCount(newValue: number) {
31      this.__playCount.set(newValue);
32    }
33
34    // generated constructor
35    constructor(compilerAssignedUniqueChildId: string, parent: View,
36      params: {}) {
37      super(compilerAssignedUniqueChildId, parent);
38
39      // tsc will add initialization of @State and regular variables with defauilt values below super() and above this comment.
40      this.updateWithValueParams(params);
41    }
42
43
44    // will be called on 2nd render (generated)
45    public updateWithValueParams(params: {}): void {
46      console.debug(`${this.id()}:${this.constructor.name}: updateWithValueParams start`);
47      console.debug(`${this.id()}:${this.constructor.name}: updateWithValueParams done`);
48    }
49
50
51    // will be called when View is no longer used (like destructor, generated)
52    public aboutToBeDeleted() {
53      console.debug(`${this.id()}:${this.constructor.name}: aboutToBeDeleted`);
54      this.__playCount.aboutToBeDeleted();
55      SubscriberManager.Get().delete(this.id());
56    }
57
58    render() {
59      console.debug(`${this.id()}:${this.constructor.name}: render playCount ${this.playCount}`);
60    }
61  }
62
63  // simulate the process
64  console.debug("create Player ...")
65  let playerView: Player;
66
67  tcase("create View and read back", () => {
68    playerView = new Player("0", undefined, {});
69    test("readback following creation, playCount has correct value", Array.from(as.keys()).toString() == ["playCount"].toString());
70    test("readback following creation, playCount has correct value", as.get<number>("playCount") == 0);
71  });
72
73
74  let linkToplayCount: SubscribedAbstractProperty<number>;
75
76  tcase("Simulate an event handler mutating parent's @StorageLink", () => {
77
78    let spyPlayerPropertyHasChanged = spyOn(playerView, "propertyHasChanged");
79
80    linkToplayCount = as.link("playCount" /* no subscriber */)
81
82    playerView.playCount = 1;
83
84    test(`playerView.aPropertyHasChanged was called`, spyPlayerPropertyHasChanged.called);
85    test("readback from AppStorage, playCount has correct value", as.get<number>("playCount") == 1);
86    test("readback from link variable, playCount has correct value", linkToplayCount.get() == 1);
87  });
88
89  tcase("Simulate an event handler mutating link to AppStorage prop.", () => {
90
91    let spyPlayerPropertyHasChanged = spyOn(playerView, "propertyHasChanged");
92
93    linkToplayCount.set(7);
94
95    test("readback from AppStorage, playCount has correct value", as.get<number>("playCount") == 7);
96    test(`playerView.aPropertyHasChanged was called`, spyPlayerPropertyHasChanged.called);
97    test(`playerView.aPropertyHasChanged param correct`, spyPlayerPropertyHasChanged.args[0] == "playCount");
98    test("readback from link variable, playCount has correct value", playerView.playCount == 7);
99  });
100});
101