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