Chromium Code Reviews| 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 }
|