1 /*
2  * Copyright (c) 2022 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 #include <unordered_map>
16 #include "gtest/gtest.h"
17 #include "json_node.h"
18 #include "ui_strategy.h"
19 
20 using namespace testing::ext;
21 using namespace Updater;
22 
23 namespace Updater {
operator ==(const ComInfo & lhs,const ComInfo & rhs)24 bool operator == (const ComInfo &lhs, const ComInfo &rhs)
25 {
26     return lhs.comId == rhs.comId && lhs.pageId == rhs.pageId;
27 }
28 
operator ==(const ProgressPage & lhs,const ProgressPage & rhs)29 bool operator == (const ProgressPage &lhs, const ProgressPage &rhs)
30 {
31     return lhs.logoComId == rhs.logoComId && lhs.logoType == rhs.logoType && lhs.progressComId == rhs.progressComId &&
32         lhs.progressPageId == rhs.progressPageId && lhs.progressType == rhs.progressType &&
33         lhs.warningComId == rhs.warningComId;
34 }
35 
operator ==(const ResPage & lhs,const ResPage & rhs)36 bool operator == (const ResPage &lhs, const ResPage &rhs)
37 {
38     return lhs.successPageId == rhs.successPageId && lhs.failPageId == rhs.failPageId;
39 }
40 
operator ==(const UiStrategyCfg & lhs,const UiStrategyCfg & rhs)41 bool operator == (const UiStrategyCfg &lhs, const UiStrategyCfg &rhs)
42 {
43     return lhs.confirmPageId == rhs.confirmPageId && lhs.labelLogId == rhs.labelLogId &&
44         lhs.labelLogResId == rhs.labelLogResId && lhs.progressPage == rhs.progressPage &&
45         lhs.labelUpdId == rhs.labelUpdId && lhs.resPage == rhs.resPage;
46 }
47 }
48 
49 namespace {
50 class UpdaterUiStrategyUnitTest : public testing::Test {
51 public:
SetUpTestCase(void)52     static void SetUpTestCase(void) {}
TearDownTestCase(void)53     static void TearDownTestCase(void) {}
SetUp()54     void SetUp() override {}
TearDown()55     void TearDown() override {}
56 };
57 
58 HWTEST_F(UpdaterUiStrategyUnitTest, test_load_invalid_strategy, TestSize.Level1)
59 {
60     EXPECT_FALSE(UiStrategy::LoadStrategy(JsonNode {Fs::path {"/data/updater/ui/strategy/strategy_invalid.json"}}));
61     EXPECT_TRUE(UiStrategy::GetStrategy().empty());
62 }
63 
64 HWTEST_F(UpdaterUiStrategyUnitTest, test_load_strategy_for_each_mode, TestSize.Level1)
65 {
66     EXPECT_TRUE(UiStrategy::LoadStrategy(JsonNode {Fs::path {"/data/updater/ui/strategy/strategy_valid.json"}}));
67     UiStrategyCfg defaultCfg { "confirm", {"", ""}, {"", ""}, {"", ""}, ProgressPage {"upd:update",
68         "ProgressUpdBoxDark_Progress", "bar", "OHOSIconDark_Image", "img", "PowerLongPressWarning_Image"},
69         ResPage {"upd:updateSuccess", "upd:normalUpdateFailed"}
70     };
71     std::unordered_map<std::string, Updater::UiStrategyCfg> expected {};
72     auto &sdCardCfg = expected[UPDATERMODE_SDCARD];
73     sdCardCfg = defaultCfg;
74     sdCardCfg.progressPage = ProgressPage {"upd:sdUpdate", "UpdBox_Progress", "bar",
75         "OHOSIcon_Image", "img", "PowerLongPressWarning_Image"};
76     sdCardCfg.labelLogResId = {"upd", "UpdateInfoDark_Label"};
77     sdCardCfg.resPage = {"upd:updateSuccess", "upd:updateFailedNoButton"};
78 
79     auto &factoryRstCfg = expected[UPDATERMODE_FACTORYRST];
80     factoryRstCfg = defaultCfg;
81     factoryRstCfg.progressPage = ProgressPage {"upd:reset", "UpdBox_Progress", "bar", "OHOSIcon_Image", "img", ""};
82     factoryRstCfg.labelLogResId = {"upd", "UpdateInfoDark_Label"};
83     factoryRstCfg.labelUpdId = {"upd", "RstInfo_Label"};
84     factoryRstCfg.resPage = {"menu:normal", "upd:FactoryRstFailed"};
85 
86     auto &rebootFactoryRstCfg = expected[UPDATERMODE_REBOOTFACTORYRST];
87     rebootFactoryRstCfg = defaultCfg;
88     rebootFactoryRstCfg.labelLogResId = {"upd", "RstInfo_Label"};
89     rebootFactoryRstCfg.progressPage = {"upd:reset", "UpdBox_Progress", "bar", "OHOSIcon_Image", "img", ""};
90     rebootFactoryRstCfg.resPage = {"upd:reset", "upd:FactoryRstFailed"};
91 
92     EXPECT_EQ(UiStrategy::GetStrategy(), (std::unordered_map<std::string, Updater::UiStrategyCfg> {
93         {UPDATERMODE_SDCARD, sdCardCfg}, {UPDATERMODE_FACTORYRST, factoryRstCfg},
94         {UPDATERMODE_REBOOTFACTORYRST, rebootFactoryRstCfg}, {UPDATERMODE_OTA, defaultCfg},
95         {UPDATERMODE_RECOVER, defaultCfg}, {UPDATERMODE_NIGHTUPDATE, defaultCfg}
96     }));
97 }
98 } // namespace
99