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 for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui.queries;
18
19import static com.android.documentsui.queries.CommandInterceptor.COMMAND_PREFIX;
20
21import android.support.test.filters.SmallTest;
22import android.support.test.runner.AndroidJUnit4;
23
24import com.android.documentsui.testing.TestEventHandler;
25import com.android.documentsui.testing.TestFeatures;
26
27import org.junit.Assert;
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31
32@RunWith(AndroidJUnit4.class)
33@SmallTest
34public final class CommandInterceptorTest {
35
36    private TestEventHandler<String[]> mCommand0;
37    private TestEventHandler<String[]> mCommand1;
38    private CommandInterceptor mProcessor;
39
40    @Before
41    public void setUp() {
42        mCommand0 = new TestEventHandler<>();
43        mCommand1 = new TestEventHandler<>();
44        mProcessor = new CommandInterceptor(new TestFeatures());
45        mProcessor.add(mCommand0);
46        mProcessor.add(mCommand1);
47    }
48
49    @Test
50    public void testTriesAllCommands() {
51        mProcessor.accept(COMMAND_PREFIX + "poodles");
52        mCommand0.assertCalled();
53        mCommand1.assertCalled();
54    }
55
56    @Test
57    public void testStopsAfterCommandHandled() {
58        mCommand0.nextReturn(true);
59        mProcessor.accept(":poodles");
60        mCommand0.assertCalled();
61        mCommand1.assertNotCalled();
62    }
63
64    @Test
65    public void testConveysArguments() {
66        mCommand0.nextReturn(true);
67        mProcessor.accept(COMMAND_PREFIX + "cheese doodles");
68
69        String[] expected = {"cheese", "doodles"};
70        Assert.assertArrayEquals(expected, mCommand0.getLastValue());
71    }
72
73    @Test
74    public void testMissingCommand() {
75        mProcessor.accept(COMMAND_PREFIX);
76        mCommand0.assertNotCalled();
77        mCommand1.assertNotCalled();
78    }
79}
80