geolocation_exceptions_table_model_unittest.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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/geolocation/geolocation_exceptions_table_model.h"
6
7#include "chrome/browser/browser_thread.h"
8#include "chrome/browser/renderer_host/test/test_render_view_host.h"
9#include "chrome/common/content_settings_helper.h"
10#include "chrome/test/testing_profile.h"
11#include "grit/generated_resources.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace {
15const GURL kUrl0("http://www.example.com");
16const GURL kUrl1("http://www.example1.com");
17const GURL kUrl2("http://www.example2.com");
18}  // namespace
19
20class GeolocationExceptionsTableModelTest : public RenderViewHostTestHarness {
21 public:
22  GeolocationExceptionsTableModelTest()
23      : ui_thread_(BrowserThread::UI, MessageLoop::current()) {}
24
25  virtual ~GeolocationExceptionsTableModelTest() {}
26
27  virtual void SetUp() {
28    RenderViewHostTestHarness::SetUp();
29    ResetModel();
30  }
31
32  virtual void TearDown() {
33    model_.reset(NULL);
34    RenderViewHostTestHarness::TearDown();
35  }
36
37  virtual void ResetModel() {
38    model_.reset(new GeolocationExceptionsTableModel(
39        profile()->GetGeolocationContentSettingsMap()));
40  }
41
42  void CreateAllowedSamples() {
43    scoped_refptr<GeolocationContentSettingsMap> map(
44        profile()->GetGeolocationContentSettingsMap());
45    map->SetContentSetting(kUrl0, kUrl0, CONTENT_SETTING_ALLOW);
46    map->SetContentSetting(kUrl0, kUrl1, CONTENT_SETTING_ALLOW);
47    map->SetContentSetting(kUrl0, kUrl2, CONTENT_SETTING_ALLOW);
48    ResetModel();
49    EXPECT_EQ(3, model_->RowCount());
50  }
51
52 protected:
53  BrowserThread ui_thread_;
54  scoped_ptr<GeolocationExceptionsTableModel> model_;
55};
56
57TEST_F(GeolocationExceptionsTableModelTest, CanRemoveException) {
58  EXPECT_EQ(0, model_->RowCount());
59
60  scoped_refptr<GeolocationContentSettingsMap> map(
61      profile()->GetGeolocationContentSettingsMap());
62
63  // Ensure a single entry can be removed.
64  map->SetContentSetting(kUrl0, kUrl0, CONTENT_SETTING_ALLOW);
65  ResetModel();
66  EXPECT_EQ(1, model_->RowCount());
67  GeolocationExceptionsTableModel::Rows rows;
68  rows.insert(0U);
69  EXPECT_TRUE(model_->CanRemoveRows(rows));
70
71
72  // Ensure an entry with children can't be removed.
73  map->SetContentSetting(kUrl0, kUrl0, CONTENT_SETTING_DEFAULT);
74  map->SetContentSetting(kUrl0, kUrl1, CONTENT_SETTING_ALLOW);
75  map->SetContentSetting(kUrl0, kUrl2, CONTENT_SETTING_BLOCK);
76  ResetModel();
77  EXPECT_EQ(3, model_->RowCount());
78  EXPECT_FALSE(model_->CanRemoveRows(rows));
79
80  // Ensure it can be removed if removing all children.
81  rows.clear();
82  rows.insert(1U);
83  rows.insert(2U);
84  EXPECT_TRUE(model_->CanRemoveRows(rows));
85}
86
87TEST_F(GeolocationExceptionsTableModelTest, RemoveExceptions) {
88  CreateAllowedSamples();
89  scoped_refptr<GeolocationContentSettingsMap> map(
90      profile()->GetGeolocationContentSettingsMap());
91
92  // Test removing parent exception.
93  GeolocationExceptionsTableModel::Rows rows;
94  rows.insert(0U);
95  model_->RemoveRows(rows);
96  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl0));
97  EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetContentSetting(kUrl0, kUrl1));
98  EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetContentSetting(kUrl0, kUrl2));
99
100  ResetModel();
101  EXPECT_EQ(3, model_->RowCount());
102
103  // Test removing remaining children.
104  rows.clear();
105  rows.insert(1U);
106  rows.insert(2U);
107  model_->RemoveRows(rows);
108  EXPECT_EQ(0, model_->RowCount());
109  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl0));
110  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl1));
111  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl2));
112}
113
114TEST_F(GeolocationExceptionsTableModelTest, RemoveAll) {
115  CreateAllowedSamples();
116  scoped_refptr<GeolocationContentSettingsMap> map(
117      profile()->GetGeolocationContentSettingsMap());
118
119  model_->RemoveAll();
120  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl0));
121  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl1));
122  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl2));
123  EXPECT_EQ(0, model_->RowCount());
124}
125
126TEST_F(GeolocationExceptionsTableModelTest, GetText) {
127  CreateAllowedSamples();
128
129  // Ensure the parent doesn't have any indentation.
130  string16 text = model_->GetText(0, IDS_EXCEPTIONS_HOSTNAME_HEADER);
131  EXPECT_EQ(content_settings_helper::OriginToString16(kUrl0), text);
132
133  // Ensure there's some indentation on the children nodes.
134  text = model_->GetText(1, IDS_EXCEPTIONS_HOSTNAME_HEADER);
135  EXPECT_NE(content_settings_helper::OriginToString16(kUrl1), text);
136  EXPECT_NE(string16::npos,
137            text.find(content_settings_helper::OriginToString16(kUrl1)));
138
139  text = model_->GetText(2, IDS_EXCEPTIONS_HOSTNAME_HEADER);
140  EXPECT_NE(content_settings_helper::OriginToString16(kUrl2), text);
141  EXPECT_NE(string16::npos,
142            text.find(content_settings_helper::OriginToString16(kUrl2)));
143}
144