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 }
61
SetUp(void)62 void PreDnsAdapterTest::SetUp(void)
63 {}
64
TearDown(void)65 void PreDnsAdapterTest::TearDown(void)
66 {}
67
68 /**
69 * @tc.name: PreDnsAdapterTest_001.
70 * @tc.desc: test pre dns
71 * @tc.type: FUNC
72 * @tc.require: issueI5W74R
73 */
74 HWTEST_F(PreDnsAdapterTest, PreDnsAdapterTest_001, TestSize.Level1)
75 {
76 std::vector<std::string> hostInfo;
77 EXPECT_CALL(*g_adapterMock, GetHostnames(::testing::_))
78 .Times(1)
79 .WillRepeatedly(::testing::SetArgReferee<0>(hostInfo));
80 PreDnsInThread();
81
82 hostInfo.push_back("m.pinduoduo.com");
83 hostInfo.push_back("mcdn.pinduoduo.com");
84 EXPECT_CALL(*g_adapterMock, GetHostnames(::testing::_))
85 .Times(1)
86 .WillRepeatedly(::testing::SetArgReferee<0>(hostInfo));
87 PreDnsInThread();
88
89 hostInfo.push_back("test_error");
90 EXPECT_CALL(*g_adapterMock, GetHostnames(::testing::_))
91 .Times(1)
92 .WillRepeatedly(::testing::SetArgReferee<0>(hostInfo));
93 PreDnsInThread();
94 }
95
96 } // namespace NWeb
97