Chromium Code Reviews| Index: chrome/test/chromedriver/commands_unittest.cc |
| diff --git a/chrome/test/chromedriver/commands_unittest.cc b/chrome/test/chromedriver/commands_unittest.cc |
| index 74078d9bc05c63c288456272d15347556656466f..c01905e955a44b27d101edd00efbe5f2a5c0b289 100644 |
| --- a/chrome/test/chromedriver/commands_unittest.cc |
| +++ b/chrome/test/chromedriver/commands_unittest.cc |
| @@ -204,10 +204,27 @@ namespace { |
| class FindElementChrome : public StubChrome { |
| public: |
| - explicit FindElementChrome(bool element_exists) |
| - : element_exists_(element_exists), called_(false) {} |
| + FindElementChrome(bool element_exists, bool only_one, int query_count) |
|
kkania
2013/01/15 22:24:36
this is a bit ugly. just wondering if there's a cl
chrisgao (Use stgao instead)
2013/01/16 19:06:24
Done.
|
| + : element_exists_(element_exists), only_one_(only_one), |
| + query_count_(query_count), current_count_(0) { |
| + if (only_one_) { |
| + base::DictionaryValue element; |
| + element.SetString("ELEMENT", "1"); |
| + result_.reset(element.DeepCopy()); |
| + } else { |
| + base::DictionaryValue element1; |
| + element1.SetString("ELEMENT", "1"); |
| + base::DictionaryValue element2; |
| + element2.SetString("ELEMENT", "2"); |
| + base::ListValue list; |
| + list.Append(element1.DeepCopy()); |
| + list.Append(element2.DeepCopy()); |
| + result_.reset(list.DeepCopy()); |
| + } |
| + } |
| virtual ~FindElementChrome() {} |
| + const base::Value* GetResult() { return result_.get(); } |
|
kkania
2013/01/15 22:24:36
i don't understand the purpose of this
chrisgao (Use stgao instead)
2013/01/16 19:06:24
Used to verified that the returned result is expec
|
| const std::string& GetFrame() { return frame_; } |
| const std::string& GetFunction() { return function_; } |
| const base::ListValue* GetArgs() { return args_.get(); } |
| @@ -217,22 +234,23 @@ class FindElementChrome : public StubChrome { |
| const std::string& function, |
| const base::ListValue& args, |
| scoped_ptr<base::Value>* result) OVERRIDE { |
| - if (element_exists_ && called_) { |
| - base::DictionaryValue element; |
| - element.SetString("ELEMENT", "1"); |
| - result->reset(element.DeepCopy()); |
| + ++current_count_; |
| + if (element_exists_ && current_count_ >= query_count_) { |
| + result->reset(result_->DeepCopy()); |
| frame_ = frame; |
| function_ = function; |
| args_.reset(args.DeepCopy()); |
| } else { |
| result->reset(base::Value::CreateNullValue()); |
| } |
| - called_ = true; |
| return Status(kOk); |
| } |
| private: |
| bool element_exists_; |
| - bool called_; |
| + bool only_one_; |
| + int query_count_; |
| + int current_count_; |
| + scoped_ptr<base::Value> result_; |
| std::string frame_; |
| std::string function_; |
| scoped_ptr<base::ListValue> args_; |
| @@ -241,7 +259,7 @@ class FindElementChrome : public StubChrome { |
| } // namespace |
| TEST(CommandsTest, SuccessfulFindElement) { |
| - FindElementChrome* chrome = new FindElementChrome(true); |
| + FindElementChrome* chrome = new FindElementChrome(true, true, 2); |
| Session session("id", scoped_ptr<Chrome>(chrome)); |
| session.implicit_wait = 100; |
| session.frame = "frame_id1"; |
| @@ -250,28 +268,23 @@ TEST(CommandsTest, SuccessfulFindElement) { |
| params.SetString("value", "a"); |
| scoped_ptr<base::Value> value; |
| ASSERT_EQ(kOk, ExecuteFindElement(&session, params, &value).code()); |
| - base::DictionaryValue* element; |
| - ASSERT_TRUE(value->GetAsDictionary(&element)); |
| - ASSERT_EQ(1U, element->size()); |
| - std::string element_id; |
| - ASSERT_TRUE(element->GetString("ELEMENT", &element_id)); |
| - ASSERT_EQ("1", element_id); |
| + ASSERT_TRUE(value.get()); |
| + ASSERT_TRUE(value->Equals(chrome->GetResult())); |
| ASSERT_EQ("frame_id1", chrome->GetFrame()); |
| ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT), |
| chrome->GetFunction()); |
| const base::ListValue* args = chrome->GetArgs(); |
| ASSERT_TRUE(args); |
| - ASSERT_EQ(1U, args->GetSize()); |
| - const base::DictionaryValue* dict; |
| - ASSERT_TRUE(args->GetDictionary(0U, &dict)); |
| - ASSERT_EQ(1U, dict->size()); |
| - std::string id; |
| - ASSERT_TRUE(dict->GetString("id", &id)); |
| - ASSERT_EQ("a", id); |
| + base::DictionaryValue param; |
| + param.SetString("id", "a"); |
| + base::ListValue expected_args; |
| + expected_args.Append(param.DeepCopy()); |
| + ASSERT_TRUE(expected_args.Equals(args)); |
| } |
| TEST(CommandsTest, FailedFindElement) { |
| - Session session("id", scoped_ptr<Chrome>(new FindElementChrome(false))); |
| + Session session("id", |
| + scoped_ptr<Chrome>(new FindElementChrome(false, true, 1))); |
|
kkania
2013/01/15 22:24:36
4 spaces; fix others too
chrisgao (Use stgao instead)
2013/01/16 19:06:24
Done.
|
| session.implicit_wait = 0; |
| base::DictionaryValue params; |
| params.SetString("using", "id"); |
| @@ -281,53 +294,8 @@ TEST(CommandsTest, FailedFindElement) { |
| ExecuteFindElement(&session, params, &value).code()); |
| } |
| -namespace { |
| - |
| -class FindElementsChrome : public StubChrome { |
| - public: |
| - explicit FindElementsChrome(bool element_exists) |
| - : element_exists_(element_exists), called_(false) {} |
| - virtual ~FindElementsChrome() {} |
| - |
| - const std::string& GetFrame() { return frame_; } |
| - const std::string& GetFunction() { return function_; } |
| - const base::ListValue* GetArgs() { return args_.get(); } |
| - |
| - // Overridden from Chrome: |
| - virtual Status CallFunction(const std::string& frame, |
| - const std::string& function, |
| - const base::ListValue& args, |
| - scoped_ptr<base::Value>* result) OVERRIDE { |
| - if (element_exists_ && called_) { |
| - base::DictionaryValue element1; |
| - element1.SetString("ELEMENT", "1"); |
| - base::DictionaryValue element2; |
| - element2.SetString("ELEMENT", "2"); |
| - base::ListValue list; |
| - list.Append(element1.DeepCopy()); |
| - list.Append(element2.DeepCopy()); |
| - result->reset(list.DeepCopy()); |
| - frame_ = frame; |
| - function_ = function; |
| - args_.reset(args.DeepCopy()); |
| - } else { |
| - result->reset(new base::ListValue()); |
| - } |
| - called_ = true; |
| - return Status(kOk); |
| - } |
| - private: |
| - bool element_exists_; |
| - bool called_; |
| - std::string frame_; |
| - std::string function_; |
| - scoped_ptr<base::ListValue> args_; |
| -}; |
| - |
| -} //namespace |
| - |
| TEST(CommandsTest, SuccessfulFindElements) { |
| - FindElementsChrome* chrome = new FindElementsChrome(true); |
| + FindElementChrome* chrome = new FindElementChrome(true, false, 2); |
| Session session("id", scoped_ptr<Chrome>(chrome)); |
| session.implicit_wait = 100; |
| session.frame = "frame_id2"; |
| @@ -336,37 +304,23 @@ TEST(CommandsTest, SuccessfulFindElements) { |
| params.SetString("value", "b"); |
| scoped_ptr<base::Value> value; |
| ASSERT_EQ(kOk, ExecuteFindElements(&session, params, &value).code()); |
| - base::ListValue* list; |
| - ASSERT_TRUE(value->GetAsList(&list)); |
| - ASSERT_EQ(2U, list->GetSize()); |
| - base::DictionaryValue* element1; |
| - ASSERT_TRUE(list->GetDictionary(0U, &element1)); |
| - ASSERT_EQ(1U, element1->size()); |
| - std::string element1_id; |
| - ASSERT_TRUE(element1->GetString("ELEMENT", &element1_id)); |
| - ASSERT_EQ("1", element1_id); |
| - base::DictionaryValue* element2; |
| - ASSERT_TRUE(list->GetDictionary(1U, &element2)); |
| - ASSERT_EQ(1U, element2->size()); |
| - std::string element2_id; |
| - ASSERT_TRUE(element2->GetString("ELEMENT", &element2_id)); |
| - ASSERT_EQ("2", element2_id); |
| + ASSERT_TRUE(value.get()); |
| + ASSERT_TRUE(value->Equals(chrome->GetResult())); |
| ASSERT_EQ("frame_id2", chrome->GetFrame()); |
| ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS), |
| chrome->GetFunction()); |
| const base::ListValue* args = chrome->GetArgs(); |
| ASSERT_TRUE(args); |
| - ASSERT_EQ(1U, args->GetSize()); |
| - const base::DictionaryValue* dict; |
| - ASSERT_TRUE(args->GetDictionary(0U, &dict)); |
| - ASSERT_EQ(1U, dict->size()); |
| - std::string name; |
| - ASSERT_TRUE(dict->GetString("name", &name)); |
| - ASSERT_EQ("b", name); |
| + base::DictionaryValue param; |
| + param.SetString("name", "b"); |
| + base::ListValue expected_args; |
| + expected_args.Append(param.DeepCopy()); |
| + ASSERT_TRUE(expected_args.Equals(args)); |
| } |
| TEST(CommandsTest, FailedFindElements) { |
| - Session session("id", scoped_ptr<Chrome>(new FindElementsChrome(false))); |
| + Session session("id", |
| + scoped_ptr<Chrome>(new FindElementChrome(false, false, 1))); |
| session.implicit_wait = 0; |
| base::DictionaryValue params; |
| params.SetString("using", "id"); |
| @@ -378,11 +332,95 @@ TEST(CommandsTest, FailedFindElements) { |
| ASSERT_EQ(0U, list->GetSize()); |
| } |
| +TEST(CommandsTest, SuccessfulFindChildElement) { |
| + FindElementChrome* chrome = new FindElementChrome(true, true, 2); |
| + Session session("id", scoped_ptr<Chrome>(chrome)); |
| + session.implicit_wait = 100; |
|
chrisgao (Use stgao instead)
2013/01/15 20:53:59
I think we need these new testcases, but maybe we
kkania
2013/01/15 22:24:36
I think it'd be better to test it in both.
|
| + session.frame = "frame_id3"; |
| + base::DictionaryValue params; |
| + params.SetString("using", "tag name"); |
| + params.SetString("value", "div"); |
| + params.SetString("id", "1"); |
| + scoped_ptr<base::Value> value; |
| + ASSERT_EQ(kOk, ExecuteFindChildElement(&session, params, &value).code()); |
| + ASSERT_TRUE(value.get()); |
| + ASSERT_TRUE(value->Equals(chrome->GetResult())); |
| + ASSERT_EQ("frame_id3", chrome->GetFrame()); |
|
kkania
2013/01/15 22:24:36
i think a lot of this code is duplicated, right? c
chrisgao (Use stgao instead)
2013/01/16 19:06:24
Done.
|
| + ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT), |
| + chrome->GetFunction()); |
| + const base::ListValue* args = chrome->GetArgs(); |
| + ASSERT_TRUE(args); |
| + base::DictionaryValue locator_param; |
| + locator_param.SetString("tag name", "div"); |
| + base::DictionaryValue root_element_param; |
| + root_element_param.SetString("ELEMENT", "1"); |
| + base::ListValue expected_args; |
| + expected_args.Append(locator_param.DeepCopy()); |
| + expected_args.Append(root_element_param.DeepCopy()); |
| + ASSERT_TRUE(expected_args.Equals(args)); |
| +} |
| + |
| +TEST(CommandsTest, FailedFindChildElement) { |
| + Session session("id", |
| + scoped_ptr<Chrome>(new FindElementChrome(false, true, 1))); |
| + session.implicit_wait = 0; |
| + base::DictionaryValue params; |
| + params.SetString("using", "id"); |
| + params.SetString("value", "a"); |
| + params.SetString("id", "1"); |
| + scoped_ptr<base::Value> value; |
| + ASSERT_EQ(kNoSuchElement, |
| + ExecuteFindChildElement(&session, params, &value).code()); |
| +} |
| + |
| +TEST(CommandsTest, SuccessfulFindChildElements) { |
| + FindElementChrome* chrome = new FindElementChrome(true, false, 2); |
| + Session session("id", scoped_ptr<Chrome>(chrome)); |
| + session.implicit_wait = 100; |
| + session.frame = "frame_id4"; |
| + base::DictionaryValue params; |
| + params.SetString("using", "class name"); |
| + params.SetString("value", "c"); |
| + params.SetString("id", "1"); |
| + scoped_ptr<base::Value> value; |
| + ASSERT_EQ(kOk, ExecuteFindChildElements(&session, params, &value).code()); |
| + ASSERT_TRUE(value.get()); |
| + ASSERT_TRUE(value->Equals(chrome->GetResult())); |
| + ASSERT_EQ("frame_id4", chrome->GetFrame()); |
| + ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS), |
| + chrome->GetFunction()); |
| + const base::ListValue* args = chrome->GetArgs(); |
| + ASSERT_TRUE(args); |
| + base::DictionaryValue locator_param; |
| + locator_param.SetString("class name", "c"); |
| + base::DictionaryValue root_element_param; |
| + root_element_param.SetString("ELEMENT", "1"); |
| + base::ListValue expected_args; |
| + expected_args.Append(locator_param.DeepCopy()); |
| + expected_args.Append(root_element_param.DeepCopy()); |
| + ASSERT_TRUE(expected_args.Equals(args)); |
| +} |
| + |
| +TEST(CommandsTest, FailedFindChildElements) { |
| + Session session("id", |
| + scoped_ptr<Chrome>(new FindElementChrome(false, false, 1))); |
| + session.implicit_wait = 0; |
|
kkania
2013/01/15 22:24:36
isn't this set to 0 by default; fix the other = 0
chrisgao (Use stgao instead)
2013/01/16 19:06:24
Done.
|
| + base::DictionaryValue params; |
| + params.SetString("using", "id"); |
| + params.SetString("value", "a"); |
| + params.SetString("id", "1"); |
| + scoped_ptr<base::Value> value; |
| + ASSERT_EQ(kOk, ExecuteFindChildElements(&session, params, &value).code()); |
| + base::ListValue* list; |
| + ASSERT_TRUE(value->GetAsList(&list)); |
| + ASSERT_EQ(0U, list->GetSize()); |
| +} |
| + |
| namespace { |
| class ErrorCallFunctionChrome : public StubChrome { |
| public: |
| - ErrorCallFunctionChrome() {} |
| + explicit ErrorCallFunctionChrome(StatusCode code) : code_(code) {} |
| virtual ~ErrorCallFunctionChrome() {} |
| // Overridden from Chrome: |
| @@ -390,14 +428,18 @@ class ErrorCallFunctionChrome : public StubChrome { |
| const std::string& function, |
| const base::ListValue& args, |
| scoped_ptr<base::Value>* result) OVERRIDE { |
| - return Status(kUnknownError); |
| + return Status(code_); |
| } |
| + |
| + private: |
| + StatusCode code_; |
| }; |
| } // namespace |
| TEST(CommandsTest, ErrorFindElement) { |
| - Session session("id", scoped_ptr<Chrome>(new ErrorCallFunctionChrome())); |
| + Session session("id", |
| + scoped_ptr<Chrome>(new ErrorCallFunctionChrome(kUnknownError))); |
| base::DictionaryValue params; |
| params.SetString("using", "id"); |
| params.SetString("value", "a"); |
| @@ -406,3 +448,17 @@ TEST(CommandsTest, ErrorFindElement) { |
| ASSERT_EQ(kUnknownError, |
| ExecuteFindElements(&session, params, &value).code()); |
| } |
| + |
| +TEST(CommandsTest, ErrorFindChildElement) { |
| + Session session("id", |
| + scoped_ptr<Chrome>(new ErrorCallFunctionChrome(kStaleElementReference))); |
| + base::DictionaryValue params; |
| + params.SetString("using", "id"); |
| + params.SetString("value", "a"); |
| + params.SetString("id", "1"); |
| + scoped_ptr<base::Value> value; |
| + ASSERT_EQ(kStaleElementReference, |
| + ExecuteFindChildElement(&session, params, &value).code()); |
| + ASSERT_EQ(kStaleElementReference, |
| + ExecuteFindChildElements(&session, params, &value).code()); |
| +} |