Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_galleries_permissions_unittest.cc
blob7393f4af7b51691d1a88fc746e4c7f7df458ac82
1 // Copyright 2013 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "chrome/browser/extensions/extension_prefs_unittest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
10 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/storage_monitor/test_storage_monitor.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_system.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace {
21 void AddGalleryPermission(MediaGalleryPrefId gallery,
22 bool has_access,
23 std::vector<MediaGalleryPermission>* vector) {
24 MediaGalleryPermission permission;
25 permission.pref_id = gallery;
26 permission.has_permission = has_access;
27 vector->push_back(permission);
30 } // namespace
32 // Test the MediaGalleries permissions functions.
33 class MediaGalleriesPermissionsTest : public extensions::ExtensionPrefsTest {
34 protected:
35 MediaGalleriesPermissionsTest()
36 : file_thread_(content::BrowserThread::FILE) {
38 ~MediaGalleriesPermissionsTest() override {}
40 // This is the same implementation as ExtensionPrefsTest::TearDown(), except
41 // for also resetting the ExtensionPrefs used by |gallery_prefs_| after
42 // TestExtensionPrefs reconstructs them.
43 void TearDown() override {
44 Verify();
46 // Reset ExtensionPrefs, and re-verify.
47 prefs_.ResetPrefRegistry();
48 RegisterPreferences(prefs_.pref_registry().get());
49 prefs_.RecreateExtensionPrefs();
50 gallery_prefs_->SetExtensionPrefsForTesting(prefs());
51 Verify();
52 prefs_.pref_service()->CommitPendingWrite();
53 message_loop_.RunUntilIdle();
55 storage_monitor::TestStorageMonitor::Destroy();
57 testing::Test::TearDown();
60 void Initialize() override {
61 file_thread_.Start();
63 ASSERT_TRUE(storage_monitor::TestStorageMonitor::CreateAndInstall());
64 profile_.reset(new TestingProfile);
65 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
66 base::RunLoop loop;
67 gallery_prefs_->EnsureInitialized(loop.QuitClosure());
68 loop.Run();
70 gallery_prefs_->SetExtensionPrefsForTesting(prefs());
72 extension1_id_ = prefs_.AddExtensionAndReturnId("test1");
73 extension2_id_ = prefs_.AddExtensionAndReturnId("test2");
74 extension3_id_ = prefs_.AddExtensionAndReturnId("test3");
75 // Id4 isn't used to ensure that an empty permission list is ok.
76 extension4_id_ = prefs_.AddExtensionAndReturnId("test4");
77 Verify();
79 gallery_prefs_->SetGalleryPermissionInPrefs(extension1_id_, 1, false);
80 AddGalleryPermission(1, false, &extension1_expectation_);
81 Verify();
83 gallery_prefs_->SetGalleryPermissionInPrefs(extension1_id_, 2, true);
84 AddGalleryPermission(2, true, &extension1_expectation_);
85 Verify();
87 gallery_prefs_->SetGalleryPermissionInPrefs(extension1_id_, 2, false);
88 extension1_expectation_[1].has_permission = false;
89 Verify();
91 gallery_prefs_->SetGalleryPermissionInPrefs(extension2_id_, 1, true);
92 gallery_prefs_->SetGalleryPermissionInPrefs(extension2_id_, 3, true);
93 gallery_prefs_->SetGalleryPermissionInPrefs(extension2_id_, 4, true);
94 AddGalleryPermission(1, true, &extension2_expectation_);
95 AddGalleryPermission(3, true, &extension2_expectation_);
96 AddGalleryPermission(4, true, &extension2_expectation_);
97 Verify();
99 gallery_prefs_->SetGalleryPermissionInPrefs(extension3_id_, 3, true);
100 AddGalleryPermission(3, true, &extension3_expectation_);
101 Verify();
103 gallery_prefs_->RemoveGalleryPermissionsFromPrefs(3);
104 extension2_expectation_.erase(extension2_expectation_.begin() + 1);
105 extension3_expectation_.erase(extension3_expectation_.begin());
106 Verify();
108 gallery_prefs_->UnsetGalleryPermissionInPrefs(extension1_id_, 1);
109 extension1_expectation_.erase(extension1_expectation_.begin());
110 Verify();
113 void Verify() override {
114 struct TestData {
115 std::string* id;
116 std::vector<MediaGalleryPermission>* expectation;
119 const TestData test_data[] = {{&extension1_id_, &extension1_expectation_},
120 {&extension2_id_, &extension2_expectation_},
121 {&extension3_id_, &extension3_expectation_},
122 {&extension4_id_, &extension4_expectation_}};
123 for (size_t i = 0; i < arraysize(test_data); i++) {
124 std::vector<MediaGalleryPermission> actual =
125 gallery_prefs_->GetGalleryPermissionsFromPrefs(*test_data[i].id);
126 EXPECT_EQ(test_data[i].expectation->size(), actual.size());
127 for (size_t permission_entry = 0;
128 permission_entry < test_data[i].expectation->size() &&
129 permission_entry < actual.size();
130 permission_entry++) {
131 EXPECT_EQ(test_data[i].expectation->at(permission_entry).pref_id,
132 actual[permission_entry].pref_id);
133 EXPECT_EQ(test_data[i].expectation->at(permission_entry).has_permission,
134 actual[permission_entry].has_permission);
139 std::string extension1_id_;
140 std::string extension2_id_;
141 std::string extension3_id_;
142 std::string extension4_id_;
144 std::vector<MediaGalleryPermission> extension1_expectation_;
145 std::vector<MediaGalleryPermission> extension2_expectation_;
146 std::vector<MediaGalleryPermission> extension3_expectation_;
147 std::vector<MediaGalleryPermission> extension4_expectation_;
149 // Needed for |gallery_prefs_| to initialize correctly.
150 EnsureMediaDirectoriesExists ensure_media_directories_exists_;
151 content::TestBrowserThread file_thread_;
153 scoped_ptr<TestingProfile> profile_;
154 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
157 TEST_F(MediaGalleriesPermissionsTest, MediaGalleries) {}