| /* |
| * Copyright (C) 2018 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| #include "tinyxml_layout_parser.h" |
| |
| #include "gtest/gtest.h" |
| |
| using startop::CanCompileLayout; |
| using std::string; |
| |
| namespace { |
| void ValidateXmlText(const string& xml, bool expected) { |
| tinyxml2::XMLDocument doc; |
| doc.Parse(xml.c_str()); |
| EXPECT_EQ(CanCompileLayout(doc), expected); |
| } |
| } // namespace |
| |
| TEST(LayoutValidationTest, SingleButtonLayout) { |
| const string xml = R"(<?xml version="1.0" encoding="utf-8"?> |
| <Button xmlns:android="http://schemas.android.com/apk/res/android" |
| android:layout_width="match_parent" |
| android:layout_height="match_parent" |
| android:text="Hello, World!"> |
| |
| </Button>)"; |
| ValidateXmlText(xml, /*expected=*/true); |
| } |
| |
| TEST(LayoutValidationTest, SmallConstraintLayout) { |
| const string xml = R"(<?xml version="1.0" encoding="utf-8"?> |
| <android.support.constraint.ConstraintLayout |
| xmlns:android="http://schemas.android.com/apk/res/android" |
| xmlns:app="http://schemas.android.com/apk/res-auto" |
| xmlns:tools="http://schemas.android.com/tools" |
| android:layout_width="match_parent" |
| android:layout_height="match_parent"> |
| |
| <Button |
| android:id="@+id/button6" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:layout_marginEnd="16dp" |
| android:layout_marginBottom="16dp" |
| android:text="Button" |
| app:layout_constraintBottom_toBottomOf="parent" |
| app:layout_constraintEnd_toEndOf="parent" /> |
| |
| <Button |
| android:id="@+id/button7" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:layout_marginEnd="8dp" |
| android:layout_marginBottom="16dp" |
| android:text="Button2" |
| app:layout_constraintBottom_toBottomOf="parent" |
| app:layout_constraintEnd_toStartOf="@+id/button6" /> |
| |
| <Button |
| android:id="@+id/button8" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:layout_marginEnd="8dp" |
| android:layout_marginBottom="16dp" |
| android:text="Button1" |
| app:layout_constraintBottom_toBottomOf="parent" |
| app:layout_constraintEnd_toStartOf="@+id/button7" /> |
| </android.support.constraint.ConstraintLayout>)"; |
| ValidateXmlText(xml, /*expected=*/true); |
| } |
| |
| TEST(LayoutValidationTest, MergeNode) { |
| const string xml = R"(<?xml version="1.0" encoding="utf-8"?> |
| <merge xmlns:android="http://schemas.android.com/apk/res/android"> |
| |
| <TextView |
| android:id="@+id/textView3" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:text="TextView" /> |
| |
| <Button |
| android:id="@+id/button9" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:text="Button" /> |
| </merge>)"; |
| ValidateXmlText(xml, /*expected=*/false); |
| } |
| |
| TEST(LayoutValidationTest, IncludeLayout) { |
| const string xml = R"(<?xml version="1.0" encoding="utf-8"?> |
| <android.support.constraint.ConstraintLayout |
| xmlns:android="http://schemas.android.com/apk/res/android" |
| xmlns:app="http://schemas.android.com/apk/res-auto" |
| android:layout_width="match_parent" |
| android:layout_height="match_parent"> |
| |
| <include |
| layout="@layout/single_button_layout" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| app:layout_constraintBottom_toBottomOf="parent" |
| app:layout_constraintEnd_toEndOf="parent" |
| app:layout_constraintStart_toStartOf="parent" |
| app:layout_constraintTop_toTopOf="parent" /> |
| </android.support.constraint.ConstraintLayout>)"; |
| ValidateXmlText(xml, /*expected=*/false); |
| } |
| |
| TEST(LayoutValidationTest, ViewNode) { |
| const string xml = R"(<?xml version="1.0" encoding="utf-8"?> |
| <android.support.constraint.ConstraintLayout |
| xmlns:android="http://schemas.android.com/apk/res/android" |
| xmlns:app="http://schemas.android.com/apk/res-auto" |
| android:layout_width="match_parent" |
| android:layout_height="match_parent"> |
| |
| <view |
| class="android.support.design.button.MaterialButton" |
| id="@+id/view" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| app:layout_constraintBottom_toBottomOf="parent" |
| app:layout_constraintEnd_toEndOf="parent" |
| app:layout_constraintStart_toStartOf="parent" |
| app:layout_constraintTop_toTopOf="parent" /> |
| </android.support.constraint.ConstraintLayout>)"; |
| ValidateXmlText(xml, /*expected=*/false); |
| } |
| |
| TEST(LayoutValidationTest, FragmentNode) { |
| // This test case is from https://developer.android.com/guide/components/fragments |
| const string xml = R"(<?xml version="1.0" encoding="utf-8"?> |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| android:orientation="horizontal" |
| android:layout_width="match_parent" |
| android:layout_height="match_parent"> |
| <fragment android:name="com.example.news.ArticleListFragment" |
| android:id="@+id/list" |
| android:layout_weight="1" |
| android:layout_width="0dp" |
| android:layout_height="match_parent" /> |
| <fragment android:name="com.example.news.ArticleReaderFragment" |
| android:id="@+id/viewer" |
| android:layout_weight="2" |
| android:layout_width="0dp" |
| android:layout_height="match_parent" /> |
| </LinearLayout>)"; |
| ValidateXmlText(xml, /*expected=*/false); |
| } |