update_query_params_unittest.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2014 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 "components/update_client/update_query_params.h"
  5. #include "base/strings/stringprintf.h"
  6. #include "base/system/sys_info.h"
  7. #include "components/update_client/update_query_params_delegate.h"
  8. #include "components/version_info/version_info.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. using base::StringPrintf;
  11. namespace update_client {
  12. namespace {
  13. bool Contains(const std::string& source, const std::string& target) {
  14. return source.find(target) != std::string::npos;
  15. }
  16. class TestUpdateQueryParamsDelegate : public UpdateQueryParamsDelegate {
  17. std::string GetExtraParams() override { return "&cat=dog"; }
  18. };
  19. } // namespace
  20. void TestParams(UpdateQueryParams::ProdId prod_id, bool extra_params) {
  21. std::string params = UpdateQueryParams::Get(prod_id);
  22. // This doesn't so much test what the values are (since that would be an
  23. // almost exact duplication of code with update_query_params.cc, and wouldn't
  24. // really test anything) as it is a verification that all the params are
  25. // present in the generated string.
  26. EXPECT_TRUE(
  27. Contains(params, StringPrintf("os=%s", UpdateQueryParams::GetOS())));
  28. EXPECT_TRUE(
  29. Contains(params, StringPrintf("arch=%s", UpdateQueryParams::GetArch())));
  30. EXPECT_TRUE(Contains(
  31. params,
  32. StringPrintf("os_arch=%s",
  33. base::SysInfo().OperatingSystemArchitecture().c_str())));
  34. EXPECT_TRUE(Contains(
  35. params,
  36. StringPrintf("prod=%s", UpdateQueryParams::GetProdIdString(prod_id))));
  37. if (extra_params)
  38. EXPECT_TRUE(Contains(params, "cat=dog"));
  39. }
  40. void TestProdVersion() {
  41. EXPECT_EQ(version_info::GetVersionNumber(),
  42. UpdateQueryParams::GetProdVersion());
  43. }
  44. TEST(UpdateQueryParamsTest, GetParams) {
  45. TestProdVersion();
  46. TestParams(UpdateQueryParams::CRX, false);
  47. TestParams(UpdateQueryParams::CHROME, false);
  48. TestUpdateQueryParamsDelegate delegate;
  49. UpdateQueryParams::SetDelegate(&delegate);
  50. TestParams(UpdateQueryParams::CRX, true);
  51. TestParams(UpdateQueryParams::CHROME, true);
  52. }
  53. } // namespace update_client