Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: ios/web/webui/web_ui_mojo_inttest.mm

Issue 2006273005: [ios Mojo] Integration test for Mojo WebUI. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed review comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/web/test/web_test_suite.mm ('k') | tools/gritsettings/resource_ids » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <memory>
6
7 #include "base/run_loop.h"
8 #import "base/test/ios/wait_util.h"
9 #include "ios/public/provider/web/web_ui_ios_controller.h"
10 #include "ios/public/provider/web/web_ui_ios_controller_factory.h"
11 #import "ios/web/public/navigation_manager.h"
12 #include "ios/web/public/web_ui_ios_data_source.h"
13 #include "ios/web/test/grit/test_resources.h"
14 #include "ios/web/test/mojo_test.mojom.h"
15 #include "ios/web/test/test_url_constants.h"
16 #import "ios/web/test/web_int_test.h"
17 #import "ios/web/web_state/web_state_impl.h"
18 #include "mojo/public/cpp/bindings/binding_set.h"
19 #include "services/shell/public/cpp/interface_registry.h"
20 #include "url/gurl.h"
21 #include "url/scheme_host_port.h"
22
23 namespace web {
24
25 namespace {
26
27 // Hostname for test WebUI page.
28 const char kTestWebUIURLHost[] = "testwebui";
29
30 // UI handler class which communicates with test WebUI page as follows:
31 // - page sends "syn" message to |TestUIHandler|
32 // - |TestUIHandler| replies with "ack" message
33 // - page replies back with "fin"
34 //
35 // Once "fin" is received |IsFinReceived()| call will return true, indicating
36 // that communication was successfull. See test WebUI page for more details:
37 // ios/web/test/data/mojo_test.js
38 class TestUIHandler : public TestUIHandlerMojo,
39 public shell::InterfaceFactory<TestUIHandlerMojo> {
40 public:
41 TestUIHandler() : syn_received_(false), fin_received_(false) {}
42 ~TestUIHandler() override {}
43
44 // Returns true if "fin" has been received.
45 bool IsFinReceived() { return fin_received_; }
46
47 // TestUIHandlerMojo overrides.
48 void HandleJsMessage(const mojo::String& message, TestPagePtr page) override {
49 if (message.get() == "syn") {
50 // Received "syn" message from WebUI page, send "ack" as reply.
51 DCHECK(!syn_received_);
52 DCHECK(!fin_received_);
53 syn_received_ = true;
54 NativeMessageResultMojoPtr result(NativeMessageResultMojo::New());
55 result->message = mojo::String::From("ack");
56 page->HandleNativeMessage(std::move(result));
57 } else if (message.get() == "fin") {
58 // Received "fin" from the WebUI page in response to "ack".
59 DCHECK(syn_received_);
60 DCHECK(!fin_received_);
61 fin_received_ = true;
62 } else {
63 NOTREACHED();
64 }
65 }
66
67 private:
68 // shell::InterfaceFactory overrides.
69 void Create(shell::Connection* connection,
70 mojo::InterfaceRequest<TestUIHandlerMojo> request) override {
71 bindings_.AddBinding(this, std::move(request));
72 }
73
74 // Bindings required by shell::InterfaceFactory.
Ken Rockot(use gerrit already) 2016/05/26 22:00:40 nit: This comment isn't really accurate or particu
Eugene But (OOO till 7-30) 2016/05/26 22:38:52 Done.
75 mojo::BindingSet<TestUIHandlerMojo> bindings_;
76 // |true| if "syn" has been received.
77 bool syn_received_;
Ken Rockot(use gerrit already) 2016/05/26 22:00:40 nit: use default values instead of initializers? i
Eugene But (OOO till 7-30) 2016/05/26 22:38:52 Done.
78 // |true| if "fin" has been received.
79 bool fin_received_;
80 };
81
82 // Controller for test WebUI.
83 class TestUI : public WebUIIOSController {
84 public:
85 // Constructs controller from |web_ui| and |ui_handler| which will communicate
86 // with test WebUI page.
87 TestUI(WebUIIOS* web_ui, TestUIHandler* ui_handler)
88 : WebUIIOSController(web_ui) {
89 web::WebUIIOSDataSource* source =
90 web::WebUIIOSDataSource::Create(kTestWebUIURLHost);
91
92 source->AddResourcePath("mojo_test.js", IDR_MOJO_TEST_JS);
93 source->AddResourcePath("ios/web/test/mojo_test.mojom",
94 IDR_MOJO_TEST_MOJO_JS);
95 source->SetDefaultResource(IDR_MOJO_TEST_HTML);
96
97 web::WebState* web_state = web_ui->GetWebState();
98 web::WebUIIOSDataSource::Add(web_state->GetBrowserState(), source);
99
100 web_state->GetMojoInterfaceRegistry()->AddInterface(ui_handler);
101 }
102 };
103
104 // Factory that creates TestUI controller.
105 class TestWebUIControllerFactory : public WebUIIOSControllerFactory {
106 public:
107 // Constructs a controller factory which will eventually create |ui_handler|.
108 explicit TestWebUIControllerFactory(TestUIHandler* ui_handler)
109 : ui_handler_(ui_handler) {}
110
111 // WebUIIOSControllerFactory overrides.
112 WebUIIOSController* CreateWebUIIOSControllerForURL(
113 WebUIIOS* web_ui,
114 const GURL& url) const override {
115 DCHECK_EQ(url.scheme(), kTestWebUIScheme);
116 DCHECK_EQ(url.host(), kTestWebUIURLHost);
117 return new TestUI(web_ui, ui_handler_);
118 }
119
120 private:
121 // UI handler class which communicates with test WebUI page.
122 TestUIHandler* ui_handler_;
123 };
124 } // namespace
125
126 // A test fixture for verifying mojo comminication for WebUI.
127 class WebUIMojoTest : public WebIntTest {
128 protected:
129 WebUIMojoTest()
130 : web_state_(new WebStateImpl(GetBrowserState())),
131 ui_handler_(new TestUIHandler()) {
132 web_state_->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, 0);
133 WebUIIOSControllerFactory::RegisterFactory(
134 new TestWebUIControllerFactory(ui_handler_.get()));
135 }
136
137 // Returns WebState which loads test WebUI page.
138 WebStateImpl* web_state() { return web_state_.get(); }
139 // Returns UI handler which communicates with WebUI page.
140 TestUIHandler* test_ui_handler() { return ui_handler_.get(); }
141
142 private:
143 std::unique_ptr<WebStateImpl> web_state_;
144 std::unique_ptr<TestUIHandler> ui_handler_;
145 };
146
147 // Tests that JS can send messages to the native code and vice versa.
148 // TestUIHandler is used for communication and test suceeds only when
149 // |TestUIHandler| sucessfully receives "ack" message from WebUI page.
150 TEST_F(WebUIMojoTest, MessageExchange) {
151 web_state()->SetWebUsageEnabled(true);
152 web_state()->GetWebController().useMojoForWebUI = YES;
153 web_state()->GetView(); // WebState won't load URL without view.
154 NavigationManager::WebLoadParams load_params(GURL(
155 url::SchemeHostPort(kTestWebUIScheme, kTestWebUIURLHost, 0).Serialize()));
156 web_state()->GetNavigationManager()->LoadURLWithParams(load_params);
157
158 // Wait until |TestUIHandler| receives "ack" message from WebUI page.
159 base::test::ios::WaitUntilCondition(^{
160 base::RunLoop().RunUntilIdle();
161 return test_ui_handler()->IsFinReceived();
162 });
163 }
164
165 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/test/web_test_suite.mm ('k') | tools/gritsettings/resource_ids » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698