1 #include <stdint.h>
2 #include "gtest/gtest.h"
3 #include "mozilla/XULStore.h"
4 #include "nsCOMPtr.h"
5 #include "nsString.h"
6 
7 using mozilla::XULStoreIterator;
8 using mozilla::XULStore::GetAttrs;
9 using mozilla::XULStore::GetIDs;
10 using mozilla::XULStore::GetValue;
11 using mozilla::XULStore::HasValue;
12 using mozilla::XULStore::RemoveValue;
13 using mozilla::XULStore::SetValue;
14 
TEST(XULStore,SetGetValue)15 TEST(XULStore, SetGetValue)
16 {
17   nsAutoString doc(u"SetGetValue"_ns);
18   nsAutoString id(u"foo"_ns);
19   nsAutoString attr(u"bar"_ns);
20   nsAutoString value;
21 
22   EXPECT_EQ(GetValue(doc, id, attr, value), NS_OK);
23   EXPECT_TRUE(value.EqualsASCII(""));
24 
25   {
26     nsAutoString value(u"baz"_ns);
27     EXPECT_EQ(SetValue(doc, id, attr, value), NS_OK);
28   }
29 
30   EXPECT_EQ(GetValue(doc, id, attr, value), NS_OK);
31   EXPECT_TRUE(value.EqualsASCII("baz"));
32 }
33 
TEST(XULStore,HasValue)34 TEST(XULStore, HasValue)
35 {
36   nsAutoString doc(u"HasValue"_ns);
37   nsAutoString id(u"foo"_ns);
38   nsAutoString attr(u"bar"_ns);
39   bool hasValue = true;
40   EXPECT_EQ(HasValue(doc, id, attr, hasValue), NS_OK);
41   EXPECT_FALSE(hasValue);
42   nsAutoString value(u"baz"_ns);
43   EXPECT_EQ(SetValue(doc, id, attr, value), NS_OK);
44   EXPECT_EQ(HasValue(doc, id, attr, hasValue), NS_OK);
45   EXPECT_TRUE(hasValue);
46 }
47 
TEST(XULStore,RemoveValue)48 TEST(XULStore, RemoveValue)
49 {
50   nsAutoString doc(u"RemoveValue"_ns);
51   nsAutoString id(u"foo"_ns);
52   nsAutoString attr(u"bar"_ns);
53   nsAutoString value(u"baz"_ns);
54   EXPECT_EQ(SetValue(doc, id, attr, value), NS_OK);
55   EXPECT_EQ(GetValue(doc, id, attr, value), NS_OK);
56   EXPECT_TRUE(value.EqualsASCII("baz"));
57   EXPECT_EQ(RemoveValue(doc, id, attr), NS_OK);
58   EXPECT_EQ(GetValue(doc, id, attr, value), NS_OK);
59   EXPECT_TRUE(value.EqualsASCII(""));
60 }
61 
TEST(XULStore,GetIDsIterator)62 TEST(XULStore, GetIDsIterator)
63 {
64   nsAutoString doc(u"idIterDoc"_ns);
65   nsAutoString id1(u"id1"_ns);
66   nsAutoString id2(u"id2"_ns);
67   nsAutoString id3(u"id3"_ns);
68   nsAutoString attr(u"attr"_ns);
69   nsAutoString value(u"value"_ns);
70   nsAutoString id;
71 
72   // Confirm that the store doesn't have any IDs yet.
73   mozilla::UniquePtr<XULStoreIterator> iter;
74   EXPECT_EQ(GetIDs(doc, iter), NS_OK);
75   EXPECT_FALSE(iter->HasMore());
76   // EXPECT_EQ(iter->GetNext(&id), NS_ERROR_FAILURE);
77 
78   // Insert with IDs in non-alphanumeric order to confirm
79   // that store will order them when iterating them.
80   EXPECT_EQ(SetValue(doc, id3, attr, value), NS_OK);
81   EXPECT_EQ(SetValue(doc, id1, attr, value), NS_OK);
82   EXPECT_EQ(SetValue(doc, id2, attr, value), NS_OK);
83 
84   // Insert different ID for another doc to confirm that store
85   // won't return it when iterating IDs for our doc.
86   nsAutoString otherDoc(u"otherDoc"_ns);
87   nsAutoString otherID(u"otherID"_ns);
88   EXPECT_EQ(SetValue(otherDoc, otherID, attr, value), NS_OK);
89 
90   EXPECT_EQ(GetIDs(doc, iter), NS_OK);
91   EXPECT_TRUE(iter->HasMore());
92   EXPECT_EQ(iter->GetNext(&id), NS_OK);
93   EXPECT_TRUE(id.EqualsASCII("id1"));
94   EXPECT_TRUE(iter->HasMore());
95   EXPECT_EQ(iter->GetNext(&id), NS_OK);
96   EXPECT_TRUE(id.EqualsASCII("id2"));
97   EXPECT_TRUE(iter->HasMore());
98   EXPECT_EQ(iter->GetNext(&id), NS_OK);
99   EXPECT_TRUE(id.EqualsASCII("id3"));
100   EXPECT_FALSE(iter->HasMore());
101 }
102 
TEST(XULStore,GetAttributeIterator)103 TEST(XULStore, GetAttributeIterator)
104 {
105   nsAutoString doc(u"attrIterDoc"_ns);
106   nsAutoString id(u"id"_ns);
107   nsAutoString attr1(u"attr1"_ns);
108   nsAutoString attr2(u"attr2"_ns);
109   nsAutoString attr3(u"attr3"_ns);
110   nsAutoString value(u"value"_ns);
111   nsAutoString attr;
112 
113   mozilla::UniquePtr<XULStoreIterator> iter;
114   EXPECT_EQ(GetAttrs(doc, id, iter), NS_OK);
115   EXPECT_FALSE(iter->HasMore());
116   // EXPECT_EQ(iter->GetNext(&attr), NS_ERROR_FAILURE);
117 
118   // Insert with attributes in non-alphanumeric order to confirm
119   // that store will order them when iterating them.
120   EXPECT_EQ(SetValue(doc, id, attr3, value), NS_OK);
121   EXPECT_EQ(SetValue(doc, id, attr1, value), NS_OK);
122   EXPECT_EQ(SetValue(doc, id, attr2, value), NS_OK);
123 
124   // Insert different attribute for another ID to confirm that store
125   // won't return it when iterating attributes for our ID.
126   nsAutoString otherID(u"otherID"_ns);
127   nsAutoString otherAttr(u"otherAttr"_ns);
128   EXPECT_EQ(SetValue(doc, otherID, otherAttr, value), NS_OK);
129 
130   EXPECT_EQ(GetAttrs(doc, id, iter), NS_OK);
131   EXPECT_TRUE(iter->HasMore());
132   EXPECT_EQ(iter->GetNext(&attr), NS_OK);
133   EXPECT_TRUE(attr.EqualsASCII("attr1"));
134   EXPECT_TRUE(iter->HasMore());
135   EXPECT_EQ(iter->GetNext(&attr), NS_OK);
136   EXPECT_TRUE(attr.EqualsASCII("attr2"));
137   EXPECT_TRUE(iter->HasMore());
138   EXPECT_EQ(iter->GetNext(&attr), NS_OK);
139   EXPECT_TRUE(attr.EqualsASCII("attr3"));
140   EXPECT_FALSE(iter->HasMore());
141 }
142