• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 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("@StorageLink - AppStorge", () => {
17
18
19  class SubscribingViewPU implements IMultiPropertiesChangeSubscriber {
20
21    private id_: number = SubscriberManager.MakeId();
22    private label_: string;
23
24    constructor(label: string) {
25      SubscriberManager.Add(this);
26      this.label_ = label;
27    }
28
29    // globally unique id
30    id__(): number {
31      return this.id_;
32    }
33
34    // inform the subscribed property
35    // that the subscriber is about to be deleted
36    // hence , unsubscribe
37    aboutToBeDeleted(): void {
38      SubscriberManager.Delete(this.id__());
39    }
40
41    // implements IMultiPropertiesChangePublisher
42    propertyHasChanged(info?: PropertyInfo): void {
43      console.log(`SubscribingView '${this.label_}' object property ${info ? info : 'unknown'} has changed.`);
44    }
45  }
46
47  let storeView = new SubscribingViewPU("v1p1b1");
48
49  /* @StorageLink(moveDistance) storeProp:number=  0;  */
50  let __storeProp: SubscribedAbstractProperty<number> =
51    AppStorage.setAndLink<number>('storeProp', /* default value */  7,  /* subscriber */ storeView, /* var name in View */ "viewVar");
52
53
54  tcase("read back", () => {
55    test("readback from AppStorage", AppStorage.get("storeProp") == 7);
56    test("readback from view variable", __storeProp.get() == 7);
57  });
58
59  tcase("modify via AppStore", () => {
60    let spy = spyOn(storeView, "propertyHasChanged");
61    AppStorage.set("storeProp", 47);
62
63    test("readback from AppStorage", AppStorage.get("storeProp") == 47);
64    test("readback from view variable", __storeProp.get() == 47);
65    test("View.propertyHasChanged has been called", spy.called && spy.args[0] == "viewVar");
66  });
67
68  tcase("modify via @StorageLink", () => {
69    let spy2 = spyOn(storeView, "propertyHasChanged");
70    __storeProp.set(101);
71    test("readback from AppStorage", AppStorage.get("storeProp") == 101);
72    test("readback from view variable", __storeProp.get() == 101);
73    test("View.propertyHasChanged has been called", spy2.called && spy2.args[0] == "viewVar");
74  });
75
76});