blob: 45050d6c0f1ba554f0e108b1f67e8289d53e34d4 [file] [log] [blame]
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +09001// Copyright 2018 The Chromium OS 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 "crash-reporter/test_util.h"
6
Tim Zheng2d09b952019-03-08 16:20:59 -08007#include <base/files/file_enumerator.h>
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +09008#include <base/files/file_util.h>
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +09009#include <gtest/gtest.h>
10
11using testing::Invoke;
12using testing::_;
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090013
14namespace test_util {
15
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090016namespace {
17
18std::map<std::string, std::string>* g_active_sessions;
19
20// Implementation of
21// SessionManagerInterfaceProxyMock::RetrieveActiveSessions().
22bool RetrieveActiveSessionsImpl(
23 std::map<std::string, std::string>* out_sessions,
24 brillo::ErrorPtr* error,
25 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
26 DCHECK(g_active_sessions); // Set in SetActiveSessions().
27 *out_sessions = *g_active_sessions;
28 return true;
29}
30
31} // namespace
32
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090033bool CreateFile(const base::FilePath& file_path, base::StringPiece content) {
34 if (!base::CreateDirectory(file_path.DirName()))
35 return false;
36 return base::WriteFile(file_path, content.data(), content.size()) ==
37 content.size();
38}
39
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090040void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock,
41 const std::map<std::string, std::string>& sessions) {
42 if (g_active_sessions)
43 delete g_active_sessions;
44 g_active_sessions = new std::map<std::string, std::string>(sessions);
45
46 EXPECT_CALL(*mock, RetrieveActiveSessions(_, _, _))
47 .WillRepeatedly(Invoke(&RetrieveActiveSessionsImpl));
48}
49
Tim Zheng2d09b952019-03-08 16:20:59 -080050bool DirectoryHasFileWithPattern(const base::FilePath& directory,
51 const std::string& pattern,
52 base::FilePath* found_file_path) {
53 base::FileEnumerator enumerator(
54 directory, false, base::FileEnumerator::FileType::FILES, pattern);
55 base::FilePath path = enumerator.Next();
56 if (!path.empty() && found_file_path)
57 *found_file_path = path;
58 return !path.empty();
59}
60
Chris Morin92419a72019-06-19 12:35:22 -070061base::FilePath GetTestDataPath(const std::string& name) {
62 return base::FilePath(getenv("SRC")).Append(name);
63}
64
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090065} // namespace test_util