1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License static for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 package com.android.settings.search;
19 
20 import android.content.Intent;
21 import com.android.settings.SettingsRobolectricTestRunner;
22 import com.android.settings.TestConfig;
23 import com.android.settings.search2.DatabaseIndexingManager.DatabaseRow;
24 import com.android.settings.search2.DatabaseIndexingManager.DatabaseRow.Builder;
25 import com.android.settings.search2.IntentPayload;
26 import com.android.settings.search2.ResultPayload;
27 import com.android.settings.search2.ResultPayloadUtils;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.annotation.Config;
32 
33 import static com.google.common.truth.Truth.assertThat;
34 
35 @RunWith(SettingsRobolectricTestRunner.class)
36 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
37 public class DatabaseRowTest {
38     private Builder builder;
39 
40     private static final String LOCALE = "locale";
41     private static final String UPDATED_TITLE = "updated title";
42     private static final String NORMALIZED_TITLE = "normal title";
43     private static final String UPDATED_SUMMARY_ON = "updated summary on";
44     private static final String NORMALIZED_SUMMARY_ON = "normalized summary on";
45     private static final String UPDATED_SUMMARY_OFF = "updated summary off";
46     private static final String NORMALIZED_SUMMARY_OFF = "normalized summary off";
47     private static final String ENTRIES = "entries";
48     private static final String CLASS_NAME = "class name";
49     private static final String SCREEN_TITLE = "sceen title";
50     private static final int ICON_RES_ID = 0xff;
51     private static final int RANK = 1;
52     private static final String SPACE_DELIMITED_KEYWORDS = "keywords";
53     private static final String INTENT_ACTION = "intent action";
54     private static final String INTENT_TARGET_PACKAGE = "target package";
55     private static final String INTENT_TARGET_CLASS = "target class";
56     private static final boolean ENABLED = true;
57     private static final String KEY = "key";
58     private static final int USER_ID = 1;
59     private static IntentPayload intentPayload;
60 
61     private final String EXTRA_KEY = "key";
62     private final String EXTRA_VALUE = "value";
63 
64     @Before
setUp()65     public void setUp() {
66         Intent intent = new Intent();
67         intent.putExtra(EXTRA_KEY, EXTRA_VALUE);
68         intentPayload = new IntentPayload(intent);
69 
70         builder = new DatabaseRow.Builder();
71     }
72 
73     @Test
testFullRowBuild_NonNull()74     public void testFullRowBuild_NonNull() {
75         DatabaseRow row = generateRow();
76         assertThat(row).isNotNull();
77     }
78 
79     @Test
testPrimativesBuild_NoDataLoss()80     public void testPrimativesBuild_NoDataLoss() {
81         DatabaseRow row = generateRow();
82 
83         assertThat(row.locale).isEqualTo(LOCALE);
84         assertThat(row.updatedTitle).isEqualTo(UPDATED_TITLE);
85         assertThat(row.normalizedTitle).isEqualTo(NORMALIZED_TITLE);
86         assertThat(row.updatedSummaryOn).isEqualTo(UPDATED_SUMMARY_ON);
87         assertThat(row.normalizedSummaryOn).isEqualTo(NORMALIZED_SUMMARY_ON);
88         assertThat(row.updatedSummaryOff).isEqualTo(UPDATED_SUMMARY_OFF);
89         assertThat(row.normalizedSummaryOff).isEqualTo(NORMALIZED_SUMMARY_OFF);
90         assertThat(row.entries).isEqualTo(ENTRIES);
91         assertThat(row.className).isEqualTo(CLASS_NAME);
92         assertThat(row.screenTitle).isEqualTo(SCREEN_TITLE);
93         assertThat(row.iconResId).isEqualTo(ICON_RES_ID);
94         assertThat(row.rank).isEqualTo(RANK);
95         assertThat(row.spaceDelimitedKeywords).isEqualTo(SPACE_DELIMITED_KEYWORDS);
96         assertThat(row.intentAction).isEqualTo(INTENT_ACTION);
97         assertThat(row.intentTargetClass).isEqualTo(INTENT_TARGET_CLASS);
98         assertThat(row.intentTargetPackage).isEqualTo(INTENT_TARGET_PACKAGE);
99         assertThat(row.enabled).isEqualTo(ENABLED);
100         assertThat(row.userId).isEqualTo(USER_ID);
101         assertThat(row.key).isEqualTo(KEY);
102         assertThat(row.payloadType).isEqualTo(ResultPayload.PayloadType.INTENT);
103     }
104 
105     @Test
testPayload_PayloadTypeAdded()106     public void testPayload_PayloadTypeAdded() {
107         DatabaseRow row = generateRow();
108         byte[] marshalledPayload = row.payload;
109         IntentPayload payload = ResultPayloadUtils.unmarshall(marshalledPayload,
110                 IntentPayload.CREATOR);
111 
112         Intent intent = payload.intent;
113         assertThat(intent.getExtra(EXTRA_KEY)).isEqualTo(EXTRA_VALUE);
114     }
115 
116     @Test
TestNullPayload_NoCrash()117     public void TestNullPayload_NoCrash() {
118         Builder builder = new Builder();
119         builder.setPayload(null);
120         DatabaseRow row = builder.build();
121 
122         assertThat(row.payload).isNull();
123     }
124 
generateRow()125     private DatabaseRow generateRow() {
126         builder.setLocale(LOCALE)
127                 .setUpdatedTitle(UPDATED_TITLE)
128                 .setNormalizedTitle(NORMALIZED_TITLE)
129                 .setUpdatedSummaryOn(UPDATED_SUMMARY_ON)
130                 .setNormalizedSummaryOn(NORMALIZED_SUMMARY_ON)
131                 .setUpdatedSummaryOff(UPDATED_SUMMARY_OFF)
132                 .setNormalizedSummaryOff(NORMALIZED_SUMMARY_OFF)
133                 .setEntries(ENTRIES)
134                 .setClassName(CLASS_NAME)
135                 .setScreenTitle(SCREEN_TITLE)
136                 .setIconResId(ICON_RES_ID)
137                 .setRank(RANK)
138                 .setSpaceDelimitedKeywords(SPACE_DELIMITED_KEYWORDS)
139                 .setIntentAction(INTENT_ACTION)
140                 .setIntentTargetPackage(INTENT_TARGET_PACKAGE)
141                 .setIntentTargetClass(INTENT_TARGET_CLASS)
142                 .setEnabled(ENABLED)
143                 .setKey(KEY)
144                 .setUserId(USER_ID)
145                 .setPayload(intentPayload);
146 
147         return(builder.build());
148     }
149 }
150 ;
151