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/push_messaging_api.h" | |
| 11 #include "net/url_request/test_url_fetcher_factory.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::ObfuscatedGaiaIdFetcher::Delegate { | |
| 22 public: | |
| 23 // inherited methods | |
| 24 virtual void OnObfuscatedGaiaIdFetchSuccess( | |
| 25 const std::string& obfuscated_id) { | |
|
Mihai Parparita -not on Chrome
2012/08/22 21:40:45
These should have the OVERRIDE annotation (making
Pete Williamson
2012/08/23 17:20:50
Done.
| |
| 26 succeeded_ = true; | |
| 27 } | |
| 28 virtual void OnObfuscatedGaiaIdFetchFailure( | |
| 29 const GoogleServiceAuthError& error) { | |
| 30 failed_ = true; | |
| 31 } | |
| 32 TestDelegate() : succeeded_(false), failed_(false) {} | |
| 33 virtual ~TestDelegate() {} | |
| 34 bool succeeded() const { return succeeded_; } | |
| 35 bool failed() const { return failed_; } | |
| 36 | |
| 37 private: | |
| 38 bool succeeded_; | |
| 39 bool failed_; | |
| 40 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
| 41 }; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 namespace extensions { | |
| 46 | |
| 47 TEST(ObfuscatedGaiaIdFetcherTest, ParseResponse) { | |
| 48 // Try a good response string. | |
| 49 std::string channel_id_out1; | |
| 50 bool ret1 = ObfuscatedGaiaIdFetcher::ParseResponse( | |
| 51 kGoodData, &channel_id_out1); | |
| 52 EXPECT_EQ("My-channel-id", channel_id_out1); | |
| 53 EXPECT_TRUE(ret1); | |
| 54 | |
| 55 // Try badly formatted JSON. | |
| 56 std::string channel_id_out2; | |
| 57 bool ret2 = ObfuscatedGaiaIdFetcher::ParseResponse( | |
| 58 kBadJsonData, &channel_id_out2); | |
| 59 EXPECT_TRUE(channel_id_out2.empty()); | |
| 60 EXPECT_FALSE(ret2); | |
| 61 | |
| 62 // Try a JSON dictionary with no "id" value. | |
| 63 std::string channel_id_out3; | |
| 64 bool ret3 = ObfuscatedGaiaIdFetcher::ParseResponse( | |
| 65 kDictionaryMissingIdData, &channel_id_out3); | |
| 66 EXPECT_TRUE(channel_id_out3.empty()); | |
| 67 EXPECT_FALSE(ret3); | |
| 68 | |
| 69 // Try JSON, but not a dictionary. | |
| 70 std::string channel_id_out4; | |
| 71 bool ret4 = ObfuscatedGaiaIdFetcher::ParseResponse( | |
| 72 kNonDictionaryJsonData, &channel_id_out4); | |
| 73 EXPECT_TRUE(channel_id_out4.empty()); | |
| 74 EXPECT_FALSE(ret4); | |
| 75 } | |
| 76 | |
| 77 TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) { | |
| 78 TestDelegate delegate; | |
| 79 ObfuscatedGaiaIdFetcher fetcher(NULL, &delegate, std::string()); | |
| 80 | |
| 81 net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL); | |
| 82 url_fetcher.set_status( | |
| 83 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | |
| 84 url_fetcher.SetResponseString(kGoodData); | |
| 85 | |
| 86 // Test the happy path. | |
| 87 fetcher.ProcessApiCallSuccess(&url_fetcher); | |
| 88 EXPECT_TRUE(delegate.succeeded()); | |
| 89 } | |
| 90 | |
| 91 TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) { | |
| 92 TestDelegate delegate; | |
| 93 ObfuscatedGaiaIdFetcher fetcher(NULL, &delegate, std::string()); | |
| 94 | |
| 95 net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL); | |
| 96 url_fetcher.set_status( | |
| 97 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | |
| 98 url_fetcher.SetResponseString(kDictionaryMissingIdData); | |
| 99 | |
| 100 // Test with bad data, ensure it fails. | |
| 101 fetcher.ProcessApiCallSuccess(&url_fetcher); | |
| 102 EXPECT_TRUE(delegate.failed()); | |
| 103 | |
| 104 // TODO(petewil): add case for when the base class fails and calls | |
| 105 // ProcessApiCallFailure | |
| 106 } | |
| 107 | |
| 108 } // namespace extensions | |
| OLD | NEW |