usb_printer_id_unittest.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chromeos/printing/usb_printer_id.h"
  5. #include <algorithm>
  6. #include <map>
  7. #include <string>
  8. #include "base/strings/string_util.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace chromeos {
  12. namespace {
  13. using testing::IsEmpty;
  14. using MapType = std::map<std::string, std::vector<std::string>>;
  15. MapType GetDefaultDeviceId() {
  16. MapType ret;
  17. // Make.
  18. ret["MFG"].push_back("EPSON");
  19. // Model.
  20. ret["MDL"].push_back("ET-2700");
  21. // Command set.
  22. ret["CMD"].push_back("ESCPL2");
  23. ret["CMD"].push_back("BDC");
  24. ret["CMD"].push_back("D4");
  25. ret["CMD"].push_back("END4");
  26. ret["CMD"].push_back("GENEP");
  27. return ret;
  28. }
  29. std::string MapToString(const MapType& map) {
  30. std::vector<std::string> terms;
  31. for (auto& term : map) {
  32. std::string values = base::JoinString(term.second, ",");
  33. terms.push_back(base::JoinString({term.first, values}, ":"));
  34. }
  35. std::string device_id_str = "xx"; // Two unused bytes for the length.
  36. device_id_str += base::JoinString(terms, ";") + ";";
  37. return device_id_str;
  38. }
  39. std::vector<uint8_t> MapToBuffer(const MapType& map) {
  40. std::string device_id_str = MapToString(map);
  41. std::vector<uint8_t> ret;
  42. std::copy(device_id_str.begin(), device_id_str.end(),
  43. std::back_inserter(ret));
  44. return ret;
  45. }
  46. TEST(UsbPrinterIdTest, EmptyDeviceId) {
  47. EXPECT_THAT(BuildDeviceIdMapping({}), IsEmpty());
  48. }
  49. // Tests that we get the same map back after parsing.
  50. TEST(UsbPrinterIdTest, SimpleSanityTest) {
  51. MapType mapping = GetDefaultDeviceId();
  52. std::vector<uint8_t> buffer = MapToBuffer(mapping);
  53. EXPECT_EQ(mapping, BuildDeviceIdMapping(buffer));
  54. }
  55. } // namespace
  56. } // namespace chromeos