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) { | |
| 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(channel_id_out1, "My-channel-id"); | |
|
dcheng
2012/08/20 22:52:22
By convention, the "expected" arg should come firs
Pete Williamson
2012/08/20 23:19:28
Done.
| |
| 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_NE(channel_id_out4, "My-channel-id"); | |
| 74 EXPECT_FALSE(ret4); | |
| 75 } | |
| 76 | |
| 77 TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) { | |
| 78 scoped_ptr<TestDelegate> delegate(new TestDelegate()); | |
| 79 scoped_ptr<ObfuscatedGaiaIdFetcher> fetcher( | |
| 80 new ObfuscatedGaiaIdFetcher(NULL, delegate.get(), std::string())); | |
|
dcheng
2012/08/20 22:52:22
Minor nit. You can just stack allocate these.
Test
Pete Williamson
2012/08/20 23:19:28
Done.
| |
| 81 | |
| 82 net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL); | |
| 83 url_fetcher.set_status( | |
| 84 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | |
| 85 url_fetcher.SetResponseString(kGoodData); | |
| 86 | |
| 87 // Test the happy path. | |
| 88 fetcher->ProcessApiCallSuccess(&url_fetcher); | |
| 89 EXPECT_TRUE(delegate->succeeded()); | |
| 90 } | |
| 91 | |
| 92 TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) { | |
| 93 scoped_ptr<TestDelegate> delegate(new TestDelegate()); | |
| 94 scoped_ptr<ObfuscatedGaiaIdFetcher> fetcher( | |
| 95 new ObfuscatedGaiaIdFetcher(NULL, delegate.get(), std::string())); | |
| 96 | |
| 97 net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL); | |
| 98 url_fetcher.set_status( | |
| 99 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | |
| 100 url_fetcher.SetResponseString(kDictionaryMissingIdData); | |
| 101 | |
| 102 // Test with bad data, ensure it fails. | |
| 103 fetcher->ProcessApiCallSuccess(&url_fetcher); | |
| 104 EXPECT_TRUE(delegate->failed()); | |
| 105 | |
| 106 // TODO(petewil): add case for when the base class fails and calls | |
| 107 // ProcessApiCallFailure | |
| 108 } | |
| 109 | |
| 110 } // namespace extensions | |
| OLD | NEW |