OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/fetcher/data_fetcher.h" | 5 #include "mojo/shell/fetcher/data_fetcher.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "mojo/public/cpp/system/data_pipe.h" | 18 #include "mojo/public/cpp/system/data_pipe.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 namespace fetcher { | 22 namespace shell { |
23 namespace { | 23 namespace { |
24 | 24 |
25 class FetchCallbackHelper { | 25 class FetchCallbackHelper { |
26 public: | 26 public: |
27 FetchCallbackHelper() : run_loop_(nullptr) {} | 27 FetchCallbackHelper() : run_loop_(nullptr) {} |
28 ~FetchCallbackHelper() {} | 28 ~FetchCallbackHelper() {} |
29 | 29 |
30 shell::Fetcher::FetchCallback GetCallback() { | 30 Fetcher::FetchCallback GetCallback() { |
31 return base::Bind(&FetchCallbackHelper::CallbackHandler, | 31 return base::Bind(&FetchCallbackHelper::CallbackHandler, |
32 base::Unretained(this)); | 32 base::Unretained(this)); |
33 } | 33 } |
34 | 34 |
35 void WaitForCallback() { | 35 void WaitForCallback() { |
36 base::RunLoop run_loop; | 36 base::RunLoop run_loop; |
37 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop); | 37 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop); |
38 run_loop.Run(); | 38 run_loop.Run(); |
39 } | 39 } |
40 | 40 |
41 shell::Fetcher* fetcher() const { return fetcher_.get(); } | 41 Fetcher* fetcher() const { return fetcher_.get(); } |
42 | 42 |
43 private: | 43 private: |
44 void CallbackHandler(scoped_ptr<shell::Fetcher> fetcher) { | 44 void CallbackHandler(scoped_ptr<Fetcher> fetcher) { |
45 fetcher_ = std::move(fetcher); | 45 fetcher_ = std::move(fetcher); |
46 if (run_loop_) | 46 if (run_loop_) |
47 run_loop_->Quit(); | 47 run_loop_->Quit(); |
48 } | 48 } |
49 | 49 |
50 // If it is not null, it points to a stack-allocated base::RunLoop instance in | 50 // If it is not null, it points to a stack-allocated base::RunLoop instance in |
51 // WaitForCallback(). | 51 // WaitForCallback(). |
52 base::RunLoop* run_loop_; | 52 base::RunLoop* run_loop_; |
53 scoped_ptr<shell::Fetcher> fetcher_; | 53 scoped_ptr<Fetcher> fetcher_; |
54 DISALLOW_COPY_AND_ASSIGN(FetchCallbackHelper); | 54 DISALLOW_COPY_AND_ASSIGN(FetchCallbackHelper); |
55 }; | 55 }; |
56 | 56 |
57 class DataFetcherTest : public testing::Test { | 57 class DataFetcherTest : public testing::Test { |
58 public: | 58 public: |
59 DataFetcherTest() {} | 59 DataFetcherTest() {} |
60 ~DataFetcherTest() override {} | 60 ~DataFetcherTest() override {} |
61 | 61 |
62 protected: | 62 protected: |
63 void TestFetchURL(const std::string& url, | 63 void TestFetchURL(const std::string& url, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 TEST_F(DataFetcherTest, BasicSuccess) { | 108 TEST_F(DataFetcherTest, BasicSuccess) { |
109 TestFetchURL("data:text/html,Hello world", 200, "text/html", "Hello world"); | 109 TestFetchURL("data:text/html,Hello world", 200, "text/html", "Hello world"); |
110 } | 110 } |
111 | 111 |
112 TEST_F(DataFetcherTest, BasicFailure) { | 112 TEST_F(DataFetcherTest, BasicFailure) { |
113 TestFetchURL("about:blank", 400, std::string(), std::string()); | 113 TestFetchURL("about:blank", 400, std::string(), std::string()); |
114 TestFetchURL("data:;base64,aGVs_-_-", 400, std::string(), std::string()); | 114 TestFetchURL("data:;base64,aGVs_-_-", 400, std::string(), std::string()); |
115 } | 115 } |
116 | 116 |
117 } // namespace | 117 } // namespace |
118 } // namespace fetcher | 118 } // namespace shell |
119 } // namespace mojo | 119 } // namespace mojo |
OLD | NEW |