blob: fba7b990f73bb8e6c9b7b308009ff74f0d472898 [file] [log] [blame]
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package crostini
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"time"
"chromiumos/tast/errors"
"chromiumos/tast/local/apps"
"chromiumos/tast/local/chrome"
"chromiumos/tast/local/chrome/uiauto"
"chromiumos/tast/local/chrome/uiauto/filesapp"
"chromiumos/tast/local/chrome/uiauto/nodewith"
"chromiumos/tast/local/chrome/uiauto/role"
"chromiumos/tast/local/crostini"
"chromiumos/tast/local/input"
"chromiumos/tast/testing"
)
func init() {
testing.AddTest(&testing.Test{
Func: CopyFilesToLinuxFiles,
LacrosStatus: testing.LacrosVariantUnneeded,
Desc: "Tests copying files to Linux files using a pre-built crostini image",
Contacts: []string{"jinrongwu@google.com", "cros-containers-dev@google.com"},
Attr: []string{"group:mainline", "informational"},
SoftwareDeps: []string{"chrome", "vm_host"},
Params: []testing.Param{
// Parameters generated by params_test.go. DO NOT EDIT.
{
Name: "buster_stable",
ExtraSoftwareDeps: []string{"dlc"},
ExtraHardwareDeps: crostini.CrostiniStable,
Fixture: "crostiniBuster",
Timeout: 7 * time.Minute,
}, {
Name: "buster_unstable",
ExtraAttr: []string{"informational"},
ExtraSoftwareDeps: []string{"dlc"},
ExtraHardwareDeps: crostini.CrostiniUnstable,
Fixture: "crostiniBuster",
Timeout: 7 * time.Minute,
}, {
Name: "bullseye_stable",
ExtraSoftwareDeps: []string{"dlc"},
ExtraHardwareDeps: crostini.CrostiniStable,
Fixture: "crostiniBullseye",
Timeout: 7 * time.Minute,
}, {
Name: "bullseye_unstable",
ExtraAttr: []string{"informational"},
ExtraSoftwareDeps: []string{"dlc"},
ExtraHardwareDeps: crostini.CrostiniUnstable,
Fixture: "crostiniBullseye",
Timeout: 7 * time.Minute,
},
},
})
}
func CopyFilesToLinuxFiles(ctx context.Context, s *testing.State) {
tconn := s.FixtValue().(crostini.FixtureData).Tconn
cont := s.FixtValue().(crostini.FixtureData).Cont
keyboard := s.FixtValue().(crostini.FixtureData).KB
if err := cont.Cleanup(ctx, "."); err != nil {
s.Fatal("Failed to cleanup the home directory before the test: ", err)
}
// Open Files app.
filesApp, err := filesapp.Launch(ctx, tconn)
if err != nil {
s.Fatal("Failed to open Files app: ", err)
}
testFiles := []string{"testfile1.txt", "testfile2.txt", "testfile3.txt"}
s.Log("Test copying files to Linux files")
// Create some files in Downloads.
for _, file := range testFiles {
path := filepath.Join(filesapp.DownloadPath, file)
if err := ioutil.WriteFile(path, []byte("test"), 0644); err != nil {
s.Fatal("Failed to create file in Downloads: ", err)
}
defer os.Remove(path)
}
// Copy files from Downloads to Linux files.
if err := copyFilesToLinuxfiles(ctx, tconn, filesApp, keyboard, testFiles); err != nil {
s.Fatal("Failed to copy test files to Linux files: ", err)
}
// Check the file list in home directory is equal to the copied file list.
fileList, err := cont.GetFileList(ctx, ".")
if err != nil {
s.Fatal("Failed to get files in home directory in container: ", err)
}
if !reflect.DeepEqual(testFiles, fileList) {
s.Fatalf("Found unexpected files in Linux files; got %q, want %q", fileList, testFiles)
}
}
// copyFilesToLinuxfiles copies all files in Downloads to Linux files.
func copyFilesToLinuxfiles(ctx context.Context, tconn *chrome.TestConn, filesApp *filesapp.FilesApp, keyboard *input.KeyboardEventWriter, testFiles []string) error {
steps := []uiauto.Action{filesApp.OpenDownloads()}
// Steps to wait all files to display.
for _, file := range testFiles {
steps = append(steps, filesApp.SelectFile(file))
}
copyMsg := nodewith.Name(fmt.Sprintf("Copying %d items to %s", len(testFiles), "Linux files")).Role(role.StaticText).Ancestor(filesapp.WindowFinder(apps.Files.ID))
ui := uiauto.New(tconn)
steps = append(steps,
// Select all files.
keyboard.AccelAction("ctrl+A"),
// Copy all files.
keyboard.AccelAction("ctrl+C"),
// Open "Linux files" to paste.
filesApp.OpenLinuxFiles(),
// Paste all files.
keyboard.AccelAction("ctrl+V"))
if err := uiauto.Combine("copy files from Downloads to Linux files", steps...)(ctx); err != nil {
return err
}
if err := ui.WithTimeout(10 * time.Second).WaitUntilExists(copyMsg)(ctx); err != nil {
testing.ContextLog(ctx, "Copying message was not found")
}
if err := ui.WithTimeout(time.Minute).WaitUntilGone(copyMsg)(ctx); err != nil {
return errors.Wrap(err, "failed to copy files to Linux files in 1 minute")
}
return nil
}