|
| 1 | +package main |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright The CryptOS Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +// fakeFontSwitcher is a fontSwitcher fake so the fallback decision is tested |
| 25 | +// without a real console tty. |
| 26 | +type fakeFontSwitcher struct { |
| 27 | + calls int |
| 28 | + err error |
| 29 | +} |
| 30 | + |
| 31 | +func (f *fakeFontSwitcher) setDefaultFont() error { |
| 32 | + f.calls++ |
| 33 | + return f.err |
| 34 | +} |
| 35 | + |
| 36 | +func TestEnsureConsoleFitsRequestsTheFallbackBelowTheFloor(t *testing.T) { |
| 37 | + sw := &fakeFontSwitcher{} |
| 38 | + cols, rows := ensureConsoleFits(sw, func() (int, int) { return 80, 24 }, 38, 11) |
| 39 | + if sw.calls != 1 { |
| 40 | + t.Fatalf("setDefaultFont calls = %d, want 1", sw.calls) |
| 41 | + } |
| 42 | + if cols != 80 || rows != 24 { |
| 43 | + t.Fatalf("got %dx%d, want the re-read 80x24", cols, rows) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestEnsureConsoleFitsSkipsTheFallbackAtOrAboveTheFloor(t *testing.T) { |
| 48 | + sw := &fakeFontSwitcher{} |
| 49 | + readSize := func() (int, int) { |
| 50 | + t.Fatal("readSize must not be called when the console already fits") |
| 51 | + return 0, 0 |
| 52 | + } |
| 53 | + cols, rows := ensureConsoleFits(sw, readSize, 40, 12) |
| 54 | + if sw.calls != 0 { |
| 55 | + t.Fatalf("setDefaultFont calls = %d, want 0", sw.calls) |
| 56 | + } |
| 57 | + if cols != 40 || rows != 12 { |
| 58 | + t.Fatalf("got %dx%d, want the original 40x12 unchanged", cols, rows) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestEnsureConsoleFitsKeepsTheOriginalSizeOnFallbackError(t *testing.T) { |
| 63 | + sw := &fakeFontSwitcher{err: errors.New("ioctl failed")} |
| 64 | + readSize := func() (int, int) { |
| 65 | + t.Fatal("readSize must not be called when the fallback failed") |
| 66 | + return 0, 0 |
| 67 | + } |
| 68 | + cols, rows := ensureConsoleFits(sw, readSize, 38, 11) |
| 69 | + if sw.calls != 1 { |
| 70 | + t.Fatalf("setDefaultFont calls = %d, want 1", sw.calls) |
| 71 | + } |
| 72 | + if cols != 38 || rows != 11 { |
| 73 | + t.Fatalf("got %dx%d, want the original 38x11 kept", cols, rows) |
| 74 | + } |
| 75 | +} |
0 commit comments