• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use ylong_runtime::sync::mpsc;
15 
16 use super::*;
17 use crate::tests::test_init;
18 
19 // @tc.name: ut_account_check_oh
20 // @tc.desc: Test account subscription and status change functionality
21 // @tc.precon: NA
22 // @tc.step: 1. Initialize test environment
23 //           2. Subscribe to account changes
24 //           3. Verify account status updates correctly
25 // @tc.expect: Account status changes are detected and processed
26 // @tc.type: FUNC
27 // @tc.require: issues#ICN16H
28 #[test]
ut_account_check_oh()29 fn ut_account_check_oh() {
30     test_init();
31 
32     assert_eq!(0, FOREGROUND_ACCOUNT.load(Ordering::SeqCst));
33     assert!(BACKGROUND_ACCOUNTS.lock().unwrap().is_none());
34 
35     let (tx, mut rx) = mpsc::unbounded_channel();
36     let task_manager = TaskManagerTx { tx };
37     registry_account_subscribe(task_manager);
38     ylong_runtime::block_on(async {
39         let msg = rx.recv().await.unwrap();
40         assert!(matches!(
41             msg,
42             TaskManagerEvent::Account(AccountEvent::Changed)
43         ));
44         assert_ne!(FOREGROUND_ACCOUNT.load(Ordering::SeqCst), 0);
45         assert!(BACKGROUND_ACCOUNTS.lock().unwrap().is_some());
46     })
47 }
48 
49 // @tc.name: ut_account_update
50 // @tc.desc: Test account update mechanism
51 // @tc.precon: NA
52 // @tc.step: 1. Initialize test environment and create account updater
53 //           2. Trigger account update event
54 //           3. Verify account change notification is received
55 // @tc.expect: Account change event is properly propagated
56 // @tc.type: FUNC
57 // @tc.require: issues#ICN16H
58 #[test]
ut_account_update()59 fn ut_account_update() {
60     test_init();
61     ylong_runtime::block_on(async {
62         let (tx, mut rx) = mpsc::unbounded_channel();
63         let task_manager = TaskManagerTx { tx };
64         let updater = AccountUpdater::new(task_manager.clone());
65         drop(updater);
66         ylong_runtime::time::sleep(std::time::Duration::from_secs(2)).await;
67         assert!(rx.is_empty());
68         let mut updater = AccountUpdater::new(task_manager);
69         updater.change_flag = true;
70         drop(updater);
71         let msg = rx.recv().await.unwrap();
72         assert!(matches!(
73             msg,
74             TaskManagerEvent::Account(AccountEvent::Changed)
75         ));
76     })
77 }
78 
79 // @tc.name: ut_account_update_branch
80 // @tc.desc: Test account update branch conditions
81 // @tc.precon: NA
82 // @tc.step: 1. Compare different background account configurations
83 //           2. Verify branch conditions for account updates
84 // @tc.expect: Branch conditions correctly identify account changes
85 // @tc.type: FUNC
86 // @tc.require: issues#ICN16H
87 #[test]
ut_account_update_branch()88 fn ut_account_update_branch() {
89     let old_background = Option::<Vec<i32>>::None;
90     let background_accounts = vec![100];
91     assert!(!old_background.is_some_and(|old_background| old_background == background_accounts));
92     let old_background = Option::<Vec<i32>>::Some(vec![101]);
93     assert!(!old_background.is_some_and(|old_background| old_background == background_accounts));
94     let old_background = Option::<Vec<i32>>::Some(vec![100]);
95     assert!(old_background.is_some_and(|old_background| old_background == background_accounts));
96 }