Index: network_property_test.cc |
diff --git a/network_property_test.cc b/network_property_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ae1ee6e6b5bff6408b7c409e5cfd34923783c1bc |
--- /dev/null |
+++ b/network_property_test.cc |
@@ -0,0 +1,154 @@ |
+// Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <dlfcn.h> |
+#include <glib-object.h> |
+#include <map> |
+#include <vector> |
+ |
+#include <base/logging.h> |
+#include <base/time.h> |
+#include <base/values.h> |
+ |
+#include "chromeos_cros_api.h" // NOLINT |
+#include "chromeos_network.h" // NOLINT |
+#include "chromeos/dbus/dbus.h" // NOLINT |
+#include "chromeos/glib/object.h" // NOLINT |
+#include "monitor_utils.h" //NOLINT |
+ |
+static void PrintProperty(const char* path, |
+ const char* key, |
+ const Value* value) { |
+ std::string prelude("PropertyChanged ["); |
+ prelude += path; |
+ prelude += "] "; |
+ prelude += key; |
+ prelude += " : "; |
+ if (value->IsType(Value::TYPE_STRING)) { |
+ std::string strval; |
+ value->GetAsString(&strval); |
+ LOG(INFO) << prelude << "\"" << strval << "\""; |
+ } else if (value->IsType(Value::TYPE_BOOLEAN)) { |
+ bool boolval; |
+ value->GetAsBoolean(&boolval); |
+ LOG(INFO) << prelude << boolval; |
+ } else if (value->IsType(Value::TYPE_INTEGER)) { |
+ int intval; |
+ value->GetAsInteger(&intval); |
+ LOG(INFO) << prelude << intval; |
+ } else if (value->IsType(Value::TYPE_LIST)) { |
+ const ListValue* list = static_cast<const ListValue*>(value); |
+ Value *itemval; |
+ std::string liststr; |
+ size_t index = 0; |
+ while (list->Get(index, &itemval)) { |
+ if (!itemval->IsType(Value::TYPE_STRING)) { |
+ ++index; |
+ continue; |
+ } |
+ std::string itemstr; |
+ itemval->GetAsString(&itemstr); |
+ liststr += itemstr; |
+ ++index; |
+ if (index < list->GetSize()) |
+ liststr += ", "; |
+ } |
+ LOG(INFO) << prelude << "\"" << liststr << "\""; |
+ } else if (value->IsType(Value::TYPE_DICTIONARY)) { |
+ const DictionaryValue* dict = static_cast<const DictionaryValue*>(value); |
+ std::string items; |
+ std::string itemval; |
+ size_t n = 0; |
+ DictionaryValue::key_iterator iter = dict->begin_keys(); |
+ while (iter != dict->end_keys()) { |
+ std::string key = *iter; |
+ items += "{'" + key + "': '"; |
+ if (dict->GetStringWithoutPathExpansion(key, &itemval)) |
+ items += itemval + "'}"; |
+ else |
+ items += "<not-a-string>'}"; |
+ if (n < dict->size()) |
+ items += ", "; |
+ ++iter; |
+ ++n; |
+ } |
+ LOG(INFO) << prelude << items; |
+ } else |
+ LOG(INFO) << prelude << "<type " << value->GetType() << ">"; |
+} |
+ |
+class CallbackMonitorNetwork { |
+ public: |
+ static void Run(void *object, |
+ const char *path, |
+ const char *key, |
+ const Value* value) { |
+ PrintProperty(path, key, value); |
+ } |
+}; |
+ |
+// A simple program exercising the {Set,Clear}{Device,Service}Property methods. |
Eric Shienbrood
2011/04/21 21:48:26
This is all a little bit bogus, since (a) the empt
|
+int main(int argc, const char** argv) { |
+ ::g_type_init(); |
+ GMainLoop* loop = ::g_main_loop_new(NULL, false); |
+ |
+ DCHECK(loop) << "Failed to create main loop"; |
+ if (!LoadCrosLibrary(argv)) |
+ LOG(INFO) << "Failed to load cros .so"; |
+ |
+ if (argc != 4) { |
+ LOG(INFO) << "Program must be invoked with one path argument, " |
+ << "one property name, and one value."; |
+ return 1; |
+ } |
+ |
+ chromeos::PropertyChangeMonitor device_mon; |
+ if (strncmp(argv[1], "/service/", 9) == 0) { |
+ |
+ LOG(INFO) << "Requesting properties messages on service '" |
+ << argv[1] << "'"; |
+ |
+ device_mon = chromeos::MonitorNetworkService(&CallbackMonitorNetwork::Run, |
+ argv[1], NULL); |
+ if (strlen(argv[3]) != 0) { |
+ LOG(INFO) << "Setting property '" << argv[2] << "' on '" |
+ << argv[1] << "'"; |
+ chromeos::SetNetworkServiceProperty(argv[1], argv[2], |
+ Value::CreateStringValue(argv[3])); |
+ } else { |
+ LOG(INFO) << "Clearing property '" << argv[2] << "' on '" |
+ << argv[1] << "'"; |
+ chromeos::ClearNetworkServiceProperty(argv[1], argv[2]); |
+ } |
+ } else if (strncmp(argv[1], "/device/", 8) == 0) { |
+ LOG(INFO) << "Requesting properties messages on device '" << argv[1] << "'"; |
+ |
+ device_mon = chromeos::MonitorNetworkDevice(&CallbackMonitorNetwork::Run, |
+ argv[1], NULL); |
+ |
+ if (strlen(argv[3]) != 0) { |
+ LOG(INFO) << "Setting property '" << argv[2] << "' on '" |
+ << argv[1] << "'"; |
+ chromeos::SetNetworkDeviceProperty(argv[1], argv[2], |
+ Value::CreateStringValue(argv[3])); |
+ } else { |
+ LOG(INFO) << "Clearing property '" << argv[2] << "' on '" |
+ << argv[1] << "'"; |
+ chromeos::ClearNetworkDeviceProperty(argv[1], argv[2]); |
+ } |
+ } else { |
+ LOG(INFO) << "Don't know what to do with path '" << argv[1] << "' " |
+ << "neither a device nor a service"; |
+ return 1; |
+ } |
+ |
+ LOG(INFO) << "Starting g_main_loop."; |
+ |
+ ::g_main_loop_run(loop); |
+ |
+ LOG(INFO) << "Shutting down."; |
+ |
+ chromeos::DisconnectPropertyChangeMonitor(device_mon); |
+ return 0; |
+} |