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 "obfuscated_gaia_id_fetcher.h" | |
| 10 #include "stub_url_fetcher.h" | |
|
Munjal (Google)
2012/08/09 21:44:29
Include paths should be relative to src/
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 11 | |
| 12 namespace extensions { | |
| 13 | |
| 14 const std::string fakeGoodData("{ \"id\" : \"My-channel-id\" }"); | |
| 15 const std::string fakeBadData1("I am not a JSON string"); | |
| 16 const std::string fakeBadData2("{ \"foo\" : \"bar\" }"); | |
| 17 const std::string fakeBadData3("{ 0.5 }"); | |
|
Munjal (Google)
2012/08/09 21:44:29
Put all of these as static const char[] in anonymo
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 18 std::string channelIdOut; | |
| 19 net::URLFetcher* fakeSource; | |
|
Munjal (Google)
2012/08/09 21:44:29
Do not use non-const global variables.
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 20 | |
| 21 // delegate class for catching notifications from teh ObfuscatedGaiaIdFetcher | |
|
Munjal (Google)
2012/08/09 21:44:29
Nit teh -> the
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 22 class FetcherDelegate : public ObfuscatedGaiaIdFetcher::Delegate { | |
|
Munjal (Google)
2012/08/09 21:44:29
Name this TestDelegate and put it in an anonymous
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 23 public: | |
| 24 // inherited methods | |
| 25 virtual void OnObfuscatedGaiaIdFetchSuccess( | |
| 26 const std::string& access_token) { | |
| 27 succeeded_ = true; | |
| 28 } | |
| 29 virtual void OnObfuscatedGaiaIdFetchFailure( | |
| 30 const GoogleServiceAuthError& error) { | |
| 31 failed_ = true; | |
| 32 } | |
| 33 FetcherDelegate() { | |
| 34 Clear(); | |
| 35 } | |
| 36 virtual ~FetcherDelegate() {} | |
| 37 void Clear() { | |
| 38 succeeded_ = false; | |
| 39 failed_ = false; | |
| 40 } | |
| 41 bool Succeeded() { return succeeded_; } | |
| 42 bool Failed() { return failed_; } | |
| 43 | |
| 44 private: | |
| 45 bool succeeded_; | |
| 46 bool failed_; | |
| 47 }; | |
| 48 | |
| 49 class ObfuscatedGaiaIdFetcherTest : public testing::Test { | |
| 50 public: | |
| 51 virtual void SetUp() { | |
| 52 // Create a fetcher that we can use for the tests, and initialize it well | |
| 53 // enough for the tests to pass. | |
| 54 ObfuscatedGaiaIdFetcher::Parameters params; | |
| 55 fetcher_ = new ObfuscatedGaiaIdFetcher(NULL, &delegate_, params); | |
| 56 | |
| 57 // set up mocks | |
| 58 goodFetcher_ = new net::StubURLFetcher(fakeGoodData, true); | |
| 59 badFetcher1_ = new net::StubURLFetcher(fakeBadData1, false); | |
| 60 badFetcher2_ = new net::StubURLFetcher(fakeBadData2, true); | |
| 61 badFetcher3_ = new net::StubURLFetcher(fakeBadData3, true); | |
|
Munjal (Google)
2012/08/09 21:44:29
It is better for each individual test to create th
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 62 } | |
| 63 | |
| 64 ObfuscatedGaiaIdFetcher* GetFetcher() { return fetcher_; } | |
| 65 | |
| 66 protected: | |
| 67 // TODO: use a scoped pointer to release these at dtor time | |
|
Munjal (Google)
2012/08/09 21:44:29
You missed doing this TODO. I think it is anyway b
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 68 ObfuscatedGaiaIdFetcher* fetcher_; | |
| 69 FetcherDelegate delegate_; | |
| 70 net::StubURLFetcher* goodFetcher_; | |
| 71 net::StubURLFetcher* badFetcher1_; | |
| 72 net::StubURLFetcher* badFetcher2_; | |
| 73 net::StubURLFetcher* badFetcher3_; | |
| 74 | |
| 75 }; | |
| 76 | |
| 77 TEST_F( ObfuscatedGaiaIdFetcherTest, ParseResponse) { | |
| 78 ObfuscatedGaiaIdFetcher* fetcher = GetFetcher(); | |
| 79 bool ret = false; | |
| 80 ret = fetcher->ParseResponse(fakeGoodData, &channelIdOut); | |
| 81 ASSERT_TRUE(channelIdOut == "My-channel-id" && ret); | |
|
Munjal (Google)
2012/08/09 21:44:29
Split the compound check into multiple asserts so
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 82 channelIdOut.clear(); | |
|
Munjal (Google)
2012/08/09 21:44:29
I generally don't link reusing the variables like
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 83 ret = fetcher->ParseResponse(fakeBadData1, &channelIdOut); | |
|
Munjal (Google)
2012/08/09 21:44:29
Either replace fakeData1 with "I am not a JSON str
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 84 ASSERT_TRUE(channelIdOut != "My-channel-id" && !ret); | |
| 85 channelIdOut.clear(); | |
| 86 ret = fetcher->ParseResponse(fakeBadData2, &channelIdOut); | |
| 87 ASSERT_TRUE(channelIdOut != "My-channel-id" && !ret); | |
| 88 channelIdOut.clear(); | |
| 89 ret = fetcher->ParseResponse(fakeBadData3, &channelIdOut); | |
| 90 ASSERT_TRUE(channelIdOut != "My-channel-id" && !ret); | |
| 91 } | |
| 92 | |
| 93 TEST_F( ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess) { | |
| 94 ObfuscatedGaiaIdFetcher* fetcher = GetFetcher(); | |
| 95 | |
| 96 // happy path | |
| 97 delegate_.Clear(); | |
|
Munjal (Google)
2012/08/09 21:44:29
Create fetcher and delegate locally to eliminate t
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 98 fetcher->ProcessApiCallSuccess(goodFetcher_); | |
| 99 ASSERT_TRUE(delegate_.Succeeded()); | |
| 100 } | |
| 101 | |
| 102 TEST_F( ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure) { | |
| 103 ObfuscatedGaiaIdFetcher* fetcher = GetFetcher(); | |
| 104 // several varieties of failures should all return failure | |
| 105 delegate_.Clear(); | |
| 106 fetcher->ProcessApiCallSuccess(badFetcher1_); | |
|
Munjal (Google)
2012/08/09 21:44:29
Since you tested ParseResponse thoroughly with eac
Pete Williamson
2012/08/13 21:21:14
Done.
| |
| 107 ASSERT_TRUE(delegate_.Failed()); | |
| 108 delegate_.Clear(); | |
| 109 fetcher->ProcessApiCallSuccess(badFetcher2_); | |
| 110 ASSERT_TRUE(delegate_.Failed()); | |
| 111 delegate_.Clear(); | |
| 112 fetcher->ProcessApiCallSuccess(badFetcher3_); | |
| 113 ASSERT_TRUE(delegate_.Failed()); | |
| 114 } | |
| 115 | |
| 116 } // namespace extensions | |
| OLD | NEW |