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