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() { | |
|
dcheng
2012/08/20 19:10:01
Prefer initializer syntax.
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 33 succeeded_ = false; | |
| 34 failed_ = false; | |
| 35 } | |
| 36 virtual ~TestDelegate() {} | |
| 37 bool Succeeded() { return succeeded_; } | |
|
dcheng
2012/08/20 19:10:01
bool succeeded() const { return succeeded_; }
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 38 bool Failed() { return failed_; } | |
|
dcheng
2012/08/20 19:10:01
Ditto.
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 39 | |
| 40 private: | |
| 41 bool succeeded_; | |
| 42 bool failed_; | |
| 43 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
| 44 }; | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 namespace extensions { | |
| 49 | |
| 50 TEST(ObfuscatedGaiaIdFetcherTest, ParseResponse) { | |
| 51 scoped_ptr<TestDelegate> delegate(new TestDelegate()); | |
| 52 std::string refresh_token; | |
| 53 ObfuscatedGaiaIdFetcher fetcher(NULL, delegate.get(), refresh_token); | |
| 54 | |
| 55 // Try a good response string. | |
| 56 std::string channel_id_out1; | |
| 57 bool ret1 = fetcher.ParseResponse(kGoodData, &channel_id_out1); | |
| 58 EXPECT_EQ(channel_id_out1, "My-channel-id"); | |
| 59 EXPECT_TRUE(ret1); | |
| 60 | |
| 61 // Try badly formatted JSON. | |
| 62 std::string channel_id_out2; | |
| 63 bool ret2 = fetcher.ParseResponse(kBadJsonData, &channel_id_out2); | |
|
dcheng
2012/08/20 19:10:01
Isn't this a static method? We can just do Obfusca
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 64 EXPECT_TRUE(channel_id_out2.empty()); | |
| 65 EXPECT_FALSE(ret2); | |
| 66 | |
| 67 // Try a JSON dictionary with no "id" value. | |
| 68 std::string channel_id_out3; | |
| 69 bool ret3 = fetcher.ParseResponse(kDictionaryMissingIdData, &channel_id_out3); | |
| 70 EXPECT_TRUE(channel_id_out3.empty()); | |
| 71 EXPECT_FALSE(ret3); | |
| 72 | |
| 73 // Try JSON, but not a dictionary. | |
| 74 std::string channel_id_out4; | |
| 75 bool ret4 = fetcher.ParseResponse(kNonDictionaryJsonData, &channel_id_out4); | |
| 76 EXPECT_NE(channel_id_out4, "My-channel-id"); | |
| 77 EXPECT_FALSE(ret4); | |
| 78 } | |
| 79 | |
| 80 TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) { | |
| 81 scoped_ptr<TestDelegate> delegate(new TestDelegate()); | |
| 82 std::string refresh_token; | |
| 83 ObfuscatedGaiaIdFetcher* fetcher = | |
|
dcheng
2012/08/20 19:10:01
scoped_ptr, as I think this now leaks.
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 84 new ObfuscatedGaiaIdFetcher(NULL, delegate.get(), refresh_token); | |
|
dcheng
2012/08/20 19:10:01
Nit: You can probably just say std::string(), as p
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 85 | |
| 86 net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL); | |
| 87 url_fetcher.set_status( | |
| 88 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | |
| 89 url_fetcher.SetResponseString(kGoodData); | |
| 90 | |
| 91 // Test the happy path. | |
| 92 fetcher->ProcessApiCallSuccess(&url_fetcher); | |
| 93 EXPECT_TRUE(delegate->Succeeded()); | |
| 94 } | |
| 95 | |
| 96 TEST(ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) { | |
| 97 scoped_ptr<TestDelegate> delegate(new TestDelegate()); | |
| 98 std::string refresh_token; | |
| 99 ObfuscatedGaiaIdFetcher* fetcher = | |
|
dcheng
2012/08/20 19:10:01
scoped_ptr.
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 100 new ObfuscatedGaiaIdFetcher(NULL, delegate.get(), refresh_token); | |
|
dcheng
2012/08/20 19:10:01
Same as above about refresh_token.
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 101 | |
| 102 net::TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL); | |
| 103 url_fetcher.set_status( | |
| 104 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | |
| 105 url_fetcher.SetResponseString(kDictionaryMissingIdData); | |
| 106 | |
| 107 // Test with bad data, ensure it fails. | |
| 108 fetcher->ProcessApiCallSuccess(&url_fetcher); | |
| 109 EXPECT_TRUE(delegate->Failed()); | |
| 110 | |
| 111 // TODO: add case for when the base class fails and calls | |
|
dcheng
2012/08/20 19:10:01
TODO(petewil)
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 112 // ProcessApiCallFailure | |
| 113 } | |
| 114 | |
| 115 } // namespace extensions | |
| OLD | NEW |