1// Copyright (C) 2014 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (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
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <libaddressinput/preload_supplier.h>
16
17#include <libaddressinput/address_data.h>
18#include <libaddressinput/callback.h>
19#include <libaddressinput/null_storage.h>
20#include <libaddressinput/util/basictypes.h>
21#include <libaddressinput/util/scoped_ptr.h>
22
23#include <cstddef>
24#include <string>
25
26#include <gtest/gtest.h>
27
28#include "lookup_key.h"
29#include "rule.h"
30#include "testdata_source.h"
31
32namespace {
33
34using i18n::addressinput::AddressData;
35using i18n::addressinput::BuildCallback;
36using i18n::addressinput::LookupKey;
37using i18n::addressinput::NullStorage;
38using i18n::addressinput::PreloadSupplier;
39using i18n::addressinput::Rule;
40using i18n::addressinput::scoped_ptr;
41using i18n::addressinput::TestdataSource;
42
43class PreloadSupplierTest : public testing::Test {
44 protected:
45  PreloadSupplierTest()
46      : supplier_(new TestdataSource(true), new NullStorage),
47        loaded_callback_(BuildCallback(this, &PreloadSupplierTest::OnLoaded)) {}
48
49  PreloadSupplier supplier_;
50  const scoped_ptr<const PreloadSupplier::Callback> loaded_callback_;
51
52 private:
53  void OnLoaded(bool success, const std::string& region_code, int num_rules) {
54    ASSERT_TRUE(success);
55    ASSERT_FALSE(region_code.empty());
56    ASSERT_LT(0, num_rules);
57    ASSERT_TRUE(supplier_.IsLoaded(region_code));
58  }
59
60  DISALLOW_COPY_AND_ASSIGN(PreloadSupplierTest);
61};
62
63TEST_F(PreloadSupplierTest, GetUsRule) {
64  supplier_.LoadRules("US", *loaded_callback_);
65  LookupKey us_key;
66  AddressData us_address;
67  us_address.region_code = "US";
68  us_key.FromAddress(us_address);
69  const Rule* rule = supplier_.GetRule(us_key);
70  ASSERT_TRUE(rule != NULL);
71  EXPECT_EQ("data/US", rule->GetId());
72}
73
74TEST_F(PreloadSupplierTest, GetUsCaRule) {
75  supplier_.LoadRules("US", *loaded_callback_);
76  LookupKey ca_key;
77  AddressData ca_address;
78  ca_address.region_code = "US";
79  ca_address.administrative_area = "CA";
80  ca_key.FromAddress(ca_address);
81  const Rule* rule = supplier_.GetRule(ca_key);
82  ASSERT_TRUE(rule != NULL);
83  EXPECT_EQ("data/US/CA", rule->GetId());
84}
85
86TEST_F(PreloadSupplierTest, GetZwRule) {
87  supplier_.LoadRules("ZW", *loaded_callback_);
88  LookupKey zw_key;
89  AddressData zw_address;
90  zw_address.region_code = "ZW";
91  zw_key.FromAddress(zw_address);
92  const Rule* rule = supplier_.GetRule(zw_key);
93  ASSERT_TRUE(rule != NULL);
94  EXPECT_EQ("data/ZW", rule->GetId());
95}
96
97TEST_F(PreloadSupplierTest, GetUnknownRule) {
98  supplier_.LoadRules("US", *loaded_callback_);
99  LookupKey unknown_key;
100  AddressData unknown_address;
101  unknown_address.region_code = "US";
102  unknown_address.administrative_area = "ZZ";
103  unknown_key.FromAddress(unknown_address);
104  const Rule* rule = supplier_.GetRule(unknown_key);
105  EXPECT_TRUE(rule == NULL);
106}
107
108TEST_F(PreloadSupplierTest, GetTooPreciseRule) {
109  supplier_.LoadRules("US", *loaded_callback_);
110  LookupKey precise_key;
111  AddressData precise_address;
112  precise_address.region_code = "US";
113  precise_address.administrative_area = "CA";
114  precise_address.locality = "Mountain View";
115  precise_key.FromAddress(precise_address);
116  const Rule* rule = supplier_.GetRule(precise_key);
117  EXPECT_TRUE(rule == NULL);
118}
119
120TEST_F(PreloadSupplierTest, GetRulesForRegion) {
121  supplier_.LoadRules("CN", *loaded_callback_);
122  const std::map<std::string, const Rule*>& rules =
123      supplier_.GetRulesForRegion("CN");
124  EXPECT_TRUE(rules.find("data/CN") != rules.end());
125  EXPECT_LT(1U, rules.size());
126}
127
128}  // namespace
129