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..d7d44e7a4b252ceeec133ac7f8407de8b1fcc1ef |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher_unittest.cc |
| @@ -0,0 +1,114 @@ |
| +// 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" |
|
Munjal (Google)
2012/08/15 23:04:19
I think I commented about this before. There are a
Pete Williamson
2012/08/19 22:05:14
I replied to your comment before with some reasons
|
| +#include "chrome/browser/extensions/api/push_messaging/push_messaging_api.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::PushMessagingGetChannelIdFunction { |
| + public: |
| + // inherited methods |
| + virtual void OnObfuscatedGaiaIdFetchSuccess( |
| + const std::string& access_token) { |
| + succeeded_ = true; |
| + } |
| + virtual void OnObfuscatedGaiaIdFetchFailure( |
| + const GoogleServiceAuthError& error) { |
| + failed_ = true; |
| + } |
| + TestDelegate() { |
| + succeeded_ = false; |
| + failed_ = false; |
| + } |
| + |
| + bool Succeeded() { return succeeded_; } |
| + bool Failed() { return failed_; } |
| + |
| + private: |
| +virtual ~TestDelegate() {} |
|
Munjal (Google)
2012/08/15 23:04:19
Indentation.
Pete Williamson
2012/08/19 22:05:14
Done.
|
| + bool succeeded_; |
| + bool failed_; |
|
dcheng
2012/08/16 19:12:43
DISALLOW_COPY_AND_ASSIGN.
Pete Williamson
2012/08/19 22:05:14
Done, but why? It seems like this class is not in
dcheng
2012/08/19 22:38:35
Unless a class needs to be copyable or assignable,
|
| +}; |
| + |
| +} // namespace |
| + |
| +namespace extensions { |
| + |
| +TEST(ObfuscatedGaiaIdFetcherTest, ParseResponse) { |
| + std::string channel_id_out1; |
| + std::string channel_id_out2; |
| + std::string channel_id_out3; |
| + std::string channel_id_out4; |
|
Munjal (Google)
2012/08/15 23:04:19
I generalyl prefer separate scopes since even if y
dcheng
2012/08/16 19:12:43
I don't care about scoped vs non-scoped. However,
Pete Williamson
2012/08/19 22:05:14
I generally prefer not to use scopes unless the co
Pete Williamson
2012/08/19 22:05:14
Done, but why? These unit tests are really fast a
dcheng
2012/08/19 22:38:35
This isn't for optimization reasons, just readabil
|
| + TestDelegate* delegate = new TestDelegate(); |
|
dcheng
2012/08/16 19:12:43
Do we leak this? In general, all "new" allocations
Pete Williamson
2012/08/19 22:05:14
Nope, it deletes itself with the current design.
|
| + std::string refresh_token; |
| + ObfuscatedGaiaIdFetcher fetcher(NULL, delegate, refresh_token); |
| + |
| + // Try a good response string. |
| + bool ret1 = fetcher.ParseResponse(kGoodData, &channel_id_out1); |
| + ASSERT_EQ(channel_id_out1, "My-channel-id"); |
|
dcheng
2012/08/16 19:12:43
Prefer EXPECT instead of ASSERT. ASSERT() should b
Pete Williamson
2012/08/19 22:05:14
Done, changed all ASSERTs in this file to EXPECT.
dcheng
2012/08/19 22:38:35
This is just the general convention with googletes
|
| + ASSERT_TRUE(ret1); |
| + |
| + // Try badly formattedf JSON. |
| + bool ret2 = fetcher.ParseResponse(kBadJsonData, &channel_id_out2); |
| + ASSERT_NE(channel_id_out2, "My-channel-id"); |
| + ASSERT_FALSE(ret2); |
| + |
| + // Try a JSON dictionary with no "id" value. |
| + bool ret3 = fetcher.ParseResponse(kDictionaryMissingIdData, &channel_id_out3); |
| + ASSERT_NE(channel_id_out3, "My-channel-id"); |
|
dcheng
2012/08/16 19:12:43
If channel_id_out3 is supposed to be empty, then w
|
| + ASSERT_FALSE(ret3); |
| + |
| + // Try JSON, but not a dictionary. |
| + bool ret4 = fetcher.ParseResponse(kNonDictionaryJsonData, &channel_id_out4); |
| + ASSERT_NE(channel_id_out4, "My-channel-id"); |
| + ASSERT_FALSE(ret4); |
| +} |
| + |
| +TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) { |
| + TestDelegate* delegate = new TestDelegate(); |
|
Munjal (Google)
2012/08/15 23:04:19
Let us not use bare pointers. Use scoped_ptr or sc
Pete Williamson
2012/08/19 22:05:14
As noted earlier, this is an artifact of using a s
|
| + std::string refresh_token; |
| + ObfuscatedGaiaIdFetcher* fetcher = |
| + new ObfuscatedGaiaIdFetcher(NULL, delegate, refresh_token); |
| + const net::StubURLFetcher goodFetcher(kGoodData, true); |
| + |
| + // Test the happy path. |
| + |
| + // Keep the delegate alive, since the callback would make its refcount go to 0 |
| + delegate->AddRef(); |
|
Munjal (Google)
2012/08/15 23:04:19
Again, don't reuse delegates, or any such objects,
Pete Williamson
2012/08/19 22:05:14
We aren't re-using the delegate here. This is to
|
| + fetcher->ProcessApiCallSuccess(&goodFetcher); |
| + ASSERT_TRUE(delegate->Succeeded()); |
| + delegate->Release(); |
| +} |
| + |
| +TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) { |
| + TestDelegate* delegate = new TestDelegate(); |
| + std::string refresh_token; |
| + ObfuscatedGaiaIdFetcher* fetcher = |
| + new ObfuscatedGaiaIdFetcher(NULL, delegate, refresh_token); |
| + const net::StubURLFetcher badFetcher(kDictionaryMissingIdData, true); |
| + |
| + // Test with bad data, ensure it fails. |
| + // Keep the delegate alive, since the callback would make its refcount go to 0 |
| + delegate->AddRef(); |
| + fetcher->ProcessApiCallSuccess(&badFetcher); |
| + ASSERT_TRUE(delegate->Failed()); |
| + delegate->Release(); |
| + |
| + // TODO: add case for when the base class fails and calls |
| + // ProcessApiCallFailure |
| +} |
| + |
| +} // namespace extensions |