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