view_prop_unittest.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2011 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 "ui/base/view_prop.h"
  5. #include <memory>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace {
  8. const char kKey1[] = "key_1";
  9. const char kKey2[] = "key_2";
  10. } // namespace
  11. namespace ui {
  12. // Test a handful of viewprop assertions.
  13. TEST(ViewPropTest, Basic) {
  14. gfx::AcceleratedWidget nv1 = reinterpret_cast<gfx::AcceleratedWidget>(1);
  15. gfx::AcceleratedWidget nv2 = reinterpret_cast<gfx::AcceleratedWidget>(2);
  16. void* data1 = reinterpret_cast<void*>(11);
  17. void* data2 = reinterpret_cast<void*>(12);
  18. // Initial value for a new view/key pair should be NULL.
  19. EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1));
  20. {
  21. // Register a value for a view/key pair.
  22. ViewProp prop(nv1, kKey1, data1);
  23. EXPECT_EQ(data1, ViewProp::GetValue(nv1, kKey1));
  24. }
  25. // The property fell out of scope, so the value should now be NULL.
  26. EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1));
  27. {
  28. // Register a value for a view/key pair.
  29. std::unique_ptr<ViewProp> v1(new ViewProp(nv1, kKey1, data1));
  30. EXPECT_EQ(data1, ViewProp::GetValue(nv1, kKey1));
  31. // Register a value for the same view/key pair.
  32. std::unique_ptr<ViewProp> v2(new ViewProp(nv1, kKey1, data2));
  33. // The new value should take over.
  34. EXPECT_EQ(data2, ViewProp::GetValue(nv1, kKey1));
  35. // Null out the first ViewProp, which should NULL out the value.
  36. v1.reset(NULL);
  37. EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1));
  38. }
  39. // The property fell out of scope, so the value should now be NULL.
  40. EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1));
  41. {
  42. // Register a value for a view/key pair.
  43. std::unique_ptr<ViewProp> v1(new ViewProp(nv1, kKey1, data1));
  44. std::unique_ptr<ViewProp> v2(new ViewProp(nv2, kKey2, data2));
  45. EXPECT_EQ(data1, ViewProp::GetValue(nv1, kKey1));
  46. EXPECT_EQ(data2, ViewProp::GetValue(nv2, kKey2));
  47. v1.reset(NULL);
  48. EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1));
  49. EXPECT_EQ(data2, ViewProp::GetValue(nv2, kKey2));
  50. v2.reset(NULL);
  51. EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1));
  52. EXPECT_EQ(NULL, ViewProp::GetValue(nv2, kKey2));
  53. }
  54. }
  55. } // namespace ui