summaryrefslogtreecommitdiffstats
path: root/chrome/browser/geolocation/geolocation_exceptions_table_model_unittest.cc
blob: 66d1168ed9dc71dcf86e52b406c5c8360b527332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/geolocation/geolocation_exceptions_table_model.h"

#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/renderer_host/test/test_render_view_host.h"
#include "chrome/common/content_settings_helper.h"
#include "chrome/test/testing_profile.h"
#include "grit/generated_resources.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
const GURL kUrl0("http://www.example.com");
const GURL kUrl1("http://www.example1.com");
const GURL kUrl2("http://www.example2.com");
}  // namespace

class GeolocationExceptionsTableModelTest : public RenderViewHostTestHarness {
 public:
  GeolocationExceptionsTableModelTest()
      : ui_thread_(ChromeThread::UI, MessageLoop::current()) {}

  virtual ~GeolocationExceptionsTableModelTest() {}

  virtual void SetUp() {
    RenderViewHostTestHarness::SetUp();
    ResetModel();
  }

  virtual void TearDown() {
    model_.reset(NULL);
    RenderViewHostTestHarness::TearDown();
  }

  virtual void ResetModel() {
    model_.reset(new GeolocationExceptionsTableModel(
        profile()->GetGeolocationContentSettingsMap()));
  }

  void CreateAllowedSamples() {
    scoped_refptr<GeolocationContentSettingsMap> map =
        profile()->GetGeolocationContentSettingsMap();
    map->SetContentSetting(kUrl0, kUrl0, CONTENT_SETTING_ALLOW);
    map->SetContentSetting(kUrl0, kUrl1, CONTENT_SETTING_ALLOW);
    map->SetContentSetting(kUrl0, kUrl2, CONTENT_SETTING_ALLOW);
    ResetModel();
    EXPECT_EQ(3, model_->RowCount());
  }

 protected:
  ChromeThread ui_thread_;
  scoped_ptr<GeolocationExceptionsTableModel> model_;
};

TEST_F(GeolocationExceptionsTableModelTest, CanRemoveException) {
  EXPECT_EQ(0, model_->RowCount());

  scoped_refptr<GeolocationContentSettingsMap> map =
      profile()->GetGeolocationContentSettingsMap();

  // Ensure a single entry can be removed.
  map->SetContentSetting(kUrl0, kUrl0, CONTENT_SETTING_ALLOW);
  ResetModel();
  EXPECT_EQ(1, model_->RowCount());
  GeolocationExceptionsTableModel::Rows rows;
  rows.insert(0U);
  EXPECT_TRUE(model_->CanRemoveRows(rows));


  // Ensure an entry with children can't be removed.
  map->SetContentSetting(kUrl0, kUrl0, CONTENT_SETTING_DEFAULT);
  map->SetContentSetting(kUrl0, kUrl1, CONTENT_SETTING_ALLOW);
  map->SetContentSetting(kUrl0, kUrl2, CONTENT_SETTING_BLOCK);
  ResetModel();
  EXPECT_EQ(3, model_->RowCount());
  EXPECT_FALSE(model_->CanRemoveRows(rows));

  // Ensure it can be removed if removing all children.
  rows.clear();
  rows.insert(1U);
  rows.insert(2U);
  EXPECT_TRUE(model_->CanRemoveRows(rows));
}

TEST_F(GeolocationExceptionsTableModelTest, RemoveExceptions) {
  CreateAllowedSamples();
  scoped_refptr<GeolocationContentSettingsMap> map =
      profile()->GetGeolocationContentSettingsMap();

  // Test removing parent exception.
  GeolocationExceptionsTableModel::Rows rows;
  rows.insert(0U);
  model_->RemoveRows(rows);
  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl0));
  EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetContentSetting(kUrl0, kUrl1));
  EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetContentSetting(kUrl0, kUrl2));

  ResetModel();
  EXPECT_EQ(3, model_->RowCount());

  // Test removing remaining children.
  rows.clear();
  rows.insert(1U);
  rows.insert(2U);
  model_->RemoveRows(rows);
  EXPECT_EQ(0, model_->RowCount());
  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl0));
  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl1));
  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl2));
}

TEST_F(GeolocationExceptionsTableModelTest, RemoveAll) {
  CreateAllowedSamples();
  scoped_refptr<GeolocationContentSettingsMap> map =
      profile()->GetGeolocationContentSettingsMap();

  model_->RemoveAll();
  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl0));
  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl1));
  EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(kUrl0, kUrl2));
  EXPECT_EQ(0, model_->RowCount());
}

TEST_F(GeolocationExceptionsTableModelTest, GetText) {
  CreateAllowedSamples();

  // Ensure the parent doesn't have any indentation.
  std::wstring text(model_->GetText(0, IDS_EXCEPTIONS_HOSTNAME_HEADER));
  EXPECT_EQ(content_settings_helper::OriginToWString(kUrl0), text);

  // Ensure there's some indentation on the children nodes.
  text = model_->GetText(1, IDS_EXCEPTIONS_HOSTNAME_HEADER);
  EXPECT_NE(content_settings_helper::OriginToWString(kUrl1), text);
  EXPECT_NE(std::wstring::npos,
            text.find(content_settings_helper::OriginToWString(kUrl1)));

  text = model_->GetText(2, IDS_EXCEPTIONS_HOSTNAME_HEADER);
  EXPECT_NE(content_settings_helper::OriginToWString(kUrl2), text);
  EXPECT_NE(std::wstring::npos,
            text.find(content_settings_helper::OriginToWString(kUrl2)));
}