Add ICU message format support
[chromium-blink-merge.git] / mojo / runner / about_fetcher_unittest.cc
blob08470b5340724109ccb9782abd37dfb75f64bd15
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.
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"
21 namespace mojo {
22 namespace runner {
23 namespace {
25 class TestContentHandler : public ApplicationDelegate,
26 public InterfaceFactory<ContentHandler>,
27 public ContentHandler {
28 public:
29 TestContentHandler() : response_number_(0) {}
30 ~TestContentHandler() override {}
32 size_t response_number() const { return response_number_; }
33 const URLResponse* latest_response() const { return latest_response_.get(); }
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;
43 // Overridden from InterfaceFactory<ContentHandler>:
44 void Create(ApplicationConnection* connection,
45 InterfaceRequest<ContentHandler> request) override {
46 bindings_.AddBinding(this, request.Pass());
49 // Overridden from ContentHandler:
50 void StartApplication(InterfaceRequest<Application> application,
51 URLResponsePtr response) override {
52 response_number_++;
53 latest_response_ = response.Pass();
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.
62 size_t response_number_;
63 URLResponsePtr latest_response_;
64 WeakBindingSet<ContentHandler> bindings_;
66 DISALLOW_COPY_AND_ASSIGN(TestContentHandler);
69 class TestApplicationManagerDelegate
70 : public shell::ApplicationManager::Delegate {
71 public:
72 TestApplicationManagerDelegate() {}
73 ~TestApplicationManagerDelegate() override {}
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::Start(url, loader_callback);
84 return true;
87 return false;
90 DISALLOW_COPY_AND_ASSIGN(TestApplicationManagerDelegate);
93 class TestLoader : public shell::ApplicationLoader {
94 public:
95 explicit TestLoader(ApplicationDelegate* delegate) : delegate_(delegate) {}
96 ~TestLoader() override {}
98 private:
99 // Overridden from ApplicationLoader:
100 void Load(const GURL& url, InterfaceRequest<Application> request) override {
101 app_.reset(new ApplicationImpl(delegate_, request.Pass()));
104 ApplicationDelegate* delegate_;
105 scoped_ptr<ApplicationImpl> app_;
107 DISALLOW_COPY_AND_ASSIGN(TestLoader);
110 class AboutFetcherTest : public testing::Test {
111 public:
112 AboutFetcherTest() {}
113 ~AboutFetcherTest() override {}
115 protected:
116 const TestContentHandler* html_content_handler() const {
117 return &html_content_handler_;
120 void ConnectAndWait(const std::string& url) {
121 base::RunLoop run_loop;
123 ServiceProviderPtr service_provider;
124 InterfaceRequest<ServiceProvider> service_provider_request =
125 GetProxy(&service_provider);
126 // This connection error handler will be called when:
127 // - TestContentHandler::StartApplication() has been called (please see
128 // comments in that method); or
129 // - the application manager fails to fetch the requested URL.
130 service_provider.set_connection_error_handler(
131 [&run_loop]() { run_loop.Quit(); });
133 URLRequestPtr request(URLRequest::New());
134 request->url = url;
135 application_manager_->ConnectToApplication(
136 nullptr, request.Pass(), std::string(), GURL(),
137 service_provider_request.Pass(), nullptr, shell::CapabilityFilter(),
138 base::Closure());
140 run_loop.Run();
143 // Overridden from testing::Test:
144 void SetUp() override {
145 application_manager_.reset(new shell::ApplicationManager(&test_delegate_));
146 application_manager_->SetLoaderForURL(
147 make_scoped_ptr(new TestLoader(&html_content_handler_)),
148 GURL("test:html_content_handler"));
149 application_manager_->RegisterContentHandler(
150 "text/html", GURL("test:html_content_handler"));
153 void TearDown() override { application_manager_.reset(); }
155 private:
156 base::ShadowingAtExitManager at_exit_;
157 TestApplicationManagerDelegate test_delegate_;
158 TestContentHandler html_content_handler_;
159 base::MessageLoop loop_;
160 scoped_ptr<shell::ApplicationManager> application_manager_;
162 DISALLOW_COPY_AND_ASSIGN(AboutFetcherTest);
165 TEST_F(AboutFetcherTest, AboutBlank) {
166 ConnectAndWait("about:blank");
168 ASSERT_EQ(1u, html_content_handler()->response_number());
170 const URLResponse* response = html_content_handler()->latest_response();
171 EXPECT_EQ("about:blank", response->url);
172 EXPECT_EQ(200u, response->status_code);
173 EXPECT_EQ("text/html", response->mime_type);
174 EXPECT_FALSE(response->body.is_valid());
177 TEST_F(AboutFetcherTest, UnrecognizedURL) {
178 ConnectAndWait("about:some_unrecognized_url");
180 ASSERT_EQ(0u, html_content_handler()->response_number());
183 } // namespace
184 } // namespace runner
185 } // namespace mojo