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 "remoting/host/gcd_state_updater.h"
10 #include "base/strings/stringize_macros.h"
11 #include "base/test/simple_test_clock.h"
12 #include "base/test/test_mock_time_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "remoting/base/constants.h"
16 #include "remoting/host/fake_oauth_token_getter.h"
17 #include "remoting/host/gcd_rest_client.h"
18 #include "remoting/signaling/fake_signal_strategy.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 using testing::Return
;
27 class GcdStateUpdaterTest
: public testing::Test
{
30 : task_runner_(new base::TestMockTimeTaskRunner()),
31 runner_handler_(task_runner_
),
32 clock_(task_runner_
->GetMockClock()),
33 token_getter_(OAuthTokenGetter::SUCCESS
,
35 "<fake_access_token>"),
36 rest_client_(new GcdRestClient(
37 "http://gcd_base_url",
41 signal_strategy_("local_jid") {
42 rest_client_
->SetClockForTest(make_scoped_ptr(new base::SimpleTestClock
));
45 void OnSuccess() { on_success_count_
++; }
47 void OnHostIdError() { on_host_id_error_count_
++; }
50 scoped_refptr
<base::TestMockTimeTaskRunner
> task_runner_
;
51 base::ThreadTaskRunnerHandle runner_handler_
;
52 scoped_ptr
<base::Clock
> clock_
;
53 net::TestURLFetcherFactory url_fetcher_factory_
;
54 FakeOAuthTokenGetter token_getter_
;
55 scoped_ptr
<GcdRestClient
> rest_client_
;
56 FakeSignalStrategy signal_strategy_
;
57 int on_success_count_
= 0;
58 int on_host_id_error_count_
= 0;
61 TEST_F(GcdStateUpdaterTest
, Success
) {
62 scoped_ptr
<GcdStateUpdater
> updater(new GcdStateUpdater(
63 base::Bind(&GcdStateUpdaterTest::OnSuccess
, base::Unretained(this)),
64 base::Bind(&GcdStateUpdaterTest::OnHostIdError
, base::Unretained(this)),
65 &signal_strategy_
, rest_client_
.Pass()));
67 signal_strategy_
.Connect();
68 task_runner_
->RunUntilIdle();
69 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
71 EXPECT_EQ("{\"patches\":[{\"patch\":{"
72 "\"base\":{\"_hostVersion\":\"" STRINGIZE(VERSION
) "\","
73 "\"_jabberId\":\"local_jid\"}},"
74 "\"timeMs\":0.0}],\"requestTimeMs\":0.0}",
75 fetcher
->upload_data());
76 fetcher
->set_response_code(200);
77 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
78 EXPECT_EQ(1, on_success_count_
);
82 EXPECT_EQ(0, on_host_id_error_count_
);
85 TEST_F(GcdStateUpdaterTest
, QueuedRequests
) {
86 scoped_ptr
<GcdStateUpdater
> updater(new GcdStateUpdater(
87 base::Bind(&GcdStateUpdaterTest::OnSuccess
, base::Unretained(this)),
88 base::Bind(&GcdStateUpdaterTest::OnHostIdError
, base::Unretained(this)),
89 &signal_strategy_
, rest_client_
.Pass()));
91 // Connect, then re-connect with a different JID while the status
92 // update for the first connection is pending.
93 signal_strategy_
.Connect();
94 task_runner_
->RunUntilIdle();
95 signal_strategy_
.Disconnect();
96 task_runner_
->RunUntilIdle();
97 signal_strategy_
.SetLocalJid("local_jid2");
98 signal_strategy_
.Connect();
99 task_runner_
->RunUntilIdle();
101 // Let the first status update finish. This should be a no-op in
102 // the updater because the local JID has changed since this request
104 net::TestURLFetcher
* fetcher0
= url_fetcher_factory_
.GetFetcherByID(0);
105 fetcher0
->set_response_code(200);
106 fetcher0
->delegate()->OnURLFetchComplete(fetcher0
);
107 EXPECT_EQ(0, on_success_count_
);
109 // Wait for the next retry.
110 task_runner_
->FastForwardBy(base::TimeDelta::FromSeconds(1));
112 // There should be a new pending request now with the new local JID.
113 net::TestURLFetcher
* fetcher1
= url_fetcher_factory_
.GetFetcherByID(0);
114 ASSERT_TRUE(fetcher1
);
115 EXPECT_EQ("{\"patches\":[{\"patch\":{"
116 "\"base\":{\"_hostVersion\":\"" STRINGIZE(VERSION
) "\","
117 "\"_jabberId\":\"local_jid2\"}},"
118 "\"timeMs\":0.0}],\"requestTimeMs\":0.0}",
119 fetcher1
->upload_data());
120 fetcher1
->set_response_code(200);
121 fetcher1
->delegate()->OnURLFetchComplete(fetcher1
);
122 EXPECT_EQ(1, on_success_count_
);
126 EXPECT_EQ(0, on_host_id_error_count_
);
129 TEST_F(GcdStateUpdaterTest
, Retry
) {
130 scoped_ptr
<GcdStateUpdater
> updater(new GcdStateUpdater(
131 base::Bind(&GcdStateUpdaterTest::OnSuccess
, base::Unretained(this)),
132 base::Bind(&GcdStateUpdaterTest::OnHostIdError
, base::Unretained(this)),
133 &signal_strategy_
, rest_client_
.Pass()));
135 signal_strategy_
.Connect();
136 task_runner_
->RunUntilIdle();
137 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
138 ASSERT_TRUE(fetcher
);
139 fetcher
->set_response_code(0);
140 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
141 task_runner_
->FastForwardBy(base::TimeDelta::FromSeconds(1));
142 EXPECT_EQ(1.0, clock_
->Now().ToDoubleT());
143 fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
144 ASSERT_TRUE(fetcher
);
145 fetcher
->set_response_code(200);
146 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
147 EXPECT_EQ(1, on_success_count_
);
151 EXPECT_EQ(0, on_host_id_error_count_
);
154 TEST_F(GcdStateUpdaterTest
, UnknownHost
) {
155 scoped_ptr
<GcdStateUpdater
> updater(new GcdStateUpdater(
156 base::Bind(&GcdStateUpdaterTest::OnSuccess
, base::Unretained(this)),
157 base::Bind(&GcdStateUpdaterTest::OnHostIdError
, base::Unretained(this)),
158 &signal_strategy_
, rest_client_
.Pass()));
160 signal_strategy_
.Connect();
161 task_runner_
->RunUntilIdle();
162 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
163 ASSERT_TRUE(fetcher
);
164 fetcher
->set_response_code(404);
165 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
166 EXPECT_EQ(0, on_success_count_
);
167 EXPECT_EQ(1, on_host_id_error_count_
);
170 } // namespace remoting