File: support_tool_util_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (142 lines) | stat: -rw-r--r-- 5,797 bytes parent folder | download | duplicates (3)
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
// Copyright 2023 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 <string>
#include <vector>

#include "base/check.h"
#include "base/files/file_path.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/profile_policy_connector_builder.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/support_tool/data_collection_module.pb.h"
#include "chrome/browser/support_tool/support_tool_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/login/login_manager_test.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_helper.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

class Profile;

namespace {

// Support packet details for testing.
constexpr char kCaseId[] = "case-id";
constexpr char kEmail[] = "test@test.com";
constexpr char kIssueDescription[] = "fake issue description";
constexpr char kUploadId[] = "testing_id";

class SupportToolUtilTest : public InProcessBrowserTest {
 public:
  SupportToolUtilTest() = default;

  void SetUpInProcessBrowserTestFixture() override {
    // Set-up policy provider for PolicyDataCollector to work.
    policy_provider_.SetDefaultReturns(
        /*is_initialization_complete_return=*/true,
        /*is_first_policy_load_complete_return=*/true);
    policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
        &policy_provider_);
    policy::PushProfilePolicyConnectorProviderForTesting(&policy_provider_);
    InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
  }

 protected:
  testing::NiceMock<policy::MockConfigurationPolicyProvider> policy_provider_;
};

#if BUILDFLAG(IS_CHROMEOS)

class SupportToolUtilLoginScreenTest : public ash::LoginManagerTest {
 public:
  SupportToolUtilLoginScreenTest() = default;
};

#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace

// Verifies that `GetFilepathToExport()` function works.
IN_PROC_BROWSER_TEST_F(SupportToolUtilTest, GetFilepathToExport) {
  // Time for testing.
  base::Time time;
  ASSERT_TRUE(base::Time::FromUTCString("30 April 2011 22:42:09", &time));

  // The returned filename should be in format of
  // <filename_prefix>_<case_id>_UTCYYYYMMDD_HHmm.
  const base::FilePath kExpectedFilepath =
      base::FilePath(FILE_PATH_LITERAL("target_directory"))
          .Append(
              FILE_PATH_LITERAL("test-file-prefix_case-id_UTC20110430_2242"));
  EXPECT_EQ(
      kExpectedFilepath,
      GetFilepathToExport(base::FilePath(FILE_PATH_LITERAL("target_directory")),
                          "test-file-prefix", "case-id", time));

  // Verify that case ID field is not included when an empty string is given.
  const base::FilePath kExpectedFilepathWithoutCaseId =
      base::FilePath(FILE_PATH_LITERAL("target_directory"))
          .Append(FILE_PATH_LITERAL("test-file-prefix_UTC20110430_2242"));
  EXPECT_EQ(
      kExpectedFilepathWithoutCaseId,
      GetFilepathToExport(base::FilePath(FILE_PATH_LITERAL("target_directory")),
                          "test-file-prefix", std::string(), time));
}

// Verifies that all data collectors available are added to
// `SupportToolHandler`.
IN_PROC_BROWSER_TEST_F(SupportToolUtilTest, GetSupportToolHandler) {
  std::vector<support_tool::DataCollectorType> data_collectors =
      GetAllAvailableDataCollectorsOnDevice();

  std::unique_ptr<SupportToolHandler> handler = GetSupportToolHandler(
      kCaseId, kEmail, kIssueDescription, kUploadId, browser()->profile(),
      std::set<support_tool::DataCollectorType>(data_collectors.begin(),
                                                data_collectors.end()));
  EXPECT_EQ(data_collectors.size(),
            handler->GetDataCollectorsForTesting().size());
}

#if BUILDFLAG(IS_CHROMEOS)
// Verifies that all data collectors available on login screen are added to
// `SupportToolHandler`.
IN_PROC_BROWSER_TEST_F(SupportToolUtilLoginScreenTest, GetSupportToolHandler) {
  Profile* signin_profile = Profile::FromBrowserContext(
      ash::BrowserContextHelper::Get()->GetSigninBrowserContext());
  ASSERT_TRUE(signin_profile);

  std::vector<support_tool::DataCollectorType> all_data_collectors =
      GetAllAvailableDataCollectorsOnDevice();
  // These data collectors shouldn't be included on login screen because they
  // depend on data from user session.
  std::set<support_tool::DataCollectorType> excluded_data_collectors = {
      support_tool::DataCollectorType::CHROMEOS_CHROME_USER_LOGS,
      support_tool::DataCollectorType::SIGN_IN_STATE};

  std::unique_ptr<SupportToolHandler> handler = GetSupportToolHandler(
      kCaseId, kEmail, kIssueDescription, kUploadId, signin_profile,
      std::set<support_tool::DataCollectorType>(all_data_collectors.begin(),
                                                all_data_collectors.end()));
  EXPECT_EQ(all_data_collectors.size() - excluded_data_collectors.size(),
            handler->GetDataCollectorsForTesting().size());

  // Verify that the data collectors are excluded when they're not supported on
  // login screen.
  handler = GetSupportToolHandler(kCaseId, kEmail, kIssueDescription, kUploadId,
                                  signin_profile, excluded_data_collectors);
  EXPECT_EQ(0U, handler->GetDataCollectorsForTesting().size());
}

#endif  // BUILDFLAG(IS_CHROMEOS)