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

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 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..3484d755df734b75ccd77d4feef0b0e79b2066bd
--- /dev/null
+++ b/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher_unittest.cc
@@ -0,0 +1,110 @@
+// 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/stub_url_fetcher.h"
+
+namespace {
+
+const char goodData[] = "{ \"id\" : \"My-channel-id\" }";
+const char badJsonData[] = "I am not a JSON string";
+const char dictionaryMissingIdData[] = "{ \"foo\" : \"bar\" }";
+const char nonDictionaryJsonData[] = "{ 0.5 }";
Munjal (Google) 2012/08/15 20:15:44 Style issues in naming the constants. Use kGoodDat
Pete Williamson 2012/08/15 22:02:29 Done.
+
+// delegate class for catching notifications from the ObfuscatedGaiaIdFetcher
+class TestDelegate : public extensions::ObfuscatedGaiaIdFetcher::Delegate {
+ public:
+ // inherited methods
+ virtual void OnObfuscatedGaiaIdFetchSuccess(
+ const std::string& access_token) {
+ succeeded_ = true;
+ }
+ virtual void OnObfuscatedGaiaIdFetchFailure(
+ const GoogleServiceAuthError& error) {
+ failed_ = true;
+ }
+ TestDelegate() {
+ Clear();
+ }
+ virtual ~TestDelegate() {}
+ void Clear() {
+ succeeded_ = false;
+ failed_ = false;
+ }
Munjal (Google) 2012/08/15 20:15:44 It is best to not reuse instances of TestDelegate.
Pete Williamson 2012/08/15 22:02:29 Removed the clear method (it is already not being
+ bool Succeeded() { return succeeded_; }
+ bool Failed() { return failed_; }
+
+ private:
+ bool succeeded_;
+ bool failed_;
+};
+
+} // namespace anonymous
Munjal (Google) 2012/08/15 20:15:44 Nit: remove "anonymous"
Pete Williamson 2012/08/15 22:02:29 Done.
+
+namespace extensions {
+
+TEST( ObfuscatedGaiaIdFetcherTest, ParseResponse) {
Munjal (Google) 2012/08/15 20:15:44 Nit: remove space after (
Pete Williamson 2012/08/15 22:02:29 Done.
+ bool ret = false;
+ std::string channelIdOut1;
+ std::string channelIdOut2;
+ std::string channelIdOut3;
+ std::string channelIdOut4;
Munjal (Google) 2012/08/15 20:15:44 style issues in naming varibles. Use underscores.
Pete Williamson 2012/08/15 22:02:29 Done.
+ TestDelegate delegate;
+ std::string refreshToken;
+ std::vector<std::string> scopes;
+ ObfuscatedGaiaIdFetcher fetcher(NULL, &delegate, refreshToken, scopes);
+
+ // good response string
+ ret = fetcher.ParseResponse(goodData, &channelIdOut1);
+ ASSERT_EQ(channelIdOut1, "My-channel-id");
+ ASSERT_TRUE(ret);
+
+ // badly formattedf JSON
+ ret = fetcher.ParseResponse(badJsonData, &channelIdOut2);
Munjal (Google) 2012/08/15 20:15:44 Do not reuse "ret" variable. Instead add a new sco
Pete Williamson 2012/08/15 22:02:29 Fixed by using 4 separate variables (I think it is
+ ASSERT_NE(channelIdOut2, "My-channel-id");
+ ASSERT_FALSE(ret);
+
+ // JSON dictionary with no "id" value
+ ret = fetcher.ParseResponse(dictionaryMissingIdData, &channelIdOut3);
+ ASSERT_NE(channelIdOut3, "My-channel-id");
+ ASSERT_FALSE(ret);
+
+ // JSON, but not a dictionary
+ ret = fetcher.ParseResponse(nonDictionaryJsonData, &channelIdOut4);
+ ASSERT_NE(channelIdOut4, "My-channel-id");
+ ASSERT_FALSE(ret);
+}
+
+TEST( ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) {
+ TestDelegate delegate;
+ std::string refreshToken;
+ std::vector<std::string> scopes;
+ ObfuscatedGaiaIdFetcher fetcher(NULL, &delegate, refreshToken, scopes);
+ const net::StubURLFetcher goodFetcher(goodData, true);
+
+ // happy path
+ fetcher.ProcessApiCallSuccess(&goodFetcher);
+ ASSERT_TRUE(delegate.Succeeded());
+}
+
+TEST( ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) {
+ TestDelegate delegate;
+ std::string refreshToken;
+ std::vector<std::string> scopes;
+ ObfuscatedGaiaIdFetcher fetcher(NULL, &delegate, refreshToken, scopes);
+ const net::StubURLFetcher badFetcher(dictionaryMissingIdData, true);
+
+ // test with bad data, ensure it fails
+ fetcher.ProcessApiCallSuccess(&badFetcher);
+ ASSERT_TRUE(delegate.Failed());
+
+ // TODO: add case for when the base class fails and calls
+ // ProcessApiCallFailure
+}
+
+} // namespace extensions
Munjal (Google) 2012/08/15 20:15:44 Nit: style issue - two spaces after }

Powered by Google App Engine
This is Rietveld 408576698