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
operator <<(std::ostream & os,const ComInfo & com)48 std::ostream &operator<<(std::ostream &os, const ComInfo &com)
49 {
50 os << "pageId: " << com.pageId << " comId: " << com.comId;
51 return os;
52 }
53 }
54
55 namespace {
56 class UpdaterUiStrategyUnitTest : public testing::Test {
57 public:
SetUpTestCase(void)58 static void SetUpTestCase(void) {}
TearDownTestCase(void)59 static void TearDownTestCase(void) {}
SetUp()60 void SetUp() override {}
TearDown()61 void TearDown() override {}
62 };
63
64 HWTEST_F(UpdaterUiStrategyUnitTest, test_load_invalid_strategy, TestSize.Level1)
65 {
66 EXPECT_FALSE(UiStrategy::LoadStrategy(JsonNode {Fs::path {"/data/updater/ui/strategy/strategy_invalid.json"}}));
67 EXPECT_TRUE(UiStrategy::GetStrategy().empty());
68 }
69
70 HWTEST_F(UpdaterUiStrategyUnitTest, test_load_strategy_for_each_mode, TestSize.Level1)
71 {
72 EXPECT_TRUE(UiStrategy::LoadStrategy(JsonNode {Fs::path {"/data/updater/ui/strategy/strategy_valid.json"}}));
73 UiStrategyCfg defaultCfg { "confirm", {"", ""}, {"", ""}, {"", ""}, ProgressPage {"upd:update",
74 "ProgressUpdBoxDark_Progress", "bar", "OHOSIconDark_Image", "img", "PowerLongPressWarning_Image"},
75 ResPage {"upd:updateSuccess", "upd:normalUpdateFailed"}
76 };
77 std::unordered_map<Updater::UpdaterMode, Updater::UiStrategyCfg> expected {};
78 auto &sdCardCfg = expected[UpdaterMode::SDCARD];
79 sdCardCfg = defaultCfg;
80 sdCardCfg.progressPage = ProgressPage {"upd:sdUpdate", "UpdBox_Progress", "bar",
81 "OHOSIcon_Image", "img", "PowerLongPressWarning_Image"};
82 sdCardCfg.labelLogResId = {"upd", "UpdateInfoDark_Label"};
83 sdCardCfg.resPage = {"upd:updateSuccess", "upd:updateFailedNoButton"};
84
85 auto &factoryRstCfg = expected[UpdaterMode::FACTORYRST];
86 factoryRstCfg = defaultCfg;
87 factoryRstCfg.progressPage = ProgressPage {"upd:reset", "UpdBox_Progress", "bar", "OHOSIcon_Image", "img", ""};
88 factoryRstCfg.labelLogResId = {"upd", "UpdateInfoDark_Label"};
89 factoryRstCfg.labelUpdId = {"upd", "RstInfo_Label"};
90 factoryRstCfg.resPage = {"menu:normal", "upd:FactoryRstFailed"};
91
92 auto &rebootFactoryRstCfg = expected[UpdaterMode::REBOOTFACTORYRST];
93 rebootFactoryRstCfg = defaultCfg;
94 rebootFactoryRstCfg.labelLogResId = {"upd", "RstInfo_Label"};
95 rebootFactoryRstCfg.progressPage = {"upd:reset", "UpdBox_Progress", "bar", "OHOSIcon_Image", "img", ""};
96 rebootFactoryRstCfg.resPage = {"upd:reset", "upd:FactoryRstFailed"};
97
98 auto &otaCfg = expected[UpdaterMode::OTA];
99 otaCfg = defaultCfg;
100 EXPECT_EQ(UiStrategy::GetStrategy(), (std::unordered_map<Updater::UpdaterMode, Updater::UiStrategyCfg> {
101 {UpdaterMode::SDCARD, sdCardCfg}, {UpdaterMode::FACTORYRST, factoryRstCfg},
102 {UpdaterMode::REBOOTFACTORYRST, rebootFactoryRstCfg}, {UpdaterMode::OTA, otaCfg}
103 }));
104 }
105 } // namespace
106