1// Copyright 2014 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 "base/strings/string_number_conversions.h"
6#include "base/strings/utf_string_conversions.h"
7#include "chrome/browser/media_galleries/media_galleries_dialog_controller_mock.h"
8#include "chrome/browser/ui/views/extensions/media_galleries_dialog_views.h"
9#include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h"
10#include "components/storage_monitor/storage_info.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "ui/views/controls/button/checkbox.h"
13
14using ::testing::_;
15using ::testing::AnyNumber;
16using ::testing::Mock;
17using ::testing::NiceMock;
18using ::testing::Return;
19using ::testing::ReturnPointee;
20
21namespace {
22
23MediaGalleryPrefInfo MakePrefInfoForTesting(MediaGalleryPrefId id) {
24  MediaGalleryPrefInfo gallery;
25  gallery.pref_id = id;
26  gallery.device_id = storage_monitor::StorageInfo::MakeDeviceId(
27      storage_monitor::StorageInfo::FIXED_MASS_STORAGE,
28      base::Int64ToString(id));
29  gallery.display_name = base::ASCIIToUTF16("Display Name");
30  return gallery;
31}
32
33}  // namespace
34
35class MediaGalleriesDialogTest : public testing::Test {
36 public:
37  MediaGalleriesDialogTest() {}
38  virtual ~MediaGalleriesDialogTest() {}
39  virtual void SetUp() OVERRIDE {
40    std::vector<base::string16> headers;
41    headers.push_back(base::string16());
42    headers.push_back(base::ASCIIToUTF16("header2"));
43    ON_CALL(controller_, GetSectionHeaders()).
44        WillByDefault(Return(headers));
45    EXPECT_CALL(controller_, GetSectionEntries(_)).
46        Times(AnyNumber());
47  }
48
49  virtual void TearDown() OVERRIDE {
50    Mock::VerifyAndClearExpectations(&controller_);
51  }
52
53  NiceMock<MediaGalleriesDialogControllerMock>* controller() {
54    return &controller_;
55  }
56
57 private:
58  // TODO(gbillock): Get rid of this mock; make something specialized.
59  NiceMock<MediaGalleriesDialogControllerMock> controller_;
60
61  DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest);
62};
63
64// Tests that checkboxes are initialized according to the contents of
65// permissions in the registry.
66TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) {
67  MediaGalleriesDialogController::Entries attached_permissions;
68  attached_permissions.push_back(
69      MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
70  attached_permissions.push_back(
71      MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(2), false));
72  EXPECT_CALL(*controller(), GetSectionEntries(0)).
73      WillRepeatedly(Return(attached_permissions));
74
75  MediaGalleriesDialogViews dialog(controller());
76  EXPECT_EQ(2U, dialog.checkbox_map_.size());
77
78  MediaGalleryCheckboxView* checkbox_view1 = dialog.checkbox_map_[1];
79  EXPECT_TRUE(checkbox_view1->checkbox()->checked());
80
81  MediaGalleryCheckboxView* checkbox_view2 = dialog.checkbox_map_[2];
82  EXPECT_FALSE(checkbox_view2->checkbox()->checked());
83}
84
85// Tests that toggling checkboxes updates the controller.
86TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) {
87  MediaGalleriesDialogController::Entries attached_permissions;
88  attached_permissions.push_back(
89      MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
90  EXPECT_CALL(*controller(), GetSectionEntries(0)).
91      WillRepeatedly(Return(attached_permissions));
92
93  MediaGalleriesDialogViews dialog(controller());
94  EXPECT_EQ(1U, dialog.checkbox_map_.size());
95  views::Checkbox* checkbox = dialog.checkbox_map_[1]->checkbox();
96  EXPECT_TRUE(checkbox->checked());
97
98  ui::KeyEvent dummy_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
99  EXPECT_CALL(*controller(), DidToggleEntry(1, false));
100  checkbox->SetChecked(false);
101  dialog.ButtonPressed(checkbox, dummy_event);
102
103  EXPECT_CALL(*controller(), DidToggleEntry(1, true));
104  checkbox->SetChecked(true);
105  dialog.ButtonPressed(checkbox, dummy_event);
106}
107
108// Tests that UpdateGallery will add a new checkbox, but only if it refers to
109// a gallery that the dialog hasn't seen before.
110TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
111  MediaGalleriesDialogViews dialog(controller());
112
113  MediaGalleriesDialogController::Entries attached_permissions;
114  EXPECT_CALL(*controller(), GetSectionEntries(0)).
115      WillRepeatedly(ReturnPointee(&attached_permissions));
116
117  EXPECT_TRUE(dialog.checkbox_map_.empty());
118
119  MediaGalleryPrefInfo gallery1 = MakePrefInfoForTesting(1);
120  attached_permissions.push_back(
121      MediaGalleriesDialogController::Entry(gallery1, true));
122  dialog.UpdateGalleries();
123  EXPECT_EQ(1U, dialog.checkbox_map_.size());
124
125  MediaGalleryPrefInfo gallery2 = MakePrefInfoForTesting(2);
126  attached_permissions.push_back(
127      MediaGalleriesDialogController::Entry(gallery2, true));
128  dialog.UpdateGalleries();
129  EXPECT_EQ(2U, dialog.checkbox_map_.size());
130
131  attached_permissions.push_back(
132      MediaGalleriesDialogController::Entry(gallery2, false));
133  dialog.UpdateGalleries();
134  EXPECT_EQ(2U, dialog.checkbox_map_.size());
135}
136
137TEST_F(MediaGalleriesDialogTest, ForgetDeletes) {
138  MediaGalleriesDialogViews dialog(controller());
139
140  MediaGalleriesDialogController::Entries attached_permissions;
141  EXPECT_CALL(*controller(), GetSectionEntries(0)).
142      WillRepeatedly(ReturnPointee(&attached_permissions));
143
144  EXPECT_TRUE(dialog.checkbox_map_.empty());
145
146  MediaGalleryPrefInfo gallery1 = MakePrefInfoForTesting(1);
147  attached_permissions.push_back(
148      MediaGalleriesDialogController::Entry(gallery1, true));
149  dialog.UpdateGalleries();
150  EXPECT_EQ(1U, dialog.checkbox_map_.size());
151
152  MediaGalleryPrefInfo gallery2 = MakePrefInfoForTesting(2);
153  attached_permissions.push_back(
154      MediaGalleriesDialogController::Entry(gallery2, true));
155  dialog.UpdateGalleries();
156  EXPECT_EQ(2U, dialog.checkbox_map_.size());
157
158  attached_permissions.pop_back();
159  dialog.UpdateGalleries();
160  EXPECT_EQ(1U, dialog.checkbox_map_.size());
161}
162