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
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include "nweb_pre_dns_adapter.h"
19 #include "ohos_adapter_helper.h"
20 #include "ohos_web_data_base_adapter.h"
21 #define private public
22 #include "ohos_web_dns_data_base_adapter_impl.h"
23 #undef private
24
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::NWeb;
29
30 namespace OHOS::NWeb {
31 class OhosWebDnsDataBaseAdapterMock : public OhosWebDnsDataBaseAdapterImpl {
32 public:
33 MOCK_CONST_METHOD1(GetHostnames, void(std::vector<std::string>&));
34 };
35
36 class PreDnsAdapterTest : public testing::Test {
37 public:
38 static void SetUpTestCase(void);
39 static void TearDownTestCase(void);
40 void SetUp();
41 void TearDown();
42 };
43
44 static OhosWebDnsDataBaseAdapterMock *g_adapterMock;
GetWebDnsDataBaseInstance()45 OhosWebDnsDataBaseAdapter &OhosAdapterHelper::GetWebDnsDataBaseInstance()
46 {
47 std::cout << "GetWebDnsDataBaseInstance\n";
48 return *g_adapterMock;
49 }
50
SetUpTestCase(void)51 void PreDnsAdapterTest::SetUpTestCase(void)
52 {
53 g_adapterMock = new OhosWebDnsDataBaseAdapterMock();
54 ASSERT_NE(g_adapterMock, nullptr);
55 }
56
TearDownTestCase(void)57 void PreDnsAdapterTest::TearDownTestCase(void)
58 {
59 delete g_adapterMock;
60 g_adapterMock = nullptr;
61 }
62
SetUp(void)63 void PreDnsAdapterTest::SetUp(void)
64 {}
65
TearDown(void)66 void PreDnsAdapterTest::TearDown(void)
67 {}
68
69 /**
70 * @tc.name: PreDnsAdapterTest_001.
71 * @tc.desc: test pre dns
72 * @tc.type: FUNC
73 * @tc.require: issueI5W74R
74 */
75 HWTEST_F(PreDnsAdapterTest, PreDnsAdapterTest_001, TestSize.Level1)
76 {
77 std::vector<std::string> hostInfo;
78 EXPECT_CALL(*g_adapterMock, GetHostnames(::testing::_))
79 .Times(1)
80 .WillRepeatedly(::testing::SetArgReferee<0>(hostInfo));
81 PreDnsInThread();
82
83 hostInfo.push_back("m.pinduoduo.com");
84 hostInfo.push_back("mcdn.pinduoduo.com");
85 EXPECT_CALL(*g_adapterMock, GetHostnames(::testing::_))
86 .Times(1)
87 .WillRepeatedly(::testing::SetArgReferee<0>(hostInfo));
88 PreDnsInThread();
89
90 hostInfo.push_back("test_error");
91 EXPECT_CALL(*g_adapterMock, GetHostnames(::testing::_))
92 .Times(1)
93 .WillRepeatedly(::testing::SetArgReferee<0>(hostInfo));
94 PreDnsInThread();
95 }
96
97 } // namespace NWeb
98