tests: Greatly simplify and clean up the layer activation-order test

The test has been through a major refactor, lifting out common parts, improving
comments, and naming.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/867/head
Gergely Nagy 4 years ago
parent d90820afc7
commit ffcd17ba6e
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -23,52 +23,52 @@
KEYMAPS( KEYMAPS(
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
( (
Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A Key_0 ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_A ,Key_A ,Key_A ,M(0) ,XXX ,XXX ,XXX ,M(0)
,ShiftToLayer(1) ,ShiftToLayer(1)
,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,Key_0
,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,Key_A ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_A ,Key_A ,Key_A ,Key_A ,XXX ,XXX ,XXX ,XXX
,ShiftToLayer(2) ,ShiftToLayer(2)
), ),
[1] = KEYMAP_STACKED [1] = KEYMAP_STACKED
( (
Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B Key_1 ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX
,___ ,___
,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,Key_1
,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_B ,Key_B ,Key_B ,Key_B ,XXX ,XXX ,XXX ,XXX
,___ ,___
), ),
[2] = KEYMAP_STACKED [2] = KEYMAP_STACKED
( (
Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C Key_2 ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX
,___ ,___
,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,Key_2
,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX ,XXX
,Key_C ,Key_C ,Key_C ,Key_C ,XXX ,XXX ,XXX ,XXX
,___ ,___
) )
) // KEYMAPS( ) // KEYMAPS(

@ -26,150 +26,179 @@ using ::testing::IsEmpty;
class LayerActivationOrder : public VirtualDeviceTest { class LayerActivationOrder : public VirtualDeviceTest {
public: public:
void SingleKeyTest(Key k) { const KeyAddr KEYSWITCH_TOP_LEFT = KeyAddr{0, 0}; // layer-dependent key
sim_.Press(0, 0); // k const KeyAddr KEYSWITCH_TOP_RIGHT = KeyAddr{0, 15}; // layer-dependent key
auto state = RunCycle(); const KeyAddr KEYSWITCH_LEFT_PALM = KeyAddr{3, 6}; // ShiftToLayer(1)
const KeyAddr KEYSWITCH_RIGHT_PALM = KeyAddr{3, 9}; // ShiftToLayer(2)
const KeyAddr KEYSWITCH_LEFT_THUMB_RIGHTMOST = KeyAddr{3, 7}; // L0 deactivate macro
const Key LAYER0_KEY = Key_0;
const Key LAYER1_KEY = Key_1;
const Key LAYER2_KEY = Key_2;
void pressKeyswitch(const KeyAddr& addr) {
sim_.Press(addr.row(), addr.col());
}
void releaseKeyswitch(const KeyAddr& addr) {
sim_.Release(addr.row(), addr.col());
}
auto pressKeyswitchAndCycle(const KeyAddr& addr) {
pressKeyswitch(addr);
return RunCycle();
}
auto releaseKeyswitchAndCycle(const KeyAddr& addr) {
releaseKeyswitch(addr);
return RunCycle();
}
void assertSingleReportThatContains(std::unique_ptr<State> &state, Key k) {
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
EXPECT_THAT( EXPECT_THAT(
state->HIDReports()->Keyboard(0).ActiveKeycodes(), state->HIDReports()->Keyboard(0).ActiveKeycodes(),
Contains(k)); Contains(k));
}
sim_.Release(0, 0); // k void assertSingleReportThatDoesNotContain(std::unique_ptr<State> &state, Key k) {
state = RunCycle(); ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
EXPECT_THAT(
state->HIDReports()->Keyboard(0).ActiveKeycodes(),
::testing::Not(Contains(k)));
}
void assertSingleEmptyReport(std::unique_ptr<State> &state) {
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
EXPECT_THAT( EXPECT_THAT(
state->HIDReports()->Keyboard(0).ActiveKeycodes(), state->HIDReports()->Keyboard(0).ActiveKeycodes(),
IsEmpty()); IsEmpty());
}
void assertNoReport(std::unique_ptr<State> &state) {
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 0);
}
void assertNoReportAfterCycle() {
auto state = RunCycle();
assertNoReport(state);
}
void TestPressAndRelease(const KeyAddr& addr, Key k) {
auto state = pressKeyswitchAndCycle(addr);
assertSingleReportThatContains(state, k);
state = RunCycle(); state = releaseKeyswitchAndCycle(addr);
assertSingleEmptyReport(state);
// 2 cycles after releasing k assertNoReportAfterCycle();
EXPECT_EQ(state->HIDReports()->Keyboard().size(), 0);
} }
}; };
TEST_F(LayerActivationOrder, BaseLayerHasNotRegressed) { TEST_F(LayerActivationOrder, BaseLayerHasNotRegressed) {
SingleKeyTest(Key_A); TestPressAndRelease(KEYSWITCH_TOP_LEFT, LAYER0_KEY);
} }
TEST_F(LayerActivationOrder, ShifToLayerOne) { TEST_F(LayerActivationOrder, ShifToLayerOne) {
// Pressing (3,6) shifts to Layer 1, and we stay there until release. // Pressing (KEYSWITCH_LEFT_PALM) shifts to Layer 1, and we stay there until release.
auto state = pressKeyswitchAndCycle(KEYSWITCH_LEFT_PALM);
TestPressAndRelease(KEYSWITCH_TOP_LEFT, LAYER1_KEY);
sim_.Press(3, 6); // ShiftToLayer(1) // Releasing (KEYSWITCH_LEFT_PALM) gets us back to the base layer
auto state = RunCycle(); state = releaseKeyswitchAndCycle(KEYSWITCH_LEFT_PALM);
TestPressAndRelease(KEYSWITCH_TOP_LEFT, LAYER0_KEY);
SingleKeyTest(Key_B);
// Releasing (3,6) gets us back to the base layer
sim_.Release(3, 6); // ShiftToLayer(1)
state = RunCycle();
SingleKeyTest(Key_A);
} }
TEST_F(LayerActivationOrder, ShiftingWithCaching) { TEST_F(LayerActivationOrder, ShiftingWithCaching) {
// Pressing (0, 0) will activate A // Pressing (KEYSWITCH_TOP_LEFT) will activate the key on layer 0
sim_.Press(0, 0); auto state = pressKeyswitchAndCycle(KEYSWITCH_TOP_LEFT);
auto state = RunCycle(); assertSingleReportThatContains(state, LAYER0_KEY);
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1);
EXPECT_THAT(
state->HIDReports()->Keyboard(0).ActiveKeycodes(),
Contains(Key_A));
// Pressing (3, 6) will switch to Layer 1 // Pressing (KEYSWITCH_LEFT_PALM) will switch to Layer 1
sim_.Press(3, 6); state = pressKeyswitchAndCycle(KEYSWITCH_LEFT_PALM);
state = RunCycle();
// ...since we're still pressing (0, 0), and there was no change in the HID
// states, we shouldn't emit a report.
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 0);
// Pressing (0, 1), the report shall contain 'A' _and_ 'B'. // ...since we're still pressing (KEYSWITCH_TOP_LEFT), and there was no change
sim_.Press(0, 1); // in the HID states, we shouldn't emit a report.
state = RunCycle(); assertNoReport(state);
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); // Pressing (KEYSWITCH_TOP_RIGHT), the report shall contain keys from both
EXPECT_THAT( // layer 0 and layer1, because we started holding the layer 0 key prior to
state->HIDReports()->Keyboard(0).ActiveKeycodes(), // switching layers, so it's code should remain cached.
Contains(Key_A)); state = pressKeyswitchAndCycle(KEYSWITCH_TOP_RIGHT);
EXPECT_THAT( assertSingleReportThatContains(state, LAYER0_KEY);
state->HIDReports()->Keyboard(0).ActiveKeycodes(), assertSingleReportThatContains(state, LAYER1_KEY);
Contains(Key_B));
// Releasing (0, 0), the report should now contain B only
sim_.Release(0, 0);
state = RunCycle();
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); // Releasing (KEYSWITCH_TOP_LEFT), the report should now contain the key from
EXPECT_THAT( // layer1 only, and should not contain the layer0 key anymore.
state->HIDReports()->Keyboard(0).ActiveKeycodes(), state = releaseKeyswitchAndCycle(KEYSWITCH_TOP_LEFT);
Contains(Key_B)); assertSingleReportThatContains(state, LAYER1_KEY);
assertSingleReportThatDoesNotContain(state, LAYER0_KEY);
// Release (0, 1) // Release (KEYSWITCH_TOP_RIGHT)
sim_.Release(0, 1); state = releaseKeyswitchAndCycle(KEYSWITCH_TOP_RIGHT);
state = RunCycle();
// Test B in isolation again // Test the layer 1 key in isolation again
SingleKeyTest(Key_B); TestPressAndRelease(KEYSWITCH_TOP_LEFT, LAYER1_KEY);
// Release the layer key as well. // Release the layer key as well.
sim_.Release(3, 6); state = releaseKeyswitchAndCycle(KEYSWITCH_LEFT_PALM);
state = RunCycle();
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 0); // Since the layer key release is internal to us, we shouldn't send a report.
assertNoReport(state);
} }
TEST_F(LayerActivationOrder, Ordering) { TEST_F(LayerActivationOrder, Ordering) {
// Pressing (3, 9) will switch to Layer 2 // Pressing (KEYSWITCH_RIGHT_PALM) will switch to Layer 2
sim_.Press(3, 9); auto state = pressKeyswitchAndCycle(KEYSWITCH_RIGHT_PALM);
auto state = RunCycle();
// Pressing (KEYSWITCH_TOP_LEFT) will activate a key on layer 2
// Pressing (0, 0) will activate 'C' state = pressKeyswitchAndCycle(KEYSWITCH_TOP_LEFT);
sim_.Press(0, 0); assertSingleReportThatContains(state, LAYER2_KEY);
state = RunCycle();
// Pressing (KEYSWITCH_LEFT_PALM) will activate Layer 1
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); state = pressKeyswitchAndCycle(KEYSWITCH_LEFT_PALM);
EXPECT_THAT(
state->HIDReports()->Keyboard(0).ActiveKeycodes(), // Pressing (KEYSWITCH_TOP_RIGHT) will activate the layer 1 key now, due to
Contains(Key_C)); // activation ordering.
state = pressKeyswitchAndCycle(KEYSWITCH_TOP_RIGHT);
// Pressing (3, 6) will activate Layer 1
sim_.Press(3, 6); // We should have both the layer 1 and the layer 2 key active, because we're
// holding both.
assertSingleReportThatContains(state, LAYER1_KEY);
assertSingleReportThatContains(state, LAYER2_KEY);
// Releaseing all held keys, we should get an empty report.
releaseKeyswitch(KEYSWITCH_TOP_LEFT);
releaseKeyswitch(KEYSWITCH_TOP_RIGHT);
releaseKeyswitch(KEYSWITCH_LEFT_PALM);
releaseKeyswitch(KEYSWITCH_RIGHT_PALM);
state = RunCycle(); state = RunCycle();
// Pressing (0, 1) will activate 'B' now, due to activation ordering. assertSingleEmptyReport(state);
sim_.Press(0, 1);
state = RunCycle();
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); // One more cycle, and we should generate no report at all
EXPECT_THAT(
state->HIDReports()->Keyboard(0).ActiveKeycodes(),
Contains(Key_C));
EXPECT_THAT(
state->HIDReports()->Keyboard(0).ActiveKeycodes(),
Contains(Key_B));
sim_.Release(0, 0);
sim_.Release(0, 1);
sim_.Release(3, 6);
sim_.Release(3, 9);
state = RunCycle(); state = RunCycle();
assertNoReport(state);
} }
TEST_F(LayerActivationOrder, LayerZero) { TEST_F(LayerActivationOrder, LayerZero) {
sim_.Press(3, 7); // Macro#0: Layer.deactivate(0) // Pressing the rightmost of the left thumb keys should deactivate layer 0
auto state = RunCycle(); auto state = pressKeyswitchAndCycle(KEYSWITCH_LEFT_THUMB_RIGHTMOST);
// Pressing KEYSWITCH_TOP_LEFT should fall back to activating the key on layer 0
state = pressKeyswitchAndCycle(KEYSWITCH_TOP_LEFT);
assertSingleReportThatContains(state, LAYER0_KEY);
sim_.Press(0, 0); // A // Releasing all keys should generate a single empty report
releaseKeyswitch(KEYSWITCH_TOP_LEFT);
releaseKeyswitch(KEYSWITCH_LEFT_THUMB_RIGHTMOST);
state = RunCycle(); state = RunCycle();
ASSERT_EQ(state->HIDReports()->Keyboard().size(), 1); assertSingleEmptyReport(state);
EXPECT_THAT(
state->HIDReports()->Keyboard(0).ActiveKeycodes(), // Afterwards, we should generate no more reports.
Contains(Key_A)); state = RunCycle();
assertNoReport(state);
} }
} // namespace } // namespace

Loading…
Cancel
Save