OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media_gallery/media_galleries_dialog_controller.h" | 5 #include "chrome/browser/media_gallery/media_galleries_dialog_controller_mock.h" |
6 #include "chrome/browser/ui/gtk/extensions/media_galleries_dialog_gtk.h" | 6 #include "chrome/browser/ui/gtk/extensions/media_galleries_dialog_gtk.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
| 9 using ::testing::_; |
| 10 using ::testing::NiceMock; |
| 11 using ::testing::ReturnRef; |
| 12 |
9 namespace chrome { | 13 namespace chrome { |
10 | 14 |
11 class MediaGalleriesDialogTest : public testing::Test, | 15 class MediaGalleriesDialogTest : public testing::Test { |
12 public MediaGalleriesDialogController { | |
13 public: | |
14 MediaGalleriesDialogTest() : toggles_(0) {} | |
15 virtual ~MediaGalleriesDialogTest() {} | |
16 | |
17 virtual void DialogFinished(bool accepted) OVERRIDE {} | |
18 virtual void DidToggleGallery(const MediaGalleryPrefInfo* pref_info, | |
19 bool enabled) OVERRIDE { | |
20 toggles_++; | |
21 MediaGalleriesDialogController::DidToggleGallery(pref_info, enabled); | |
22 } | |
23 | |
24 protected: | |
25 // Counter that tracks the number of times a checkbox has been toggled. | |
26 size_t toggles_; | |
27 }; | 16 }; |
28 | 17 |
29 // Tests that checkboxes are initialized according to the contents of | 18 // Tests that checkboxes are initialized according to the contents of |
30 // |known_galleries|. | 19 // permissions(). |
31 TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) { | 20 TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) { |
| 21 NiceMock<MediaGalleriesDialogControllerMock> controller; |
| 22 |
| 23 MediaGalleriesDialogController::KnownGalleryPermissions permissions; |
32 MediaGalleryPrefInfo gallery1; | 24 MediaGalleryPrefInfo gallery1; |
33 gallery1.pref_id = 1; | 25 gallery1.pref_id = 1; |
34 known_galleries_[1] = GalleryPermission(gallery1, true); | 26 permissions[1] = |
| 27 MediaGalleriesDialogController::GalleryPermission(gallery1, true); |
35 MediaGalleryPrefInfo gallery2; | 28 MediaGalleryPrefInfo gallery2; |
36 gallery2.pref_id = 2; | 29 gallery2.pref_id = 2; |
37 known_galleries_[2] = GalleryPermission(gallery2, false); | 30 permissions[2] = |
| 31 MediaGalleriesDialogController::GalleryPermission(gallery2, false); |
| 32 EXPECT_CALL(controller, permissions()). |
| 33 WillRepeatedly(ReturnRef(permissions)); |
38 | 34 |
39 scoped_ptr<MediaGalleriesDialogGtk> | 35 // Initializing checkboxes should not cause them to be toggled. |
40 dialog_(new MediaGalleriesDialogGtk(this)); | 36 EXPECT_CALL(controller, DidToggleGallery(_, _)). |
41 EXPECT_EQ(2U, dialog_->checkbox_map_.size()); | 37 Times(0); |
42 | 38 |
43 GtkWidget* checkbox1 = dialog_->checkbox_map_[&known_galleries_[1].pref_info]; | 39 MediaGalleriesDialogGtk dialog(&controller); |
| 40 EXPECT_EQ(2U, dialog.checkbox_map_.size()); |
| 41 |
| 42 GtkWidget* checkbox1 = dialog.checkbox_map_[&permissions[1].pref_info]; |
44 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox1)); | 43 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox1)); |
45 EXPECT_TRUE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox1))); | 44 EXPECT_TRUE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox1))); |
46 | 45 |
47 GtkWidget* checkbox2 = dialog_->checkbox_map_[&known_galleries_[2].pref_info]; | 46 GtkWidget* checkbox2 = dialog.checkbox_map_[&permissions[2].pref_info]; |
48 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox2)); | 47 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox2)); |
49 EXPECT_FALSE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox2))); | 48 EXPECT_FALSE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox2))); |
50 | |
51 // Initializing checkboxes should not cause them to be toggled. | |
52 EXPECT_EQ(0U, toggles_); | |
53 } | 49 } |
54 | 50 |
55 // Tests that toggling checkboxes updates the controller. | 51 // Tests that toggling checkboxes updates the controller. |
56 TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) { | 52 TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) { |
| 53 NiceMock<MediaGalleriesDialogControllerMock> controller; |
| 54 |
| 55 MediaGalleriesDialogController::KnownGalleryPermissions permissions; |
57 MediaGalleryPrefInfo gallery1; | 56 MediaGalleryPrefInfo gallery1; |
58 gallery1.pref_id = 1; | 57 gallery1.pref_id = 1; |
59 known_galleries_[1] = GalleryPermission(gallery1, true); | 58 permissions[1] = |
| 59 MediaGalleriesDialogController::GalleryPermission(gallery1, true); |
| 60 EXPECT_CALL(controller, permissions()). |
| 61 WillRepeatedly(ReturnRef(permissions)); |
60 | 62 |
61 scoped_ptr<MediaGalleriesDialogGtk> | 63 MediaGalleriesDialogGtk dialog(&controller); |
62 dialog_(new MediaGalleriesDialogGtk(this)); | 64 EXPECT_EQ(1U, dialog.checkbox_map_.size()); |
63 EXPECT_EQ(1U, dialog_->checkbox_map_.size()); | |
64 GtkWidget* checkbox = | 65 GtkWidget* checkbox = |
65 dialog_->checkbox_map_[&known_galleries_[1].pref_info]; | 66 dialog.checkbox_map_[&permissions[1].pref_info]; |
66 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox)); | 67 ASSERT_TRUE(GTK_IS_TOGGLE_BUTTON(checkbox)); |
67 EXPECT_TRUE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox))); | 68 EXPECT_TRUE(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox))); |
68 | 69 |
| 70 EXPECT_CALL(controller, DidToggleGallery(_, false)); |
69 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), FALSE); | 71 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), FALSE); |
70 EXPECT_FALSE(known_galleries_[1].allowed); | 72 |
| 73 EXPECT_CALL(controller, DidToggleGallery(_, true)); |
71 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), TRUE); | 74 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), TRUE); |
72 EXPECT_TRUE(known_galleries_[1].allowed); | |
73 | |
74 EXPECT_EQ(2U, toggles_); | |
75 } | 75 } |
76 | 76 |
77 // Tests that UpdateGallery will add a new checkbox, but only if it refers to | 77 // Tests that UpdateGallery will add a new checkbox, but only if it refers to |
78 // a gallery that the dialog hasn't seen before. | 78 // a gallery that the dialog hasn't seen before. |
79 TEST_F(MediaGalleriesDialogTest, UpdateAdds) { | 79 TEST_F(MediaGalleriesDialogTest, UpdateAdds) { |
80 scoped_ptr<MediaGalleriesDialogGtk> | 80 NiceMock<MediaGalleriesDialogControllerMock> controller; |
81 dialog_(new MediaGalleriesDialogGtk(this)); | |
82 | 81 |
83 EXPECT_TRUE(dialog_->checkbox_map_.empty()); | 82 MediaGalleriesDialogController::KnownGalleryPermissions permissions; |
| 83 EXPECT_CALL(controller, permissions()). |
| 84 WillRepeatedly(ReturnRef(permissions)); |
| 85 |
| 86 MediaGalleriesDialogGtk dialog(&controller); |
| 87 |
| 88 EXPECT_TRUE(dialog.checkbox_map_.empty()); |
84 | 89 |
85 MediaGalleryPrefInfo gallery1; | 90 MediaGalleryPrefInfo gallery1; |
86 dialog_->UpdateGallery(&gallery1, true); | 91 dialog.UpdateGallery(&gallery1, true); |
87 EXPECT_EQ(1U, dialog_->checkbox_map_.size()); | 92 EXPECT_EQ(1U, dialog.checkbox_map_.size()); |
88 | 93 |
89 MediaGalleryPrefInfo gallery2; | 94 MediaGalleryPrefInfo gallery2; |
90 dialog_->UpdateGallery(&gallery2, true); | 95 dialog.UpdateGallery(&gallery2, true); |
91 EXPECT_EQ(2U, dialog_->checkbox_map_.size()); | 96 EXPECT_EQ(2U, dialog.checkbox_map_.size()); |
92 | 97 |
93 dialog_->UpdateGallery(&gallery2, false); | 98 dialog.UpdateGallery(&gallery2, false); |
94 EXPECT_EQ(2U, dialog_->checkbox_map_.size()); | 99 EXPECT_EQ(2U, dialog.checkbox_map_.size()); |
95 } | 100 } |
96 | 101 |
97 } // namespace chrome | 102 } // namespace chrome |
OLD | NEW |