Index: components/gcm_driver/gcm_channel_status_request_unittest.cc |
diff --git a/components/gcm_driver/gcm_channel_status_request_unittest.cc b/components/gcm_driver/gcm_channel_status_request_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a9a117950d22c312b0659890b5a2a2f184dbd39a |
--- /dev/null |
+++ b/components/gcm_driver/gcm_channel_status_request_unittest.cc |
@@ -0,0 +1,197 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/message_loop/message_loop.h" |
+#include "components/gcm_driver/gcm_channel_status_request.h" |
+#include "components/gcm_driver/proto/gcm_channel_status.pb.h" |
+#include "net/url_request/test_url_fetcher_factory.h" |
+#include "net/url_request/url_request_test_util.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace gcm { |
+ |
+class GCMChannelStatusRequestTest : public testing::Test { |
+ public: |
+ GCMChannelStatusRequestTest(); |
+ virtual ~GCMChannelStatusRequestTest(); |
+ |
+ protected: |
+ enum GCMStatus { |
+ NOT_SPECIFIED, |
+ GCM_ENABLED, |
+ GCM_DISABLED, |
+ }; |
+ |
+ void StartRequest(); |
+ void SetResponseStatusAndString(net::HttpStatusCode status_code, |
+ const std::string& response_body); |
+ void SetResponseProtoData(GCMStatus status, int poll_interval_seconds); |
+ void CompleteFetch(); |
+ void OnRequestCompleted(bool enabled, const base::TimeDelta& poll_interval); |
+ |
+ scoped_ptr<GCMChannelStatusRequest> request_; |
+ base::MessageLoop message_loop_; |
+ net::TestURLFetcherFactory url_fetcher_factory_; |
+ scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
+ bool request_callback_invoked_; |
+ bool enabled_; |
+ base::TimeDelta poll_interval_; |
+}; |
+ |
+GCMChannelStatusRequestTest::GCMChannelStatusRequestTest() |
+ : url_request_context_getter_(new net::TestURLRequestContextGetter( |
+ message_loop_.message_loop_proxy())), |
+ request_callback_invoked_(false), |
+ enabled_(false) { |
Nicolas Zea
2014/09/04 23:57:50
Presumably we'll normally be enabled by default ri
jianli
2014/09/05 18:41:55
Done.
|
+} |
+ |
+GCMChannelStatusRequestTest::~GCMChannelStatusRequestTest() { |
+} |
+ |
+void GCMChannelStatusRequestTest::StartRequest() { |
+ request_.reset(new GCMChannelStatusRequest( |
+ "", |
+ url_request_context_getter_.get(), |
+ base::Bind(&GCMChannelStatusRequestTest::OnRequestCompleted, |
+ base::Unretained(this)))); |
+ request_->Start(); |
+} |
+ |
+void GCMChannelStatusRequestTest::SetResponseStatusAndString( |
+ net::HttpStatusCode status_code, |
+ const std::string& response_body) { |
+ net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
+ ASSERT_TRUE(fetcher); |
+ fetcher->set_response_code(status_code); |
+ fetcher->SetResponseString(response_body); |
+} |
+ |
+void GCMChannelStatusRequestTest::SetResponseProtoData( |
+ GCMStatus status, int poll_interval_seconds) { |
+ gcm_proto::ExperimentStatusResponse response_proto; |
+ if (status != NOT_SPECIFIED) |
+ response_proto.mutable_gcm_channel()->set_enabled(status == GCM_ENABLED); |
+ |
+ // Zero |poll_interval_seconds| means the optional field is not set. |
+ if (poll_interval_seconds) |
+ response_proto.set_poll_interval_seconds(poll_interval_seconds); |
+ |
+ std::string response_string; |
+ response_proto.SerializeToString(&response_string); |
+ SetResponseStatusAndString(net::HTTP_OK, response_string); |
+} |
+ |
+void GCMChannelStatusRequestTest::CompleteFetch() { |
+ request_callback_invoked_ = false; |
+ net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
+ ASSERT_TRUE(fetcher); |
+ fetcher->delegate()->OnURLFetchComplete(fetcher); |
+} |
+ |
+void GCMChannelStatusRequestTest::OnRequestCompleted( |
+ bool enabled, const base::TimeDelta& poll_interval) { |
+ request_callback_invoked_ = true; |
+ enabled_ = enabled; |
+ poll_interval_ = poll_interval; |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseHttpStatusNotOK) { |
+ StartRequest(); |
+ SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, ""); |
+ CompleteFetch(); |
+ |
+ EXPECT_FALSE(request_callback_invoked_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseEmpty) { |
+ StartRequest(); |
+ SetResponseStatusAndString(net::HTTP_OK, ""); |
+ CompleteFetch(); |
+ |
+ EXPECT_FALSE(request_callback_invoked_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseNotInProtoFormat) { |
+ StartRequest(); |
+ SetResponseStatusAndString(net::HTTP_OK, "foo"); |
+ CompleteFetch(); |
+ |
+ EXPECT_FALSE(request_callback_invoked_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseEmptyProtoData) { |
+ StartRequest(); |
+ SetResponseProtoData(NOT_SPECIFIED, 0); |
+ CompleteFetch(); |
+ |
+ EXPECT_FALSE(request_callback_invoked_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatus) { |
+ StartRequest(); |
+ SetResponseProtoData(GCM_DISABLED, 0); |
+ CompleteFetch(); |
+ |
+ EXPECT_TRUE(request_callback_invoked_); |
+ EXPECT_FALSE(enabled_); |
+ EXPECT_EQ(GCMChannelStatusRequest::default_poll_interval_for_testing(), |
+ poll_interval_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseWithEnabledStatus) { |
+ StartRequest(); |
+ SetResponseProtoData(GCM_ENABLED, 0); |
+ CompleteFetch(); |
+ |
+ EXPECT_TRUE(request_callback_invoked_); |
+ EXPECT_TRUE(enabled_); |
+ EXPECT_EQ(GCMChannelStatusRequest::default_poll_interval_for_testing(), |
+ poll_interval_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseWithPollInterval) { |
+ // Setting a poll interval 15 minutes longer than the minimum interval we |
+ // enforce. |
+ base::TimeDelta poll_interval = |
+ GCMChannelStatusRequest::min_poll_interval_for_testing() + |
+ base::TimeDelta::FromMinutes(15); |
Nicolas Zea
2014/09/04 23:57:50
Isn't the minimum 30 minutes?
jianli
2014/09/05 18:41:55
Yes, the minimum is 30 minutes. Here we're setting
|
+ StartRequest(); |
+ SetResponseProtoData(NOT_SPECIFIED, poll_interval.InSeconds()); |
+ CompleteFetch(); |
+ |
+ EXPECT_TRUE(request_callback_invoked_); |
+ EXPECT_TRUE(enabled_); |
+ EXPECT_EQ(poll_interval, poll_interval_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseWithShortPollInterval) { |
+ // Setting a poll interval 15 minutes shorter than the minimum interval we |
+ // enforce. |
+ base::TimeDelta poll_interval = |
+ GCMChannelStatusRequest::min_poll_interval_for_testing() - |
+ base::TimeDelta::FromMinutes(15); |
+ StartRequest(); |
+ SetResponseProtoData(NOT_SPECIFIED, poll_interval.InSeconds()); |
+ CompleteFetch(); |
+ |
+ EXPECT_TRUE(request_callback_invoked_); |
+ EXPECT_TRUE(enabled_); |
+ EXPECT_EQ(GCMChannelStatusRequest::min_poll_interval_for_testing(), |
+ poll_interval_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatusAndPollInterval) { |
+ base::TimeDelta poll_interval = |
+ GCMChannelStatusRequest::min_poll_interval_for_testing() + |
+ base::TimeDelta::FromMinutes(15); |
+ StartRequest(); |
+ SetResponseProtoData(GCM_DISABLED, poll_interval.InSeconds()); |
+ CompleteFetch(); |
+ |
+ EXPECT_TRUE(request_callback_invoked_); |
+ EXPECT_FALSE(enabled_); |
+ EXPECT_EQ(poll_interval, poll_interval_); |
+} |
+ |
+} // namespace gcm |