15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/media_galleries/media_galleries_dialog_controller_mock.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/views/extensions/media_galleries_dialog_views.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/ui/views/extensions/media_gallery_checkbox_view.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/storage_monitor/storage_info.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/controls/button/checkbox.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::testing::_;
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::AnyNumber;
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using ::testing::Mock;
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::testing::NiceMock;
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::testing::Return;
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using ::testing::ReturnPointee;
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MediaGalleryPrefInfo MakePrefInfoForTesting(MediaGalleryPrefId id) {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MediaGalleryPrefInfo gallery;
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gallery.pref_id = id;
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gallery.device_id = storage_monitor::StorageInfo::MakeDeviceId(
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      storage_monitor::StorageInfo::FIXED_MASS_STORAGE,
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Int64ToString(id));
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gallery.display_name = base::ASCIIToUTF16("Display Name");
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return gallery;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MediaGalleriesDialogTest : public testing::Test {
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MediaGalleriesDialogTest() {}
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~MediaGalleriesDialogTest() {}
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    std::vector<base::string16> headers;
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    headers.push_back(base::string16());
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    headers.push_back(base::ASCIIToUTF16("header2"));
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ON_CALL(controller_, GetSectionHeaders()).
4446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        WillByDefault(Return(headers));
4546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_CALL(controller_, GetSectionEntries(_)).
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        Times(AnyNumber());
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    Mock::VerifyAndClearExpectations(&controller_);
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  NiceMock<MediaGalleriesDialogControllerMock>* controller() {
5446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return &controller_;
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
5846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // TODO(gbillock): Get rid of this mock; make something specialized.
5946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  NiceMock<MediaGalleriesDialogControllerMock> controller_;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest);
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Tests that checkboxes are initialized according to the contents of
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// permissions in the registry.
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) {
6746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleriesDialogController::Entries attached_permissions;
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.push_back(
6946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.push_back(
7146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(2), false));
7246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*controller(), GetSectionEntries(0)).
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      WillRepeatedly(Return(attached_permissions));
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleriesDialogViews dialog(controller());
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2U, dialog.checkbox_map_.size());
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MediaGalleryCheckboxView* checkbox_view1 = dialog.checkbox_map_[1];
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(checkbox_view1->checkbox()->checked());
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MediaGalleryCheckboxView* checkbox_view2 = dialog.checkbox_map_[2];
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(checkbox_view2->checkbox()->checked());
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Tests that toggling checkboxes updates the controller.
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) {
8746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleriesDialogController::Entries attached_permissions;
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.push_back(
8946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MediaGalleriesDialogController::Entry(MakePrefInfoForTesting(1), true));
9046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*controller(), GetSectionEntries(0)).
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      WillRepeatedly(Return(attached_permissions));
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
9346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleriesDialogViews dialog(controller());
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, dialog.checkbox_map_.size());
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  views::Checkbox* checkbox = dialog.checkbox_map_[1]->checkbox();
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(checkbox->checked());
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ui::KeyEvent dummy_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*controller(), DidToggleEntry(1, false));
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  checkbox->SetChecked(false);
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  dialog.ButtonPressed(checkbox, dummy_event);
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*controller(), DidToggleEntry(1, true));
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  checkbox->SetChecked(true);
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  dialog.ButtonPressed(checkbox, dummy_event);
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Tests that UpdateGallery will add a new checkbox, but only if it refers to
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// a gallery that the dialog hasn't seen before.
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
11146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleriesDialogViews dialog(controller());
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
11346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleriesDialogController::Entries attached_permissions;
11446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*controller(), GetSectionEntries(0)).
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      WillRepeatedly(ReturnPointee(&attached_permissions));
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(dialog.checkbox_map_.empty());
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
11946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleryPrefInfo gallery1 = MakePrefInfoForTesting(1);
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.push_back(
12146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MediaGalleriesDialogController::Entry(gallery1, true));
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dialog.UpdateGalleries();
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, dialog.checkbox_map_.size());
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleryPrefInfo gallery2 = MakePrefInfoForTesting(2);
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.push_back(
12746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MediaGalleriesDialogController::Entry(gallery2, true));
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dialog.UpdateGalleries();
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2U, dialog.checkbox_map_.size());
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.push_back(
13246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MediaGalleriesDialogController::Entry(gallery2, false));
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dialog.UpdateGalleries();
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2U, dialog.checkbox_map_.size());
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(MediaGalleriesDialogTest, ForgetDeletes) {
13846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleriesDialogViews dialog(controller());
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleriesDialogController::Entries attached_permissions;
14146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*controller(), GetSectionEntries(0)).
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      WillRepeatedly(ReturnPointee(&attached_permissions));
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(dialog.checkbox_map_.empty());
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleryPrefInfo gallery1 = MakePrefInfoForTesting(1);
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.push_back(
14846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MediaGalleriesDialogController::Entry(gallery1, true));
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dialog.UpdateGalleries();
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, dialog.checkbox_map_.size());
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
15246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MediaGalleryPrefInfo gallery2 = MakePrefInfoForTesting(2);
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.push_back(
15446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MediaGalleriesDialogController::Entry(gallery2, true));
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dialog.UpdateGalleries();
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2U, dialog.checkbox_map_.size());
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  attached_permissions.pop_back();
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dialog.UpdateGalleries();
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, dialog.checkbox_map_.size());
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
162