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