Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "testing/gtest/include/gtest/gtest.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetche r.h" | |
| 10 #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
| |
| 11 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 static const char kGoodData[] = "{ \"id\" : \"My-channel-id\" }"; | |
| 16 static const char kBadJsonData[] = "I am not a JSON string"; | |
| 17 static const char kDictionaryMissingIdData[] = "{ \"foo\" : \"bar\" }"; | |
| 18 static const char kNonDictionaryJsonData[] = "{ 0.5 }"; | |
| 19 | |
| 20 // Delegate class for catching notifications from the ObfuscatedGaiaIdFetcher. | |
| 21 class TestDelegate : public extensions::PushMessagingGetChannelIdFunction { | |
| 22 public: | |
| 23 // inherited methods | |
| 24 virtual void OnObfuscatedGaiaIdFetchSuccess( | |
| 25 const std::string& access_token) { | |
| 26 succeeded_ = true; | |
| 27 } | |
| 28 virtual void OnObfuscatedGaiaIdFetchFailure( | |
| 29 const GoogleServiceAuthError& error) { | |
| 30 failed_ = true; | |
| 31 } | |
| 32 TestDelegate() { | |
| 33 succeeded_ = false; | |
| 34 failed_ = false; | |
| 35 } | |
| 36 | |
| 37 bool Succeeded() { return succeeded_; } | |
| 38 bool Failed() { return failed_; } | |
| 39 | |
| 40 private: | |
| 41 virtual ~TestDelegate() {} | |
|
Munjal (Google)
2012/08/15 23:04:19
Indentation.
Pete Williamson
2012/08/19 22:05:14
Done.
| |
| 42 bool succeeded_; | |
| 43 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,
| |
| 44 }; | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 namespace extensions { | |
| 49 | |
| 50 TEST(ObfuscatedGaiaIdFetcherTest, ParseResponse) { | |
| 51 std::string channel_id_out1; | |
| 52 std::string channel_id_out2; | |
| 53 std::string channel_id_out3; | |
| 54 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
| |
| 55 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.
| |
| 56 std::string refresh_token; | |
| 57 ObfuscatedGaiaIdFetcher fetcher(NULL, delegate, refresh_token); | |
| 58 | |
| 59 // Try a good response string. | |
| 60 bool ret1 = fetcher.ParseResponse(kGoodData, &channel_id_out1); | |
| 61 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
| |
| 62 ASSERT_TRUE(ret1); | |
| 63 | |
| 64 // Try badly formattedf JSON. | |
| 65 bool ret2 = fetcher.ParseResponse(kBadJsonData, &channel_id_out2); | |
| 66 ASSERT_NE(channel_id_out2, "My-channel-id"); | |
| 67 ASSERT_FALSE(ret2); | |
| 68 | |
| 69 // Try a JSON dictionary with no "id" value. | |
| 70 bool ret3 = fetcher.ParseResponse(kDictionaryMissingIdData, &channel_id_out3); | |
| 71 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
| |
| 72 ASSERT_FALSE(ret3); | |
| 73 | |
| 74 // Try JSON, but not a dictionary. | |
| 75 bool ret4 = fetcher.ParseResponse(kNonDictionaryJsonData, &channel_id_out4); | |
| 76 ASSERT_NE(channel_id_out4, "My-channel-id"); | |
| 77 ASSERT_FALSE(ret4); | |
| 78 } | |
| 79 | |
| 80 TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) { | |
| 81 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
| |
| 82 std::string refresh_token; | |
| 83 ObfuscatedGaiaIdFetcher* fetcher = | |
| 84 new ObfuscatedGaiaIdFetcher(NULL, delegate, refresh_token); | |
| 85 const net::StubURLFetcher goodFetcher(kGoodData, true); | |
| 86 | |
| 87 // Test the happy path. | |
| 88 | |
| 89 // Keep the delegate alive, since the callback would make its refcount go to 0 | |
| 90 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
| |
| 91 fetcher->ProcessApiCallSuccess(&goodFetcher); | |
| 92 ASSERT_TRUE(delegate->Succeeded()); | |
| 93 delegate->Release(); | |
| 94 } | |
| 95 | |
| 96 TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) { | |
| 97 TestDelegate* delegate = new TestDelegate(); | |
| 98 std::string refresh_token; | |
| 99 ObfuscatedGaiaIdFetcher* fetcher = | |
| 100 new ObfuscatedGaiaIdFetcher(NULL, delegate, refresh_token); | |
| 101 const net::StubURLFetcher badFetcher(kDictionaryMissingIdData, true); | |
| 102 | |
| 103 // Test with bad data, ensure it fails. | |
| 104 // Keep the delegate alive, since the callback would make its refcount go to 0 | |
| 105 delegate->AddRef(); | |
| 106 fetcher->ProcessApiCallSuccess(&badFetcher); | |
| 107 ASSERT_TRUE(delegate->Failed()); | |
| 108 delegate->Release(); | |
| 109 | |
| 110 // TODO: add case for when the base class fails and calls | |
| 111 // ProcessApiCallFailure | |
| 112 } | |
| 113 | |
| 114 } // namespace extensions | |
| OLD | NEW |