OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/battor_agent/battor_agent.h" | 5 #include "tools/battor_agent/battor_agent.h" |
6 | 6 |
7 #include "base/test/test_simple_task_runner.h" | 7 #include "base/test/test_simple_task_runner.h" |
8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 465 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
466 CreateFrame(frame_header1, frame1, 3)); | 466 CreateFrame(frame_header1, frame1, 3)); |
467 | 467 |
468 BattOrFrameHeader frame_header2{0, 1 * sizeof(RawBattOrSample)}; | 468 BattOrFrameHeader frame_header2{0, 1 * sizeof(RawBattOrSample)}; |
469 RawBattOrSample frame2[] = {RawBattOrSample{1, 1}}; | 469 RawBattOrSample frame2[] = {RawBattOrSample{1, 1}}; |
470 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 470 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
471 CreateFrame(frame_header2, frame2, 1)); | 471 CreateFrame(frame_header2, frame2, 1)); |
472 | 472 |
473 // Send an empty last frame to indicate that we're done. | 473 // Send an empty last frame to indicate that we're done. |
474 BattOrFrameHeader frame_header3{0, 0 * sizeof(RawBattOrSample)}; | 474 BattOrFrameHeader frame_header3{0, 0 * sizeof(RawBattOrSample)}; |
475 RawBattOrSample frame3[] = {}; | |
476 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 475 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
477 CreateFrame(frame_header3, frame3, 0)); | 476 CreateFrame(frame_header3, nullptr, 0)); |
478 | 477 |
479 EXPECT_TRUE(IsCommandComplete()); | 478 EXPECT_TRUE(IsCommandComplete()); |
480 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 479 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
481 EXPECT_EQ("0.00 -0.3 -0.6\n1.00 0.3 0.6\n2.00 0.9 1.8\n3.00 -0.3 -0.6\n", | 480 EXPECT_EQ("0.00 -0.3 -0.6\n1.00 0.3 0.6\n2.00 0.9 1.8\n3.00 -0.3 -0.6\n", |
482 GetTrace()); | 481 GetTrace()); |
483 } | 482 } |
484 | 483 |
485 TEST_F(BattOrAgentTest, StopTracingFailsWithoutConnection) { | 484 TEST_F(BattOrAgentTest, StopTracingFailsWithoutConnection) { |
486 GetAgent()->StopTracing(); | 485 GetAgent()->StopTracing(); |
487 GetTaskRunner()->RunUntilIdle(); | 486 GetTaskRunner()->RunUntilIdle(); |
(...skipping 28 matching lines...) Expand all Loading... |
516 GetAgent()->OnBytesSent(true); | 515 GetAgent()->OnBytesSent(true); |
517 GetTaskRunner()->RunUntilIdle(); | 516 GetTaskRunner()->RunUntilIdle(); |
518 | 517 |
519 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; | 518 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; |
520 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; | 519 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; |
521 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 520 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
522 CreateFrame(cal_frame_header, cal_frame, 1)); | 521 CreateFrame(cal_frame_header, cal_frame, 1)); |
523 GetTaskRunner()->RunUntilIdle(); | 522 GetTaskRunner()->RunUntilIdle(); |
524 | 523 |
525 BattOrFrameHeader frame_header{0, 0}; | 524 BattOrFrameHeader frame_header{0, 0}; |
526 RawBattOrSample frame[] = {}; | |
527 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 525 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
528 CreateFrame(frame_header, frame, 0)); | 526 CreateFrame(frame_header, nullptr, 0)); |
529 GetTaskRunner()->RunUntilIdle(); | 527 GetTaskRunner()->RunUntilIdle(); |
530 | 528 |
531 EXPECT_TRUE(IsCommandComplete()); | 529 EXPECT_TRUE(IsCommandComplete()); |
532 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 530 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
533 } | 531 } |
534 | 532 |
535 TEST_F(BattOrAgentTest, StopTracingFailsIfEEPROMReadFails) { | 533 TEST_F(BattOrAgentTest, StopTracingFailsIfEEPROMReadFails) { |
536 RunStopTracingTo(BattOrAgentState::EEPROM_REQUEST_SENT); | 534 RunStopTracingTo(BattOrAgentState::EEPROM_REQUEST_SENT); |
537 | 535 |
538 for (int i = 0; i < 20; i++) { | 536 for (int i = 0; i < 20; i++) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | 568 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
571 GetTaskRunner()->RunUntilIdle(); | 569 GetTaskRunner()->RunUntilIdle(); |
572 | 570 |
573 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; | 571 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; |
574 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; | 572 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; |
575 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 573 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
576 CreateFrame(cal_frame_header, cal_frame, 1)); | 574 CreateFrame(cal_frame_header, cal_frame, 1)); |
577 GetTaskRunner()->RunUntilIdle(); | 575 GetTaskRunner()->RunUntilIdle(); |
578 | 576 |
579 BattOrFrameHeader frame_header{0, 0}; | 577 BattOrFrameHeader frame_header{0, 0}; |
580 RawBattOrSample frame[] = {}; | |
581 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 578 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
582 CreateFrame(frame_header, frame, 0)); | 579 CreateFrame(frame_header, nullptr, 0)); |
583 GetTaskRunner()->RunUntilIdle(); | 580 GetTaskRunner()->RunUntilIdle(); |
584 | 581 |
585 EXPECT_TRUE(IsCommandComplete()); | 582 EXPECT_TRUE(IsCommandComplete()); |
586 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 583 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
587 } | 584 } |
588 | 585 |
589 TEST_F(BattOrAgentTest, StopTracingFailsWithManyCalibrationFrameReadFailures) { | 586 TEST_F(BattOrAgentTest, StopTracingFailsWithManyCalibrationFrameReadFailures) { |
590 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 587 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
591 | 588 |
592 // We attempt the read a max of 20 times: send that many failures. | 589 // We attempt the read a max of 20 times: send that many failures. |
593 for (int i = 0; i < 20; i++) { | 590 for (int i = 0; i < 20; i++) { |
594 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | 591 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
595 GetTaskRunner()->RunUntilIdle(); | 592 GetTaskRunner()->RunUntilIdle(); |
596 } | 593 } |
597 | 594 |
598 EXPECT_TRUE(IsCommandComplete()); | 595 EXPECT_TRUE(IsCommandComplete()); |
599 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | 596 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); |
600 } | 597 } |
601 | 598 |
602 TEST_F(BattOrAgentTest, StopTracingSucceedsWithOneDataFrameReadFailure) { | 599 TEST_F(BattOrAgentTest, StopTracingSucceedsWithOneDataFrameReadFailure) { |
603 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_SENT); | 600 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_SENT); |
604 | 601 |
605 // Make a read fail in order to make sure that the agent will retry. | 602 // Make a read fail in order to make sure that the agent will retry. |
606 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | 603 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
607 GetTaskRunner()->RunUntilIdle(); | 604 GetTaskRunner()->RunUntilIdle(); |
608 | 605 |
609 BattOrFrameHeader frame_header{0, 0}; | 606 BattOrFrameHeader frame_header{0, 0}; |
610 RawBattOrSample frame[] = {}; | |
611 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 607 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
612 CreateFrame(frame_header, frame, 0)); | 608 CreateFrame(frame_header, nullptr, 0)); |
613 GetTaskRunner()->RunUntilIdle(); | 609 GetTaskRunner()->RunUntilIdle(); |
614 | 610 |
615 EXPECT_TRUE(IsCommandComplete()); | 611 EXPECT_TRUE(IsCommandComplete()); |
616 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 612 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
617 } | 613 } |
618 | 614 |
619 TEST_F(BattOrAgentTest, StopTracingFailsWithManyDataFrameReadFailures) { | 615 TEST_F(BattOrAgentTest, StopTracingFailsWithManyDataFrameReadFailures) { |
620 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_SENT); | 616 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_SENT); |
621 | 617 |
622 // We attempt the read a max of 20 times: send that many failures. | 618 // We attempt the read a max of 20 times: send that many failures. |
(...skipping 21 matching lines...) Expand all Loading... |
644 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 640 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
645 CreateFrame(frame_header1, frame1, 1)); | 641 CreateFrame(frame_header1, frame1, 1)); |
646 GetTaskRunner()->RunUntilIdle(); | 642 GetTaskRunner()->RunUntilIdle(); |
647 | 643 |
648 for (int i = 0; i < 11; i++) { | 644 for (int i = 0; i < 11; i++) { |
649 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | 645 GetAgent()->OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
650 GetTaskRunner()->RunUntilIdle(); | 646 GetTaskRunner()->RunUntilIdle(); |
651 } | 647 } |
652 | 648 |
653 BattOrFrameHeader frame_header2{0, 0}; | 649 BattOrFrameHeader frame_header2{0, 0}; |
654 RawBattOrSample frame2[] = {}; | |
655 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 650 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
656 CreateFrame(frame_header2, frame2, 0)); | 651 CreateFrame(frame_header2, nullptr, 0)); |
657 GetTaskRunner()->RunUntilIdle(); | 652 GetTaskRunner()->RunUntilIdle(); |
658 | 653 |
659 EXPECT_TRUE(IsCommandComplete()); | 654 EXPECT_TRUE(IsCommandComplete()); |
660 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 655 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
661 } | 656 } |
662 | 657 |
663 TEST_F(BattOrAgentTest, StopTracingFailsIfSamplesReadHasWrongType) { | 658 TEST_F(BattOrAgentTest, StopTracingFailsIfSamplesReadHasWrongType) { |
664 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 659 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
665 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 660 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
666 ToCharVector(kInitAck)); | 661 ToCharVector(kInitAck)); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
747 | 742 |
748 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 743 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
749 std::move(frame_bytes)); | 744 std::move(frame_bytes)); |
750 GetTaskRunner()->RunUntilIdle(); | 745 GetTaskRunner()->RunUntilIdle(); |
751 | 746 |
752 EXPECT_TRUE(IsCommandComplete()); | 747 EXPECT_TRUE(IsCommandComplete()); |
753 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 748 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
754 } | 749 } |
755 | 750 |
756 } // namespace battor | 751 } // namespace battor |
OLD | NEW |