1// Copyright (c) 2012 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.
4
5#include <string>
6#include <vector>
7
8#include "base/bind.h"
9#include "base/files/file_path.h"
10#include "chrome/test/chromedriver/chrome/chrome_finder.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace {
14
15bool PathIn(const std::vector<base::FilePath>& list,
16            const base::FilePath& path) {
17  for (size_t i = 0; i < list.size(); ++i) {
18    if (list[i] == path)
19      return true;
20  }
21  return false;
22}
23
24void AssertFound(const base::FilePath& found,
25                 const std::vector<base::FilePath>& existing_paths,
26                 const std::vector<base::FilePath>& rel_paths,
27                 const std::vector<base::FilePath>& locations) {
28  base::FilePath exe;
29  ASSERT_TRUE(internal::FindExe(
30      base::Bind(&PathIn, existing_paths),
31      rel_paths,
32      locations,
33      &exe));
34  ASSERT_EQ(found, exe);
35}
36
37}  // namespace
38
39TEST(ChromeFinderTest, FindExeFound) {
40  base::FilePath found =
41      base::FilePath().AppendASCII("exists").AppendASCII("exists");
42  std::vector<base::FilePath> existing_paths;
43  existing_paths.push_back(found);
44  std::vector<base::FilePath> rel_paths;
45  rel_paths.push_back(found.BaseName());
46  std::vector<base::FilePath> locations;
47  locations.push_back(found.DirName());
48  ASSERT_NO_FATAL_FAILURE(
49      AssertFound(found, existing_paths, rel_paths, locations));
50}
51
52TEST(ChromeFinderTest, FindExeShouldGoInOrder) {
53  base::FilePath dir(FILE_PATH_LITERAL("dir"));
54  base::FilePath first = dir.AppendASCII("first");
55  base::FilePath second = dir.AppendASCII("second");
56  std::vector<base::FilePath> existing_paths;
57  existing_paths.push_back(first);
58  existing_paths.push_back(second);
59  std::vector<base::FilePath> rel_paths;
60  rel_paths.push_back(first.BaseName());
61  rel_paths.push_back(second.BaseName());
62  std::vector<base::FilePath> locations;
63  locations.push_back(dir);
64  ASSERT_NO_FATAL_FAILURE(
65      AssertFound(first, existing_paths, rel_paths, locations));
66}
67
68TEST(ChromeFinderTest, FindExeShouldPreferExeNameOverDir) {
69  base::FilePath dir1(FILE_PATH_LITERAL("dir1"));
70  base::FilePath dir2(FILE_PATH_LITERAL("dir2"));
71  base::FilePath preferred(FILE_PATH_LITERAL("preferred"));
72  base::FilePath nonpreferred(FILE_PATH_LITERAL("nonpreferred"));
73  std::vector<base::FilePath> existing_paths;
74  existing_paths.push_back(dir2.Append(preferred));
75  existing_paths.push_back(dir1.Append(nonpreferred));
76  std::vector<base::FilePath> rel_paths;
77  rel_paths.push_back(preferred);
78  rel_paths.push_back(nonpreferred);
79  std::vector<base::FilePath> locations;
80  locations.push_back(dir1);
81  locations.push_back(dir2);
82  ASSERT_NO_FATAL_FAILURE(AssertFound(
83      dir2.Append(preferred), existing_paths, rel_paths, locations));
84}
85
86TEST(ChromeFinderTest, FindExeNotFound) {
87  base::FilePath found =
88      base::FilePath().AppendASCII("exists").AppendASCII("exists");
89  std::vector<base::FilePath> existing_paths;
90  std::vector<base::FilePath> rel_paths;
91  rel_paths.push_back(found.BaseName());
92  std::vector<base::FilePath> locations;
93  locations.push_back(found.DirName());
94  base::FilePath exe;
95  ASSERT_FALSE(internal::FindExe(
96      base::Bind(&PathIn, existing_paths),
97      rel_paths,
98      locations,
99      &exe));
100}
101
102TEST(ChromeFinderTest, NoCrash) {
103  // It's not worthwhile to check the validity of the path, so just check
104  // for crashes.
105  base::FilePath exe;
106  FindChrome(&exe);
107}
108