forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb_ui_inttest.mm
153 lines (123 loc) · 4.97 KB
/
web_ui_inttest.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include "base/bind.h"
#include "base/run_loop.h"
#import "base/test/ios/wait_util.h"
#include "ios/web/grit/ios_web_resources.h"
#import "ios/web/public/navigation/navigation_manager.h"
#import "ios/web/public/test/navigation_test_util.h"
#import "ios/web/public/test/web_test_with_web_state.h"
#import "ios/web/public/test/web_view_content_test_util.h"
#import "ios/web/public/test/web_view_interaction_test_util.h"
#include "ios/web/public/webui/web_ui_ios_controller.h"
#include "ios/web/public/webui/web_ui_ios_controller_factory.h"
#include "ios/web/public/webui/web_ui_ios_data_source.h"
#include "ios/web/test/grit/test_resources.h"
#include "ios/web/test/test_url_constants.h"
#import "ios/web/web_state/web_state_impl.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using base::test::ios::kWaitForPageLoadTimeout;
using base::test::ios::WaitUntilConditionOrTimeout;
using web::test::TapWebViewElementWithId;
using web::test::WaitForWebViewContainingText;
namespace web {
namespace {
// Hostname for test WebUI page.
const char kTestWebUIURLHost[] = "testwebui";
const char kTestWebUIURLHost2[] = "testwebui2";
// The element id of the link to load a webUI page.
const char kWebUIPageLinkID[] = "link";
// Text present on the sample WebUI page.
const char kWebUIPageText[] = "WebUI page";
// Controller for test WebUI.
class TestUI : public WebUIIOSController {
public:
// Constructs controller from |web_ui| and |ui_handler| which will communicate
// with test WebUI page.
TestUI(WebUIIOS* web_ui, const std::string& host, int resource_id)
: WebUIIOSController(web_ui, host) {
web::WebUIIOSDataSource* source =
web::WebUIIOSDataSource::Create(kTestWebUIURLHost);
source->SetDefaultResource(resource_id);
web::WebState* web_state = web_ui->GetWebState();
web::WebUIIOSDataSource::Add(web_state->GetBrowserState(), source);
}
~TestUI() override = default;
};
// Factory that creates TestUI controller.
class TestWebUIControllerFactory : public WebUIIOSControllerFactory {
public:
// Constructs a controller factory.
TestWebUIControllerFactory() {}
// WebUIIOSControllerFactory overrides.
std::unique_ptr<WebUIIOSController> CreateWebUIIOSControllerForURL(
WebUIIOS* web_ui,
const GURL& url) const override {
if (!url.SchemeIs(kTestWebUIScheme))
return nullptr;
if (url.host() == kTestWebUIURLHost) {
return std::make_unique<TestUI>(web_ui, url.host(), IDR_WEBUI_TEST_HTML);
}
DCHECK_EQ(url.host(), kTestWebUIURLHost2);
return std::make_unique<TestUI>(web_ui, url.host(), IDR_WEBUI_TEST_HTML_2);
}
NSInteger GetErrorCodeForWebUIURL(const GURL& url) const override {
if (url.SchemeIs(kTestWebUIScheme))
return 0;
return NSURLErrorUnsupportedURL;
}
};
} // namespace
// A test fixture for verifying WebUI.
class WebUITest : public WebTestWithWebState {
protected:
WebUITest() : WebTestWithWebState() {}
void SetUp() override {
WebTestWithWebState::SetUp();
factory_ = std::make_unique<TestWebUIControllerFactory>();
WebUIIOSControllerFactory::RegisterFactory(factory_.get());
url::SchemeHostPort tuple(kTestWebUIScheme, kTestWebUIURLHost, 0);
GURL url(tuple.Serialize());
test::LoadUrl(web_state(), url);
// LoadIfNecessary is needed because the view is not created (but needed)
// when loading the page. TODO(crbug.com/705819): Remove this call.
web_state()->GetNavigationManager()->LoadIfNecessary();
ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^{
base::RunLoop().RunUntilIdle();
return !web_state()->IsLoading();
}));
ASSERT_EQ(url.spec(), BaseUrl());
}
void TearDown() override {
WebUIIOSControllerFactory::DeregisterFactory(factory_.get());
WebTestWithWebState::TearDown();
}
private:
std::unique_ptr<TestWebUIControllerFactory> factory_;
};
// Tests that a web UI page is loaded and that the WebState correctly reports
// |WebStateImpl::HasWebUI|.
TEST_F(WebUITest, LoadWebUIPage) {
ASSERT_TRUE(static_cast<WebStateImpl*>(web_state())->HasWebUI());
EXPECT_TRUE(WaitForWebViewContainingText(web_state(), kWebUIPageText));
}
// Tests that a web UI page is correctly loaded when navigated to from another
// webUI page.
TEST_F(WebUITest, LoadWebUIPageLoadViaLinkClick) {
ASSERT_TRUE(TapWebViewElementWithId(web_state(), kWebUIPageLinkID));
url::SchemeHostPort tuple(kTestWebUIScheme, kTestWebUIURLHost2, 0);
GURL url(tuple.Serialize());
ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^{
base::RunLoop().RunUntilIdle();
return !web_state()->IsLoading() && url.spec() == BaseUrl();
}));
ASSERT_TRUE(static_cast<WebStateImpl*>(web_state())->HasWebUI());
}
} // namespace web