1// Copyright (c) 2011 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/notifications/notification_exceptions_table_model.h"
6
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/notifications/desktop_notification_service_factory.h"
9#include "chrome/test/testing_profile.h"
10#include "content/browser/browser_thread.h"
11#include "content/browser/renderer_host/test_render_view_host.h"
12#include "grit/generated_resources.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "ui/base/l10n/l10n_util.h"
15
16class NotificationExceptionsTableModelTest : public RenderViewHostTestHarness {
17 public:
18  NotificationExceptionsTableModelTest()
19     : ui_thread_(BrowserThread::UI, MessageLoop::current()) {
20  }
21
22  virtual ~NotificationExceptionsTableModelTest() {
23  }
24
25  virtual void SetUp() {
26    RenderViewHostTestHarness::SetUp();
27    service_ = DesktopNotificationServiceFactory::GetForProfile(profile());
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 NotificationExceptionsTableModel(service_));
38  }
39
40  virtual void FillData() {
41    service_->GrantPermission(GURL("http://e-allowed2.com"));
42    service_->GrantPermission(GURL("http://allowed.com"));
43
44    service_->DenyPermission(GURL("http://denied2.com"));
45    service_->DenyPermission(GURL("http://denied.com"));
46    service_->DenyPermission(GURL("http://f-denied3.com"));
47
48    ResetModel();
49  }
50
51 protected:
52  BrowserThread ui_thread_;
53  scoped_ptr<NotificationExceptionsTableModel> model_;
54  DesktopNotificationService* service_;
55};
56
57TEST_F(NotificationExceptionsTableModelTest, CanCreate) {
58  EXPECT_EQ(0, model_->RowCount());
59}
60
61TEST_F(NotificationExceptionsTableModelTest, RemoveAll) {
62  FillData();
63  EXPECT_EQ(2u, service_->GetAllowedOrigins().size());
64  EXPECT_EQ(3u, service_->GetBlockedOrigins().size());
65  EXPECT_EQ(5, model_->RowCount());
66
67  model_->RemoveAll();
68  EXPECT_EQ(0, model_->RowCount());
69
70  EXPECT_EQ(0u, service_->GetAllowedOrigins().size());
71  EXPECT_EQ(0u, service_->GetBlockedOrigins().size());
72}
73
74TEST_F(NotificationExceptionsTableModelTest, AlphabeticalOrder) {
75  FillData();
76  EXPECT_EQ(5, model_->RowCount());
77
78  EXPECT_EQ(ASCIIToUTF16("allowed.com"),
79            model_->GetText(0, IDS_EXCEPTIONS_HOSTNAME_HEADER));
80  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON),
81            model_->GetText(0, IDS_EXCEPTIONS_ACTION_HEADER));
82
83  EXPECT_EQ(ASCIIToUTF16("denied.com"),
84            model_->GetText(1, IDS_EXCEPTIONS_HOSTNAME_HEADER));
85  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON),
86            model_->GetText(1, IDS_EXCEPTIONS_ACTION_HEADER));
87
88  EXPECT_EQ(ASCIIToUTF16("denied2.com"),
89            model_->GetText(2, IDS_EXCEPTIONS_HOSTNAME_HEADER));
90  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON),
91            model_->GetText(2, IDS_EXCEPTIONS_ACTION_HEADER));
92
93  EXPECT_EQ(ASCIIToUTF16("e-allowed2.com"),
94            model_->GetText(3, IDS_EXCEPTIONS_HOSTNAME_HEADER));
95  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON),
96            model_->GetText(3, IDS_EXCEPTIONS_ACTION_HEADER));
97
98  EXPECT_EQ(ASCIIToUTF16("f-denied3.com"),
99            model_->GetText(4, IDS_EXCEPTIONS_HOSTNAME_HEADER));
100  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON),
101            model_->GetText(4, IDS_EXCEPTIONS_ACTION_HEADER));
102}
103
104TEST_F(NotificationExceptionsTableModelTest, RemoveRows) {
105  FillData();
106  EXPECT_EQ(5, model_->RowCount());
107
108  {
109    RemoveRowsTableModel::Rows rows;
110    rows.insert(0);  // allowed.com
111    rows.insert(3);  // e-allowed2.com
112    model_->RemoveRows(rows);
113  }
114  EXPECT_EQ(3, model_->RowCount());
115  EXPECT_EQ(0u, service_->GetAllowedOrigins().size());
116  EXPECT_EQ(3u, service_->GetBlockedOrigins().size());
117
118  {
119    RemoveRowsTableModel::Rows rows;
120    rows.insert(0);
121    rows.insert(1);
122    rows.insert(2);
123    model_->RemoveRows(rows);
124  }
125  EXPECT_EQ(0, model_->RowCount());
126  EXPECT_EQ(0u, service_->GetAllowedOrigins().size());
127  EXPECT_EQ(0u, service_->GetBlockedOrigins().size());
128}
129