summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_galleries/media_folder_finder_unittest.cc
blob: b994956bfdbd671f2e2c37b82d1afdd63568782b (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media_galleries/media_folder_finder.h"

#include <set>
#include <string>

#include "base/base_paths.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_path_override.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/media_galleries/media_scan_types.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"

class MediaFolderFinderTest : public testing::Test {
 public:
  MediaFolderFinderTest() {
  }

  virtual ~MediaFolderFinderTest() {
  }

  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(fake_dir_.CreateUniqueTempDir());
  }

  virtual void TearDown() OVERRIDE {
    ASSERT_EQ(NULL, media_folder_finder_.get());
  }

 protected:
  void CreateMediaFolderFinder(
      const std::vector<base::FilePath> roots,
      bool expected_success,
      const MediaFolderFinder::MediaFolderFinderResults& expected_results) {
    EXPECT_EQ(NULL, media_folder_finder_.get());
    received_results_ = false;
    expected_success_ = expected_success;
    expected_results_ = expected_results;
    media_folder_finder_.reset(
        new MediaFolderFinder(base::Bind(&MediaFolderFinderTest::OnGotResults,
                                         base::Unretained(this))));
    media_folder_finder_->SetRootsForTesting(roots);
  }

  void StartScan() {
    media_folder_finder_->StartScan();
  }

  void DeleteMediaFolderFinder() {
    EXPECT_TRUE(media_folder_finder_.get() != NULL);
    media_folder_finder_.reset();
  }

  bool received_results() const {
    return received_results_;
  }

  const base::FilePath& fake_dir() const {
    return fake_dir_.path();
  }

  void CreateTestDir(const base::FilePath& parent_dir) {
    if (parent_dir == fake_dir())
      return;

    ASSERT_TRUE(fake_dir().IsParent(parent_dir));
    ASSERT_TRUE(base::CreateDirectory(parent_dir));
  }

  void CreateTestFile(const base::FilePath& parent_dir,
                      MediaGalleryScanFileType type,
                      size_t count,
                      bool big,
                      MediaFolderFinder::MediaFolderFinderResults* results) {
    CreateTestDir(parent_dir);

    std::string extension;
    size_t filesize;
    MediaGalleryScanResult& result = (*results)[parent_dir];
    switch (type) {
      case MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE:
        extension = "jpg";
        filesize = 10;
        result.image_count += count;
        break;
      case MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO:
        extension = "wav";
        filesize = 20;
        result.audio_count += count;
        break;
      case MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO:
        extension = "avi";
        filesize = 30;
        result.video_count += count;
        break;
      case MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN:
        extension = "txt";
        filesize = 10;
        if (IsEmptyScanResult(result))
          results->erase(parent_dir);
        break;
      default:
        NOTREACHED();
        return;
    }
    if (big)
      filesize *= 100000;

    for (size_t i = 0; i < count; ++i) {
      base::FilePath test_file(parent_dir.AppendASCII("dummy." + extension));
      int uniquifier =
          base::GetUniquePathNumber(test_file, base::FilePath::StringType());
      if (uniquifier > 0) {
        test_file = test_file.InsertBeforeExtensionASCII(
            base::StringPrintf(" (%d)", uniquifier));
        filesize += uniquifier;
      }

      std::string dummy_data;
      dummy_data.resize(filesize);

      int bytes_written =
          base::WriteFile(test_file, dummy_data.c_str(), filesize);
      ASSERT_GE(bytes_written, 0);
      ASSERT_EQ(filesize, static_cast<size_t>(bytes_written));
    }
  }

  void RunLoopUntilReceivedCallback() {
    while (!received_results())
      content::RunAllBlockingPoolTasksUntilIdle();
  }

 private:
  void OnGotResults(
      bool success,
      const MediaFolderFinder::MediaFolderFinderResults& results) {
    received_results_ = true;
    EXPECT_EQ(expected_success_, success);
    std::set<base::FilePath> expected_keys =
        GetKeysFromResults(expected_results_);
    ASSERT_EQ(expected_keys, GetKeysFromResults(results));
    for (MediaFolderFinder::MediaFolderFinderResults::const_iterator it =
             results.begin();
         it != results.end(); ++it) {
      const base::FilePath& folder = it->first;
      const MediaGalleryScanResult& expected = it->second;
      const MediaGalleryScanResult& actual = results.find(folder)->second;
      EXPECT_EQ(expected.image_count, actual.image_count)
          << " Image count for " << folder.value();
      EXPECT_EQ(expected.audio_count, actual.audio_count)
          << " Audio count for " << folder.value();
      EXPECT_EQ(expected.video_count, actual.video_count)
          << " Video count for " << folder.value();
    }
  }

  std::set<base::FilePath> GetKeysFromResults(
      const MediaFolderFinder::MediaFolderFinderResults& results) {
    std::set<base::FilePath> keys;
    for (MediaFolderFinder::MediaFolderFinderResults::const_iterator it =
             results.begin();
         it != results.end(); ++it) {
      keys.insert(it->first);
    }
    return keys;
  }

  content::TestBrowserThreadBundle thread_bundle_;

  base::ScopedTempDir fake_dir_;

  scoped_ptr<MediaFolderFinder> media_folder_finder_;

  bool expected_success_;
  MediaFolderFinder::MediaFolderFinderResults expected_results_;
  bool received_results_;

  DISALLOW_COPY_AND_ASSIGN(MediaFolderFinderTest);
};

TEST_F(MediaFolderFinderTest, NoScan) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());
  CreateMediaFolderFinder(folders, false, expected_results);
  DeleteMediaFolderFinder();
  EXPECT_TRUE(received_results());
}

TEST_F(MediaFolderFinderTest, ScanAndCancel) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());
  CreateMediaFolderFinder(folders, false, expected_results);
  StartScan();
  DeleteMediaFolderFinder();
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_TRUE(received_results());
}

TEST_F(MediaFolderFinderTest, ScanNothing) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  CreateMediaFolderFinder(folders, true, expected_results);
  StartScan();
  RunLoopUntilReceivedCallback();
  DeleteMediaFolderFinder();
}

TEST_F(MediaFolderFinderTest, EmptyScan) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());
  CreateMediaFolderFinder(folders, true, expected_results);
  StartScan();
  RunLoopUntilReceivedCallback();
  DeleteMediaFolderFinder();
}

TEST_F(MediaFolderFinderTest, ScanMediaFiles) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());

  base::FilePath dir1 = fake_dir().AppendASCII("dir1");
  base::FilePath dir2 = fake_dir().AppendASCII("dir2");
  base::FilePath dir2_3 = dir2.AppendASCII("dir2_3");
  base::FilePath dir2_4 = dir2.AppendASCII("dir2_4");
  base::FilePath dir2_4_5 = dir2_4.AppendASCII("dir2_4_5");
  base::FilePath dir2_4_empty = dir2_4.AppendASCII("dir2_4_empty");
  base::FilePath dir_empty = fake_dir().AppendASCII("dir_empty");

  CreateTestFile(dir1, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 2, true,
                 &expected_results);
  CreateTestFile(dir1, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, false,
                 &expected_results);
  CreateTestFile(dir1, MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN, 1, false,
                 &expected_results);
  CreateTestFile(dir2_3, MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO, 4, true,
                 &expected_results);
  CreateTestFile(dir2_3, MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO, 3, false,
                 &expected_results);
  CreateTestFile(dir2_4, MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN, 5, false,
                 &expected_results);
  CreateTestFile(dir2_4_5, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 2, true,
                 &expected_results);
  CreateTestFile(dir2_4_5, MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO, 4, true,
                 &expected_results);
  CreateTestFile(dir2_4_5, MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO, 1, true,
                 &expected_results);
  CreateTestFile(dir2_4_5, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 5, false,
                 &expected_results);
  CreateTestFile(dir2_4_5, MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO, 3, false,
                 &expected_results);
  CreateTestFile(dir2_4_5, MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN, 3, true,
                 &expected_results);
  CreateTestDir(dir2_4_empty);
  CreateTestDir(dir_empty);

  CreateMediaFolderFinder(folders, true, expected_results);
  StartScan();
  RunLoopUntilReceivedCallback();
  DeleteMediaFolderFinder();
}

TEST_F(MediaFolderFinderTest, SkipHiddenFiles) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());

  base::FilePath hidden_dir = fake_dir().AppendASCII(".hidden");

  CreateTestFile(hidden_dir, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 2, true,
                 &expected_results);
  expected_results.erase(hidden_dir);

  CreateMediaFolderFinder(folders, true, expected_results);
  StartScan();
  RunLoopUntilReceivedCallback();
  DeleteMediaFolderFinder();
}

TEST_F(MediaFolderFinderTest, ScanIgnoresSmallMediaFiles) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());

  base::FilePath dir1 = fake_dir().AppendASCII("dir1");
  base::FilePath dir2 = fake_dir().AppendASCII("dir2");
  base::FilePath dir_empty = fake_dir().AppendASCII("dir_empty");

  CreateTestFile(dir1, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 2, true,
                 &expected_results);
  CreateTestFile(dir1, MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO, 1, false,
                 &expected_results);
  CreateTestFile(dir1, MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN, 1, false,
                 &expected_results);
  CreateTestFile(dir2, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, false,
                 &expected_results);
  CreateTestFile(dir2, MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO, 3, false,
                 &expected_results);
  CreateTestFile(dir2, MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO, 5, false,
                 &expected_results);
  CreateTestFile(dir2, MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN, 1, true,
                 &expected_results);
  CreateTestDir(dir_empty);
  ASSERT_EQ(1U, expected_results.erase(dir2));

  CreateMediaFolderFinder(folders, true, expected_results);
  StartScan();
  RunLoopUntilReceivedCallback();
  DeleteMediaFolderFinder();
}

TEST_F(MediaFolderFinderTest, Overlap) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());
  folders.push_back(fake_dir());

  base::FilePath dir1 = fake_dir().AppendASCII("dir1");
  folders.push_back(dir1);
  folders.push_back(dir1);

  CreateTestFile(dir1, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, true,
                 &expected_results);

  CreateMediaFolderFinder(folders, true, expected_results);
  StartScan();
  RunLoopUntilReceivedCallback();
  DeleteMediaFolderFinder();
}

TEST_F(MediaFolderFinderTest, Prune) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());

#if defined(OS_WIN)
  int pruned_dir_key = base::DIR_IE_INTERNET_CACHE;
#elif defined(OS_MACOSX)
  int pruned_dir_key = chrome::DIR_USER_LIBRARY;
#else
  int pruned_dir_key = base::DIR_CACHE;
#endif

  base::FilePath fake_pruned_dir = fake_dir().AppendASCII("dir1");
  base::ScopedPathOverride scoped_fake_pruned_dir_override(pruned_dir_key,
                                                           fake_pruned_dir);

  CreateTestFile(fake_dir(), MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, true,
                 &expected_results);
  CreateTestFile(fake_pruned_dir, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, true,
                 &expected_results);

  base::FilePath test_dir = fake_pruned_dir.AppendASCII("dir2");
  CreateTestFile(test_dir, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, true,
                 &expected_results);

  // |fake_pruned_dir| and |test_dir| are pruned.
  expected_results.erase(fake_pruned_dir);
  expected_results.erase(test_dir);

  CreateMediaFolderFinder(folders, true, expected_results);
  StartScan();
  RunLoopUntilReceivedCallback();
  DeleteMediaFolderFinder();
}

TEST_F(MediaFolderFinderTest, Graylist) {
  MediaFolderFinder::MediaFolderFinderResults expected_results;
  std::vector<base::FilePath> folders;
  folders.push_back(fake_dir());

  base::FilePath fake_home_dir = fake_dir().AppendASCII("dir1");
  base::FilePath test_dir = fake_home_dir.AppendASCII("dir2");
  base::ScopedPathOverride scoped_fake_home_dir_override(base::DIR_HOME,
                                                         fake_home_dir);

  CreateTestFile(fake_dir(), MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, true,
                 &expected_results);
  CreateTestFile(fake_home_dir, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, true,
                 &expected_results);
  CreateTestFile(test_dir, MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE, 1, true,
                 &expected_results);

  // |fake_home_dir| and its ancestors do not show up in results.
  expected_results.erase(fake_dir());
  expected_results.erase(fake_home_dir);

  CreateMediaFolderFinder(folders, true, expected_results);
  StartScan();
  RunLoopUntilReceivedCallback();
  DeleteMediaFolderFinder();
}