| 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 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 | 449 |
| 450 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 450 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 451 | 451 |
| 452 // Send the calibration frame. | 452 // Send the calibration frame. |
| 453 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; | 453 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; |
| 454 RawBattOrSample cal_frame[] = { | 454 RawBattOrSample cal_frame[] = { |
| 455 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, | 455 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, |
| 456 }; | 456 }; |
| 457 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 457 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 458 CreateFrame(cal_frame_header, cal_frame, 2)); | 458 CreateFrame(cal_frame_header, cal_frame, 2)); |
| 459 GetTaskRunner()->RunUntilIdle(); |
| 459 | 460 |
| 460 // Send the two real data frames. | 461 // Send the two real data frames. |
| 461 BattOrFrameHeader frame_header1{0, 3 * sizeof(RawBattOrSample)}; | 462 BattOrFrameHeader frame_header1{0, 3 * sizeof(RawBattOrSample)}; |
| 462 RawBattOrSample frame1[] = { | 463 RawBattOrSample frame1[] = { |
| 463 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, RawBattOrSample{3, 3}, | 464 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, RawBattOrSample{3, 3}, |
| 464 }; | 465 }; |
| 465 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 466 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 466 CreateFrame(frame_header1, frame1, 3)); | 467 CreateFrame(frame_header1, frame1, 3)); |
| 468 GetTaskRunner()->RunUntilIdle(); |
| 467 | 469 |
| 468 BattOrFrameHeader frame_header2{0, 1 * sizeof(RawBattOrSample)}; | 470 BattOrFrameHeader frame_header2{0, 1 * sizeof(RawBattOrSample)}; |
| 469 RawBattOrSample frame2[] = {RawBattOrSample{1, 1}}; | 471 RawBattOrSample frame2[] = {RawBattOrSample{1, 1}}; |
| 470 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 472 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 471 CreateFrame(frame_header2, frame2, 1)); | 473 CreateFrame(frame_header2, frame2, 1)); |
| 474 GetTaskRunner()->RunUntilIdle(); |
| 472 | 475 |
| 473 // Send an empty last frame to indicate that we're done. | 476 // Send an empty last frame to indicate that we're done. |
| 474 BattOrFrameHeader frame_header3{0, 0 * sizeof(RawBattOrSample)}; | 477 BattOrFrameHeader frame_header3{0, 0 * sizeof(RawBattOrSample)}; |
| 475 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 478 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 476 CreateFrame(frame_header3, nullptr, 0)); | 479 CreateFrame(frame_header3, nullptr, 0)); |
| 480 GetTaskRunner()->RunUntilIdle(); |
| 477 | 481 |
| 478 EXPECT_TRUE(IsCommandComplete()); | 482 EXPECT_TRUE(IsCommandComplete()); |
| 479 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 483 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 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", | 484 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", |
| 481 GetTrace()); | 485 GetTrace()); |
| 482 } | 486 } |
| 483 | 487 |
| 484 TEST_F(BattOrAgentTest, StopTracingFailsWithoutConnection) { | 488 TEST_F(BattOrAgentTest, StopTracingFailsWithoutConnection) { |
| 485 GetAgent()->StopTracing(); | 489 GetAgent()->StopTracing(); |
| 486 GetTaskRunner()->RunUntilIdle(); | 490 GetTaskRunner()->RunUntilIdle(); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 673 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 670 | 674 |
| 671 // Send a calibration frame with a mismatch between the frame length in the | 675 // Send a calibration frame with a mismatch between the frame length in the |
| 672 // header and the actual frame length. | 676 // header and the actual frame length. |
| 673 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; | 677 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; |
| 674 RawBattOrSample cal_frame[] = { | 678 RawBattOrSample cal_frame[] = { |
| 675 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, | 679 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, |
| 676 }; | 680 }; |
| 677 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 681 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 678 CreateFrame(cal_frame_header, cal_frame, 2)); | 682 CreateFrame(cal_frame_header, cal_frame, 2)); |
| 683 GetTaskRunner()->RunUntilIdle(); |
| 679 | 684 |
| 680 EXPECT_TRUE(IsCommandComplete()); | 685 EXPECT_TRUE(IsCommandComplete()); |
| 681 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 686 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 682 } | 687 } |
| 683 | 688 |
| 684 TEST_F(BattOrAgentTest, StopTracingFailsIfDataFrameHasWrongLength) { | 689 TEST_F(BattOrAgentTest, StopTracingFailsIfDataFrameHasWrongLength) { |
| 685 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 690 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 686 | 691 |
| 687 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; | 692 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; |
| 688 RawBattOrSample cal_frame[] = { | 693 RawBattOrSample cal_frame[] = { |
| 689 RawBattOrSample{1, 1}, | 694 RawBattOrSample{1, 1}, |
| 690 }; | 695 }; |
| 691 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 696 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 692 CreateFrame(cal_frame_header, cal_frame, 1)); | 697 CreateFrame(cal_frame_header, cal_frame, 1)); |
| 698 GetTaskRunner()->RunUntilIdle(); |
| 693 | 699 |
| 694 // Send a data frame with a mismatch between the frame length in the | 700 // Send a data frame with a mismatch between the frame length in the |
| 695 // header and the actual frame length. | 701 // header and the actual frame length. |
| 696 BattOrFrameHeader frame_header{0, 2 * sizeof(RawBattOrSample)}; | 702 BattOrFrameHeader frame_header{0, 2 * sizeof(RawBattOrSample)}; |
| 697 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; | 703 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; |
| 698 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 704 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 699 CreateFrame(frame_header, frame, 1)); | 705 CreateFrame(frame_header, frame, 1)); |
| 700 GetTaskRunner()->RunUntilIdle(); | 706 GetTaskRunner()->RunUntilIdle(); |
| 701 | 707 |
| 702 EXPECT_TRUE(IsCommandComplete()); | 708 EXPECT_TRUE(IsCommandComplete()); |
| 703 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 709 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 704 } | 710 } |
| 705 | 711 |
| 706 TEST_F(BattOrAgentTest, StopTracingFailsIfCalibrationFrameMissingByte) { | 712 TEST_F(BattOrAgentTest, StopTracingFailsIfCalibrationFrameMissingByte) { |
| 707 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 713 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 708 | 714 |
| 709 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; | 715 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; |
| 710 RawBattOrSample cal_frame[] = { | 716 RawBattOrSample cal_frame[] = { |
| 711 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, | 717 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, |
| 712 }; | 718 }; |
| 713 | 719 |
| 714 // Remove the last byte from the frame to make it invalid. | 720 // Remove the last byte from the frame to make it invalid. |
| 715 scoped_ptr<vector<char>> cal_frame_bytes = | 721 scoped_ptr<vector<char>> cal_frame_bytes = |
| 716 CreateFrame(cal_frame_header, cal_frame, 2); | 722 CreateFrame(cal_frame_header, cal_frame, 2); |
| 717 cal_frame_bytes->pop_back(); | 723 cal_frame_bytes->pop_back(); |
| 718 | 724 |
| 719 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 725 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 720 std::move(cal_frame_bytes)); | 726 std::move(cal_frame_bytes)); |
| 727 GetTaskRunner()->RunUntilIdle(); |
| 721 | 728 |
| 722 EXPECT_TRUE(IsCommandComplete()); | 729 EXPECT_TRUE(IsCommandComplete()); |
| 723 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 730 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 724 } | 731 } |
| 725 | 732 |
| 726 TEST_F(BattOrAgentTest, StopTracingFailsIfDataFrameMissingByte) { | 733 TEST_F(BattOrAgentTest, StopTracingFailsIfDataFrameMissingByte) { |
| 727 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 734 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 728 | 735 |
| 729 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; | 736 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; |
| 730 RawBattOrSample cal_frame[] = { | 737 RawBattOrSample cal_frame[] = { |
| 731 RawBattOrSample{1, 1}, | 738 RawBattOrSample{1, 1}, |
| 732 }; | 739 }; |
| 733 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 740 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 734 CreateFrame(cal_frame_header, cal_frame, 1)); | 741 CreateFrame(cal_frame_header, cal_frame, 1)); |
| 742 GetTaskRunner()->RunUntilIdle(); |
| 735 | 743 |
| 736 BattOrFrameHeader frame_header{0, 1}; | 744 BattOrFrameHeader frame_header{0, 1}; |
| 737 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; | 745 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; |
| 738 | 746 |
| 739 // Remove the last byte from the frame to make it invalid. | 747 // Remove the last byte from the frame to make it invalid. |
| 740 scoped_ptr<vector<char>> frame_bytes = CreateFrame(frame_header, frame, 2); | 748 scoped_ptr<vector<char>> frame_bytes = CreateFrame(frame_header, frame, 2); |
| 741 frame_bytes->pop_back(); | 749 frame_bytes->pop_back(); |
| 742 | 750 |
| 743 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 751 GetAgent()->OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 744 std::move(frame_bytes)); | 752 std::move(frame_bytes)); |
| 745 GetTaskRunner()->RunUntilIdle(); | 753 GetTaskRunner()->RunUntilIdle(); |
| 746 | 754 |
| 747 EXPECT_TRUE(IsCommandComplete()); | 755 EXPECT_TRUE(IsCommandComplete()); |
| 748 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 756 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 749 } | 757 } |
| 750 | 758 |
| 751 } // namespace battor | 759 } // namespace battor |
| OLD | NEW |