github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/usersession/userd/ui/kdialog_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package ui_test
    21  
    22  import (
    23  	"fmt"
    24  	"os/exec"
    25  	"path/filepath"
    26  	"time"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/testutil"
    31  	"github.com/snapcore/snapd/usersession/userd/ui"
    32  )
    33  
    34  type kdialogSuite struct{}
    35  
    36  var _ = Suite(&kdialogSuite{})
    37  
    38  func (s *kdialogSuite) TestYesNoSimpleYes(c *C) {
    39  	mock := testutil.MockCommand(c, "kdialog", "")
    40  	defer mock.Restore()
    41  
    42  	z := &ui.KDialog{}
    43  	answeredYes := z.YesNo("primary", "secondary", nil)
    44  	c.Check(answeredYes, Equals, true)
    45  	c.Check(mock.Calls(), DeepEquals, [][]string{
    46  		{"kdialog", "--yesno=<p><big><b>primary</b></big></p><p>secondary</p>"},
    47  	})
    48  }
    49  
    50  func (s *kdialogSuite) TestYesNoSimpleNo(c *C) {
    51  	mock := testutil.MockCommand(c, "kdialog", "false")
    52  	defer mock.Restore()
    53  
    54  	z := &ui.KDialog{}
    55  	answeredYes := z.YesNo("primary", "secondary", nil)
    56  	c.Check(answeredYes, Equals, false)
    57  	c.Check(mock.Calls(), DeepEquals, [][]string{
    58  		{"kdialog", "--yesno=<p><big><b>primary</b></big></p><p>secondary</p>"},
    59  	})
    60  }
    61  
    62  func (s *kdialogSuite) TestYesNoSimpleFooter(c *C) {
    63  	mock := testutil.MockCommand(c, "kdialog", "")
    64  	defer mock.Restore()
    65  
    66  	z := &ui.KDialog{}
    67  	answeredYes := z.YesNo("primary", "secondary", &ui.DialogOptions{Footer: "footer"})
    68  	c.Check(answeredYes, Equals, true)
    69  	c.Check(mock.Calls(), DeepEquals, [][]string{
    70  		{"kdialog", "--yesno=<p><big><b>primary</b></big></p><p>secondary</p><p><small>footer</small></p>"},
    71  	})
    72  }
    73  
    74  func (s *kdialogSuite) TestYesNoSimpleTimeout(c *C) {
    75  	killSleeper := filepath.Join(c.MkDir(), "kill-sleeper")
    76  	script := fmt.Sprintf(`#!/bin/sh
    77  # store kill-script for the cleanup
    78  echo "kill $$" > %s
    79  exec sleep 30
    80  `, killSleeper)
    81  	defer func() { exec.Command("/bin/sh", killSleeper).Run() }()
    82  
    83  	mock := testutil.MockCommand(c, "kdialog", script)
    84  	defer mock.Restore()
    85  
    86  	z := &ui.KDialog{}
    87  	answeredYes := z.YesNo("primary", "secondary", &ui.DialogOptions{Timeout: 1 * time.Second})
    88  	// this got auto-canceled after 1sec
    89  	c.Check(answeredYes, Equals, false)
    90  	c.Check(mock.Calls(), DeepEquals, [][]string{
    91  		{"kdialog", "--yesno=<p><big><b>primary</b></big></p><p>secondary</p>"},
    92  	})
    93  }