buffet: Migrate StateChange to native_types::Object.
chromeos::Any is convenient for DBus, but the rest of
the system prefers to work with higher level abstractions.
Also it is somewhat too late to analyze Any when processing
StateChange, as to do that properly one needs full package
definition.
BUG=chromium:434767
TEST=cros_workon_make --test buffet
Change-Id: Iaf12c2e8e7e8f9c84f6492d9fbd8a495be3c0a94
Reviewed-on: https://chromium-review.googlesource.com/231035
Tested-by: Anton Muhin <antonm@chromium.org>
Reviewed-by: Anton Muhin <antonm@chromium.org>
Commit-Queue: Anton Muhin <antonm@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/commands/unittest_utils.h b/buffet/commands/unittest_utils.h
index 665a2d1..577405a 100644
--- a/buffet/commands/unittest_utils.h
+++ b/buffet/commands/unittest_utils.h
@@ -10,6 +10,9 @@
#include <base/values.h>
+#include "buffet/commands/prop_types.h"
+#include "buffet/commands/prop_values.h"
+
namespace buffet {
namespace unittests {
@@ -25,6 +28,34 @@
// apostrophes for easy comparisons in C++ source code.
std::string ValueToString(const base::Value* value);
+template <typename PV, typename T> std::shared_ptr<const PV>
+make_prop_value(const PropType* type, const T& value) {
+ auto result = std::make_shared<PV>(type);
+ result->SetValue(value);
+ return result;
+}
+
+inline std::shared_ptr<const IntValue> make_int_prop_value(int value) {
+ static const PropType* int_prop_type = new IntPropType();
+ return make_prop_value<IntValue, int>(int_prop_type, value);
+}
+
+inline std::shared_ptr<const DoubleValue> make_double_prop_value(double value) {
+ static const PropType* double_prop_type = new DoublePropType();
+ return make_prop_value<DoubleValue, double>(double_prop_type, value);
+}
+
+inline std::shared_ptr<const BooleanValue> make_bool_prop_value(bool value) {
+ static const PropType* boolean_prop_type = new BooleanPropType();
+ return make_prop_value<BooleanValue, bool>(boolean_prop_type, value);
+}
+
+inline std::shared_ptr<const StringValue>
+make_string_prop_value(const std::string& value) {
+ static const PropType* string_prop_type = new StringPropType();
+ return make_prop_value<StringValue, std::string>(string_prop_type, value);
+}
+
} // namespace unittests
} // namespace buffet
diff --git a/buffet/states/mock_state_change_queue_interface.h b/buffet/states/mock_state_change_queue_interface.h
index 11a84b4..54b2ccc 100644
--- a/buffet/states/mock_state_change_queue_interface.h
+++ b/buffet/states/mock_state_change_queue_interface.h
@@ -18,7 +18,7 @@
MOCK_CONST_METHOD0(IsEmpty, bool());
MOCK_METHOD2(NotifyPropertiesUpdated,
bool(base::Time timestamp,
- chromeos::VariantDictionary changed_properties));
+ native_types::Object changed_properties));
MOCK_METHOD0(GetAndClearRecordedStateChanges, std::vector<StateChange>());
};
diff --git a/buffet/states/state_change_queue.cc b/buffet/states/state_change_queue.cc
index f27b339..74ac682 100644
--- a/buffet/states/state_change_queue.cc
+++ b/buffet/states/state_change_queue.cc
@@ -15,7 +15,7 @@
bool StateChangeQueue::NotifyPropertiesUpdated(
base::Time timestamp,
- chromeos::VariantDictionary changed_properties) {
+ native_types::Object changed_properties) {
DCHECK(thread_checker_.CalledOnValidThread());
auto it = state_changes_.lower_bound(timestamp);
if (it == state_changes_.end() || it->first != timestamp) {
diff --git a/buffet/states/state_change_queue.h b/buffet/states/state_change_queue.h
index 2ca46e6..9181ef4 100644
--- a/buffet/states/state_change_queue.h
+++ b/buffet/states/state_change_queue.h
@@ -24,7 +24,7 @@
bool IsEmpty() const override { return state_changes_.empty(); }
bool NotifyPropertiesUpdated(
base::Time timestamp,
- chromeos::VariantDictionary changed_properties) override;
+ native_types::Object changed_properties) override;
std::vector<StateChange> GetAndClearRecordedStateChanges() override;
private:
@@ -38,7 +38,7 @@
const size_t max_queue_size_;
// Accumulated list of device state change notifications.
- std::map<base::Time, chromeos::VariantDictionary> state_changes_;
+ std::map<base::Time, native_types::Object> state_changes_;
DISALLOW_COPY_AND_ASSIGN(StateChangeQueue);
};
diff --git a/buffet/states/state_change_queue_interface.h b/buffet/states/state_change_queue_interface.h
index 84704dd..0532788 100644
--- a/buffet/states/state_change_queue_interface.h
+++ b/buffet/states/state_change_queue_interface.h
@@ -10,6 +10,8 @@
#include <base/time/time.h>
#include <chromeos/variant_dictionary.h>
+#include "commands/schema_utils.h"
+
namespace buffet {
// A simple notification record event to track device state changes.
@@ -18,10 +20,10 @@
// which were updated at the time the event was recorded.
struct StateChange {
StateChange(base::Time time,
- chromeos::VariantDictionary properties)
+ native_types::Object properties)
: timestamp(time), changed_properties(std::move(properties)) {}
base::Time timestamp;
- chromeos::VariantDictionary changed_properties;
+ native_types::Object changed_properties;
};
// An abstract interface to StateChangeQueue to record and retrieve state
@@ -34,7 +36,7 @@
// Called by StateManager when device state properties are updated.
virtual bool NotifyPropertiesUpdated(
base::Time timestamp,
- chromeos::VariantDictionary changed_properties) = 0;
+ native_types::Object changed_properties) = 0;
// Returns the recorded state changes since last time this method was called.
virtual std::vector<StateChange> GetAndClearRecordedStateChanges() = 0;
diff --git a/buffet/states/state_change_queue_unittest.cc b/buffet/states/state_change_queue_unittest.cc
index 01f5f9f..68fadcc 100644
--- a/buffet/states/state_change_queue_unittest.cc
+++ b/buffet/states/state_change_queue_unittest.cc
@@ -7,6 +7,8 @@
#include <gtest/gtest.h>
+#include "buffet/commands/unittest_utils.h"
+
namespace buffet {
class StateChangeQueueTest : public ::testing::Test {
@@ -30,7 +32,7 @@
TEST_F(StateChangeQueueTest, UpdateOne) {
StateChange change{
base::Time::Now(),
- chromeos::VariantDictionary{{"prop.name", int{23}}}
+ native_types::Object{{"prop.name", unittests::make_int_prop_value(23)}}
};
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(change.timestamp,
change.changed_properties));
@@ -46,16 +48,16 @@
TEST_F(StateChangeQueueTest, UpdateMany) {
StateChange change1{
base::Time::Now(),
- chromeos::VariantDictionary{{"prop.name1", int{23}}}
+ native_types::Object{{"prop.name1", unittests::make_int_prop_value(23)}}
};
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(change1.timestamp,
change1.changed_properties));
StateChange change2{
base::Time::Now(),
- chromeos::VariantDictionary{
- {"prop.name1", int{17}},
- {"prop.name2", double{1.0}},
- {"prop.name3", bool{false}},
+ native_types::Object{
+ {"prop.name1", unittests::make_int_prop_value(17)},
+ {"prop.name2", unittests::make_double_prop_value(1.0)},
+ {"prop.name3", unittests::make_bool_prop_value(false)},
}
};
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(change2.timestamp,
@@ -77,29 +79,29 @@
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(
timestamp,
- chromeos::VariantDictionary{{"prop.name1", int{1}}}));
+ native_types::Object{{"prop.name1", unittests::make_int_prop_value(1)}}));
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(
timestamp,
- chromeos::VariantDictionary{{"prop.name2", int{2}}}));
+ native_types::Object{{"prop.name2", unittests::make_int_prop_value(2)}}));
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(
timestamp,
- chromeos::VariantDictionary{{"prop.name1", int{3}}}));
+ native_types::Object{{"prop.name1", unittests::make_int_prop_value(3)}}));
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(
timestamp + time_delta,
- chromeos::VariantDictionary{{"prop.name1", int{4}}}));
+ native_types::Object{{"prop.name1", unittests::make_int_prop_value(4)}}));
auto changes = queue_->GetAndClearRecordedStateChanges();
ASSERT_EQ(2, changes.size());
- chromeos::VariantDictionary expected1{
- {"prop.name1", int{3}},
- {"prop.name2", int{2}},
+ native_types::Object expected1{
+ {"prop.name1", unittests::make_int_prop_value(3)},
+ {"prop.name2", unittests::make_int_prop_value(2)},
};
- chromeos::VariantDictionary expected2{
- {"prop.name1", int{4}},
+ native_types::Object expected2{
+ {"prop.name1", unittests::make_int_prop_value(4)},
};
EXPECT_EQ(timestamp, changes[0].timestamp);
EXPECT_EQ(expected1, changes[0].changed_properties);
@@ -115,39 +117,39 @@
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(
start_time,
- chromeos::VariantDictionary{
- {"prop.name1", int{1}},
- {"prop.name2", int{2}},
+ native_types::Object{
+ {"prop.name1", unittests::make_int_prop_value(1)},
+ {"prop.name2", unittests::make_int_prop_value(2)},
}));
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(
start_time + time_delta1,
- chromeos::VariantDictionary{
- {"prop.name1", int{3}},
- {"prop.name3", int{4}},
+ native_types::Object{
+ {"prop.name1", unittests::make_int_prop_value(3)},
+ {"prop.name3", unittests::make_int_prop_value(4)},
}));
ASSERT_TRUE(queue_->NotifyPropertiesUpdated(
start_time + time_delta2,
- chromeos::VariantDictionary{
- {"prop.name10", int{10}},
- {"prop.name11", int{11}},
+ native_types::Object{
+ {"prop.name10", unittests::make_int_prop_value(10)},
+ {"prop.name11", unittests::make_int_prop_value(11)},
}));
auto changes = queue_->GetAndClearRecordedStateChanges();
ASSERT_EQ(2, changes.size());
- chromeos::VariantDictionary expected1{
- {"prop.name1", int{3}},
- {"prop.name2", int{2}},
- {"prop.name3", int{4}},
+ native_types::Object expected1{
+ {"prop.name1", unittests::make_int_prop_value(3)},
+ {"prop.name2", unittests::make_int_prop_value(2)},
+ {"prop.name3", unittests::make_int_prop_value(4)},
};
EXPECT_EQ(start_time + time_delta1, changes[0].timestamp);
EXPECT_EQ(expected1, changes[0].changed_properties);
- chromeos::VariantDictionary expected2{
- {"prop.name10", int{10}},
- {"prop.name11", int{11}},
+ native_types::Object expected2{
+ {"prop.name10", unittests::make_int_prop_value(10)},
+ {"prop.name11", unittests::make_int_prop_value(11)},
};
EXPECT_EQ(start_time + time_delta2, changes[1].timestamp);
EXPECT_EQ(expected2, changes[1].changed_properties);
diff --git a/buffet/states/state_manager.cc b/buffet/states/state_manager.cc
index 80fdc8c..b621d02 100644
--- a/buffet/states/state_manager.cc
+++ b/buffet/states/state_manager.cc
@@ -110,7 +110,8 @@
if (!package->SetPropertyValue(property_name, value, error))
return false;
- chromeos::VariantDictionary prop_set{{full_property_name, value}};
+ native_types::Object prop_set{{full_property_name,
+ package->GetProperty(property_name)}};
state_change_queue_->NotifyPropertiesUpdated(timestamp, prop_set);
return true;
}
diff --git a/buffet/states/state_manager_unittest.cc b/buffet/states/state_manager_unittest.cc
index bdfad8c..b035175 100644
--- a/buffet/states/state_manager_unittest.cc
+++ b/buffet/states/state_manager_unittest.cc
@@ -103,8 +103,8 @@
}
TEST_F(StateManagerTest, SetPropertyValue) {
- chromeos::VariantDictionary expected_prop_set{
- {"terminator.target", std::string{"John Connor"}},
+ native_types::Object expected_prop_set{
+ {"terminator.target", unittests::make_string_prop_value("John Connor")},
};
base::Time timestamp = base::Time::Now();
EXPECT_CALL(mock_state_change_queue_,
@@ -166,8 +166,8 @@
std::vector<StateChange> expected_val;
expected_val.emplace_back(
timestamp,
- chromeos::VariantDictionary{{"terminator.target",
- std::string{"John Connor"}}});
+ native_types::Object{{"terminator.target",
+ unittests::make_string_prop_value("John Connor")}});
EXPECT_CALL(mock_state_change_queue_, GetAndClearRecordedStateChanges())
.WillOnce(Return(expected_val));
auto changes = mgr_->GetAndClearRecordedStateChanges();
@@ -177,5 +177,4 @@
changes.back().changed_properties);
}
-
} // namespace buffet
diff --git a/buffet/states/state_package.h b/buffet/states/state_package.h
index b2e1383..1430b5c 100644
--- a/buffet/states/state_package.h
+++ b/buffet/states/state_package.h
@@ -65,6 +65,13 @@
const chromeos::Any& value,
chromeos::ErrorPtr* error);
+ std::shared_ptr<const PropValue>
+ GetProperty(const std::string& property_name) const {
+ auto it = values_.find(property_name);
+ return it != values_.end() ?
+ it->second : std::shared_ptr<const PropValue>{};
+ }
+
// Returns the name of the this package.
const std::string& GetName() const { return name_; }