country_combobox_model_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 "chrome/browser/ui/autofill/country_combobox_model.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/test/base/testing_profile.h"
10#include "components/autofill/core/browser/autofill_country.h"
11#include "components/autofill/core/browser/test_personal_data_manager.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
14#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui_component.h"
15#include "third_party/libaddressinput/src/cpp/include/libaddressinput/localization.h"
16
17namespace autofill {
18
19class CountryComboboxModelTest : public testing::Test {
20 public:
21  CountryComboboxModelTest() {
22    manager_.Init(NULL, profile_.GetPrefs(), false);
23    manager_.set_timezone_country_code("KR");
24    model_.reset(new CountryComboboxModel());
25    model_->SetCountries(manager_, base::Callback<bool(const std::string&)>());
26  }
27
28  TestPersonalDataManager* manager() { return &manager_; }
29  CountryComboboxModel* model() { return model_.get(); }
30
31 private:
32  // NB: order is important here - |profile_| must go down after |manager_|.
33  TestingProfile profile_;
34  TestPersonalDataManager manager_;
35  scoped_ptr<CountryComboboxModel> model_;
36};
37
38TEST_F(CountryComboboxModelTest, DefaultCountryCode) {
39  std::string default_country = model()->GetDefaultCountryCode();
40  EXPECT_EQ(manager()->GetDefaultCountryCodeForNewAddress(), default_country);
41
42  AutofillCountry country(default_country,
43                          g_browser_process->GetApplicationLocale());
44  EXPECT_EQ(country.name(), model()->GetItemAt(0));
45}
46
47TEST_F(CountryComboboxModelTest, AllCountriesHaveComponents) {
48  ::i18n::addressinput::Localization localization;
49  std::string unused;
50  for (int i = 0; i < model()->GetItemCount(); ++i) {
51    if (model()->IsItemSeparatorAt(i))
52      continue;
53
54    std::string country_code = model()->countries()[i]->country_code();
55    std::vector< ::i18n::addressinput::AddressUiComponent> components =
56        ::i18n::addressinput::BuildComponents(
57            country_code, localization, std::string(), &unused);
58    EXPECT_FALSE(components.empty());
59  }
60}
61
62}  // namespace autofill
63