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 "remoting/host/gcd_state_updater.h" | 5 #include "remoting/host/gcd_state_updater.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/strings/stringize_macros.h" | 12 #include "base/strings/stringize_macros.h" |
12 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" |
13 #include "base/test/test_mock_time_task_runner.h" | 14 #include "base/test/test_mock_time_task_runner.h" |
14 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
15 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
16 #include "remoting/base/constants.h" | 17 #include "remoting/base/constants.h" |
17 #include "remoting/host/fake_oauth_token_getter.h" | 18 #include "remoting/host/fake_oauth_token_getter.h" |
18 #include "remoting/host/gcd_rest_client.h" | 19 #include "remoting/host/gcd_rest_client.h" |
19 #include "remoting/signaling/fake_signal_strategy.h" | 20 #include "remoting/signaling/fake_signal_strategy.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 12 matching lines...) Expand all Loading... |
33 clock_(task_runner_->GetMockClock()), | 34 clock_(task_runner_->GetMockClock()), |
34 token_getter_(OAuthTokenGetter::SUCCESS, | 35 token_getter_(OAuthTokenGetter::SUCCESS, |
35 "<fake_user_email>", | 36 "<fake_user_email>", |
36 "<fake_access_token>"), | 37 "<fake_access_token>"), |
37 rest_client_(new GcdRestClient( | 38 rest_client_(new GcdRestClient( |
38 "http://gcd_base_url", | 39 "http://gcd_base_url", |
39 "<gcd_device_id>", | 40 "<gcd_device_id>", |
40 nullptr, | 41 nullptr, |
41 &token_getter_)), | 42 &token_getter_)), |
42 signal_strategy_("local_jid") { | 43 signal_strategy_("local_jid") { |
43 rest_client_->SetClockForTest(make_scoped_ptr(new base::SimpleTestClock)); | 44 rest_client_->SetClockForTest(base::WrapUnique(new base::SimpleTestClock)); |
44 } | 45 } |
45 | 46 |
46 void OnSuccess() { on_success_count_++; } | 47 void OnSuccess() { on_success_count_++; } |
47 | 48 |
48 void OnHostIdError() { on_host_id_error_count_++; } | 49 void OnHostIdError() { on_host_id_error_count_++; } |
49 | 50 |
50 protected: | 51 protected: |
51 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 52 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
52 base::ThreadTaskRunnerHandle runner_handler_; | 53 base::ThreadTaskRunnerHandle runner_handler_; |
53 scoped_ptr<base::Clock> clock_; | 54 std::unique_ptr<base::Clock> clock_; |
54 net::TestURLFetcherFactory url_fetcher_factory_; | 55 net::TestURLFetcherFactory url_fetcher_factory_; |
55 FakeOAuthTokenGetter token_getter_; | 56 FakeOAuthTokenGetter token_getter_; |
56 scoped_ptr<GcdRestClient> rest_client_; | 57 std::unique_ptr<GcdRestClient> rest_client_; |
57 FakeSignalStrategy signal_strategy_; | 58 FakeSignalStrategy signal_strategy_; |
58 int on_success_count_ = 0; | 59 int on_success_count_ = 0; |
59 int on_host_id_error_count_ = 0; | 60 int on_host_id_error_count_ = 0; |
60 }; | 61 }; |
61 | 62 |
62 TEST_F(GcdStateUpdaterTest, Success) { | 63 TEST_F(GcdStateUpdaterTest, Success) { |
63 scoped_ptr<GcdStateUpdater> updater(new GcdStateUpdater( | 64 std::unique_ptr<GcdStateUpdater> updater(new GcdStateUpdater( |
64 base::Bind(&GcdStateUpdaterTest::OnSuccess, base::Unretained(this)), | 65 base::Bind(&GcdStateUpdaterTest::OnSuccess, base::Unretained(this)), |
65 base::Bind(&GcdStateUpdaterTest::OnHostIdError, base::Unretained(this)), | 66 base::Bind(&GcdStateUpdaterTest::OnHostIdError, base::Unretained(this)), |
66 &signal_strategy_, std::move(rest_client_))); | 67 &signal_strategy_, std::move(rest_client_))); |
67 | 68 |
68 signal_strategy_.Connect(); | 69 signal_strategy_.Connect(); |
69 task_runner_->RunUntilIdle(); | 70 task_runner_->RunUntilIdle(); |
70 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 71 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
71 ASSERT_TRUE(fetcher); | 72 ASSERT_TRUE(fetcher); |
72 EXPECT_EQ("{\"patches\":[{\"patch\":{" | 73 EXPECT_EQ("{\"patches\":[{\"patch\":{" |
73 "\"base\":{\"_hostVersion\":\"" STRINGIZE(VERSION) "\"," | 74 "\"base\":{\"_hostVersion\":\"" STRINGIZE(VERSION) "\"," |
74 "\"_jabberId\":\"local_jid\"}}," | 75 "\"_jabberId\":\"local_jid\"}}," |
75 "\"timeMs\":0.0}],\"requestTimeMs\":0.0}", | 76 "\"timeMs\":0.0}],\"requestTimeMs\":0.0}", |
76 fetcher->upload_data()); | 77 fetcher->upload_data()); |
77 fetcher->set_response_code(200); | 78 fetcher->set_response_code(200); |
78 fetcher->delegate()->OnURLFetchComplete(fetcher); | 79 fetcher->delegate()->OnURLFetchComplete(fetcher); |
79 EXPECT_EQ(1, on_success_count_); | 80 EXPECT_EQ(1, on_success_count_); |
80 | 81 |
81 updater.reset(); | 82 updater.reset(); |
82 | 83 |
83 EXPECT_EQ(0, on_host_id_error_count_); | 84 EXPECT_EQ(0, on_host_id_error_count_); |
84 } | 85 } |
85 | 86 |
86 TEST_F(GcdStateUpdaterTest, QueuedRequests) { | 87 TEST_F(GcdStateUpdaterTest, QueuedRequests) { |
87 scoped_ptr<GcdStateUpdater> updater(new GcdStateUpdater( | 88 std::unique_ptr<GcdStateUpdater> updater(new GcdStateUpdater( |
88 base::Bind(&GcdStateUpdaterTest::OnSuccess, base::Unretained(this)), | 89 base::Bind(&GcdStateUpdaterTest::OnSuccess, base::Unretained(this)), |
89 base::Bind(&GcdStateUpdaterTest::OnHostIdError, base::Unretained(this)), | 90 base::Bind(&GcdStateUpdaterTest::OnHostIdError, base::Unretained(this)), |
90 &signal_strategy_, std::move(rest_client_))); | 91 &signal_strategy_, std::move(rest_client_))); |
91 | 92 |
92 // Connect, then re-connect with a different JID while the status | 93 // Connect, then re-connect with a different JID while the status |
93 // update for the first connection is pending. | 94 // update for the first connection is pending. |
94 signal_strategy_.Connect(); | 95 signal_strategy_.Connect(); |
95 task_runner_->RunUntilIdle(); | 96 task_runner_->RunUntilIdle(); |
96 signal_strategy_.Disconnect(); | 97 signal_strategy_.Disconnect(); |
97 task_runner_->RunUntilIdle(); | 98 task_runner_->RunUntilIdle(); |
(...skipping 23 matching lines...) Expand all Loading... |
121 fetcher1->set_response_code(200); | 122 fetcher1->set_response_code(200); |
122 fetcher1->delegate()->OnURLFetchComplete(fetcher1); | 123 fetcher1->delegate()->OnURLFetchComplete(fetcher1); |
123 EXPECT_EQ(1, on_success_count_); | 124 EXPECT_EQ(1, on_success_count_); |
124 | 125 |
125 updater.reset(); | 126 updater.reset(); |
126 | 127 |
127 EXPECT_EQ(0, on_host_id_error_count_); | 128 EXPECT_EQ(0, on_host_id_error_count_); |
128 } | 129 } |
129 | 130 |
130 TEST_F(GcdStateUpdaterTest, Retry) { | 131 TEST_F(GcdStateUpdaterTest, Retry) { |
131 scoped_ptr<GcdStateUpdater> updater(new GcdStateUpdater( | 132 std::unique_ptr<GcdStateUpdater> updater(new GcdStateUpdater( |
132 base::Bind(&GcdStateUpdaterTest::OnSuccess, base::Unretained(this)), | 133 base::Bind(&GcdStateUpdaterTest::OnSuccess, base::Unretained(this)), |
133 base::Bind(&GcdStateUpdaterTest::OnHostIdError, base::Unretained(this)), | 134 base::Bind(&GcdStateUpdaterTest::OnHostIdError, base::Unretained(this)), |
134 &signal_strategy_, std::move(rest_client_))); | 135 &signal_strategy_, std::move(rest_client_))); |
135 | 136 |
136 signal_strategy_.Connect(); | 137 signal_strategy_.Connect(); |
137 task_runner_->RunUntilIdle(); | 138 task_runner_->RunUntilIdle(); |
138 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 139 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
139 ASSERT_TRUE(fetcher); | 140 ASSERT_TRUE(fetcher); |
140 fetcher->set_response_code(0); | 141 fetcher->set_response_code(0); |
141 fetcher->delegate()->OnURLFetchComplete(fetcher); | 142 fetcher->delegate()->OnURLFetchComplete(fetcher); |
142 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); | 143 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); |
143 EXPECT_EQ(1.0, clock_->Now().ToDoubleT()); | 144 EXPECT_EQ(1.0, clock_->Now().ToDoubleT()); |
144 fetcher = url_fetcher_factory_.GetFetcherByID(0); | 145 fetcher = url_fetcher_factory_.GetFetcherByID(0); |
145 ASSERT_TRUE(fetcher); | 146 ASSERT_TRUE(fetcher); |
146 fetcher->set_response_code(200); | 147 fetcher->set_response_code(200); |
147 fetcher->delegate()->OnURLFetchComplete(fetcher); | 148 fetcher->delegate()->OnURLFetchComplete(fetcher); |
148 EXPECT_EQ(1, on_success_count_); | 149 EXPECT_EQ(1, on_success_count_); |
149 | 150 |
150 updater.reset(); | 151 updater.reset(); |
151 | 152 |
152 EXPECT_EQ(0, on_host_id_error_count_); | 153 EXPECT_EQ(0, on_host_id_error_count_); |
153 } | 154 } |
154 | 155 |
155 TEST_F(GcdStateUpdaterTest, UnknownHost) { | 156 TEST_F(GcdStateUpdaterTest, UnknownHost) { |
156 scoped_ptr<GcdStateUpdater> updater(new GcdStateUpdater( | 157 std::unique_ptr<GcdStateUpdater> updater(new GcdStateUpdater( |
157 base::Bind(&GcdStateUpdaterTest::OnSuccess, base::Unretained(this)), | 158 base::Bind(&GcdStateUpdaterTest::OnSuccess, base::Unretained(this)), |
158 base::Bind(&GcdStateUpdaterTest::OnHostIdError, base::Unretained(this)), | 159 base::Bind(&GcdStateUpdaterTest::OnHostIdError, base::Unretained(this)), |
159 &signal_strategy_, std::move(rest_client_))); | 160 &signal_strategy_, std::move(rest_client_))); |
160 | 161 |
161 signal_strategy_.Connect(); | 162 signal_strategy_.Connect(); |
162 task_runner_->RunUntilIdle(); | 163 task_runner_->RunUntilIdle(); |
163 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 164 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
164 ASSERT_TRUE(fetcher); | 165 ASSERT_TRUE(fetcher); |
165 fetcher->set_response_code(404); | 166 fetcher->set_response_code(404); |
166 fetcher->delegate()->OnURLFetchComplete(fetcher); | 167 fetcher->delegate()->OnURLFetchComplete(fetcher); |
167 EXPECT_EQ(0, on_success_count_); | 168 EXPECT_EQ(0, on_success_count_); |
168 EXPECT_EQ(1, on_host_id_error_count_); | 169 EXPECT_EQ(1, on_host_id_error_count_); |
169 } | 170 } |
170 | 171 |
171 } // namespace remoting | 172 } // namespace remoting |
OLD | NEW |