Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5004)

Unified Diff: chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher_unittest.cc

Issue 10836182: Obfuscated Gaia ID fetcher (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: CR changes per Munjal and DCheng Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher_unittest.cc
diff --git a/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher_unittest.cc b/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e4efeda80152e4be12d3767f6ee24d482f129773
--- /dev/null
+++ b/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher_unittest.cc
@@ -0,0 +1,115 @@
+// Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h"
+
+#include <string>
+
+#include "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.h"
+#include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h"
+#include "net/url_request/test_url_fetcher_factory.h"
+
+namespace {
+
+static const char kGoodData[] = "{ \"id\" : \"My-channel-id\" }";
+static const char kBadJsonData[] = "I am not a JSON string";
+static const char kDictionaryMissingIdData[] = "{ \"foo\" : \"bar\" }";
+static const char kNonDictionaryJsonData[] = "{ 0.5 }";
+
+// Delegate class for catching notifications from the ObfuscatedGaiaIdFetcher.
+class TestDelegate : public extensions::ObfuscatedGaiaIdFetcher::Delegate {
+ public:
+ // inherited methods
+ virtual void OnObfuscatedGaiaIdFetchSuccess(
+ const std::string& obfuscated_id) {
+ succeeded_ = true;
+ }
+ virtual void OnObfuscatedGaiaIdFetchFailure(
+ const GoogleServiceAuthError& error) {
+ failed_ = true;
+ }
+ TestDelegate() {
dcheng 2012/08/20 19:10:01 Prefer initializer syntax.
Pete Williamson 2012/08/20 20:53:05 Done.
+ succeeded_ = false;
+ failed_ = false;
+ }
+ virtual ~TestDelegate() {}
+ bool Succeeded() { return succeeded_; }
dcheng 2012/08/20 19:10:01 bool succeeded() const { return succeeded_; }
Pete Williamson 2012/08/20 20:53:05 Done.
+ bool Failed() { return failed_; }
dcheng 2012/08/20 19:10:01 Ditto.
Pete Williamson 2012/08/20 20:53:05 Done.
+
+ private:
+ bool succeeded_;
+ bool failed_;
+ DISALLOW_COPY_AND_ASSIGN(TestDelegate);
+};
+
+} // namespace
+
+namespace extensions {
+
+TEST(ObfuscatedGaiaIdFetcherTest, ParseResponse) {
+ scoped_ptr<TestDelegate> delegate(new TestDelegate());
+ std::string refresh_token;
+ ObfuscatedGaiaIdFetcher fetcher(NULL, delegate.get(), refresh_token);
+
+ // Try a good response string.
+ std::string channel_id_out1;
+ bool ret1 = fetcher.ParseResponse(kGoodData, &channel_id_out1);
+ EXPECT_EQ(channel_id_out1, "My-channel-id");
+ EXPECT_TRUE(ret1);
+
+ // Try badly formatted JSON.
+ std::string channel_id_out2;
+ bool ret2 = fetcher.ParseResponse(kBadJsonData, &channel_id_out2);
dcheng 2012/08/20 19:10:01 Isn't this a static method? We can just do Obfusca
Pete Williamson 2012/08/20 20:53:05 Done.
+ EXPECT_TRUE(channel_id_out2.empty());
+ EXPECT_FALSE(ret2);
+
+ // Try a JSON dictionary with no "id" value.
+ std::string channel_id_out3;
+ bool ret3 = fetcher.ParseResponse(kDictionaryMissingIdData, &channel_id_out3);
+ EXPECT_TRUE(channel_id_out3.empty());
+ EXPECT_FALSE(ret3);
+
+ // Try JSON, but not a dictionary.
+ std::string channel_id_out4;
+ bool ret4 = fetcher.ParseResponse(kNonDictionaryJsonData, &channel_id_out4);
+ EXPECT_NE(channel_id_out4, "My-channel-id");
+ EXPECT_FALSE(ret4);
+}
+
+TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) {
+ scoped_ptr<TestDelegate> delegate(new TestDelegate());
+ std::string refresh_token;
+ ObfuscatedGaiaIdFetcher* fetcher =
dcheng 2012/08/20 19:10:01 scoped_ptr, as I think this now leaks.
Pete Williamson 2012/08/20 20:53:05 Done.
+ new ObfuscatedGaiaIdFetcher(NULL, delegate.get(), refresh_token);
dcheng 2012/08/20 19:10:01 Nit: You can probably just say std::string(), as p
Pete Williamson 2012/08/20 20:53:05 Done.
+
+ net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
+ url_fetcher.set_status(
+ net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
+ url_fetcher.SetResponseString(kGoodData);
+
+ // Test the happy path.
+ fetcher->ProcessApiCallSuccess(&url_fetcher);
+ EXPECT_TRUE(delegate->Succeeded());
+}
+
+TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) {
+ scoped_ptr<TestDelegate> delegate(new TestDelegate());
+ std::string refresh_token;
+ ObfuscatedGaiaIdFetcher* fetcher =
dcheng 2012/08/20 19:10:01 scoped_ptr.
Pete Williamson 2012/08/20 20:53:05 Done.
+ new ObfuscatedGaiaIdFetcher(NULL, delegate.get(), refresh_token);
dcheng 2012/08/20 19:10:01 Same as above about refresh_token.
Pete Williamson 2012/08/20 20:53:05 Done.
+
+ net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
+ url_fetcher.set_status(
+ net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
+ url_fetcher.SetResponseString(kDictionaryMissingIdData);
+
+ // Test with bad data, ensure it fails.
+ fetcher->ProcessApiCallSuccess(&url_fetcher);
+ EXPECT_TRUE(delegate->Failed());
+
+ // TODO: add case for when the base class fails and calls
dcheng 2012/08/20 19:10:01 TODO(petewil)
Pete Williamson 2012/08/20 20:53:05 Done.
+ // ProcessApiCallFailure
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698