File: documents_provider_root_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (191 lines) | stat: -rw-r--r-- 7,137 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/file_manager/documents_provider_root_manager.h"

#include <memory>
#include <string>
#include <vector>

#include "chrome/browser/ash/arc/fileapi/arc_file_system_operation_runner.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/experiences/arc/arc_features.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "chromeos/ash/experiences/arc/test/connection_holder_util.h"
#include "chromeos/ash/experiences/arc/test/fake_file_system_instance.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace file_manager {

namespace {

class TestObserver : public DocumentsProviderRootManager::Observer {
 public:
  // DocumentsProviderRootManager::Observer overrides:
  void OnDocumentsProviderRootAdded(
      const std::string& authority,
      const std::string& root_id,
      const std::string& document_id,
      const std::string& title,
      const std::string& summary,
      const GURL& icon_url,
      bool read_only,
      const std::vector<std::string>& mime_types) override {
    added_authorities_.push_back(authority);
  }
  void OnDocumentsProviderRootRemoved(const std::string& authority,
                                      const std::string& root_id) override {
    removed_authorities_.push_back(authority);
  }

  void Reset() {
    added_authorities_.clear();
    removed_authorities_.clear();
  }
  std::vector<std::string> added_authorities() { return added_authorities_; }
  std::vector<std::string> removed_authorities() {
    return removed_authorities_;
  }

 private:
  std::vector<std::string> added_authorities_;
  std::vector<std::string> removed_authorities_;
};

}  // namespace

class DocumentsProviderRootManagerTest : public testing::Test {
 public:
  DocumentsProviderRootManagerTest() = default;

  DocumentsProviderRootManagerTest(const DocumentsProviderRootManagerTest&) =
      delete;
  DocumentsProviderRootManagerTest& operator=(
      const DocumentsProviderRootManagerTest&) = delete;

  ~DocumentsProviderRootManagerTest() override = default;

  void SetUp() override {
    arc_service_manager_ = std::make_unique<arc::ArcServiceManager>();
    profile_ = std::make_unique<TestingProfile>();
    arc::ArcFileSystemBridge::GetForBrowserContextForTesting(profile_.get());
    runner_ = arc::ArcFileSystemOperationRunner::CreateForTesting(
        profile_.get(), arc_service_manager_->arc_bridge_service());
    root_manager_ = std::make_unique<DocumentsProviderRootManager>(
        profile_.get(), runner_.get());
    root_manager_->AddObserver(&observer_);
    arc_service_manager_->arc_bridge_service()->file_system()->SetInstance(
        &file_system_instance_);
    WaitForInstanceReady(
        arc_service_manager_->arc_bridge_service()->file_system());
    ASSERT_TRUE(file_system_instance_.InitCalled());
  }

  void TearDown() override {
    root_manager_->RemoveObserver(&observer_);
    root_manager_.reset();
    arc_service_manager_->arc_bridge_service()->file_system()->CloseInstance(
        &file_system_instance_);
    if (runner_) {
      runner_->Shutdown();
    }
  }

  void AddFakeRoot(const std::string& authority,
                   const std::string& root_id,
                   const std::string& document_id,
                   const std::string& title,
                   int64_t available_bytes,
                   int64_t capacity_bytes) {
    file_system_instance_.AddRoot(arc::FakeFileSystemInstance::Root(
        authority, root_id, document_id, title, available_bytes,
        capacity_bytes));
  }

  TestObserver& observer() { return observer_; }

 protected:
  std::unique_ptr<DocumentsProviderRootManager> root_manager_;

 private:
  content::BrowserTaskEnvironment task_environment_;
  arc::FakeFileSystemInstance file_system_instance_;
  std::unique_ptr<arc::ArcServiceManager> arc_service_manager_;
  std::unique_ptr<TestingProfile> profile_;
  std::unique_ptr<arc::ArcFileSystemOperationRunner> runner_;
  TestObserver observer_;
};

TEST_F(DocumentsProviderRootManagerTest, AddMultipleRoots) {
  AddFakeRoot("authority1", "123", "", "", 10, 100);
  AddFakeRoot("authority2", "456", "", "", 100, 1000);
  root_manager_->SetEnabled(true);
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(2u, observer().added_authorities().size());
  EXPECT_EQ(0u, observer().removed_authorities().size());
  EXPECT_EQ("authority1", observer().added_authorities()[0]);
  EXPECT_EQ("authority2", observer().added_authorities()[1]);
}

TEST_F(DocumentsProviderRootManagerTest, ExcludeDenylistedRoots) {
  AddFakeRoot("authority1", "123", "", "", 10, 100);
  AddFakeRoot("com.android.externalstorage.documents", "", "", "", -1, -1);
  AddFakeRoot("com.android.providers.downloads.documents", "", "", "", -1, -1);
  AddFakeRoot("com.android.providers.media.documents", "", "", "", -1, -1);
  AddFakeRoot("com.google.android.apps.docs.storage", "", "", "", -1, -1);
  root_manager_->SetEnabled(true);
  base::RunLoop().RunUntilIdle();

  // Only authority1 should be notified and all other authorities should be
  // ignored.
  EXPECT_EQ(1u, observer().added_authorities().size());
  EXPECT_EQ(0u, observer().removed_authorities().size());
}

TEST_F(DocumentsProviderRootManagerTest, DisableRootManager) {
  AddFakeRoot("authority1", "123", "", "", 10, 100);
  AddFakeRoot("authority2", "456", "", "", 100, 1000);
  root_manager_->SetEnabled(true);
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(2u, observer().added_authorities().size());
  EXPECT_EQ(0u, observer().removed_authorities().size());
  observer().Reset();

  // When disabled, OnDocumentsProviderRootRemoved() should be called for each
  // existing root to clear them from the Files app UI.
  root_manager_->SetEnabled(false);
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(0u, observer().added_authorities().size());
  EXPECT_EQ(2u, observer().removed_authorities().size());
}

TEST_F(DocumentsProviderRootManagerTest, DoNotNotifyUnchangedRoot) {
  AddFakeRoot("authority1", "123", "", "", 10, 100);
  AddFakeRoot("authority2", "456", "", "", 100, 1000);
  root_manager_->SetEnabled(true);
  base::RunLoop().RunUntilIdle();

  // Root list is now ["authority1", "authority2"].
  EXPECT_EQ(2u, observer().added_authorities().size());
  EXPECT_EQ(0u, observer().removed_authorities().size());
  observer().Reset();

  AddFakeRoot("authority3", "789", "", "", 1000, 10000);
  root_manager_->OnRootsChanged();
  base::RunLoop().RunUntilIdle();

  // Root list is now ["authority1", "authority2", "authority3"].
  // Only authority3 should be notified by the OnRootsChanged() above.
  // (authority1, authority2 should not be notified multiple times)
  EXPECT_EQ(1u, observer().added_authorities().size());
  EXPECT_EQ("authority3", observer().added_authorities()[0]);
  EXPECT_EQ(0u, observer().removed_authorities().size());
}

}  // namespace file_manager