OLD | NEW |
---|---|
(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/bind.h" | |
7 #include "base/logging.h" | |
8 #include "base/macros.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/run_loop.h" | |
12 #include "mojo/fetcher/network_fetcher.h" | |
13 #include "mojo/public/cpp/bindings/strong_binding.h" | |
14 #include "mojo/runner/context.h" | |
15 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | |
16 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace mojo { | |
20 namespace fetcher { | |
21 namespace { | |
22 | |
23 const char k200Request[] = "http://request_expect_200"; | |
24 const char k404Request[] = "http://request_expect_404"; | |
25 const char k504Request[] = "http://request_expect_504"; | |
26 const char kErrorRequest[] = "http://request_expect_error"; | |
27 | |
28 class TestURLLoaderImpl : public URLLoader { | |
29 public: | |
30 explicit TestURLLoaderImpl(InterfaceRequest<URLLoader> request) | |
31 : binding_(this, request.Pass()) {} | |
32 ~TestURLLoaderImpl() override {} | |
33 | |
34 private: | |
35 // URLLoader implementation. | |
36 void Start(URLRequestPtr request, | |
37 const Callback<void(URLResponsePtr)>& callback) override { | |
38 URLResponsePtr response(URLResponse::New()); | |
39 response->url = request->url; | |
40 if (request->url == std::string(k200Request)) { | |
41 response->mime_type = "text/html"; | |
42 response->status_code = 200; | |
43 } else if (request->url == std::string(k404Request)) { | |
44 response->mime_type = "text/html"; | |
45 response->status_code = 404; | |
46 } else if (request->url == std::string(k504Request)) { | |
47 response->mime_type = "text/html"; | |
48 response->status_code = 504; | |
49 } else { | |
50 response->error = NetworkError::New(); | |
51 response->error->code = -2; | |
52 } | |
53 callback.Run(response.Pass()); | |
54 } | |
55 void FollowRedirect(const Callback<void(URLResponsePtr)>& callback) override { | |
56 NOTREACHED(); | |
57 } | |
58 void QueryStatus( | |
59 const Callback<void(URLLoaderStatusPtr)>& callback) override { | |
60 NOTREACHED(); | |
61 } | |
62 | |
63 StrongBinding<URLLoader> binding_; | |
64 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderImpl); | |
65 }; | |
66 | |
67 class TestURLLoaderFactoryImpl : public URLLoaderFactory { | |
68 public: | |
69 explicit TestURLLoaderFactoryImpl(InterfaceRequest<URLLoaderFactory> request) | |
70 : binding_(this, request.Pass()) {} | |
71 ~TestURLLoaderFactoryImpl() override {} | |
72 | |
73 private: | |
74 // URLLoaderFactory implementation. | |
75 void CreateURLLoader(InterfaceRequest<URLLoader> loader) override { | |
76 new TestURLLoaderImpl(loader.Pass()); | |
77 } | |
78 | |
79 StrongBinding<URLLoaderFactory> binding_; | |
80 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactoryImpl); | |
81 }; | |
82 | |
83 class FetchCallbackHelper { | |
84 public: | |
85 FetchCallbackHelper() : run_loop_(nullptr) {} | |
86 ~FetchCallbackHelper() {} | |
87 | |
88 shell::Fetcher::FetchCallback GetCallback() { | |
89 return base::Bind(&FetchCallbackHelper::CallbackHandler, | |
90 base::Unretained(this)); | |
91 } | |
92 | |
93 void WaitForCallback() { | |
94 base::RunLoop run_loop; | |
95 run_loop_ = &run_loop; | |
sky
2015/09/16 01:14:45
nit: base::AutoReset
yzshen1
2015/09/16 05:24:54
Done.
| |
96 run_loop_->Run(); | |
97 run_loop_ = nullptr; | |
98 } | |
99 | |
100 shell::Fetcher* fetcher() const { return fetcher_.get(); } | |
101 | |
102 private: | |
103 void CallbackHandler(scoped_ptr<shell::Fetcher> fetcher) { | |
104 fetcher_ = fetcher.Pass(); | |
105 if (run_loop_) | |
106 run_loop_->Quit(); | |
107 } | |
108 | |
109 base::RunLoop* run_loop_; | |
sky
2015/09/16 01:14:45
document what where this comes from (especially ow
yzshen1
2015/09/16 05:24:54
Done.
| |
110 scoped_ptr<shell::Fetcher> fetcher_; | |
111 DISALLOW_COPY_AND_ASSIGN(FetchCallbackHelper); | |
112 }; | |
113 | |
114 class NetworkFetcherTest : public testing::Test { | |
115 public: | |
116 NetworkFetcherTest() {} | |
117 ~NetworkFetcherTest() override {} | |
118 | |
119 protected: | |
120 // Overridden from testing::Test: | |
121 void SetUp() override { | |
122 runner::Context::EnsureEmbedderIsInitialized(); | |
123 // Automatically destroyed when |url_loader_factory_| is closed. | |
124 new TestURLLoaderFactoryImpl(GetProxy(&url_loader_factory_)); | |
125 } | |
126 | |
127 // When |expect_fetch_success| is false, |expected_status_code| is ignored. | |
128 void TestFetchURL(const std::string& url, | |
129 bool expect_fetch_success, | |
130 uint32_t expected_status_code) { | |
131 FetchCallbackHelper helper; | |
132 | |
133 URLRequestPtr request(URLRequest::New()); | |
134 request->url = url; | |
135 new NetworkFetcher(true, request.Pass(), url_loader_factory_.get(), | |
136 helper.GetCallback()); | |
137 helper.WaitForCallback(); | |
138 | |
139 if (!expect_fetch_success) { | |
140 ASSERT_FALSE(helper.fetcher()); | |
141 } else { | |
142 ASSERT_TRUE(helper.fetcher()); | |
143 URLResponsePtr response = helper.fetcher()->AsURLResponse(nullptr, 0); | |
144 ASSERT_TRUE(response); | |
145 EXPECT_EQ(url, response->url); | |
146 EXPECT_EQ(expected_status_code, response->status_code); | |
147 } | |
148 } | |
149 | |
150 private: | |
151 base::ShadowingAtExitManager at_exit_; | |
152 base::MessageLoop loop_; | |
153 URLLoaderFactoryPtr url_loader_factory_; | |
154 | |
155 DISALLOW_COPY_AND_ASSIGN(NetworkFetcherTest); | |
156 }; | |
157 | |
158 TEST_F(NetworkFetcherTest, FetchSucceeded) { | |
159 TestFetchURL(k200Request, true, 200u); | |
sky
2015/09/16 01:14:45
I would separate each into its own test. That way
yzshen1
2015/09/16 05:24:54
Done.
| |
160 TestFetchURL(k404Request, true, 404u); | |
161 TestFetchURL(k504Request, true, 504u); | |
162 } | |
163 | |
164 TEST_F(NetworkFetcherTest, FetchFailed) { | |
165 TestFetchURL(kErrorRequest, false, 0u); | |
166 } | |
167 | |
168 } // namespace | |
169 } // namespace fetcher | |
170 } // namespace mojo | |
OLD | NEW |