| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "chromeos/components/tether/device_status_util.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "chromeos/components/tether/fake_tether_host_fetcher.h" |
| 14 #include "components/cryptauth/remote_device.h" |
| 15 #include "components/cryptauth/remote_device_test_util.h" |
| 16 #include "components/prefs/testing_pref_service.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chromeos { |
| 20 |
| 21 namespace tether { |
| 22 |
| 23 namespace { |
| 24 |
| 25 const char kDoNotSetStringField[] = "doNotSetField"; |
| 26 const int kDoNotSetIntField = -100; |
| 27 |
| 28 // Creates a DeviceStatus object using the parameters provided. If |
| 29 // |kDoNotSetStringField| or |kDoNotSetIntField| are passed, these fields will |
| 30 // not be set in the output. |
| 31 DeviceStatus CreateFakeDeviceStatus(const std::string& cell_provider_name, |
| 32 int battery_percentage, |
| 33 int connection_strength) { |
| 34 // TODO(khorimoto): Once a ConnectedWifiSsid field is added as a property of |
| 35 // Tether networks, give an option to pass a parameter for that field as well. |
| 36 WifiStatus wifi_status; |
| 37 wifi_status.set_status_code( |
| 38 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); |
| 39 wifi_status.set_ssid("Google A"); |
| 40 |
| 41 DeviceStatus device_status; |
| 42 if (battery_percentage != kDoNotSetIntField) { |
| 43 device_status.set_battery_percentage(battery_percentage); |
| 44 } |
| 45 if (cell_provider_name != kDoNotSetStringField) { |
| 46 device_status.set_cell_provider(cell_provider_name); |
| 47 } |
| 48 if (connection_strength != kDoNotSetIntField) { |
| 49 device_status.set_connection_strength(connection_strength); |
| 50 } |
| 51 |
| 52 device_status.mutable_wifi_status()->CopyFrom(wifi_status); |
| 53 |
| 54 return device_status; |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 class DeviceStatusUtilTest : public testing::Test { |
| 60 public: |
| 61 DeviceStatusUtilTest() {} |
| 62 |
| 63 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(DeviceStatusUtilTest); |
| 65 }; |
| 66 |
| 67 TEST_F(DeviceStatusUtilTest, TestNotPresent) { |
| 68 DeviceStatus status = |
| 69 CreateFakeDeviceStatus(kDoNotSetStringField /* cell_provider_name */, |
| 70 kDoNotSetIntField /* battery_percentage */, |
| 71 kDoNotSetIntField /* connection_strength */); |
| 72 |
| 73 std::string carrier; |
| 74 int32_t battery_percentage; |
| 75 int32_t signal_strength; |
| 76 |
| 77 NormalizeDeviceStatus(status, &carrier, &battery_percentage, |
| 78 &signal_strength); |
| 79 |
| 80 EXPECT_EQ("unknown-carrier", carrier); |
| 81 EXPECT_EQ(100, battery_percentage); |
| 82 EXPECT_EQ(100, signal_strength); |
| 83 } |
| 84 |
| 85 TEST_F(DeviceStatusUtilTest, TestEmptyCellProvider) { |
| 86 DeviceStatus status = CreateFakeDeviceStatus( |
| 87 "" /* cell_provider_name */, kDoNotSetIntField /* battery_percentage */, |
| 88 kDoNotSetIntField /* connection_strength */); |
| 89 |
| 90 std::string carrier; |
| 91 int32_t battery_percentage; |
| 92 int32_t signal_strength; |
| 93 |
| 94 NormalizeDeviceStatus(status, &carrier, &battery_percentage, |
| 95 &signal_strength); |
| 96 |
| 97 EXPECT_EQ("unknown-carrier", carrier); |
| 98 EXPECT_EQ(100, battery_percentage); |
| 99 EXPECT_EQ(100, signal_strength); |
| 100 } |
| 101 |
| 102 TEST_F(DeviceStatusUtilTest, TestBelowMinValue) { |
| 103 DeviceStatus status = CreateFakeDeviceStatus( |
| 104 "cellProvider" /* cell_provider_name */, -1 /* battery_percentage */, |
| 105 -1 /* connection_strength */); |
| 106 |
| 107 std::string carrier; |
| 108 int32_t battery_percentage; |
| 109 int32_t signal_strength; |
| 110 |
| 111 NormalizeDeviceStatus(status, &carrier, &battery_percentage, |
| 112 &signal_strength); |
| 113 |
| 114 EXPECT_EQ("cellProvider", carrier); |
| 115 EXPECT_EQ(0, battery_percentage); |
| 116 EXPECT_EQ(0, signal_strength); |
| 117 } |
| 118 |
| 119 TEST_F(DeviceStatusUtilTest, TestAboveMaxValue) { |
| 120 DeviceStatus status = CreateFakeDeviceStatus( |
| 121 "cellProvider" /* cell_provider_name */, 101 /* battery_percentage */, |
| 122 5 /* connection_strength */); |
| 123 |
| 124 std::string carrier; |
| 125 int32_t battery_percentage; |
| 126 int32_t signal_strength; |
| 127 |
| 128 NormalizeDeviceStatus(status, &carrier, &battery_percentage, |
| 129 &signal_strength); |
| 130 |
| 131 EXPECT_EQ("cellProvider", carrier); |
| 132 EXPECT_EQ(100, battery_percentage); |
| 133 EXPECT_EQ(100, signal_strength); |
| 134 } |
| 135 |
| 136 TEST_F(DeviceStatusUtilTest, TestValidValues) { |
| 137 DeviceStatus status = CreateFakeDeviceStatus( |
| 138 "cellProvider" /* cell_provider_name */, 50 /* battery_percentage */, |
| 139 2 /* connection_strength */); |
| 140 |
| 141 std::string carrier; |
| 142 int32_t battery_percentage; |
| 143 int32_t signal_strength; |
| 144 |
| 145 NormalizeDeviceStatus(status, &carrier, &battery_percentage, |
| 146 &signal_strength); |
| 147 |
| 148 EXPECT_EQ("cellProvider", carrier); |
| 149 EXPECT_EQ(50, battery_percentage); |
| 150 EXPECT_EQ(50, signal_strength); |
| 151 } |
| 152 |
| 153 } // namespace tether |
| 154 |
| 155 } // namespace cryptauth |
| OLD | NEW |