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

Side by Side Diff: mojo/runner/about_fetcher_unittest.cc

Issue 1260813007: Mandoline: Support about:blank URL. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 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
OLDNEW
(Empty)
1 // Copyright 2015 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 "base/at_exit.h"
6 #include "base/macros.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "mojo/application/public/cpp/application_connection.h"
11 #include "mojo/application/public/cpp/application_delegate.h"
12 #include "mojo/application/public/cpp/application_impl.h"
13 #include "mojo/application/public/cpp/interface_factory.h"
14 #include "mojo/application/public/interfaces/content_handler.mojom.h"
15 #include "mojo/common/weak_binding_set.h"
16 #include "mojo/runner/about_fetcher.h"
17 #include "mojo/shell/application_loader.h"
18 #include "mojo/shell/application_manager.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace mojo {
22 namespace runner {
23 namespace {
24
25 class TestContentHandler : public ApplicationDelegate,
26 public InterfaceFactory<ContentHandler>,
27 public ContentHandler {
28 public:
29 TestContentHandler() : response_number_(0) {}
30 ~TestContentHandler() override {}
31
32 size_t response_number() const { return response_number_; }
33 const URLResponse* latest_response() const { return latest_response_.get(); }
34
35 private:
36 // Overridden from ApplicationDelegate:
37 void Initialize(ApplicationImpl* app) override {}
38 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
39 connection->AddService<ContentHandler>(this);
40 return true;
41 }
42
43 // Overridden from InterfaceFactory<ContentHandler>:
44 void Create(ApplicationConnection* connection,
45 InterfaceRequest<ContentHandler> request) override {
46 bindings_.AddBinding(this, request.Pass());
47 }
48
49 // Overridden from ContentHandler:
50 void StartApplication(InterfaceRequest<Application> application,
51 URLResponsePtr response) override {
52 response_number_++;
53 latest_response_ = response.Pass();
54
55 // Drop |application| request. This results in the application manager
56 // dropping the ServiceProvider interface request provided by the client
57 // who made the ConnectToApplication() call. Therefore the client could
58 // listen for connection error of the ServiceProvider interface to learn
59 // that StartApplication() has been called.
60 }
61
62 size_t response_number_;
63 URLResponsePtr latest_response_;
64 WeakBindingSet<ContentHandler> bindings_;
65
66 DISALLOW_COPY_AND_ASSIGN(TestContentHandler);
67 };
68
69 class TestApplicationManagerDelegate
70 : public shell::ApplicationManager::Delegate {
71 public:
72 TestApplicationManagerDelegate() {}
73 ~TestApplicationManagerDelegate() override {}
74
75 private:
76 // Overridden from ApplicationManager::Delegate:
77 GURL ResolveMappings(const GURL& url) override { return url; }
78 GURL ResolveMojoURL(const GURL& url) override { return url; }
79 bool CreateFetcher(
80 const GURL& url,
81 const shell::Fetcher::FetchCallback& loader_callback) override {
82 if (url.SchemeIs(AboutFetcher::kAboutScheme)) {
83 // AboutFetcher manages its own lifespan.
84 new AboutFetcher(url, loader_callback);
85 return true;
86 }
87
88 return false;
89 }
90
91 DISALLOW_COPY_AND_ASSIGN(TestApplicationManagerDelegate);
92 };
93
94 class TestLoader : public shell::ApplicationLoader {
95 public:
96 explicit TestLoader(ApplicationDelegate* delegate) : delegate_(delegate) {}
97 ~TestLoader() override {}
98
99 private:
100 // Overridden from ApplicationLoader:
101 void Load(const GURL& url, InterfaceRequest<Application> request) override {
102 app_.reset(new ApplicationImpl(delegate_, request.Pass()));
103 }
104
105 ApplicationDelegate* delegate_;
106 scoped_ptr<ApplicationImpl> app_;
107
108 DISALLOW_COPY_AND_ASSIGN(TestLoader);
109 };
110
111 class AboutFetcherTest : public testing::Test {
112 public:
113 AboutFetcherTest() {}
114 ~AboutFetcherTest() override {}
115
116 protected:
117 const TestContentHandler* html_content_handler() const {
118 return &html_content_handler_;
119 }
120
121 void ConnectAndWait(const std::string& url) {
122 base::RunLoop run_loop;
123
124 ServiceProviderPtr service_provider;
125 InterfaceRequest<ServiceProvider> service_provider_request =
126 GetProxy(&service_provider);
127 // This connection error handler will be called when:
128 // - TestContentHandler::StartApplication() has been called (please see
129 // comments in that method); or
130 // - the application manager fails to fetch the requested URL.
131 service_provider.set_connection_error_handler(
132 [&run_loop]() { run_loop.Quit(); });
133
134 URLRequestPtr request(URLRequest::New());
135 request->url = url;
136 application_manager_->ConnectToApplication(
137 nullptr, request.Pass(), std::string(), GURL(),
138 service_provider_request.Pass(), nullptr, shell::CapabilityFilter(),
139 base::Closure());
140
141 run_loop.Run();
142 }
143
144 // Overridden from testing::Test:
145 void SetUp() override {
146 application_manager_.reset(new shell::ApplicationManager(&test_delegate_));
147 application_manager_->SetLoaderForURL(
148 make_scoped_ptr(new TestLoader(&html_content_handler_)),
149 GURL("test:html_content_handler"));
150 application_manager_->RegisterContentHandler(
151 "text/html", GURL("test:html_content_handler"));
152 }
153
154 void TearDown() override { application_manager_.reset(); }
155
156 private:
157 base::ShadowingAtExitManager at_exit_;
158 TestApplicationManagerDelegate test_delegate_;
159 TestContentHandler html_content_handler_;
160 base::MessageLoop loop_;
161 scoped_ptr<shell::ApplicationManager> application_manager_;
162
163 DISALLOW_COPY_AND_ASSIGN(AboutFetcherTest);
164 };
165
166 TEST_F(AboutFetcherTest, AboutBlank) {
167 ConnectAndWait("about:blank");
168
169 ASSERT_EQ(1u, html_content_handler()->response_number());
170
171 const URLResponse* response = html_content_handler()->latest_response();
172 EXPECT_EQ("about:blank", response->url);
173 EXPECT_EQ(200, response->status_code);
174 EXPECT_EQ("text/html", response->mime_type);
175 EXPECT_FALSE(response->body.is_valid());
176 }
177
178 TEST_F(AboutFetcherTest, UnrecognizedURL) {
179 ConnectAndWait("about:some_unrecognized_url");
180
181 ASSERT_EQ(0u, html_content_handler()->response_number());
182 }
183
184 } // namespace
185 } // namespace runner
186 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698