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" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 const char goodData[] = "{ \"id\" : \"My-channel-id\" }"; | |
| 15 const char badJsonData[] = "I am not a JSON string"; | |
| 16 const char dictionaryMissingIdData[] = "{ \"foo\" : \"bar\" }"; | |
| 17 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.
| |
| 18 | |
| 19 // delegate class for catching notifications from the ObfuscatedGaiaIdFetcher | |
| 20 class TestDelegate : public extensions::ObfuscatedGaiaIdFetcher::Delegate { | |
| 21 public: | |
| 22 // inherited methods | |
| 23 virtual void OnObfuscatedGaiaIdFetchSuccess( | |
| 24 const std::string& access_token) { | |
| 25 succeeded_ = true; | |
| 26 } | |
| 27 virtual void OnObfuscatedGaiaIdFetchFailure( | |
| 28 const GoogleServiceAuthError& error) { | |
| 29 failed_ = true; | |
| 30 } | |
| 31 TestDelegate() { | |
| 32 Clear(); | |
| 33 } | |
| 34 virtual ~TestDelegate() {} | |
| 35 void Clear() { | |
| 36 succeeded_ = false; | |
| 37 failed_ = false; | |
| 38 } | |
|
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
| |
| 39 bool Succeeded() { return succeeded_; } | |
| 40 bool Failed() { return failed_; } | |
| 41 | |
| 42 private: | |
| 43 bool succeeded_; | |
| 44 bool failed_; | |
| 45 }; | |
| 46 | |
| 47 } // namespace anonymous | |
|
Munjal (Google)
2012/08/15 20:15:44
Nit: remove "anonymous"
Pete Williamson
2012/08/15 22:02:29
Done.
| |
| 48 | |
| 49 namespace extensions { | |
| 50 | |
| 51 TEST( ObfuscatedGaiaIdFetcherTest, ParseResponse) { | |
|
Munjal (Google)
2012/08/15 20:15:44
Nit: remove space after (
Pete Williamson
2012/08/15 22:02:29
Done.
| |
| 52 bool ret = false; | |
| 53 std::string channelIdOut1; | |
| 54 std::string channelIdOut2; | |
| 55 std::string channelIdOut3; | |
| 56 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.
| |
| 57 TestDelegate delegate; | |
| 58 std::string refreshToken; | |
| 59 std::vector<std::string> scopes; | |
| 60 ObfuscatedGaiaIdFetcher fetcher(NULL, &delegate, refreshToken, scopes); | |
| 61 | |
| 62 // good response string | |
| 63 ret = fetcher.ParseResponse(goodData, &channelIdOut1); | |
| 64 ASSERT_EQ(channelIdOut1, "My-channel-id"); | |
| 65 ASSERT_TRUE(ret); | |
| 66 | |
| 67 // badly formattedf JSON | |
| 68 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
| |
| 69 ASSERT_NE(channelIdOut2, "My-channel-id"); | |
| 70 ASSERT_FALSE(ret); | |
| 71 | |
| 72 // JSON dictionary with no "id" value | |
| 73 ret = fetcher.ParseResponse(dictionaryMissingIdData, &channelIdOut3); | |
| 74 ASSERT_NE(channelIdOut3, "My-channel-id"); | |
| 75 ASSERT_FALSE(ret); | |
| 76 | |
| 77 // JSON, but not a dictionary | |
| 78 ret = fetcher.ParseResponse(nonDictionaryJsonData, &channelIdOut4); | |
| 79 ASSERT_NE(channelIdOut4, "My-channel-id"); | |
| 80 ASSERT_FALSE(ret); | |
| 81 } | |
| 82 | |
| 83 TEST( ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) { | |
| 84 TestDelegate delegate; | |
| 85 std::string refreshToken; | |
| 86 std::vector<std::string> scopes; | |
| 87 ObfuscatedGaiaIdFetcher fetcher(NULL, &delegate, refreshToken, scopes); | |
| 88 const net::StubURLFetcher goodFetcher(goodData, true); | |
| 89 | |
| 90 // happy path | |
| 91 fetcher.ProcessApiCallSuccess(&goodFetcher); | |
| 92 ASSERT_TRUE(delegate.Succeeded()); | |
| 93 } | |
| 94 | |
| 95 TEST( ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) { | |
| 96 TestDelegate delegate; | |
| 97 std::string refreshToken; | |
| 98 std::vector<std::string> scopes; | |
| 99 ObfuscatedGaiaIdFetcher fetcher(NULL, &delegate, refreshToken, scopes); | |
| 100 const net::StubURLFetcher badFetcher(dictionaryMissingIdData, true); | |
| 101 | |
| 102 // test with bad data, ensure it fails | |
| 103 fetcher.ProcessApiCallSuccess(&badFetcher); | |
| 104 ASSERT_TRUE(delegate.Failed()); | |
| 105 | |
| 106 // TODO: add case for when the base class fails and calls | |
| 107 // ProcessApiCallFailure | |
| 108 } | |
| 109 | |
| 110 } // namespace extensions | |
|
Munjal (Google)
2012/08/15 20:15:44
Nit: style issue - two spaces after }
| |
| OLD | NEW |