File: devtools_file_system_indexer_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 (86 lines) | stat: -rw-r--r-- 2,954 bytes parent folder | download | duplicates (10)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <set>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "chrome/browser/devtools/devtools_file_system_indexer.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

class DevToolsFileSystemIndexerTest : public testing::Test {
 public:
  void SetDone(base::OnceClosure quit_closure) {
    indexing_done_ = true;
    std::move(quit_closure).Run();
  }

  void SearchCallback(base::OnceClosure quit_closure,
                      const std::vector<std::string>& results) {
    search_results_.clear();
    for (const std::string& result : results) {
      search_results_.insert(
          base::FilePath::FromUTF8Unsafe(result).BaseName().AsUTF8Unsafe());
    }
    std::move(quit_closure).Run();
  }

 protected:
  void SetUp() override {
    indexer_ = new DevToolsFileSystemIndexer();
    indexing_done_ = false;
  }

  content::BrowserTaskEnvironment task_environment_;
  scoped_refptr<DevToolsFileSystemIndexer> indexer_;
  std::set<std::string> search_results_;
  bool indexing_done_;
};

TEST_F(DevToolsFileSystemIndexerTest, BasicUsage) {
  base::FilePath base_test_path;
  base::RunLoop loop;
  base::PathService::Get(chrome::DIR_TEST_DATA, &base_test_path);
  base::FilePath index_path =
      base_test_path.Append(FILE_PATH_LITERAL("devtools"))
          .Append(FILE_PATH_LITERAL("indexer"));

  std::vector<std::string> excluded_folders;
  scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob> job =
      indexer_->IndexPath(
          index_path.AsUTF8Unsafe(), excluded_folders, base::DoNothing(),
          base::DoNothing(),
          base::BindOnce(&DevToolsFileSystemIndexerTest::SetDone,
                         base::Unretained(this), loop.QuitWhenIdleClosure()));

  loop.Run();
  ASSERT_TRUE(indexing_done_);

  base::RunLoop loop1;
  indexer_->SearchInPath(
      index_path.AsUTF8Unsafe(), "Hello",
      base::BindOnce(&DevToolsFileSystemIndexerTest::SearchCallback,
                     base::Unretained(this), loop1.QuitWhenIdleClosure()));
  loop1.Run();

  ASSERT_EQ(3lu, search_results_.size());
  ASSERT_EQ(1lu, search_results_.count("hello_world.c"));
  ASSERT_EQ(1lu, search_results_.count("hello_world.html"));
  ASSERT_EQ(1lu, search_results_.count("hello_world.js"));

  base::RunLoop loop2;
  indexer_->SearchInPath(
      index_path.AsUTF8Unsafe(), "FUNCTION",
      base::BindOnce(&DevToolsFileSystemIndexerTest::SearchCallback,
                     base::Unretained(this), loop2.QuitWhenIdleClosure()));
  loop2.Run();

  ASSERT_EQ(1lu, search_results_.size());
  ASSERT_EQ(1lu, search_results_.count("hello_world.js"));
}