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
16 #include "net_score.h"
17
18 #include "net_mgr_log_wrapper.h"
19 #include "net_conn_types.h"
20
21 #include <gtest/gtest.h>
22
23 namespace OHOS {
24 namespace NetManagerStandard {
25 using namespace testing::ext;
26 class NetScoreTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp();
31 void TearDown();
32 public:
33 std::unique_ptr<NetScore> netScore_ = nullptr;
34 };
35
SetUpTestCase()36 void NetScoreTest::SetUpTestCase()
37 {}
38
TearDownTestCase()39 void NetScoreTest::TearDownTestCase() {}
40
SetUp()41 void NetScoreTest::SetUp()
42 {
43 netScore_ = std::make_unique<NetScore>();
44 if (netScore_ == nullptr) {
45 NETMGR_LOG_E("Make NetScore failed");
46 return;
47 }
48 }
49
TearDown()50 void NetScoreTest::TearDown() {}
51
52 HWTEST_F(NetScoreTest, GetServiceScore, TestSize.Level1)
53 {
54 std::set<NetCap> netCaps {NET_CAPABILITY_MMS, NET_CAPABILITY_INTERNET};
55 std::string ident = "ident";
56 NetBearType bearerType = BEARER_CELLULAR;
57 sptr<NetSupplier> supplier = (std::make_unique<NetSupplier>(bearerType, ident, netCaps)).release();
58
59 // mock Failed to detect network
60 supplier->SetNetValid(false);
61
62 bool result = netScore_->GetServiceScore(supplier);
63 ASSERT_TRUE(result == true);
64 ASSERT_TRUE(supplier->GetNetScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
65 ASSERT_TRUE(supplier->GetRealScore() ==
66 (static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE) - NET_VALID_SCORE));
67
68 // mock successed to detect network
69 supplier->SetNetValid(true);
70
71 result = netScore_->GetServiceScore(supplier);
72 ASSERT_TRUE(result == true);
73 ASSERT_TRUE(supplier->GetNetScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
74 ASSERT_TRUE(supplier->GetRealScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
75 }
76 } // namespace NetManagerStandard
77 } // namespace OHOS