OLD | NEW |
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 #include <gtest/gtest.h> | 23 #include <gtest/gtest.h> |
24 | 24 |
25 #include "fake_downloader.h" | 25 #include "fake_downloader.h" |
26 #include "fake_storage.h" | 26 #include "fake_storage.h" |
27 #include "region_data_constants.h" | 27 #include "region_data_constants.h" |
28 | 28 |
29 namespace i18n { | 29 namespace i18n { |
30 namespace addressinput { | 30 namespace addressinput { |
31 | 31 |
32 namespace { | |
33 | |
34 const char kKey[] = "data/CA/AB--fr"; | 32 const char kKey[] = "data/CA/AB--fr"; |
35 | 33 |
36 // Empty data that the downloader can return. | 34 // Empty data that the downloader can return. |
37 const char kEmptyData[] = "{}"; | 35 const char kEmptyData[] = "{}"; |
38 | 36 |
39 // Tests for Retriever object. | 37 // Tests for Retriever object. |
40 class RetrieverTest : public testing::Test { | 38 class RetrieverTest : public testing::Test { |
41 protected: | 39 protected: |
42 RetrieverTest() | 40 RetrieverTest() |
43 : retriever_(FakeDownloader::kFakeDataUrl, | 41 : success_(false), |
44 scoped_ptr<Downloader>(new FakeDownloader), | |
45 scoped_ptr<Storage>(new FakeStorage)), | |
46 success_(false), | |
47 key_(), | 42 key_(), |
48 data_() {} | 43 data_() { |
| 44 ResetRetriever(FakeDownloader::kFakeDataUrl); |
| 45 } |
49 | 46 |
50 virtual ~RetrieverTest() {} | 47 virtual ~RetrieverTest() {} |
51 | 48 |
52 scoped_ptr<Retriever::Callback> BuildCallback() { | 49 scoped_ptr<Retriever::Callback> BuildCallback() { |
53 return ::i18n::addressinput::BuildCallback( | 50 return ::i18n::addressinput::BuildCallback( |
54 this, &RetrieverTest::OnDataReady); | 51 this, &RetrieverTest::OnDataReady); |
55 } | 52 } |
56 | 53 |
57 Retriever retriever_; | 54 void ResetRetriever(const std::string& url) { |
| 55 retriever_.reset( |
| 56 new Retriever(url, |
| 57 scoped_ptr<Downloader>(new FakeDownloader), |
| 58 scoped_ptr<Storage>(new FakeStorage))); |
| 59 } |
| 60 |
| 61 std::string GetUrlForKey(const std::string& key) { |
| 62 return retriever_->GetUrlForKey(key); |
| 63 } |
| 64 |
| 65 std::string GetKeyForUrl(const std::string& url) { |
| 66 return retriever_->GetKeyForUrl(url); |
| 67 } |
| 68 |
| 69 scoped_ptr<Retriever> retriever_; |
58 bool success_; | 70 bool success_; |
59 std::string key_; | 71 std::string key_; |
60 std::string data_; | 72 std::string data_; |
61 | 73 |
62 private: | 74 private: |
63 void OnDataReady(bool success, | 75 void OnDataReady(bool success, |
64 const std::string& key, | 76 const std::string& key, |
65 const std::string& data) { | 77 const std::string& data) { |
66 success_ = success; | 78 success_ = success; |
67 key_ = key; | 79 key_ = key; |
68 data_ = data; | 80 data_ = data; |
69 } | 81 } |
70 }; | 82 }; |
71 | 83 |
72 TEST_F(RetrieverTest, RetrieveData) { | 84 TEST_F(RetrieverTest, RetrieveData) { |
73 retriever_.Retrieve(kKey, BuildCallback()); | 85 retriever_->Retrieve(kKey, BuildCallback()); |
74 | 86 |
75 EXPECT_TRUE(success_); | 87 EXPECT_TRUE(success_); |
76 EXPECT_EQ(kKey, key_); | 88 EXPECT_EQ(kKey, key_); |
77 EXPECT_FALSE(data_.empty()); | 89 EXPECT_FALSE(data_.empty()); |
78 EXPECT_NE(kEmptyData, data_); | 90 EXPECT_NE(kEmptyData, data_); |
79 } | 91 } |
80 | 92 |
81 TEST_F(RetrieverTest, ReadDataFromStorage) { | 93 TEST_F(RetrieverTest, ReadDataFromStorage) { |
82 retriever_.Retrieve(kKey, BuildCallback()); | 94 retriever_->Retrieve(kKey, BuildCallback()); |
83 retriever_.Retrieve(kKey, BuildCallback()); | 95 retriever_->Retrieve(kKey, BuildCallback()); |
84 | 96 |
85 EXPECT_TRUE(success_); | 97 EXPECT_TRUE(success_); |
86 EXPECT_EQ(kKey, key_); | 98 EXPECT_EQ(kKey, key_); |
87 EXPECT_FALSE(data_.empty()); | 99 EXPECT_FALSE(data_.empty()); |
88 EXPECT_NE(kEmptyData, data_); | 100 EXPECT_NE(kEmptyData, data_); |
89 } | 101 } |
90 | 102 |
91 TEST_F(RetrieverTest, MissingKeyReturnsEmptyData) { | 103 TEST_F(RetrieverTest, MissingKeyReturnsEmptyData) { |
92 static const char kMissingKey[] = "junk"; | 104 static const char kMissingKey[] = "junk"; |
93 | 105 |
94 retriever_.Retrieve(kMissingKey, BuildCallback()); | 106 retriever_->Retrieve(kMissingKey, BuildCallback()); |
95 | 107 |
96 EXPECT_TRUE(success_); | 108 EXPECT_TRUE(success_); |
97 EXPECT_EQ(kMissingKey, key_); | 109 EXPECT_EQ(kMissingKey, key_); |
98 EXPECT_EQ(kEmptyData, data_); | 110 EXPECT_EQ(kEmptyData, data_); |
99 } | 111 } |
100 | 112 |
101 // The downloader that always fails. | 113 // The downloader that always fails. |
102 class FaultyDownloader : public Downloader { | 114 class FaultyDownloader : public Downloader { |
103 public: | 115 public: |
104 FaultyDownloader() {} | 116 FaultyDownloader() {} |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 scoped_ptr<Downloader>(new HangingDownloader), | 150 scoped_ptr<Downloader>(new HangingDownloader), |
139 scoped_ptr<Storage>(new FakeStorage)); | 151 scoped_ptr<Storage>(new FakeStorage)); |
140 | 152 |
141 slow_retriever.Retrieve(kKey, BuildCallback()); | 153 slow_retriever.Retrieve(kKey, BuildCallback()); |
142 EXPECT_FALSE(success_); | 154 EXPECT_FALSE(success_); |
143 EXPECT_TRUE(key_.empty()); | 155 EXPECT_TRUE(key_.empty()); |
144 | 156 |
145 EXPECT_NO_FATAL_FAILURE(slow_retriever.Retrieve(kKey, BuildCallback())); | 157 EXPECT_NO_FATAL_FAILURE(slow_retriever.Retrieve(kKey, BuildCallback())); |
146 } | 158 } |
147 | 159 |
148 } // namespace | 160 TEST_F(RetrieverTest, GetUrlForKey) { |
| 161 ResetRetriever("test:///"); |
| 162 EXPECT_EQ("test:///", GetUrlForKey("")); |
| 163 EXPECT_EQ("test:///data", GetUrlForKey("data")); |
| 164 EXPECT_EQ("test:///data/US", GetUrlForKey("data/US")); |
| 165 EXPECT_EQ("test:///data/CA--fr", GetUrlForKey("data/CA--fr")); |
| 166 } |
| 167 |
| 168 TEST_F(RetrieverTest, GetKeyForUrl) { |
| 169 ResetRetriever("test:///"); |
| 170 EXPECT_EQ("", GetKeyForUrl("test://")); |
| 171 EXPECT_EQ("", GetKeyForUrl("http://www.google.com/")); |
| 172 EXPECT_EQ("", GetKeyForUrl("")); |
| 173 EXPECT_EQ("", GetKeyForUrl("test:///")); |
| 174 EXPECT_EQ("data", GetKeyForUrl("test:///data")); |
| 175 EXPECT_EQ("data/US", GetKeyForUrl("test:///data/US")); |
| 176 EXPECT_EQ("data/CA--fr", GetKeyForUrl("test:///data/CA--fr")); |
| 177 } |
149 | 178 |
150 } // namespace addressinput | 179 } // namespace addressinput |
151 } // namespace i18n | 180 } // namespace i18n |
OLD | NEW |