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..25e6fd8eefa0e5556947b420db5cd281f92ae917 |
--- /dev/null |
+++ b/components/gcm_driver/gcm_channel_status_request_unittest.cc |
@@ -0,0 +1,193 @@ |
+// 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: |
+ void StartRequest(); |
+ void SetResponseStatusAndString(net::HttpStatusCode status_code, |
+ const std::string& response_body); |
+ void SetResponseProtoData(bool enabled_set, |
+ bool enabled, |
+ 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) { |
+} |
+ |
+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( |
+ bool enabled_set, bool enabled, int poll_interval_seconds) { |
fgorski
2014/09/03 20:42:16
you could use a "test scenario" enum instead:
{
jianli
2014/09/03 21:17:55
Done.
|
+ gcm_proto::ExperimentStatusResponse response_proto; |
+ if (enabled_set) |
+ response_proto.mutable_gcm_channel()->set_enabled(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(false, false, 0); |
+ CompleteFetch(); |
+ |
+ EXPECT_FALSE(request_callback_invoked_); |
+} |
+ |
+TEST_F(GCMChannelStatusRequestTest, ResponseWithDisabledStatus) { |
+ StartRequest(); |
+ SetResponseProtoData(true, false, 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(true, true, 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); |
+ StartRequest(); |
+ SetResponseProtoData(false, false, 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. |
fgorski
2014/09/03 20:42:16
There is an assumption here that min poll interval
jianli
2014/09/03 21:17:55
I think this still works if the min poll interval
|
+ base::TimeDelta poll_interval = |
+ GCMChannelStatusRequest::min_poll_interval_for_testing() - |
+ base::TimeDelta::FromMinutes(15); |
+ StartRequest(); |
+ SetResponseProtoData(false, false, 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(true, false, poll_interval.InSeconds()); |
+ CompleteFetch(); |
+ |
+ EXPECT_TRUE(request_callback_invoked_); |
+ EXPECT_FALSE(enabled_); |
+ EXPECT_EQ(poll_interval, poll_interval_); |
+} |
+ |
+} // namespace gcm |