Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / chromium / chrome_metadata_source_unittest.cc
1 // Copyright 2014 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 "third_party/libaddressinput/chromium/chrome_metadata_source.h"
6
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "net/url_request/test_url_fetcher_factory.h"
9 #include "net/url_request/url_request_test_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace autofill {
13
14 static const char kFakeUrl[] = "https://example.com";
15 static const char kFakeInsecureUrl[] = "http://example.com";
16
17 class ChromeMetadataSourceTest : public testing::Test {
18  public:
19   ChromeMetadataSourceTest()
20       : fake_factory_(&factory_),
21         success_(false) {}
22   virtual ~ChromeMetadataSourceTest() {}
23
24  protected:
25   // Sets the response for the download.
26   void SetFakeResponse(const std::string& payload, net::HttpStatusCode code) {
27     fake_factory_.SetFakeResponse(url_,
28                                   payload,
29                                   code,
30                                   net::URLRequestStatus::SUCCESS);
31   }
32
33   // Kicks off the download.
34   void Get() {
35     scoped_refptr<net::TestURLRequestContextGetter> getter(
36         new net::TestURLRequestContextGetter(
37             base::MessageLoopProxy::current()));
38     ChromeMetadataSource impl(std::string(), getter.get());
39     scoped_ptr< ::i18n::addressinput::Source::Callback> callback(
40         ::i18n::addressinput::BuildCallback(
41              this, &ChromeMetadataSourceTest::OnDownloaded));
42     impl.Get(url_.spec(), *callback);
43     base::MessageLoop::current()->RunUntilIdle();
44   }
45
46   void set_url(const GURL& url) { url_ = url; }
47   bool success() const { return success_; }
48   bool has_data() const { return !!data_; }
49
50   const std::string& data() const {
51     DCHECK(data_);
52     return *data_;
53   }
54
55  private:
56   // Callback for when download is finished.
57   void OnDownloaded(bool success,
58                     const std::string& url,
59                     std::string* data) {
60     ASSERT_FALSE(success && data == NULL);
61     success_ = success;
62     data_.reset(data);
63   }
64
65   base::MessageLoop loop_;
66   net::URLFetcherImplFactory factory_;
67   net::FakeURLFetcherFactory fake_factory_;
68   GURL url_;
69   scoped_ptr<std::string> data_;
70   bool success_;
71 };
72
73 TEST_F(ChromeMetadataSourceTest, Success) {
74   const char kFakePayload[] = "ham hock";
75   set_url(GURL(kFakeUrl));
76   SetFakeResponse(kFakePayload, net::HTTP_OK);
77   Get();
78   EXPECT_TRUE(success());
79   EXPECT_EQ(kFakePayload, data());
80 }
81
82 TEST_F(ChromeMetadataSourceTest, Failure) {
83   const char kFakePayload[] = "ham hock";
84   set_url(GURL(kFakeUrl));
85   SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR);
86   Get();
87   EXPECT_FALSE(success());
88   EXPECT_TRUE(!has_data() || data().empty());
89 }
90
91 TEST_F(ChromeMetadataSourceTest, RejectsInsecureScheme) {
92   const char kFakePayload[] = "ham hock";
93   set_url(GURL(kFakeInsecureUrl));
94   SetFakeResponse(kFakePayload, net::HTTP_OK);
95   Get();
96   EXPECT_FALSE(success());
97   EXPECT_TRUE(!has_data() || data().empty());
98 }
99
100 }  // namespace autofill