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

#include "chrome/browser/ash/guest_os/guest_os_terminal.h"

#include "base/json/json_reader.h"
#include "base/values.h"
#include "chrome/browser/ash/crostini/crostini_pref_names.h"
#include "chrome/browser/ash/crostini/crostini_util.h"
#include "chrome/browser/ash/guest_os/guest_id.h"
#include "chrome/browser/ash/guest_os/guest_os_pref_names.h"
#include "chrome/browser/ash/guest_os/public/types.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"

namespace guest_os {

using CrostiniTerminalTest = testing::Test;

TEST_F(CrostiniTerminalTest, GenerateTerminalURL) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  EXPECT_EQ(
      GenerateTerminalURL(&profile, "", crostini::DefaultContainerId(), "", {}),
      "chrome-untrusted://terminal/html/terminal.html"
      "?command=vmshell"
      "&args[]=--vm_name%3Dtermina"
      "&args[]=--target_container%3Dpenguin"
      "&args[]=--owner_id%3Dtest");
  EXPECT_EQ(GenerateTerminalURL(
                &profile, "red",
                guest_os::GuestId(VmType::TERMINA, "test-vm", "test-container"),
                "/home/user", {"arg1"}),
            "chrome-untrusted://terminal/html/terminal.html"
            "?command=vmshell"
            "&settings_profile=red"
            "&args[]=--vm_name%3Dtest-vm"
            "&args[]=--target_container%3Dtest-container"
            "&args[]=--owner_id%3Dtest"
            "&args[]=--cwd%3D%2Fhome%2Fuser"
            "&args[]=--&args[]=arg1");
}

TEST_F(CrostiniTerminalTest, ShortcutIdForSSH) {
  EXPECT_EQ(ShortcutIdForSSH("test-profile"),
            R"({"profileId":"test-profile","shortcut":"ssh"})");
}

TEST_F(CrostiniTerminalTest, ShortcutIdFromContainerId) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  guest_os::GuestId id(VmType::TERMINA, "test-vm", "test-container");
  std::string shortcut = ShortcutIdFromContainerId(&profile, id);
  EXPECT_EQ(shortcut, R"({"container_name":"test-container",)"
                      R"("shortcut":"terminal",)"
                      R"("vm_name":"test-vm",)"
                      R"("vm_type":0})");
  auto extras = ExtrasFromShortcutId(*base::JSONReader::ReadDict(shortcut));
  EXPECT_EQ(3u, extras.size());

  // Container with multi-profile should include settings_profile.
  auto pref = base::JSONReader::Read(R"({
    "/vsh/profile-ids": ["p1", "p2"],
    "/vsh/profiles/p1/vm-name": "test-vm",
    "/vsh/profiles/p1/container-name": "other-container",
    "/vsh/profiles/p1/terminal-profile": "red",
    "/vsh/profiles/p2/vm-name": "test-vm",
    "/vsh/profiles/p2/container-name": "test-container",
    "/vsh/profiles/p2/terminal-profile": "green"
  })");
  ASSERT_TRUE(pref.has_value());
  profile.GetPrefs()->Set(guest_os::prefs::kGuestOsTerminalSettings,
                          std::move(*pref));
  shortcut = ShortcutIdFromContainerId(&profile, id);
  EXPECT_EQ(shortcut, R"({"container_name":"test-container",)"
                      R"("settings_profile":"green",)"
                      R"("shortcut":"terminal",)"
                      R"("vm_name":"test-vm",)"
                      R"("vm_type":0})");
  extras = ExtrasFromShortcutId(*base::JSONReader::ReadDict(shortcut));
  EXPECT_EQ(4u, extras.size());
}

TEST_F(CrostiniTerminalTest, GetSSHConnections) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;

  std::vector<std::pair<std::string, std::string>> expected;
  EXPECT_EQ(GetSSHConnections(&profile), expected);

  auto pref = base::JSONReader::Read(R"({
    "/nassh/profile-ids": ["p1", "p2", "p3"],
    "/nassh/profiles/p1/description": "d1",
    "/nassh/profiles/p2/description": "d2"
  })");
  ASSERT_TRUE(pref.has_value());
  profile.GetPrefs()->Set(guest_os::prefs::kGuestOsTerminalSettings,
                          std::move(*pref));

  expected = {{"p1", "d1"}, {"p2", "d2"}};
  EXPECT_EQ(GetSSHConnections(&profile), expected);
}

TEST_F(CrostiniTerminalTest, GetTerminalSettingBackgroundColor) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;

  auto pref = base::JSONReader::Read(R"({
    "/hterm/profiles/default/background-color": "#101010",
    "/hterm/profiles/red/background-color": "#FF0000"
  })");
  ASSERT_TRUE(pref.has_value());
  profile.GetPrefs()->Set(guest_os::prefs::kGuestOsTerminalSettings,
                          std::move(*pref));

  // Use settings_profile param.
  EXPECT_EQ(GetTerminalSettingBackgroundColor(
                &profile,
                GURL("chrome-untrusted://terminal/html/"
                     "terminal.html?settings_profile=red"),
                SK_ColorGREEN),
            "#FF0000");

  // Use opener color.
  EXPECT_EQ(
      GetTerminalSettingBackgroundColor(
          &profile, GURL("chrome-untrusted://terminal/html/terminal.html"),
          SK_ColorGREEN),
      "#00ff00ff");

  // Use color from 'default' profile.
  EXPECT_EQ(
      GetTerminalSettingBackgroundColor(
          &profile, GURL("chrome-untrusted://terminal/html/terminal.html"),
          std::nullopt),
      "#101010");

  // Use default color.
  profile.GetPrefs()->SetDict(guest_os::prefs::kGuestOsTerminalSettings,
                              base::Value::Dict());
  EXPECT_EQ(
      GetTerminalSettingBackgroundColor(
          &profile, GURL("chrome-untrusted://terminal/html/terminal.html"),
          std::nullopt),
      "#202124");
}

}  // namespace guest_os